UI.php 51 KB

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