UI.php 49 KB

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