UI.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  731. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  732. {
  733. // Non-visible, or read-only attribute, do nothing
  734. }
  735. else if ($sAttCode == 'finalclass')
  736. {
  737. // This very specific field is read-only
  738. }
  739. else if ($oAttDef->IsLinkSet())
  740. {
  741. // Link set, the data is a set of link objects, encoded in JSON
  742. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  743. if (!empty($aAttributes[$sAttCode]))
  744. {
  745. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  746. $oObj->Set($sAttCode, $oLinkSet);
  747. // TO DO: detect a real modification, for now always update !!
  748. $bObjectModified = true;
  749. }
  750. }
  751. else if ($oAttDef->GetEditClass() == 'Document')
  752. {
  753. // There should be an uploaded file with the named attr_<attCode>
  754. $oDocument = utils::ReadPostedDocument('file_'.$sAttCode);
  755. if (!$oDocument->IsEmpty())
  756. {
  757. // A new file has been uploaded
  758. $oObj->Set($sAttCode, $oDocument);
  759. $bObjectModified = true;
  760. }
  761. }
  762. else if (!$oAttDef->IsExternalField())
  763. {
  764. $rawValue = utils::ReadPostedParam("attr_$sAttCode", null);
  765. if (!is_null($rawValue))
  766. {
  767. $aAttributes[$sAttCode] = trim($rawValue);
  768. $previousValue = $oObj->Get($sAttCode);
  769. if ($previousValue !== $aAttributes[$sAttCode])
  770. {
  771. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  772. $bObjectModified = true;
  773. }
  774. }
  775. }
  776. }
  777. if (!$bObjectModified)
  778. {
  779. $oP->p("No modification detected. ".MetaModel::GetName(get_class($oObj))." has <strong>not</strong> been updated.\n");
  780. }
  781. else if ($oObj->CheckToUpdate())
  782. {
  783. $oMyChange = MetaModel::NewObject("CMDBChange");
  784. $oMyChange->Set("date", time());
  785. if (UserRights::GetUser() != UserRights::GetRealUser())
  786. {
  787. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  788. }
  789. else
  790. {
  791. $sUserString = UserRights::GetUser();
  792. }
  793. $oMyChange->Set("userinfo", $sUserString);
  794. $iChangeId = $oMyChange->DBInsert();
  795. $oObj->DBUpdateTracked($oMyChange);
  796. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  797. }
  798. else
  799. {
  800. $oP->p("<strong>Error: object can not be updated!</strong>\n");
  801. //$oObj->Reload(); // restore default values!
  802. }
  803. }
  804. else
  805. {
  806. $oP->set_title("iTop - Error");
  807. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  808. }
  809. }
  810. $oObj->DisplayDetails($oP);
  811. break;
  812. case 'select_for_deletion':
  813. $sFilter = utils::ReadParam('filter', '');
  814. $sFormat = utils::ReadParam('format', '');
  815. $bSearchForm = utils::ReadParam('search_form', true);
  816. if (empty($sFilter))
  817. {
  818. $oP->set_title("iTop - Error");
  819. $oP->add("<p>'filter' must be specifed for this operation.</p>\n");
  820. }
  821. else
  822. {
  823. $oP->set_title("iTop - mass delete");
  824. $oP->add("<h1> Select the objects you want to delete</h1>\n");
  825. // TO DO: limit the search filter by the user context
  826. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  827. $oSet = new DBObjectSet($oFilter);
  828. if ($bSearchForm)
  829. {
  830. $oBlock = new DisplayBlock($oFilter, 'search', false);
  831. $oBlock->Display($oP, 0);
  832. }
  833. $oBlock = new DisplayBlock($oFilter, 'list', false);
  834. $oP->add("<form method=\"post\">\n");
  835. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete\">\n");
  836. $oP->add("<input type=\"hidden\" name=\"class\" value=\"".$oFilter->GetClass()."\">\n");
  837. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  838. $oBlock->Display($oP, 1, array('selection_type' => 'multiple', 'selection_mode' => true, 'display_limit' => false));
  839. $oP->add("<input type=\"button\" value=\" Cancel \" onClick=\"window.history.back()\">&nbsp;&nbsp;<input type=\"submit\" value=\" Next >>\">\n");
  840. $oP->add("</form>\n");
  841. }
  842. break;
  843. case 'bulk_delete':
  844. case 'bulk_delete_confirmed':
  845. $sClass = utils::ReadPostedParam('class', '');
  846. $sClassLabel = MetaModel::GetName($sClass);
  847. $aSelectObject = utils::ReadPostedParam('selectObject', '');
  848. $aObjects = array();
  849. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  850. if ( empty($sClass) || empty($aSelectObject)) // TO DO: check that the class name is valid !
  851. {
  852. throw new ApplicationException('Error: \'class\' and \'selectObject[]\' parameters must be specifed for this operation.');
  853. }
  854. if (!utils::IsTransactionValid($sTransactionId))
  855. {
  856. throw new ApplicationException('Error: objects have already been deleted!');
  857. }
  858. foreach($aSelectObject as $iId)
  859. {
  860. $aObjects[] = $oContext->GetObject($sClass, $iId);
  861. }
  862. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, DBObjectSet::FromArray($sClass, $aObjects)))
  863. {
  864. throw new SecurityException('You are not allowed to perform a bulk delete of objects of class '.$sClass);
  865. }
  866. DeleteObjects($oP, $sClass, $aObjects, ($operation == 'bulk_delete_confirmed'));
  867. break;
  868. case 'delete':
  869. case 'delete_confirmed':
  870. $sClass = utils::ReadParam('class', '');
  871. $sClassLabel = MetaModel::GetName($sClass);
  872. $id = utils::ReadParam('id', '');
  873. $oObj = $oContext->GetObject($sClass, $id);
  874. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oObj)))
  875. {
  876. throw new SecurityException('You are not allowed to do delete objects of class '.$sClass);
  877. }
  878. DeleteObjects($oP, $sClass, array($oObj), ($operation == 'delete_confirmed'));
  879. break;
  880. case 'apply_new':
  881. $oP->p('Creation of the object');
  882. $oP->p('Obsolete, should now go through the wizard...');
  883. break;
  884. case 'apply_clone':
  885. $sClass = utils::ReadPostedParam('class', '');
  886. $sClassLabel = MetaModel::GetName($sClass);
  887. $iCloneId = utils::ReadPostedParam('clone_id', '');
  888. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  889. if (!utils::IsTransactionValid($sTransactionId))
  890. {
  891. $oP->p("<strong>Error: object has already be cloned!</strong>\n");
  892. }
  893. else
  894. {
  895. $oObj = $oContext->GetObject($sClass, $iCloneId);
  896. $oMyChange = MetaModel::NewObject("CMDBChange");
  897. $oMyChange->Set("date", time());
  898. if (UserRights::GetUser() != UserRights::GetRealUser())
  899. {
  900. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  901. }
  902. else
  903. {
  904. $sUserString = UserRights::GetUser();
  905. }
  906. $oMyChange->Set("userinfo", $sUserString);
  907. $iChangeId = $oMyChange->DBInsert();
  908. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  909. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  910. {
  911. if ( ('finalclass' != $sAttCode) && // finalclass is a reserved word, hardcoded !
  912. ($sStateAttCode != $sAttCode) &&
  913. (!$oAttDef->IsExternalField()) )
  914. {
  915. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  916. $oObj->Set($sAttCode, $value);
  917. }
  918. }
  919. $oObj->DBCloneTracked($oMyChange);
  920. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  921. $oObj->DisplayDetails($oP);
  922. }
  923. break;
  924. case 'wizard_apply_new':
  925. $sJson = utils::ReadPostedParam('json_obj', '');
  926. $oWizardHelper = WizardHelper::FromJSON($sJson);
  927. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  928. if (!utils::IsTransactionValid($sTransactionId))
  929. {
  930. $oP->p("<strong>Error: object has already be created!</strong>\n");
  931. }
  932. else
  933. {
  934. $oObj = $oWizardHelper->GetTargetObject(true /* read uploaded files */);
  935. if (is_object($oObj))
  936. {
  937. $sClass = get_class($oObj);
  938. $sClassLabel = MetaModel::GetName($sClass);
  939. $oMyChange = MetaModel::NewObject("CMDBChange");
  940. $oMyChange->Set("date", time());
  941. if (UserRights::GetUser() != UserRights::GetRealUser())
  942. {
  943. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  944. }
  945. else
  946. {
  947. $sUserString = UserRights::GetUser();
  948. }
  949. $oMyChange->Set("userinfo", $sUserString);
  950. $iChangeId = $oMyChange->DBInsert();
  951. $oObj->DBInsertTracked($oMyChange);
  952. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel created");
  953. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  954. $oObj->DisplayDetails($oP);
  955. }
  956. }
  957. break;
  958. case 'stimulus':
  959. $sClass = utils::ReadParam('class', '');
  960. $id = utils::ReadParam('id', '');
  961. $sStimulus = utils::ReadParam('stimulus', '');
  962. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  963. {
  964. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  965. }
  966. else
  967. {
  968. $oObj = $oContext->GetObject($sClass, $id);
  969. if ($oObj != null)
  970. {
  971. $aTransitions = $oObj->EnumTransitions();
  972. $aStimuli = MetaModel::EnumStimuli($sClass);
  973. if (!isset($aTransitions[$sStimulus]))
  974. {
  975. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()}.</p>\n");
  976. }
  977. else
  978. {
  979. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  980. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  981. $aTransition = $aTransitions[$sStimulus];
  982. $sTargetState = $aTransition['target_state'];
  983. $aTargetStates = MetaModel::EnumStates($sClass);
  984. $oP->add("<div class=\"page_header\">\n");
  985. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  986. //$oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()} to target state: $sTargetState.</p>\n");
  987. $oP->add("</div>\n");
  988. $oObj->DisplayBareDetails($oP);
  989. $aTargetState = $aTargetStates[$sTargetState];
  990. //print_r($aTransitions[$sStimulus]);
  991. //print_r($aTargetState);
  992. $aExpectedAttributes = $aTargetState['attribute_list'];
  993. $oP->add("<div class=\"wizHeader\">\n");
  994. $oP->add("<h1>$sActionDetails</h1>\n");
  995. $oP->add("<div class=\"wizContainer\">\n");
  996. $oP->add("<form method=\"post\">\n");
  997. $aDetails = array();
  998. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  999. {
  1000. // Prompt for an attribute if
  1001. // - the attribute must be changed or must be displayed to the user for confirmation
  1002. // - or the field is mandatory and currently empty
  1003. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  1004. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  1005. {
  1006. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  1007. $oAttDef = $aAttributesDef[$sAttCode];
  1008. $aArgs = array('this' => $oObj);
  1009. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetEditValue($sAttCode), '', '', $iExpectCode, $aArgs);
  1010. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1011. }
  1012. }
  1013. $oP->details($aDetails);
  1014. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  1015. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1016. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  1017. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  1018. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1019. $oP->add($oAppContext->GetForForm());
  1020. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1021. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  1022. $oP->add("</form>\n");
  1023. $oP->add("</div>\n");
  1024. $oP->add("</div>\n");
  1025. }
  1026. }
  1027. else
  1028. {
  1029. $oP->set_title("iTop - Error");
  1030. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  1031. }
  1032. }
  1033. break;
  1034. case 'apply_stimulus':
  1035. $sClass = utils::ReadPostedParam('class', '');
  1036. $id = utils::ReadPostedParam('id', '');
  1037. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  1038. $sStimulus = utils::ReadPostedParam('stimulus', '');
  1039. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  1040. {
  1041. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  1042. }
  1043. else
  1044. {
  1045. $oObj = $oContext->GetObject($sClass, $id);
  1046. if ($oObj != null)
  1047. {
  1048. $aTransitions = $oObj->EnumTransitions();
  1049. $aStimuli = MetaModel::EnumStimuli($sClass);
  1050. if (!isset($aTransitions[$sStimulus]))
  1051. {
  1052. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()}.</p>\n");
  1053. }
  1054. else if (!utils::IsTransactionValid($sTransactionId))
  1055. {
  1056. $oP->p("<strong>Error: object has already been updated!</strong>\n");
  1057. }
  1058. else
  1059. {
  1060. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  1061. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  1062. $aTransition = $aTransitions[$sStimulus];
  1063. $sTargetState = $aTransition['target_state'];
  1064. $aTargetStates = MetaModel::EnumStates($sClass);
  1065. $oP->add("<div class=\"page_header\">\n");
  1066. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  1067. $oP->add("<p>$sActionDetails</p>\n");
  1068. $oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetStateLabel()} to target state: $sTargetState.</p>\n");
  1069. $oP->add("</div>\n");
  1070. $aTargetState = $aTargetStates[$sTargetState];
  1071. //print_r($aTransitions[$sStimulus]);
  1072. //print_r($aTargetState);
  1073. $aExpectedAttributes = $aTargetState['attribute_list'];
  1074. $aDetails = array();
  1075. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  1076. {
  1077. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  1078. {
  1079. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  1080. $oObj->Set($sAttCode, $paramValue);
  1081. }
  1082. }
  1083. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  1084. {
  1085. $oMyChange = MetaModel::NewObject("CMDBChange");
  1086. $oMyChange->Set("date", time());
  1087. if (UserRights::GetUser() != UserRights::GetRealUser())
  1088. {
  1089. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  1090. }
  1091. else
  1092. {
  1093. $sUserString = UserRights::GetUser();
  1094. }
  1095. $oMyChange->Set("userinfo", $sUserString);
  1096. $iChangeId = $oMyChange->DBInsert();
  1097. $oObj->DBUpdateTracked($oMyChange);
  1098. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  1099. }
  1100. $oObj->DisplayDetails($oP);
  1101. }
  1102. }
  1103. else
  1104. {
  1105. $oP->set_title("iTop - Error");
  1106. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  1107. }
  1108. }
  1109. break;
  1110. case 'modify_links':
  1111. $sClass = utils::ReadParam('class', '');
  1112. $sLinkAttr = utils::ReadParam('link_attr', '');
  1113. $sTargetClass = utils::ReadParam('target_class', '');
  1114. $id = utils::ReadParam('id', '');
  1115. $bAddObjects = utils::ReadParam('addObjects', false);
  1116. if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
  1117. {
  1118. $oP->set_title("iTop - Error");
  1119. $oP->add("<p>4 parameters are mandatory for this operation: class, id, target_class and link_attr.</p>\n");
  1120. }
  1121. else
  1122. {
  1123. require_once('../application/uilinkswizard.class.inc.php');
  1124. $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
  1125. $oWizard->Display($oP, $oContext, array('StartWithAdd' => $bAddObjects));
  1126. }
  1127. break;
  1128. case 'do_modify_links':
  1129. $aLinks = utils::ReadParam('linkId', array(), 'post');
  1130. $sLinksToRemove = trim(utils::ReadParam('linksToRemove', '', 'post'));
  1131. $aLinksToRemove = array();
  1132. if (!empty($sLinksToRemove))
  1133. {
  1134. $aLinksToRemove = explode(' ', trim($sLinksToRemove));
  1135. }
  1136. $sClass = utils::ReadParam('class', '', 'post');
  1137. $sLinkageAtt = utils::ReadParam('linkage', '', 'post');
  1138. $iObjectId = utils::ReadParam('object_id', '', 'post');
  1139. $sLinkingAttCode = utils::ReadParam('linking_attcode', '', 'post');
  1140. $oMyChange = MetaModel::NewObject("CMDBChange");
  1141. $oMyChange->Set("date", time());
  1142. if (UserRights::GetUser() != UserRights::GetRealUser())
  1143. {
  1144. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  1145. }
  1146. else
  1147. {
  1148. $sUserString = UserRights::GetUser();
  1149. }
  1150. $oMyChange->Set("userinfo", $sUserString);
  1151. $iChangeId = $oMyChange->DBInsert();
  1152. // Delete links that are to be deleted
  1153. foreach($aLinksToRemove as $iLinkId)
  1154. {
  1155. if ($iLinkId > 0) // Negative IDs are objects that were not even created
  1156. {
  1157. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1158. $oLink->DBDeleteTracked($oMyChange);
  1159. }
  1160. }
  1161. $aEditableFields = array();
  1162. $aData = array();
  1163. foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
  1164. {
  1165. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1166. if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  1167. {
  1168. $aEditableFields[] = $sAttCode;
  1169. $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
  1170. }
  1171. }
  1172. // Update existing links or create new links
  1173. foreach($aLinks as $iLinkId)
  1174. {
  1175. if ($iLinkId > 0)
  1176. {
  1177. // This is an existing link to be modified
  1178. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1179. // Update all the attributes of the link
  1180. foreach($aEditableFields as $sAttCode)
  1181. {
  1182. $value = $aData[$sAttCode][$iLinkId];
  1183. $oLink->Set($sAttCode, $value);
  1184. }
  1185. if ($oLink->IsModified())
  1186. {
  1187. $oLink->DBUpdateTracked($oMyChange);
  1188. }
  1189. //echo "Updated link:<br/>\n";
  1190. //var_dump($oLink);
  1191. }
  1192. else
  1193. {
  1194. // A new link must be created
  1195. $oLink = MetaModel::NewObject($sClass);
  1196. $oLinkedObjectId = -$iLinkId;
  1197. // Set all the attributes of the link
  1198. foreach($aEditableFields as $sAttCode)
  1199. {
  1200. $value = $aData[$sAttCode][$iLinkId];
  1201. $oLink->Set($sAttCode, $value);
  1202. }
  1203. // And the two external keys
  1204. $oLink->Set($sLinkageAtt, $iObjectId);
  1205. $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
  1206. // then save it
  1207. //echo "Created link:<br/>\n";
  1208. //var_dump($oLink);
  1209. $oLink->DBInsertTracked($oMyChange);
  1210. }
  1211. }
  1212. // Display again the details of the linked object
  1213. $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
  1214. $sTargetClass = $oAttDef->GetTargetClass();
  1215. $oObj = $oContext->GetObject($sTargetClass, $iObjectId);
  1216. $oSearch = $oContext->NewFilter(get_class($oObj));
  1217. $oBlock = new DisplayBlock($oSearch, 'search', false);
  1218. $oBlock->Display($oP, 0);
  1219. $oObj->DisplayDetails($oP);
  1220. break;
  1221. default:
  1222. if (is_object($oActiveNode))
  1223. {
  1224. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  1225. $oP->set_title($oActiveNode->GetMenuLabel());
  1226. }
  1227. }
  1228. ////MetaModel::ShowQueryTrace();
  1229. $oP->output();
  1230. }
  1231. catch(Exception $e)
  1232. {
  1233. require_once('../setup/setuppage.class.inc.php');
  1234. $oP = new SetupWebPage('iTop - fatal error');
  1235. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1236. $oP->error("Error: '".$e->getMessage()."'");
  1237. $oP->output();
  1238. }
  1239. catch(CoreException $e)
  1240. {
  1241. require_once('../setup/setuppage.class.inc.php');
  1242. $oP = new SetupWebPage('iTop - fatal error');
  1243. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1244. $oP->error("Error: '".$e->getHtmlDesc()."'");
  1245. $oP->output();
  1246. }
  1247. ?>