UI.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. <?php
  2. /**
  3. * Perform all the needed checks to delete one (or more) objects
  4. */
  5. function DeleteObjects(WebPage $oP, $sClass, $aObjects, $bDeleteConfirmed)
  6. {
  7. $bFoundManual = false;
  8. $bFoundStopper = false;
  9. $iTotalDelete = 0; // count of object that must be deleted
  10. $iTotalReset = 0; // count of object for which an ext key will be reset (to 0)
  11. $aTotalDeletedObjs = array();
  12. $aTotalResetedObjs = array();
  13. foreach($aObjects as $oObj)
  14. {
  15. // Evaluate the impact on the DB integrity
  16. //
  17. list ($aDeletedObjs, $aResetedObjs) = $oObj->GetDeletionScheme();
  18. // Evaluate feasibility (user access control)
  19. //
  20. foreach ($aDeletedObjs as $sRemoteClass => $aDeletes)
  21. {
  22. $iTotalDelete += count($aDeletes);
  23. foreach ($aDeletes as $iId => $aData)
  24. {
  25. $oToDelete = $aData['to_delete'];
  26. $bDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, DBObjectSet::FromObject($oToDelete));
  27. $aTotalDeletedObjs[$sRemoteClass][$iId]['auto_delete'] = $aData['auto_delete'];
  28. if (!$bDeleteAllowed)
  29. {
  30. $aTotalDeletedObjs[$sRemoteClass][$iId]['issue'] = 'not allowed to delete this object';
  31. $bFoundStopper = true;
  32. }
  33. else
  34. {
  35. $aTotalDeletedObjs[$sRemoteClass][$iId]['to_delete'] = $oToDelete;
  36. }
  37. $bAutoDel = $aData['auto_delete'];
  38. if (!$bAutoDel)
  39. {
  40. $bFoundManual = true;
  41. }
  42. }
  43. }
  44. foreach ($aResetedObjs as $sRemoteClass => $aToReset)
  45. {
  46. $iTotalReset += count($aToReset);
  47. foreach ($aToReset as $iId => $aData)
  48. {
  49. $oToReset = $aData['to_reset'];
  50. $aExtKeyLabels = array();
  51. $aForbiddenKeys = array(); // keys on which the current user is not allowed to write
  52. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  53. {
  54. $bUpdateAllowed = UserRights::IsActionAllowedOnAttribute($sClass, $sRemoteExtKey, UR_ACTION_MODIFY, DBObjectSet::FromObject($oToReset));
  55. if (!$bUpdateAllowed)
  56. {
  57. $bFoundStopper = true;
  58. $aForbiddenKeys[] = $aRemoteAttDef->GetLabel();
  59. }
  60. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  61. }
  62. $aResetedObjs[$sRemoteClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  63. if (count($aForbiddenKeys) > 0)
  64. {
  65. $aTotalResetedObjs[$sRemoteClass][$iId]['issue'] = 'you are not allowed to update some fields: '.implode(', ', $aForbiddenKeys);
  66. }
  67. else
  68. {
  69. $aTotalResetedObjs[$sRemoteClass][$iId]['to_reset'] = $oToReset;
  70. }
  71. }
  72. }
  73. // Count of dependent objects (+ the current one)
  74. $iTotalTargets = $iTotalDelete + $iTotalReset;
  75. }
  76. if ($bDeleteConfirmed)
  77. {
  78. if (count($aObjects) == 1)
  79. {
  80. $oObj = $aObjects[0];
  81. $oP->add("<h1>Deletion of ".$oObj->GetName()."</h1>\n");
  82. }
  83. else
  84. {
  85. $oP->add("<h1>Deletion of ".count($aObjects)." objects of class $sClass.</h1>\n");
  86. }
  87. // Security - do not allow the user to force a forbidden delete by the mean of page arguments...
  88. if ($bFoundStopper)
  89. {
  90. throw new SecurityException('This object could not be deleted because the current user do not have sufficient rights');
  91. }
  92. if ($bFoundManual)
  93. {
  94. throw new SecurityException('This object could not be deleted because some manual operations must be performed prior to that');
  95. }
  96. // Prepare the change reporting
  97. //
  98. $oMyChange = MetaModel::NewObject("CMDBChange");
  99. $oMyChange->Set("date", time());
  100. if (UserRights::GetUser() != UserRights::GetRealUser())
  101. {
  102. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  103. }
  104. else
  105. {
  106. $sUserString = UserRights::GetUser();
  107. }
  108. $oMyChange->Set("userinfo", $sUserString);
  109. $oMyChange->DBInsert();
  110. // Delete dependencies
  111. //
  112. $aDisplayData = array();
  113. foreach ($aTotalDeletedObjs as $sRemoteClass => $aDeletes)
  114. {
  115. foreach ($aDeletes as $iId => $aData)
  116. {
  117. $oToDelete = $aData['to_delete'];
  118. $aDisplayData[] = array(
  119. 'class' => MetaModel::GetName(get_class($oToDelete)),
  120. 'object' => $oToDelete->GetHyperLink(),
  121. 'consequence' => 'automatically deleted',
  122. );
  123. $oToDelete->DBDeleteTracked($oMyChange);
  124. }
  125. }
  126. // Update dependencies
  127. //
  128. foreach ($aTotalResetedObjs as $sRemoteClass => $aToReset)
  129. {
  130. foreach ($aToReset as $iId => $aData)
  131. {
  132. $oToReset = $aData['to_reset'];
  133. $aDisplayData[] = array(
  134. 'class' => MetaModel::GetName(get_class($oToReset)),
  135. 'object' => $oToReset->GetHyperLink(),
  136. 'consequence' => 'automatic reset of: '.$aData['attributes_list'],
  137. );
  138. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  139. {
  140. $oToReset->Set($sRemoteExtKey, 0);
  141. $oToReset->DBUpdateTracked($oMyChange);
  142. }
  143. }
  144. }
  145. // Report automatic jobs
  146. //
  147. if ($iTotalTargets > 0)
  148. {
  149. if (count($aObjects) == 1)
  150. {
  151. $oObj = $aObjects[0];
  152. $oP->p('Cleaning up any reference to '.$oObj->GetName().'...');
  153. }
  154. else
  155. {
  156. $oP->p("Cleaning up any reference to the ".count($aObjects)." objects of class $sClass.\n");
  157. }
  158. $aDisplayConfig = array();
  159. $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
  160. $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
  161. $aDisplayConfig['consequence'] = array('label' => 'Done', 'description' => 'What has been done');
  162. $oP->table($aDisplayConfig, $aDisplayData);
  163. }
  164. foreach($aObjects as $oObj)
  165. {
  166. $sName = $oObj->GetName();
  167. $sClassLabel = MetaModel::GetName(get_class($oObj));
  168. $oObj->DBDeleteTracked($oMyChange);
  169. $oP->add("<h1>".$sName." - $sClassLabel deleted</h1>\n");
  170. }
  171. }
  172. else
  173. {
  174. if (count($aObjects) == 1)
  175. {
  176. $oObj = $aObjects[0];
  177. $oP->add("<h1>Deletion of ".$oObj->GetHyperLink()."</h1>\n");
  178. }
  179. else
  180. {
  181. $oP->add("<h1>Deletion of ".count($aObjects)." objects of class $sClass.</h1>\n");
  182. }
  183. // Explain what should be done
  184. //
  185. $aDisplayData = array();
  186. foreach ($aTotalDeletedObjs as $sRemoteClass => $aDeletes)
  187. {
  188. foreach ($aDeletes as $iId => $aData)
  189. {
  190. $oToDelete = $aData['to_delete'];
  191. $bAutoDel = $aData['auto_delete'];
  192. if (array_key_exists('issue', $aData))
  193. {
  194. if ($bAutoDel)
  195. {
  196. $sConsequence = 'Should be automaticaly deleted, but you are not allowed to do so';
  197. }
  198. else
  199. {
  200. $sConsequence = 'Must be deleted manually - you are not allowed to delete this object, please contact your application admin';
  201. }
  202. }
  203. else
  204. {
  205. if ($bAutoDel)
  206. {
  207. $sConsequence = 'Will be automaticaly deleted';
  208. }
  209. else
  210. {
  211. $sConsequence = 'Must be deleted manually';
  212. }
  213. }
  214. $aDisplayData[] = array(
  215. 'class' => MetaModel::GetName(get_class($oToDelete)),
  216. 'object' => $oToDelete->GetHyperLink(),
  217. 'consequence' => $sConsequence,
  218. );
  219. }
  220. }
  221. foreach ($aTotalResetedObjs as $sRemoteClass => $aToReset)
  222. {
  223. foreach ($aToReset as $iId => $aData)
  224. {
  225. $oToReset = $aData['to_reset'];
  226. if (array_key_exists('issue', $aData))
  227. {
  228. $sConsequence = "Should be automatically updated, but: ".$aData['issue'];
  229. }
  230. else
  231. {
  232. $sConsequence = "will be automaticaly updated (reset: ".$aData['attributes_list'].")";
  233. }
  234. $aDisplayData[] = array(
  235. 'class' => MetaModel::GetName(get_class($oToReset)),
  236. 'object' => $oToReset->GetHyperLink(),
  237. 'consequence' => $sConsequence,
  238. );
  239. }
  240. }
  241. if ($iTotalTargets > 0)
  242. {
  243. if (count($aObjects) == 1)
  244. {
  245. $oObj = $aObjects[0];
  246. $oP->p("$iTotalTargets objects/links are referencing ".$oObj->GetName());
  247. }
  248. else
  249. {
  250. $oP->p("$iTotalTargets objects/links are referencing some of the objects to be deleted");
  251. }
  252. $oP->p('To ensure Database integrity, any reference should be further eliminated');
  253. $aDisplayConfig = array();
  254. $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
  255. $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
  256. $aDisplayConfig['consequence'] = array('label' => 'Consequence', 'description' => 'What will happen to this object');
  257. $oP->table($aDisplayConfig, $aDisplayData);
  258. }
  259. if ($iTotalTargets > 0 && ($bFoundManual || $bFoundStopper))
  260. {
  261. if ($bFoundStopper)
  262. {
  263. $oP->p("Sorry, you are not allowed to delete this object, please see detailed explanations above");
  264. }
  265. elseif ($bFoundManual)
  266. {
  267. $oP->p("Please do the manual operations requested above prior to requesting the deletion of this object");
  268. }
  269. $oP->add("<form method=\"post\">\n");
  270. $oP->add("<input DISABLED type=\"submit\" name=\"\" value=\" Delete! \">\n");
  271. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\" Cancel \">\n");
  272. $oP->add("</form>\n");
  273. }
  274. else
  275. {
  276. if (count($aObjects) == 1)
  277. {
  278. $oObj = $aObjects[0];
  279. $id = $oObj->GetKey();
  280. $oP->p("Please confirm that you want to delete ".$oObj->GetHyperLink());
  281. $oP->add("<form method=\"post\">\n");
  282. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"delete_confirmed\">\n");
  283. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  284. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  285. $oP->add("<input type=\"submit\" name=\"\" value=\" Delete! \">\n");
  286. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\" Cancel \">\n");
  287. $oP->add("</form>\n");
  288. }
  289. else
  290. {
  291. $oP->add("<h1>Please confirm that you want to delete the following ".count($aObjects)." objects of class $sClass.</h1>\n");
  292. $oSet = CMDBobjectSet::FromArray($sClass, $aObjects);
  293. CMDBAbstractObject::DisplaySet($oP, $oSet, array('display_limit' => false, 'menu' => false));
  294. $oP->add("<form method=\"post\">\n");
  295. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete_confirmed\">\n");
  296. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  297. foreach($aObjects as $oObj)
  298. {
  299. $oP->add("<input type=\"hidden\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\">\n");
  300. }
  301. $oP->add("<input type=\"submit\" name=\"\" value=\" Delete! \">\n");
  302. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\" Cancel \">\n");
  303. $oP->add("</form>\n");
  304. }
  305. }
  306. }
  307. }
  308. /***********************************************************************************
  309. *
  310. * Main user interface page, starts here
  311. *
  312. * ***********************************************************************************/
  313. try
  314. {
  315. require_once('../application/application.inc.php');
  316. require_once('../application/itopwebpage.class.inc.php');
  317. require_once('../application/wizardhelper.class.inc.php');
  318. require_once('../application/startup.inc.php');
  319. $oContext = new UserContext();
  320. $oAppContext = new ApplicationContext();
  321. $iActiveNodeId = utils::ReadParam('menu', '');
  322. if (empty($iActiveNodeId) && !is_numeric($iActiveNodeId))
  323. {
  324. // No menu specified, let's get the default one:
  325. // 1) It's a root menu item (parent_id == 0)
  326. // 2) with the lowest rank
  327. $oFilter = DBObjectSearch::FromOQL('SELECT menuNode AS M WHERE M.parent_id = 0');
  328. if ($oFilter)
  329. {
  330. $oMenuSet = new CMDBObjectSet($oFilter);
  331. while($oMenu = $oMenuSet->Fetch())
  332. {
  333. $aRanks[$oMenu->GetKey()] = $oMenu->Get('rank');
  334. }
  335. asort($aRanks); // sort by ascending rank: menuId => rank
  336. $aKeys = array_keys($aRanks);
  337. $iActiveNodeId = array_shift($aKeys); // Takes the first key, i.e. the menuId with the lowest rank
  338. }
  339. }
  340. $currentOrganization = utils::ReadParam('org_id', '');
  341. $operation = utils::ReadParam('operation', '');
  342. require_once('../application/loginwebpage.class.inc.php');
  343. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  344. $oP = new iTopWebPage("Welcome to ITop", $currentOrganization);
  345. // From now on the context is limited to the the selected organization ??
  346. if ($iActiveNodeId != -1)
  347. {
  348. $oActiveNode = $oContext->GetObject('menuNode', $iActiveNodeId);
  349. }
  350. else
  351. {
  352. $oActiveNode = null;
  353. }
  354. switch($operation)
  355. {
  356. case 'details':
  357. $sClass = utils::ReadParam('class', '');
  358. $sClassLabel = MetaModel::GetName($sClass);
  359. $id = utils::ReadParam('id', '');
  360. $oSearch = new DBObjectSearch($sClass);
  361. $oBlock = new DisplayBlock($oSearch, 'search', false);
  362. $oBlock->Display($oP, 0);
  363. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  364. {
  365. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  366. }
  367. else
  368. {
  369. $oObj = $oContext->GetObject($sClass, $id);
  370. if ($oObj != null)
  371. {
  372. $oP->set_title("iTop - ".$oObj->GetDisplayName()." - $sClassLabel details");
  373. $oObj->DisplayDetails($oP);
  374. }
  375. else
  376. {
  377. $oP->set_title("iTop - Error");
  378. $oP->add("<p>Sorry this object does not exist (or you are not allowed to view it).</p>\n");
  379. }
  380. }
  381. break;
  382. case 'search_oql':
  383. $sOQLClass = utils::ReadParam('oql_class', '');
  384. $sOQLClause = utils::ReadParam('oql_clause', '');
  385. $sFormat = utils::ReadParam('format', '');
  386. $bSearchForm = utils::ReadParam('search_form', true);
  387. if (empty($sOQLClass))
  388. {
  389. $oP->set_title("iTop - Error");
  390. $oP->add("<p>'oql_class' must be specifed for this operation.</p>\n");
  391. }
  392. else
  393. {
  394. $oP->set_title("iTop - Search results");
  395. $sOQL = "SELECT $sOQLClass $sOQLClause";
  396. try
  397. {
  398. $oFilter = DBObjectSearch::FromOQL($sOQL); // To Do: Make sure we don't bypass security
  399. $oSet = new DBObjectSet($oFilter);
  400. if ($bSearchForm)
  401. {
  402. $oBlock = new DisplayBlock($oFilter, 'search', false);
  403. $oBlock->Display($oP, 0);
  404. }
  405. if (strtolower($sFormat) == 'csv')
  406. {
  407. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  408. $oBlock->Display($oP, 'csv');
  409. $oPage->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  410. }
  411. else
  412. {
  413. $oBlock = new DisplayBlock($oFilter, 'list', false);
  414. $oBlock->Display($oP, 1);
  415. }
  416. }
  417. catch(CoreException $e)
  418. {
  419. $oFilter = new DBObjectSearch($sOQLClass); // To Do: Make sure we don't bypass security
  420. $oSet = new DBObjectSet($oFilter);
  421. if ($bSearchForm)
  422. {
  423. $oBlock = new DisplayBlock($oFilter, 'search', false);
  424. $oBlock->Display($oP, 0);
  425. }
  426. $oP->P("<b>Error incorrect OQL query:</b>");
  427. $oP->P($e->getHtmlDesc());
  428. }
  429. catch(Exception $e)
  430. {
  431. $oP->p('<b>An error occured while running the query:</b>');
  432. $oP->p($e->getMessage());
  433. }
  434. }
  435. break;
  436. case 'search_form':
  437. $sClass = utils::ReadParam('class', '');
  438. $sFormat = utils::ReadParam('format', 'html');
  439. $bSearchForm = utils::ReadParam('search_form', true);
  440. if (empty($sClass))
  441. {
  442. $oP->set_title("iTop - Error");
  443. $oP->add("<p>'class' must be specifed for this operation.</p>\n");
  444. }
  445. else
  446. {
  447. $oP->set_title("iTop - Search results");
  448. $oFilter = $oContext->NewFilter($sClass);
  449. $oSet = new DBObjectSet($oFilter);
  450. if ($bSearchForm)
  451. {
  452. $oBlock = new DisplayBlock($oFilter, 'search', false /* Asynchronous */, array('open' => true));
  453. $oBlock->Display($oP, 0);
  454. }
  455. if (strtolower($sFormat) == 'csv')
  456. {
  457. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  458. $oBlock->Display($oP, 1);
  459. $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  460. }
  461. else
  462. {
  463. $oBlock = new DisplayBlock($oFilter, 'list', false);
  464. $oBlock->Display($oP, 1);
  465. }
  466. }
  467. break;
  468. case 'search':
  469. $sFilter = utils::ReadParam('filter', '');
  470. $sFormat = utils::ReadParam('format', '');
  471. $bSearchForm = utils::ReadParam('search_form', true);
  472. if (empty($sFilter))
  473. {
  474. $oP->set_title("iTop - Error");
  475. $oP->add("<p>'filter' must be specifed for this operation.</p>\n");
  476. }
  477. else
  478. {
  479. $oP->set_title("iTop - Search results");
  480. // TO DO: limit the search filter by the user context
  481. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  482. $oSet = new DBObjectSet($oFilter);
  483. if ($bSearchForm)
  484. {
  485. $oBlock = new DisplayBlock($oFilter, 'search', false);
  486. $oBlock->Display($oP, 0);
  487. }
  488. if (strtolower($sFormat) == 'csv')
  489. {
  490. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  491. $oBlock->Display($oP, 'csv');
  492. $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  493. }
  494. else
  495. {
  496. $oBlock = new DisplayBlock($oFilter, 'list', false);
  497. $oBlock->Display($oP, 1);
  498. }
  499. }
  500. break;
  501. case 'full_text':
  502. $sFullText = trim(utils::ReadParam('text', ''));
  503. if (empty($sFullText))
  504. {
  505. $oP->p('Nothing to search.');
  506. }
  507. else
  508. {
  509. $oP->p("<h2>Results for '$sFullText':</h2>\n");
  510. $iCount = 0;
  511. $iBlock = 0;
  512. // Search in full text mode in all the classes
  513. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  514. {
  515. $oFilter = new DBObjectSearch($sClassName);
  516. $oFilter->AddCondition_FullText($sFullText);
  517. $oSet = new DBObjectSet($oFilter);
  518. if ($oSet->Count() > 0)
  519. {
  520. $aLeafs = array();
  521. while($oObj = $oSet->Fetch())
  522. {
  523. if (get_class($oObj) == $sClassName)
  524. {
  525. $aLeafs[] = $oObj->GetKey();
  526. }
  527. }
  528. $oLeafsFilter = new DBObjectSearch($sClassName);
  529. if (count($aLeafs) > 0)
  530. {
  531. $iCount += count($aLeafs);
  532. $oP->add("<div class=\"page_header\">\n");
  533. $oP->add("<h1><span class=\"hilite\">".Metamodel::GetName($sClassName).":</span> ".count($aLeafs)." object(s) found.</h1>\n");
  534. $oP->add("</div>\n");
  535. $oLeafsFilter->AddCondition('id', $aLeafs, 'IN');
  536. $oBlock = new DisplayBlock($oLeafsFilter, 'list', false);
  537. $oBlock->Display($oP, $iBlock++);
  538. }
  539. }
  540. }
  541. if ($iCount == 0)
  542. {
  543. $oP->p('No object found.');
  544. }
  545. }
  546. break;
  547. case 'modify':
  548. $oP->add_linked_script("../js/json.js");
  549. $oP->add_linked_script("../js/forms-json-utils.js");
  550. $oP->add_linked_script("../js/wizardhelper.js");
  551. $oP->add_linked_script("../js/wizard.utils.js");
  552. $oP->add_linked_script("../js/linkswidget.js");
  553. $oP->add_linked_script("../js/jquery.blockUI.js");
  554. $sClass = utils::ReadParam('class', '');
  555. $sClassLabel = MetaModel::GetName($sClass);
  556. $id = utils::ReadParam('id', '');
  557. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  558. {
  559. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  560. }
  561. else
  562. {
  563. // Check if the user can modify this object
  564. $oSearch = new DBObjectSearch($sClass);
  565. $oSearch->AddCondition('id', $id, '=');
  566. $oSet = new CMDBObjectSet($oSearch);
  567. if ($oSet->Count() > 0)
  568. {
  569. $oObj = $oSet->Fetch();
  570. }
  571. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  572. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  573. if( ($oObj != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  574. {
  575. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel modification");
  576. $oP->add("<div class=\"page_header\">\n");
  577. $oP->add("<h1>Modification of $sClassLabel: <span class=\"hilite\">".$oObj->GetName()."</span></h1>\n");
  578. $oP->add("</div>\n");
  579. $oP->add("<div class=\"wizContainer\">\n");
  580. $oObj->DisplayModifyForm($oP);
  581. $oP->add("</div>\n");
  582. }
  583. else
  584. {
  585. $oP->set_title("iTop - Error");
  586. $oP->add("<p>Sorry this object does not exist (or you are not allowed to view it).</p>\n");
  587. }
  588. }
  589. break;
  590. case 'clone':
  591. $sClass = utils::ReadParam('class', '');
  592. $sClassLabel = MetaModel::GetName($sClass);
  593. $id = utils::ReadParam('id', '');
  594. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  595. {
  596. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  597. }
  598. else
  599. {
  600. // Check if the user can modify this object
  601. $oSearch = new DBObjectSearch($sClass);
  602. $oSearch->AddCondition('id', $id, '=');
  603. $oSet = new CMDBObjectSet($oSearch);
  604. if ($oSet->Count() > 0)
  605. {
  606. $oObjToClone = $oSet->Fetch();
  607. }
  608. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  609. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  610. if( ($oObjToClone != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  611. {
  612. $oP->add_linked_script("../js/json.js");
  613. $oP->add_linked_script("../js/forms-json-utils.js");
  614. $oP->add_linked_script("../js/wizardhelper.js");
  615. $oP->add_linked_script("../js/wizard.utils.js");
  616. $oP->add_linked_script("../js/linkswidget.js");
  617. $oP->add_linked_script("../js/jquery.blockUI.js");
  618. $oP->set_title("iTop - ".$oObjToClone->GetName()." - $sClassLabel clone");
  619. $oP->add("<div class=\"page_header\">\n");
  620. $oP->add("<h1>Clone of $sClassLabel: <span class=\"hilite\">".$oObjToClone->GetName()."</span></h1>\n");
  621. $oP->add("</div>\n");
  622. $oP->add("<div class=\"wizContainer\">\n");
  623. cmdbAbstractObject::DisplayCreationForm($oP, $sClass, $oObjToClone);
  624. $oP->add("</div>\n");
  625. }
  626. else
  627. {
  628. $oP->set_title("iTop - Error");
  629. $oP->add("<p>Sorry this object does not exist (or you are not allowed to view it).</p>\n");
  630. }
  631. }
  632. break;
  633. case 'new':
  634. $sClass = utils::ReadParam('class', '');
  635. $sStateCode = utils::ReadParam('state', '');
  636. if ( empty($sClass) )
  637. {
  638. $oP->p("The class must be specified for this operation!");
  639. }
  640. else
  641. {
  642. $oP->add_linked_script("../js/json.js");
  643. $oP->add_linked_script("../js/forms-json-utils.js");
  644. $oP->add_linked_script("../js/wizardhelper.js");
  645. $oP->add_linked_script("../js/wizard.utils.js");
  646. $oP->add_linked_script("../js/linkswidget.js");
  647. $oP->add_linked_script("../js/jquery.blockUI.js");
  648. $oWizard = new UIWizard($oP, $sClass, $sStateCode);
  649. $oContext = new UserContext();
  650. $aArgs = array_merge($oAppContext->GetAsHash(), utils::ReadParam('default', array()));
  651. $sStateCode = $oWizard->GetTargetState(); // Will computes the default state if none was supplied
  652. $sClassLabel = MetaModel::GetName($sClass);
  653. $oP->add("<h2>Creation of a new $sClassLabel</h2>");
  654. if (!empty($sStateCode))
  655. {
  656. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  657. }
  658. $aWizardSteps = $oWizard->GetWizardStructure();
  659. // Display the structure of the wizard
  660. $iStepIndex = 1;
  661. $iMaxInputId = 0;
  662. $aFieldsMap = array();
  663. foreach($aWizardSteps['mandatory'] as $aSteps)
  664. {
  665. $oP->SetCurrentTab("Step $iStepIndex *");
  666. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, false /* no finish button */, $aArgs);
  667. //$oP->add("</div>\n");
  668. $iStepIndex++;
  669. }
  670. foreach($aWizardSteps['optional'] as $aSteps)
  671. {
  672. $oP->SetCurrentTab("Step $iStepIndex *");
  673. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true, $aArgs); // true means enable the finish button
  674. //$oP->add("</div>\n");
  675. $iStepIndex++;
  676. }
  677. $oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);
  678. $oObj = null;
  679. if (!empty($id))
  680. {
  681. $oObj = $oContext->GetObject($sClass, $id);
  682. }
  683. if (!is_object($oObj))
  684. {
  685. // new object or that can't be retrieved (corrupted id or object not allowed to this user)
  686. $id = '';
  687. $oObj = MetaModel::NewObject($sClass);
  688. }
  689. $oP->add("<script type=\"text/javascript\">
  690. // Fill the map between the fields of the form and the attributes of the object\n");
  691. $aNewFieldsMap = array();
  692. foreach($aFieldsMap as $id => $sFieldCode)
  693. {
  694. $aNewFieldsMap[$sFieldCode] = $id;
  695. }
  696. $iFieldsCount = count($aFieldsMap);
  697. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  698. $oP->add("
  699. // Initializes the object once at the beginning of the page...
  700. var oWizardHelper = new WizardHelper('$sClass');
  701. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  702. oWizardHelper.SetFieldsCount($iFieldsCount);
  703. ActivateStep(1);
  704. </script>\n");
  705. }
  706. break;
  707. case 'apply_modify':
  708. $sClass = utils::ReadPostedParam('class', '');
  709. $sClassLabel = MetaModel::GetName($sClass);
  710. $id = utils::ReadPostedParam('id', '');
  711. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  712. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  713. {
  714. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  715. }
  716. else if (!utils::IsTransactionValid($sTransactionId))
  717. {
  718. $oP->p("<strong>Error: object has already be updated!</strong>\n");
  719. }
  720. else
  721. {
  722. $oObj = $oContext->GetObject($sClass, $id);
  723. if ($oObj != null)
  724. {
  725. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel modification");
  726. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel modification</h1>\n");
  727. $bObjectModified = false;
  728. foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
  729. {
  730. if ($oAttDef->IsLinkSet())
  731. {
  732. // Link set, the data is a set of link objects, encoded in JSON
  733. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  734. if (!empty($aAttributes[$sAttCode]))
  735. {
  736. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  737. $oObj->Set($sAttCode, $oLinkSet);
  738. // TO DO: detect a real modification, for now always update !!
  739. $bObjectModified = true;
  740. }
  741. }
  742. else if (!$oAttDef->IsExternalField())
  743. {
  744. $rawValue = utils::ReadPostedParam("attr_$sAttCode", null);
  745. if (!is_null($rawValue))
  746. {
  747. $aAttributes[$sAttCode] = trim($rawValue);
  748. $previousValue = $oObj->Get($sAttCode);
  749. if ($previousValue !== $aAttributes[$sAttCode])
  750. {
  751. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  752. $bObjectModified = true;
  753. }
  754. }
  755. }
  756. else if ($oAttDef->IsWritable())
  757. {
  758. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  759. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  760. {
  761. // Non-visible, or read-only attribute, do nothing
  762. }
  763. else if ($oAttDef->GetEditClass() == 'Document')
  764. {
  765. // There should be an uploaded file with the named attr_<attCode>
  766. $oDocument = utils::ReadPostedDocument('file_'.$sAttCode);
  767. if (!$oDocument->IsEmpty())
  768. {
  769. // A new file has been uploaded
  770. $oObj->Set($sAttCode, $oDocument);
  771. $bObjectModified = true;
  772. }
  773. }
  774. }
  775. }
  776. if (!$bObjectModified)
  777. {
  778. $oP->p("No modification detected. ".MetaModel::GetName(get_class($oObj))." has <strong>not</strong> been updated.\n");
  779. }
  780. else if ($oObj->CheckToUpdate())
  781. {
  782. $oMyChange = MetaModel::NewObject("CMDBChange");
  783. $oMyChange->Set("date", time());
  784. if (UserRights::GetUser() != UserRights::GetRealUser())
  785. {
  786. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  787. }
  788. else
  789. {
  790. $sUserString = UserRights::GetUser();
  791. }
  792. $oMyChange->Set("userinfo", $sUserString);
  793. $iChangeId = $oMyChange->DBInsert();
  794. $oObj->DBUpdateTracked($oMyChange);
  795. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  796. }
  797. else
  798. {
  799. $oP->p("<strong>Error: object can not be updated!</strong>\n");
  800. //$oObj->Reload(); // restore default values!
  801. }
  802. }
  803. else
  804. {
  805. $oP->set_title("iTop - Error");
  806. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  807. }
  808. }
  809. $oObj->DisplayDetails($oP);
  810. break;
  811. case 'select_for_deletion':
  812. $sFilter = utils::ReadParam('filter', '');
  813. $sFormat = utils::ReadParam('format', '');
  814. $bSearchForm = utils::ReadParam('search_form', true);
  815. if (empty($sFilter))
  816. {
  817. $oP->set_title("iTop - Error");
  818. $oP->add("<p>'filter' must be specifed for this operation.</p>\n");
  819. }
  820. else
  821. {
  822. $oP->set_title("iTop - mass delete");
  823. $oP->add("<h1> Select the objects you want to delete</h1>\n");
  824. // TO DO: limit the search filter by the user context
  825. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  826. $oSet = new DBObjectSet($oFilter);
  827. if ($bSearchForm)
  828. {
  829. $oBlock = new DisplayBlock($oFilter, 'search', false);
  830. $oBlock->Display($oP, 0);
  831. }
  832. $oBlock = new DisplayBlock($oFilter, 'list', false);
  833. $oP->add("<form method=\"post\">\n");
  834. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete\">\n");
  835. $oP->add("<input type=\"hidden\" name=\"class\" value=\"".$oFilter->GetClass()."\">\n");
  836. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  837. $oBlock->Display($oP, 1, array('selection_type' => 'multiple', 'selection_mode' => true, 'display_limit' => false));
  838. $oP->add("<input type=\"button\" value=\" Cancel \" onClick=\"window.history.back()\">&nbsp;&nbsp;<input type=\"submit\" value=\" Next >>\">\n");
  839. $oP->add("</form>\n");
  840. }
  841. break;
  842. case 'bulk_delete':
  843. case 'bulk_delete_confirmed':
  844. $sClass = utils::ReadPostedParam('class', '');
  845. $sClassLabel = MetaModel::GetName($sClass);
  846. $aSelectObject = utils::ReadPostedParam('selectObject', '');
  847. $aObjects = array();
  848. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  849. if ( empty($sClass) || empty($aSelectObject)) // TO DO: check that the class name is valid !
  850. {
  851. throw new ApplicationException('Error: \'class\' and \'selectObject[]\' parameters must be specifed for this operation.');
  852. }
  853. if (!utils::IsTransactionValid($sTransactionId))
  854. {
  855. throw new ApplicationException('Error: objects have already been deleted!');
  856. }
  857. foreach($aSelectObject as $iId)
  858. {
  859. $aObjects[] = $oContext->GetObject($sClass, $iId);
  860. }
  861. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, DBObjectSet::FromArray($sClass, $aObjects)))
  862. {
  863. throw new SecurityException('You are not allowed to perform a bulk delete of objects of class '.$sClass);
  864. }
  865. DeleteObjects($oP, $sClass, $aObjects, ($operation == 'bulk_delete_confirmed'));
  866. break;
  867. case 'delete':
  868. case 'delete_confirmed':
  869. $sClass = utils::ReadParam('class', '');
  870. $sClassLabel = MetaModel::GetName($sClass);
  871. $id = utils::ReadParam('id', '');
  872. $oObj = $oContext->GetObject($sClass, $id);
  873. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oObj)))
  874. {
  875. throw new SecurityException('You are not allowed to do delete objects of class '.$sClass);
  876. }
  877. DeleteObjects($oP, $sClass, array($oObj), ($operation == 'delete_confirmed'));
  878. break;
  879. case 'apply_new':
  880. $oP->p('Creation of the object');
  881. $oP->p('Obsolete, should now go through the wizard...');
  882. break;
  883. case 'apply_clone':
  884. $sClass = utils::ReadPostedParam('class', '');
  885. $sClassLabel = MetaModel::GetName($sClass);
  886. $iCloneId = utils::ReadPostedParam('clone_id', '');
  887. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  888. if (!utils::IsTransactionValid($sTransactionId))
  889. {
  890. $oP->p("<strong>Error: object has already be cloned!</strong>\n");
  891. }
  892. else
  893. {
  894. $oObj = $oContext->GetObject($sClass, $iCloneId);
  895. $oMyChange = MetaModel::NewObject("CMDBChange");
  896. $oMyChange->Set("date", time());
  897. if (UserRights::GetUser() != UserRights::GetRealUser())
  898. {
  899. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  900. }
  901. else
  902. {
  903. $sUserString = UserRights::GetUser();
  904. }
  905. $oMyChange->Set("userinfo", $sUserString);
  906. $iChangeId = $oMyChange->DBInsert();
  907. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  908. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  909. {
  910. if ( ($oAttDef->IsWritable()) )
  911. {
  912. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  913. $oObj->Set($sAttCode, $value);
  914. }
  915. }
  916. $oObj->DBCloneTracked($oMyChange);
  917. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  918. $oObj->DisplayDetails($oP);
  919. }
  920. break;
  921. case 'wizard_apply_new':
  922. $sJson = utils::ReadPostedParam('json_obj', '');
  923. $oWizardHelper = WizardHelper::FromJSON($sJson);
  924. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  925. if (!utils::IsTransactionValid($sTransactionId))
  926. {
  927. $oP->p("<strong>Error: object has already be created!</strong>\n");
  928. }
  929. else
  930. {
  931. $oObj = $oWizardHelper->GetTargetObject(true /* read uploaded files */);
  932. if (is_object($oObj))
  933. {
  934. $sClass = get_class($oObj);
  935. $sClassLabel = MetaModel::GetName($sClass);
  936. $oMyChange = MetaModel::NewObject("CMDBChange");
  937. $oMyChange->Set("date", time());
  938. if (UserRights::GetUser() != UserRights::GetRealUser())
  939. {
  940. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  941. }
  942. else
  943. {
  944. $sUserString = UserRights::GetUser();
  945. }
  946. $oMyChange->Set("userinfo", $sUserString);
  947. $iChangeId = $oMyChange->DBInsert();
  948. $oObj->DBInsertTracked($oMyChange);
  949. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel created");
  950. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  951. $oObj->DisplayDetails($oP);
  952. }
  953. }
  954. break;
  955. case 'stimulus':
  956. $sClass = utils::ReadParam('class', '');
  957. $id = utils::ReadParam('id', '');
  958. $sStimulus = utils::ReadParam('stimulus', '');
  959. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  960. {
  961. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  962. }
  963. else
  964. {
  965. $oObj = $oContext->GetObject($sClass, $id);
  966. if ($oObj != null)
  967. {
  968. $aTransitions = $oObj->EnumTransitions();
  969. $aStimuli = MetaModel::EnumStimuli($sClass);
  970. if (!isset($aTransitions[$sStimulus]))
  971. {
  972. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()}.</p>\n");
  973. }
  974. else
  975. {
  976. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  977. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  978. $aTransition = $aTransitions[$sStimulus];
  979. $sTargetState = $aTransition['target_state'];
  980. $aTargetStates = MetaModel::EnumStates($sClass);
  981. $oP->add("<div class=\"page_header\">\n");
  982. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  983. //$oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()} to target state: $sTargetState.</p>\n");
  984. $oP->add("</div>\n");
  985. $oObj->DisplayBareDetails($oP);
  986. $aTargetState = $aTargetStates[$sTargetState];
  987. //print_r($aTransitions[$sStimulus]);
  988. //print_r($aTargetState);
  989. $aExpectedAttributes = $aTargetState['attribute_list'];
  990. $oP->add("<div class=\"wizHeader\">\n");
  991. $oP->add("<h1>$sActionDetails</h1>\n");
  992. $oP->add("<div class=\"wizContainer\">\n");
  993. $oP->add("<form method=\"post\">\n");
  994. $aDetails = array();
  995. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  996. {
  997. // Prompt for an attribute if
  998. // - the attribute must be changed or must be displayed to the user for confirmation
  999. // - or the field is mandatory and currently empty
  1000. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  1001. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  1002. {
  1003. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  1004. $oAttDef = $aAttributesDef[$sAttCode];
  1005. $aArgs = array('this' => $oObj);
  1006. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetEditValue($sAttCode), '', '', $iExpectCode, $aArgs);
  1007. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1008. }
  1009. }
  1010. $oP->details($aDetails);
  1011. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  1012. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1013. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  1014. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  1015. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1016. $oP->add($oAppContext->GetForForm());
  1017. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1018. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  1019. $oP->add("</form>\n");
  1020. $oP->add("</div>\n");
  1021. $oP->add("</div>\n");
  1022. }
  1023. }
  1024. else
  1025. {
  1026. $oP->set_title("iTop - Error");
  1027. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  1028. }
  1029. }
  1030. break;
  1031. case 'apply_stimulus':
  1032. $sClass = utils::ReadPostedParam('class', '');
  1033. $id = utils::ReadPostedParam('id', '');
  1034. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  1035. $sStimulus = utils::ReadPostedParam('stimulus', '');
  1036. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  1037. {
  1038. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  1039. }
  1040. else
  1041. {
  1042. $oObj = $oContext->GetObject($sClass, $id);
  1043. if ($oObj != null)
  1044. {
  1045. $aTransitions = $oObj->EnumTransitions();
  1046. $aStimuli = MetaModel::EnumStimuli($sClass);
  1047. if (!isset($aTransitions[$sStimulus]))
  1048. {
  1049. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()}.</p>\n");
  1050. }
  1051. else if (!utils::IsTransactionValid($sTransactionId))
  1052. {
  1053. $oP->p("<strong>Error: object has already been updated!</strong>\n");
  1054. }
  1055. else
  1056. {
  1057. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  1058. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  1059. $aTransition = $aTransitions[$sStimulus];
  1060. $sTargetState = $aTransition['target_state'];
  1061. $aTargetStates = MetaModel::EnumStates($sClass);
  1062. $oP->add("<div class=\"page_header\">\n");
  1063. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  1064. $oP->add("<p>$sActionDetails</p>\n");
  1065. $oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()} to target state: $sTargetState.</p>\n");
  1066. $oP->add("</div>\n");
  1067. $aTargetState = $aTargetStates[$sTargetState];
  1068. //print_r($aTransitions[$sStimulus]);
  1069. //print_r($aTargetState);
  1070. $aExpectedAttributes = $aTargetState['attribute_list'];
  1071. $aDetails = array();
  1072. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  1073. {
  1074. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  1075. {
  1076. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  1077. $oObj->Set($sAttCode, $paramValue);
  1078. }
  1079. }
  1080. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  1081. {
  1082. $oMyChange = MetaModel::NewObject("CMDBChange");
  1083. $oMyChange->Set("date", time());
  1084. if (UserRights::GetUser() != UserRights::GetRealUser())
  1085. {
  1086. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  1087. }
  1088. else
  1089. {
  1090. $sUserString = UserRights::GetUser();
  1091. }
  1092. $oMyChange->Set("userinfo", $sUserString);
  1093. $iChangeId = $oMyChange->DBInsert();
  1094. $oObj->DBUpdateTracked($oMyChange);
  1095. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  1096. }
  1097. $oObj->DisplayDetails($oP);
  1098. }
  1099. }
  1100. else
  1101. {
  1102. $oP->set_title("iTop - Error");
  1103. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  1104. }
  1105. }
  1106. break;
  1107. case 'modify_links':
  1108. $sClass = utils::ReadParam('class', '');
  1109. $sLinkAttr = utils::ReadParam('link_attr', '');
  1110. $sTargetClass = utils::ReadParam('target_class', '');
  1111. $id = utils::ReadParam('id', '');
  1112. $bAddObjects = utils::ReadParam('addObjects', false);
  1113. if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
  1114. {
  1115. $oP->set_title("iTop - Error");
  1116. $oP->add("<p>4 parameters are mandatory for this operation: class, id, target_class and link_attr.</p>\n");
  1117. }
  1118. else
  1119. {
  1120. require_once('../application/uilinkswizard.class.inc.php');
  1121. $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
  1122. $oWizard->Display($oP, $oContext, array('StartWithAdd' => $bAddObjects));
  1123. }
  1124. break;
  1125. case 'do_modify_links':
  1126. $aLinks = utils::ReadParam('linkId', array(), 'post');
  1127. $sLinksToRemove = trim(utils::ReadParam('linksToRemove', '', 'post'));
  1128. $aLinksToRemove = array();
  1129. if (!empty($sLinksToRemove))
  1130. {
  1131. $aLinksToRemove = explode(' ', trim($sLinksToRemove));
  1132. }
  1133. $sClass = utils::ReadParam('class', '', 'post');
  1134. $sLinkageAtt = utils::ReadParam('linkage', '', 'post');
  1135. $iObjectId = utils::ReadParam('object_id', '', 'post');
  1136. $sLinkingAttCode = utils::ReadParam('linking_attcode', '', 'post');
  1137. $oMyChange = MetaModel::NewObject("CMDBChange");
  1138. $oMyChange->Set("date", time());
  1139. if (UserRights::GetUser() != UserRights::GetRealUser())
  1140. {
  1141. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  1142. }
  1143. else
  1144. {
  1145. $sUserString = UserRights::GetUser();
  1146. }
  1147. $oMyChange->Set("userinfo", $sUserString);
  1148. $iChangeId = $oMyChange->DBInsert();
  1149. // Delete links that are to be deleted
  1150. foreach($aLinksToRemove as $iLinkId)
  1151. {
  1152. if ($iLinkId > 0) // Negative IDs are objects that were not even created
  1153. {
  1154. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1155. $oLink->DBDeleteTracked($oMyChange);
  1156. }
  1157. }
  1158. $aEditableFields = array();
  1159. $aData = array();
  1160. foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
  1161. {
  1162. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1163. if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  1164. {
  1165. $aEditableFields[] = $sAttCode;
  1166. $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
  1167. }
  1168. }
  1169. // Update existing links or create new links
  1170. foreach($aLinks as $iLinkId)
  1171. {
  1172. if ($iLinkId > 0)
  1173. {
  1174. // This is an existing link to be modified
  1175. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1176. // Update all the attributes of the link
  1177. foreach($aEditableFields as $sAttCode)
  1178. {
  1179. $value = $aData[$sAttCode][$iLinkId];
  1180. $oLink->Set($sAttCode, $value);
  1181. }
  1182. if ($oLink->IsModified())
  1183. {
  1184. $oLink->DBUpdateTracked($oMyChange);
  1185. }
  1186. //echo "Updated link:<br/>\n";
  1187. //var_dump($oLink);
  1188. }
  1189. else
  1190. {
  1191. // A new link must be created
  1192. $oLink = MetaModel::NewObject($sClass);
  1193. $oLinkedObjectId = -$iLinkId;
  1194. // Set all the attributes of the link
  1195. foreach($aEditableFields as $sAttCode)
  1196. {
  1197. $value = $aData[$sAttCode][$iLinkId];
  1198. $oLink->Set($sAttCode, $value);
  1199. }
  1200. // And the two external keys
  1201. $oLink->Set($sLinkageAtt, $iObjectId);
  1202. $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
  1203. // then save it
  1204. //echo "Created link:<br/>\n";
  1205. //var_dump($oLink);
  1206. $oLink->DBInsertTracked($oMyChange);
  1207. }
  1208. }
  1209. // Display again the details of the linked object
  1210. $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
  1211. $sTargetClass = $oAttDef->GetTargetClass();
  1212. $oObj = $oContext->GetObject($sTargetClass, $iObjectId);
  1213. $oSearch = $oContext->NewFilter(get_class($oObj));
  1214. $oBlock = new DisplayBlock($oSearch, 'search', false);
  1215. $oBlock->Display($oP, 0);
  1216. $oObj->DisplayDetails($oP);
  1217. break;
  1218. case 'swf_navigator':
  1219. $sClass = utils::ReadParam('class', '');
  1220. $id = utils::ReadParam('id', 0);
  1221. $sRelation = utils::ReadParam('relation', 'neighbours');
  1222. $width = 1000;
  1223. $height = 700;
  1224. $sParams = "pWidth=$width&pHeight=$height&drillUrl=".urlencode('../pages/UI.php?operation=details')."&displayController=false&xmlUrl=".urlencode("./xml.navigator.php")."&obj_class=$sClass&obj_id=$id&relation=$sRelation";
  1225. $oP->add("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"$width\" height=\"$height\" id=\"navigator\" align=\"middle\">
  1226. <param name=\"allowScriptAccess\" value=\"sameDomain\" />
  1227. <param name=\"allowFullScreen\" value=\"false\" />
  1228. <param name=\"FlashVars\" value=\"$sParams\" />
  1229. <param name=\"movie\" value=\"../navigator/navigator.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#ffffff\" />
  1230. <embed src=\"../navigator/navigator.swf\" flashVars=\"$sParams\" quality=\"high\" bgcolor=\"#ffffff\" width=\"$width\" height=\"$height\" name=\"navigator\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.adobe.com/go/getflashplayer\" />
  1231. </object>\n");
  1232. break;
  1233. default:
  1234. if (is_object($oActiveNode))
  1235. {
  1236. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  1237. $oP->set_title($oActiveNode->GetMenuLabel());
  1238. }
  1239. }
  1240. ////MetaModel::ShowQueryTrace();
  1241. $oP->output();
  1242. }
  1243. catch(Exception $e)
  1244. {
  1245. require_once('../setup/setuppage.class.inc.php');
  1246. $oP = new SetupWebPage('iTop - fatal error');
  1247. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1248. $oP->error("Error: '".$e->getMessage()."'");
  1249. $oP->output();
  1250. }
  1251. catch(CoreException $e)
  1252. {
  1253. require_once('../setup/setuppage.class.inc.php');
  1254. $oP = new SetupWebPage('iTop - fatal error');
  1255. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1256. $oP->error("Error: '".$e->getHtmlDesc()."'");
  1257. $oP->output();
  1258. }
  1259. ?>