UI.php 40 KB

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