UI.php 48 KB

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