UI.php 49 KB

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