UI.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
  294. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
  295. $oP->add("<input DISABLED type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
  296. $oP->add("</form>\n");
  297. }
  298. else
  299. {
  300. if (count($aObjects) == 1)
  301. {
  302. $oObj = $aObjects[0];
  303. $id = $oObj->GetKey();
  304. $oP->p('<h1>'.Dict::Format('UI:Delect:Confirm_Object', $oObj->GetHyperLink()).'</h1>');
  305. $oP->add("<form method=\"post\">\n");
  306. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
  307. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"delete_confirmed\">\n");
  308. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  309. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  310. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
  311. $oP->add("<input type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
  312. $oP->add("</form>\n");
  313. }
  314. else
  315. {
  316. $oP->p('<h1>'.Dict::Format('UI:Delect:Confirm_Count_ObjectsOf_Class', count($aObjects), MetaModel::GetName($sClass)).'</h1>');
  317. $oSet = CMDBobjectSet::FromArray($sClass, $aObjects);
  318. CMDBAbstractObject::DisplaySet($oP, $oSet, array('display_limit' => false, 'menu' => false));
  319. $oP->add("<form method=\"post\">\n");
  320. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::ReadParam('transaction_id')."\">\n");
  321. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete_confirmed\">\n");
  322. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  323. foreach($aObjects as $oObj)
  324. {
  325. $oP->add("<input type=\"hidden\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\">\n");
  326. }
  327. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\"".Dict::S('UI:Button:Back')."\">\n");
  328. $oP->add("<input type=\"submit\" name=\"\" value=\"".Dict::S('UI:Button:Delete')."\">\n");
  329. $oP->add("</form>\n");
  330. }
  331. }
  332. }
  333. }
  334. /***********************************************************************************
  335. *
  336. * Main user interface page, starts here
  337. *
  338. * ***********************************************************************************/
  339. try
  340. {
  341. require_once('../application/application.inc.php');
  342. require_once('../application/itopwebpage.class.inc.php');
  343. require_once('../application/wizardhelper.class.inc.php');
  344. require_once('../application/startup.inc.php');
  345. $oContext = new UserContext();
  346. $oAppContext = new ApplicationContext();
  347. $iActiveNodeId = utils::ReadParam('menu', '');
  348. if (empty($iActiveNodeId) && !is_numeric($iActiveNodeId))
  349. {
  350. // No menu specified, let's get the default one:
  351. // 1) It's a root menu item (parent_id == 0)
  352. // 2) with the lowest rank
  353. $oFilter = DBObjectSearch::FromOQL('SELECT menuNode AS M WHERE M.parent_id = 0');
  354. if ($oFilter)
  355. {
  356. $oMenuSet = new CMDBObjectSet($oFilter);
  357. while($oMenu = $oMenuSet->Fetch())
  358. {
  359. $aRanks[$oMenu->GetKey()] = $oMenu->Get('rank');
  360. }
  361. asort($aRanks); // sort by ascending rank: menuId => rank
  362. $aKeys = array_keys($aRanks);
  363. $iActiveNodeId = array_shift($aKeys); // Takes the first key, i.e. the menuId with the lowest rank
  364. }
  365. }
  366. $currentOrganization = utils::ReadParam('org_id', '');
  367. $operation = utils::ReadParam('operation', '');
  368. require_once('../application/loginwebpage.class.inc.php');
  369. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  370. $oP = new iTopWebPage(Dict::S('UI:WelcomeToITop'), $currentOrganization);
  371. // From now on the context is limited to the the selected organization ??
  372. if ($iActiveNodeId != -1)
  373. {
  374. $oActiveNode = $oContext->GetObject('menuNode', $iActiveNodeId);
  375. }
  376. else
  377. {
  378. $oActiveNode = null;
  379. }
  380. switch($operation)
  381. {
  382. case 'details':
  383. $sClass = utils::ReadParam('class', '');
  384. $sClassLabel = MetaModel::GetName($sClass);
  385. $id = utils::ReadParam('id', '');
  386. $oSearch = new DBObjectSearch($sClass);
  387. $oBlock = new DisplayBlock($oSearch, 'search', false);
  388. $oBlock->Display($oP, 0);
  389. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  390. {
  391. throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
  392. }
  393. $oObj = $oContext->GetObject($sClass, $id);
  394. if ($oObj != null)
  395. {
  396. $oP->set_title(Dict::Format('UI:DetailsPageTitle', $oObj->GetDisplayName(), $sClassLabel));
  397. $oObj->DisplayDetails($oP);
  398. }
  399. else
  400. {
  401. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  402. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  403. }
  404. break;
  405. case 'search_oql':
  406. $sOQLClass = utils::ReadParam('oql_class', '');
  407. $sOQLClause = utils::ReadParam('oql_clause', '');
  408. $sFormat = utils::ReadParam('format', '');
  409. $bSearchForm = utils::ReadParam('search_form', true);
  410. if (empty($sOQLClass))
  411. {
  412. throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'oql_class'));
  413. }
  414. $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
  415. $sOQL = "SELECT $sOQLClass $sOQLClause";
  416. try
  417. {
  418. $oFilter = DBObjectSearch::FromOQL($sOQL); // To Do: Make sure we don't bypass security
  419. $oSet = new DBObjectSet($oFilter);
  420. if ($bSearchForm)
  421. {
  422. $oBlock = new DisplayBlock($oFilter, 'search', false);
  423. $oBlock->Display($oP, 0);
  424. }
  425. if (strtolower($sFormat) == 'csv')
  426. {
  427. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  428. $oBlock->Display($oP, 'csv');
  429. $oPage->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  430. }
  431. else
  432. {
  433. $oBlock = new DisplayBlock($oFilter, 'list', false);
  434. $oBlock->Display($oP, 1);
  435. }
  436. }
  437. catch(CoreException $e)
  438. {
  439. $oFilter = new DBObjectSearch($sOQLClass); // To Do: Make sure we don't bypass security
  440. $oSet = new DBObjectSet($oFilter);
  441. if ($bSearchForm)
  442. {
  443. $oBlock = new DisplayBlock($oFilter, 'search', false);
  444. $oBlock->Display($oP, 0);
  445. }
  446. $oP->P('<b>'.Dict::Format('UI:Error:IncorrectOQLQuery_Message', $e->getHtmlDesc()).'</b>');
  447. }
  448. catch(Exception $e)
  449. {
  450. $oP->P('<b>'.Dict::Format('UI:Error:AnErrorOccuredWhileRunningTheQuery_Message', $e->getMessage()).'</b>');
  451. }
  452. break;
  453. case 'search_form':
  454. $sClass = utils::ReadParam('class', '');
  455. $sFormat = utils::ReadParam('format', 'html');
  456. $bSearchForm = utils::ReadParam('search_form', true);
  457. if (empty($sClass))
  458. {
  459. throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'class'));
  460. }
  461. $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
  462. $oFilter = $oContext->NewFilter($sClass);
  463. $oSet = new DBObjectSet($oFilter);
  464. if ($bSearchForm)
  465. {
  466. $oBlock = new DisplayBlock($oFilter, 'search', false /* Asynchronous */, array('open' => true));
  467. $oBlock->Display($oP, 0);
  468. }
  469. if (strtolower($sFormat) == 'csv')
  470. {
  471. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  472. $oBlock->Display($oP, 1);
  473. $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  474. }
  475. else
  476. {
  477. $oBlock = new DisplayBlock($oFilter, 'list', false);
  478. $oBlock->Display($oP, 1);
  479. }
  480. break;
  481. case 'search':
  482. $sFilter = utils::ReadParam('filter', '');
  483. $sFormat = utils::ReadParam('format', '');
  484. $bSearchForm = utils::ReadParam('search_form', true);
  485. if (empty($sFilter))
  486. {
  487. throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'filter'));
  488. }
  489. $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
  490. // TO DO: limit the search filter by the user context
  491. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  492. $oSet = new DBObjectSet($oFilter);
  493. if ($bSearchForm)
  494. {
  495. $oBlock = new DisplayBlock($oFilter, 'search', false);
  496. $oBlock->Display($oP, 0);
  497. }
  498. if (strtolower($sFormat) == 'csv')
  499. {
  500. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  501. $oBlock->Display($oP, 'csv');
  502. $oP->add_ready_script(" $('#csv').css('height', '95%');"); // adjust the size of the block
  503. }
  504. else
  505. {
  506. $oBlock = new DisplayBlock($oFilter, 'list', false);
  507. $oBlock->Display($oP, 1);
  508. }
  509. break;
  510. case 'full_text':
  511. $sFullText = trim(utils::ReadParam('text', ''));
  512. if (empty($sFullText))
  513. {
  514. $oP->p(Dict::S('UI:Search:NoSearch'));
  515. }
  516. else
  517. {
  518. $oP->set_title(Dict::S('UI:SearchResultsPageTitle'));
  519. $oP->p("<h1>".Dict::Format('UI:FullTextSearchTitle_Text', $sFullText)."</h1>");
  520. $iCount = 0;
  521. $iBlock = 0;
  522. // Search in full text mode in all the classes
  523. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  524. {
  525. $oFilter = new DBObjectSearch($sClassName);
  526. $oFilter->AddCondition_FullText($sFullText);
  527. $oSet = new DBObjectSet($oFilter);
  528. if ($oSet->Count() > 0)
  529. {
  530. $aLeafs = array();
  531. while($oObj = $oSet->Fetch())
  532. {
  533. if (get_class($oObj) == $sClassName)
  534. {
  535. $aLeafs[] = $oObj->GetKey();
  536. }
  537. }
  538. $oLeafsFilter = new DBObjectSearch($sClassName);
  539. if (count($aLeafs) > 0)
  540. {
  541. $iCount += count($aLeafs);
  542. $oP->add("<div class=\"page_header\">\n");
  543. $oP->add("<h2><span class=\"hilite\">".Dict::Format('UI:Search:Count_ObjectsOf_Class_Found', count($aLeafs), Metamodel::GetName($sClassName))."</h2>\n");
  544. $oP->add("</div>\n");
  545. $oLeafsFilter->AddCondition('id', $aLeafs, 'IN');
  546. $oBlock = new DisplayBlock($oLeafsFilter, 'list', false);
  547. $oBlock->Display($oP, $iBlock++);
  548. }
  549. }
  550. }
  551. if ($iCount == 0)
  552. {
  553. $oP->p(Dict::S('UI:Search:NoObjectFound'));
  554. }
  555. }
  556. break;
  557. case 'modify':
  558. $oP->add_linked_script("../js/json.js");
  559. $oP->add_linked_script("../js/forms-json-utils.js");
  560. $oP->add_linked_script("../js/wizardhelper.js");
  561. $oP->add_linked_script("../js/wizard.utils.js");
  562. $oP->add_linked_script("../js/linkswidget.js");
  563. $oP->add_linked_script("../js/jquery.blockUI.js");
  564. $sClass = utils::ReadParam('class', '');
  565. $sClassLabel = MetaModel::GetName($sClass);
  566. $id = utils::ReadParam('id', '');
  567. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  568. {
  569. throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
  570. }
  571. // Check if the user can modify this object
  572. $oSearch = new DBObjectSearch($sClass);
  573. $oSearch->AddCondition('id', $id, '=');
  574. $oSet = new CMDBObjectSet($oSearch);
  575. if ($oSet->Count() > 0)
  576. {
  577. $oObj = $oSet->Fetch();
  578. }
  579. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES) && !MetaModel::IsReadOnlyClass($sClass);
  580. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  581. if( ($oObj != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  582. {
  583. $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
  584. $oP->add("<div class=\"page_header\">\n");
  585. $oP->add("<h1>".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
  586. $oP->add("</div>\n");
  587. $oP->add("<div class=\"wizContainer\">\n");
  588. $oObj->DisplayModifyForm($oP);
  589. $oP->add("</div>\n");
  590. }
  591. else
  592. {
  593. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  594. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  595. }
  596. break;
  597. case 'clone':
  598. $sClass = utils::ReadParam('class', '');
  599. $sClassLabel = MetaModel::GetName($sClass);
  600. $id = utils::ReadParam('id', '');
  601. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  602. {
  603. throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
  604. }
  605. // Check if the user can modify this object
  606. $oSearch = new DBObjectSearch($sClass);
  607. $oSearch->AddCondition('id', $id, '=');
  608. $oSet = new CMDBObjectSet($oSearch);
  609. if ($oSet->Count() > 0)
  610. {
  611. $oObjToClone = $oSet->Fetch();
  612. }
  613. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES) && !MetaModel::IsReadOnlyClass($sClass);
  614. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  615. if( ($oObjToClone != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  616. {
  617. $oP->add_linked_script("../js/json.js");
  618. $oP->add_linked_script("../js/forms-json-utils.js");
  619. $oP->add_linked_script("../js/wizardhelper.js");
  620. $oP->add_linked_script("../js/wizard.utils.js");
  621. $oP->add_linked_script("../js/linkswidget.js");
  622. $oP->add_linked_script("../js/jquery.blockUI.js");
  623. $oP->set_title(Dict::Format('UI:ClonePageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
  624. $oP->add("<div class=\"page_header\">\n");
  625. $oP->add("<h1>".Dict::Format('UI:CloneTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
  626. $oP->add("</div>\n");
  627. $oP->add("<div class=\"wizContainer\">\n");
  628. cmdbAbstractObject::DisplayCreationForm($oP, $sClass, $oObjToClone);
  629. $oP->add("</div>\n");
  630. }
  631. else
  632. {
  633. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  634. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  635. }
  636. break;
  637. case 'new':
  638. $sClass = utils::ReadParam('class', '');
  639. $sStateCode = utils::ReadParam('state', '');
  640. if ( empty($sClass) )
  641. {
  642. throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'class'));
  643. }
  644. $oP->add_linked_script("../js/json.js");
  645. $oP->add_linked_script("../js/forms-json-utils.js");
  646. $oP->add_linked_script("../js/wizardhelper.js");
  647. $oP->add_linked_script("../js/wizard.utils.js");
  648. $oP->add_linked_script("../js/linkswidget.js");
  649. $oP->add_linked_script("../js/jquery.blockUI.js");
  650. $oWizard = new UIWizard($oP, $sClass, $sStateCode);
  651. $oContext = new UserContext();
  652. $aArgs = array_merge($oAppContext->GetAsHash(), utils::ReadParam('default', array()));
  653. $sStateCode = $oWizard->GetTargetState(); // Will computes the default state if none was supplied
  654. $sClassLabel = MetaModel::GetName($sClass);
  655. $oP->set_title(Dict::Format('UI:CreationPageTitle_Class', $sClassLabel));
  656. $oP->add("<h1>".Dict::Format('UI:CreationTitle_Class', $sClassLabel)."</h1>\n");
  657. if (!empty($sStateCode))
  658. {
  659. $sStateLabel = MetaModel::GetStateLabel($sClass, $sStateCode);
  660. }
  661. $aWizardSteps = $oWizard->GetWizardStructure();
  662. // Display the structure of the wizard
  663. $iStepIndex = 1;
  664. $iMaxInputId = 0;
  665. $aFieldsMap = array();
  666. foreach($aWizardSteps['mandatory'] as $aSteps)
  667. {
  668. $oP->SetCurrentTab("Step $iStepIndex *");
  669. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, false /* no finish button */, $aArgs);
  670. $iStepIndex++;
  671. }
  672. foreach($aWizardSteps['optional'] as $aSteps)
  673. {
  674. $oP->SetCurrentTab("Step $iStepIndex");
  675. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true, $aArgs); // true means enable the finish button
  676. $iStepIndex++;
  677. }
  678. $oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);
  679. $oObj = null;
  680. if (!empty($id))
  681. {
  682. $oObj = $oContext->GetObject($sClass, $id);
  683. }
  684. if (!is_object($oObj))
  685. {
  686. // new object or that can't be retrieved (corrupted id or object not allowed to this user)
  687. $id = '';
  688. $oObj = MetaModel::NewObject($sClass);
  689. }
  690. $oP->add("<script type=\"text/javascript\">
  691. // Fill the map between the fields of the form and the attributes of the object\n");
  692. $aNewFieldsMap = array();
  693. foreach($aFieldsMap as $id => $sFieldCode)
  694. {
  695. $aNewFieldsMap[$sFieldCode] = $id;
  696. }
  697. $iFieldsCount = count($aFieldsMap);
  698. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  699. $oP->add("
  700. // Initializes the object once at the beginning of the page...
  701. var oWizardHelper = new WizardHelper('$sClass');
  702. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  703. oWizardHelper.SetFieldsCount($iFieldsCount);
  704. ActivateStep(1);
  705. </script>\n");
  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. throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'id'));
  715. }
  716. $oObj = $oContext->GetObject($sClass, $id);
  717. if (!utils::IsTransactionValid($sTransactionId))
  718. {
  719. $oP->p("<strong>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</strong>\n");
  720. }
  721. else
  722. {
  723. if ($oObj != null)
  724. {
  725. $oP->set_title(Dict::Format('UI:ModificationPageTitle_Object_Class', $oObj->GetName(), $sClassLabel));
  726. $oP->add("<h1>".Dict::Format('UI:ModificationTitle_Class_Object', $sClassLabel, $oObj->GetName())."</h1>\n");
  727. foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
  728. {
  729. if ($oAttDef->IsLinkSet())
  730. {
  731. // Link set, the data is a set of link objects, encoded in JSON
  732. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  733. if (!empty($aAttributes[$sAttCode]))
  734. {
  735. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  736. $oObj->Set($sAttCode, $oLinkSet);
  737. // TO DO: detect a real modification, for now always update !!
  738. }
  739. }
  740. else if (!$oAttDef->IsExternalField())
  741. {
  742. $rawValue = utils::ReadPostedParam("attr_$sAttCode", null);
  743. if (!is_null($rawValue))
  744. {
  745. $aAttributes[$sAttCode] = trim($rawValue);
  746. $previousValue = $oObj->Get($sAttCode);
  747. if ($previousValue !== $aAttributes[$sAttCode])
  748. {
  749. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  750. }
  751. }
  752. }
  753. else if ($oAttDef->IsWritable())
  754. {
  755. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  756. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  757. {
  758. // Non-visible, or read-only attribute, do nothing
  759. }
  760. else if ($oAttDef->GetEditClass() == 'Document')
  761. {
  762. // There should be an uploaded file with the named attr_<attCode>
  763. $oDocument = utils::ReadPostedDocument('file_'.$sAttCode);
  764. if (!$oDocument->IsEmpty())
  765. {
  766. // A new file has been uploaded
  767. $oObj->Set($sAttCode, $oDocument);
  768. }
  769. }
  770. }
  771. }
  772. if (!$oObj->IsModified())
  773. {
  774. $oP->p(Dict::Format('UI:Class_Object_NotUpdated', MetaModel::GetName(get_class($oObj)), $oObj->GetName()));
  775. }
  776. else if ($oObj->CheckToUpdate())
  777. {
  778. $oMyChange = MetaModel::NewObject("CMDBChange");
  779. $oMyChange->Set("date", time());
  780. if (UserRights::GetUser() != UserRights::GetRealUser())
  781. {
  782. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  783. }
  784. else
  785. {
  786. $sUserString = UserRights::GetUser();
  787. }
  788. $oMyChange->Set("userinfo", $sUserString);
  789. $iChangeId = $oMyChange->DBInsert();
  790. $oObj->DBUpdateTracked($oMyChange);
  791. $oP->p(Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName()));
  792. }
  793. else
  794. {
  795. $oP->p("<strong>".Dict::S('UI:Error:ObjectCannotBeUpdated')."</strong>\n");
  796. }
  797. }
  798. else
  799. {
  800. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  801. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  802. }
  803. }
  804. $oObj->DisplayDetails($oP);
  805. break;
  806. case 'select_for_deletion':
  807. $sFilter = utils::ReadParam('filter', '');
  808. $sFormat = utils::ReadParam('format', '');
  809. if (empty($sFilter))
  810. {
  811. throw new ApplicationException(Dict::Format('UI:Error:1ParametersMissing', 'filter'));
  812. }
  813. $oP->set_title(Dict::S('UI:BulkDeletePageTitle'));
  814. $oP->add("<h1>".Dict::S('UI:BulkDeleteTitle')."</h1>\n");
  815. // TO DO: limit the search filter by the user context
  816. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  817. $oSet = new DBObjectSet($oFilter);
  818. $oBlock = new DisplayBlock($oFilter, 'list', false);
  819. $oP->add("<form method=\"post\">\n");
  820. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"bulk_delete\">\n");
  821. $oP->add("<input type=\"hidden\" name=\"class\" value=\"".$oFilter->GetClass()."\">\n");
  822. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  823. $oBlock->Display($oP, 1, array('selection_type' => 'multiple', 'selection_mode' => true, 'display_limit' => false, 'menu' => false));
  824. $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");
  825. $oP->add("</form>\n");
  826. break;
  827. case 'bulk_delete_confirmed':
  828. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  829. if (!utils::IsTransactionValid($sTransactionId))
  830. {
  831. throw new ApplicationException(Dict::S('UI:Error:ObjectsAlreadyDeleted'));
  832. }
  833. case 'bulk_delete':
  834. $sClass = utils::ReadPostedParam('class', '');
  835. $sClassLabel = MetaModel::GetName($sClass);
  836. $aSelectObject = utils::ReadPostedParam('selectObject', '');
  837. $aObjects = array();
  838. if ( empty($sClass) || empty($aSelectObject)) // TO DO: check that the class name is valid !
  839. {
  840. throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'class', 'selectObject[]'));
  841. }
  842. foreach($aSelectObject as $iId)
  843. {
  844. $aObjects[] = $oContext->GetObject($sClass, $iId);
  845. }
  846. if (MetaModel::IsReadOnlyClass($sClass) || !UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, DBObjectSet::FromArray($sClass, $aObjects)))
  847. {
  848. throw new SecurityException(Dict::S('UI:Error:BulkDeleteNotAllowedOn_Class'), $sClass);
  849. }
  850. $oP->set_title(Dict::S('UI:BulkDeletePageTitle'));
  851. DeleteObjects($oP, $sClass, $aObjects, ($operation == 'bulk_delete_confirmed'));
  852. break;
  853. case 'delete':
  854. case 'delete_confirmed':
  855. $sClass = utils::ReadParam('class', '');
  856. $sClassLabel = MetaModel::GetName($sClass);
  857. $id = utils::ReadParam('id', '');
  858. $oObj = $oContext->GetObject($sClass, $id);
  859. if (MetaModel::IsReadOnlyClass($sClass) || !UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oObj)))
  860. {
  861. throw new SecurityException(Dict::S('UI:Error:DeleteNotAllowedOn_Class'), $sClass);
  862. }
  863. DeleteObjects($oP, $sClass, array($oObj), ($operation == 'delete_confirmed'));
  864. break;
  865. case 'apply_clone':
  866. $sClass = utils::ReadPostedParam('class', '');
  867. $sClassLabel = MetaModel::GetName($sClass);
  868. $iCloneId = utils::ReadPostedParam('clone_id', '');
  869. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  870. if (!utils::IsTransactionValid($sTransactionId))
  871. {
  872. $oP->p(Dict::S('UI:Error:ObjectAlreadyCloned'));
  873. }
  874. else
  875. {
  876. $oObj = $oContext->GetObject($sClass, $iCloneId);
  877. $oMyChange = MetaModel::NewObject("CMDBChange");
  878. $oMyChange->Set("date", time());
  879. if (UserRights::GetUser() != UserRights::GetRealUser())
  880. {
  881. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  882. }
  883. else
  884. {
  885. $sUserString = UserRights::GetUser();
  886. }
  887. $oMyChange->Set("userinfo", $sUserString);
  888. $iChangeId = $oMyChange->DBInsert();
  889. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  890. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  891. {
  892. if ( ($oAttDef->IsWritable()) )
  893. {
  894. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  895. $oObj->Set($sAttCode, $value);
  896. }
  897. }
  898. $oObj->DBCloneTracked($oMyChange);
  899. $oP->set_title(Dict::S('UI:PageTitle:ObjectCreated'));
  900. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oObj->GetName(), $sClassLabel)."</h1>\n");
  901. $oObj->DisplayDetails($oP);
  902. }
  903. break;
  904. case 'wizard_apply_new':
  905. $sJson = utils::ReadPostedParam('json_obj', '');
  906. $oWizardHelper = WizardHelper::FromJSON($sJson);
  907. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  908. if (!utils::IsTransactionValid($sTransactionId))
  909. {
  910. $oP->p(Dict::S('UI:Error:ObjectAlreadyCreated'));
  911. }
  912. else
  913. {
  914. $oObj = $oWizardHelper->GetTargetObject(true /* read uploaded files */);
  915. if (is_object($oObj))
  916. {
  917. $sClass = get_class($oObj);
  918. $sClassLabel = MetaModel::GetName($sClass);
  919. $oMyChange = MetaModel::NewObject("CMDBChange");
  920. $oMyChange->Set("date", time());
  921. if (UserRights::GetUser() != UserRights::GetRealUser())
  922. {
  923. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  924. }
  925. else
  926. {
  927. $sUserString = UserRights::GetUser();
  928. }
  929. $oMyChange->Set("userinfo", $sUserString);
  930. $iChangeId = $oMyChange->DBInsert();
  931. $oObj->DBInsertTracked($oMyChange);
  932. $oP->set_title(Dict::S('UI:PageTitle:ObjectCreated'));
  933. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oObj->GetName(), $sClassLabel)."</h1>\n");
  934. $oObj->DisplayDetails($oP);
  935. }
  936. }
  937. break;
  938. case 'stimulus':
  939. $sClass = utils::ReadParam('class', '');
  940. $id = utils::ReadParam('id', '');
  941. $sStimulus = utils::ReadParam('stimulus', '');
  942. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  943. {
  944. throw new ApplicationException(Dict::Format('UI:Error:3ParametersMissing', 'class', 'id', 'stimulus'));
  945. }
  946. $oObj = $oContext->GetObject($sClass, $id);
  947. if ($oObj != null)
  948. {
  949. $aTransitions = $oObj->EnumTransitions();
  950. $aStimuli = MetaModel::EnumStimuli($sClass);
  951. if (!isset($aTransitions[$sStimulus]))
  952. {
  953. // Invalid stimulus
  954. throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulus, $oObj->GetName(), $oObj->GetStateLabel()));
  955. }
  956. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  957. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  958. $aTransition = $aTransitions[$sStimulus];
  959. $sTargetState = $aTransition['target_state'];
  960. $aTargetStates = MetaModel::EnumStates($sClass);
  961. $oP->add_linked_script("../js/json.js");
  962. $oP->add_linked_script("../js/forms-json-utils.js");
  963. $oP->add_linked_script("../js/wizardhelper.js");
  964. $oP->add_linked_script("../js/wizard.utils.js");
  965. $oP->add_linked_script("../js/linkswidget.js");
  966. $oP->add_linked_script("../js/jquery.blockUI.js");
  967. $oP->add("<div class=\"page_header\">\n");
  968. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  969. $oP->add("</div>\n");
  970. $oObj->DisplayBareDetails($oP);
  971. $aTargetState = $aTargetStates[$sTargetState];
  972. $aExpectedAttributes = $aTargetState['attribute_list'];
  973. $oP->add("<div class=\"wizHeader\">\n");
  974. $oP->add("<h1>$sActionDetails</h1>\n");
  975. $oP->add("<div class=\"wizContainer\">\n");
  976. $oP->add("<form method=\"post\">\n");
  977. $aDetails = array();
  978. $iFieldIndex = 0;
  979. $aFieldsMap = array();
  980. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  981. {
  982. // Prompt for an attribute if
  983. // - the attribute must be changed or must be displayed to the user for confirmation
  984. // - or the field is mandatory and currently empty
  985. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  986. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  987. {
  988. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  989. $oAttDef = $aAttributesDef[$sAttCode];
  990. $aArgs = array('this' => $oObj);
  991. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetEditValue($sAttCode), 'att_'.$iFieldIndex, '', $iExpectCode, $aArgs);
  992. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => "<span id=\"field_att_$iFieldIndex\">$sHTMLValue</span>");
  993. $aFieldsMap[$sAttCode] = 'att_'.$iFieldIndex;
  994. $iFieldIndex++;
  995. }
  996. }
  997. $oP->details($aDetails);
  998. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  999. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1000. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  1001. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  1002. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1003. $oP->add($oAppContext->GetForForm());
  1004. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1005. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  1006. $oP->add("</form>\n");
  1007. $oP->add("</div>\n");
  1008. $oP->add("</div>\n");
  1009. $iFieldsCount = count($aFieldsMap);
  1010. $sJsonFieldsMap = json_encode($aFieldsMap);
  1011. $oP->add_script(
  1012. <<<EOF
  1013. // Initializes the object once at the beginning of the page...
  1014. var oWizardHelper = new WizardHelper('$sClass');
  1015. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1016. oWizardHelper.SetFieldsCount($iFieldsCount);
  1017. EOF
  1018. );
  1019. }
  1020. else
  1021. {
  1022. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  1023. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  1024. }
  1025. break;
  1026. case 'apply_stimulus':
  1027. $sClass = utils::ReadPostedParam('class', '');
  1028. $id = utils::ReadPostedParam('id', '');
  1029. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  1030. $sStimulus = utils::ReadPostedParam('stimulus', '');
  1031. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  1032. {
  1033. throw new ApplicationException(Dict::Format('UI:Error:3ParametersMissing', 'class', 'id', 'stimulus'));
  1034. }
  1035. $oObj = $oContext->GetObject($sClass, $id);
  1036. if ($oObj != null)
  1037. {
  1038. $aTransitions = $oObj->EnumTransitions();
  1039. $aStimuli = MetaModel::EnumStimuli($sClass);
  1040. if (!isset($aTransitions[$sStimulus]))
  1041. {
  1042. throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulus, $oObj->GetName(), $oObj->GetStateLabel()));
  1043. }
  1044. if (!utils::IsTransactionValid($sTransactionId))
  1045. {
  1046. $oP->p(Dict::S('UI:Error:ObjectAlreadyUpdated'));
  1047. }
  1048. else
  1049. {
  1050. $sActionLabel = $aStimuli[$sStimulus]->GetLabel();
  1051. $sActionDetails = $aStimuli[$sStimulus]->GetDescription();
  1052. $aTransition = $aTransitions[$sStimulus];
  1053. $sTargetState = $aTransition['target_state'];
  1054. $aTargetStates = MetaModel::EnumStates($sClass);
  1055. $oP->add("<div class=\"page_header\">\n");
  1056. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  1057. $oP->add("<p>$sActionDetails</p>\n");
  1058. $oP->p(Dict::Format('UI:Apply_Stimulus_On_Object_In_State_ToTarget_State', $sActionLabel, $oObj->GetName(), $oObj->GetStateLabel(), $sTargetState));
  1059. $oP->add("</div>\n");
  1060. $aTargetState = $aTargetStates[$sTargetState];
  1061. $aExpectedAttributes = $aTargetState['attribute_list'];
  1062. $aDetails = array();
  1063. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  1064. {
  1065. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  1066. {
  1067. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  1068. $oObj->Set($sAttCode, $paramValue);
  1069. }
  1070. }
  1071. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  1072. {
  1073. $oMyChange = MetaModel::NewObject("CMDBChange");
  1074. $oMyChange->Set("date", time());
  1075. if (UserRights::GetUser() != UserRights::GetRealUser())
  1076. {
  1077. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  1078. }
  1079. else
  1080. {
  1081. $sUserString = UserRights::GetUser();
  1082. }
  1083. $oMyChange->Set("userinfo", $sUserString);
  1084. $iChangeId = $oMyChange->DBInsert();
  1085. $oObj->DBUpdateTracked($oMyChange);
  1086. $oP->p(Dict::Format('UI:Class_Object_Updated', get_class($oObj), $oObj->GetName()));
  1087. }
  1088. }
  1089. $oObj->DisplayDetails($oP);
  1090. }
  1091. else
  1092. {
  1093. $oP->set_title(Dict::S('UI:ErrorPageTitle'));
  1094. $oP->P(Dict::S('UI:ObjectDoesNotExist'));
  1095. }
  1096. break;
  1097. case 'modify_links':
  1098. $sClass = utils::ReadParam('class', '');
  1099. $sLinkAttr = utils::ReadParam('link_attr', '');
  1100. $sTargetClass = utils::ReadParam('target_class', '');
  1101. $id = utils::ReadParam('id', '');
  1102. $bAddObjects = utils::ReadParam('addObjects', false);
  1103. if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
  1104. {
  1105. throw new ApplicationException(Dict::Format('UI:Error:4ParametersMissing', 'class', 'id', 'target_class', 'link_attr'));
  1106. }
  1107. require_once('../application/uilinkswizard.class.inc.php');
  1108. $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
  1109. $oWizard->Display($oP, $oContext, array('StartWithAdd' => $bAddObjects));
  1110. break;
  1111. case 'do_modify_links':
  1112. $aLinks = utils::ReadParam('linkId', array(), 'post');
  1113. $sLinksToRemove = trim(utils::ReadParam('linksToRemove', '', 'post'));
  1114. $aLinksToRemove = array();
  1115. if (!empty($sLinksToRemove))
  1116. {
  1117. $aLinksToRemove = explode(' ', trim($sLinksToRemove));
  1118. }
  1119. $sClass = utils::ReadParam('class', '', 'post');
  1120. $sLinkageAtt = utils::ReadParam('linkage', '', 'post');
  1121. $iObjectId = utils::ReadParam('object_id', '', 'post');
  1122. $sLinkingAttCode = utils::ReadParam('linking_attcode', '', 'post');
  1123. $oMyChange = MetaModel::NewObject("CMDBChange");
  1124. $oMyChange->Set("date", time());
  1125. if (UserRights::GetUser() != UserRights::GetRealUser())
  1126. {
  1127. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  1128. }
  1129. else
  1130. {
  1131. $sUserString = UserRights::GetUser();
  1132. }
  1133. $oMyChange->Set("userinfo", $sUserString);
  1134. $iChangeId = $oMyChange->DBInsert();
  1135. // Delete links that are to be deleted
  1136. foreach($aLinksToRemove as $iLinkId)
  1137. {
  1138. if ($iLinkId > 0) // Negative IDs are objects that were not even created
  1139. {
  1140. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1141. $oLink->DBDeleteTracked($oMyChange);
  1142. }
  1143. }
  1144. $aEditableFields = array();
  1145. $aData = array();
  1146. foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
  1147. {
  1148. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1149. if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  1150. {
  1151. $aEditableFields[] = $sAttCode;
  1152. $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
  1153. }
  1154. }
  1155. // Update existing links or create new links
  1156. foreach($aLinks as $iLinkId)
  1157. {
  1158. if ($iLinkId > 0)
  1159. {
  1160. // This is an existing link to be modified
  1161. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1162. // Update all the attributes of the link
  1163. foreach($aEditableFields as $sAttCode)
  1164. {
  1165. $value = $aData[$sAttCode][$iLinkId];
  1166. $oLink->Set($sAttCode, $value);
  1167. }
  1168. if ($oLink->IsModified())
  1169. {
  1170. $oLink->DBUpdateTracked($oMyChange);
  1171. }
  1172. //echo "Updated link:<br/>\n";
  1173. //var_dump($oLink);
  1174. }
  1175. else
  1176. {
  1177. // A new link must be created
  1178. $oLink = MetaModel::NewObject($sClass);
  1179. $oLinkedObjectId = -$iLinkId;
  1180. // Set all the attributes of the link
  1181. foreach($aEditableFields as $sAttCode)
  1182. {
  1183. $value = $aData[$sAttCode][$iLinkId];
  1184. $oLink->Set($sAttCode, $value);
  1185. }
  1186. // And the two external keys
  1187. $oLink->Set($sLinkageAtt, $iObjectId);
  1188. $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
  1189. // then save it
  1190. //echo "Created link:<br/>\n";
  1191. //var_dump($oLink);
  1192. $oLink->DBInsertTracked($oMyChange);
  1193. }
  1194. }
  1195. // Display again the details of the linked object
  1196. $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
  1197. $sTargetClass = $oAttDef->GetTargetClass();
  1198. $oObj = $oContext->GetObject($sTargetClass, $iObjectId);
  1199. $oSearch = $oContext->NewFilter(get_class($oObj));
  1200. $oBlock = new DisplayBlock($oSearch, 'search', false);
  1201. $oBlock->Display($oP, 0);
  1202. $oObj->DisplayDetails($oP);
  1203. break;
  1204. case 'swf_navigator':
  1205. $sClass = utils::ReadParam('class', '');
  1206. $id = utils::ReadParam('id', 0);
  1207. $sRelation = utils::ReadParam('relation', 'neighbours');
  1208. $width = 1000;
  1209. $height = 700;
  1210. $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";
  1211. $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\">
  1212. <param name=\"allowScriptAccess\" value=\"sameDomain\" />
  1213. <param name=\"allowFullScreen\" value=\"false\" />
  1214. <param name=\"FlashVars\" value=\"$sParams\" />
  1215. <param name=\"movie\" value=\"../navigator/navigator.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#ffffff\" />
  1216. <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\" />
  1217. </object>\n");
  1218. break;
  1219. default:
  1220. if (is_object($oActiveNode))
  1221. {
  1222. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  1223. $oP->set_title($oActiveNode->GetMenuLabel());
  1224. }
  1225. }
  1226. ////MetaModel::ShowQueryTrace();
  1227. $oP->output();
  1228. }
  1229. catch(CoreException $e)
  1230. {
  1231. require_once('../setup/setuppage.class.inc.php');
  1232. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  1233. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1234. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  1235. $oP->output();
  1236. if (MetaModel::IsLogEnabledIssue())
  1237. {
  1238. if (MetaModel::IsValidClass('EventIssue'))
  1239. {
  1240. $oLog = new EventIssue();
  1241. $oLog->Set('message', $e->getMessage());
  1242. $oLog->Set('userinfo', '');
  1243. $oLog->Set('issue', $e->GetIssue());
  1244. $oLog->Set('impact', 'Page could not be displayed');
  1245. $oLog->Set('callstack', $e->getTrace());
  1246. $oLog->Set('data', $e->getContextData());
  1247. $oLog->DBInsertNoReload();
  1248. }
  1249. IssueLog::Error($e->getMessage());
  1250. }
  1251. // For debugging only
  1252. //throw $e;
  1253. }
  1254. catch(Exception $e)
  1255. {
  1256. require_once('../setup/setuppage.class.inc.php');
  1257. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  1258. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1259. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  1260. $oP->output();
  1261. if (MetaModel::IsLogEnabledIssue())
  1262. {
  1263. if (MetaModel::IsValidClass('EventIssue'))
  1264. {
  1265. $oLog = new EventIssue();
  1266. $oLog->Set('message', $e->getMessage());
  1267. $oLog->Set('userinfo', '');
  1268. $oLog->Set('issue', 'PHP Exception');
  1269. $oLog->Set('impact', 'Page could not be displayed');
  1270. $oLog->Set('callstack', $e->getTrace());
  1271. $oLog->Set('data', array());
  1272. $oLog->DBInsertNoReload();
  1273. }
  1274. IssueLog::Error($e->getMessage());
  1275. }
  1276. }
  1277. ?>