UI.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. $aStates = MetaModel::EnumStates($sClass);
  346. $sStateLabel = $aStates[$sStateCode]['label'];
  347. }
  348. $aWizardSteps = $oWizard->GetWizardStructure();
  349. // Display the structure of the wizard
  350. $iStepIndex = 1;
  351. $iMaxInputId = 0;
  352. $aFieldsMap = array();
  353. foreach($aWizardSteps['mandatory'] as $aSteps)
  354. {
  355. $oP->SetCurrentTab("Step $iStepIndex *");
  356. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, false /* no finish button */, $aArgs);
  357. //$oP->add("</div>\n");
  358. $iStepIndex++;
  359. }
  360. foreach($aWizardSteps['optional'] as $aSteps)
  361. {
  362. $oP->SetCurrentTab("Step $iStepIndex *");
  363. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true, $aArgs); // true means enable the finish button
  364. //$oP->add("</div>\n");
  365. $iStepIndex++;
  366. }
  367. $oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);
  368. $oObj = null;
  369. if (!empty($id))
  370. {
  371. $oObj = $oContext->GetObject($sClass, $id);
  372. }
  373. if (!is_object($oObj))
  374. {
  375. // new object or that can't be retrieved (corrupted id or object not allowed to this user)
  376. $id = '';
  377. $oObj = MetaModel::NewObject($sClass);
  378. }
  379. $oP->add("<script type=\"text/javascript\">
  380. // Fill the map between the fields of the form and the attributes of the object\n");
  381. $aNewFieldsMap = array();
  382. foreach($aFieldsMap as $id => $sFieldCode)
  383. {
  384. $aNewFieldsMap[$sFieldCode] = $id;
  385. }
  386. $iFieldsCount = count($aFieldsMap);
  387. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  388. $oP->add("
  389. // Initializes the object once at the beginning of the page...
  390. var oWizardHelper = new WizardHelper('$sClass');
  391. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  392. oWizardHelper.SetFieldsCount($iFieldsCount);
  393. ActivateStep(1);
  394. </script>\n");
  395. }
  396. break;
  397. case 'apply_modify':
  398. $sClass = utils::ReadPostedParam('class', '');
  399. $sClassLabel = MetaModel::GetName($sClass);
  400. $id = utils::ReadPostedParam('id', '');
  401. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  402. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  403. {
  404. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  405. }
  406. else if (!utils::IsTransactionValid($sTransactionId))
  407. {
  408. $oP->p("<strong>Error: object has already be updated!</strong>\n");
  409. }
  410. else
  411. {
  412. $oObj = $oContext->GetObject($sClass, $id);
  413. if ($oObj != null)
  414. {
  415. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel modification");
  416. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel modification</h1>\n");
  417. $bObjectModified = false;
  418. foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
  419. {
  420. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  421. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  422. {
  423. // Non-visible, or read-only attribute, do nothing
  424. }
  425. else if ($sAttCode == 'finalclass')
  426. {
  427. // This very specific field is read-only
  428. }
  429. else if ($oAttDef->IsLinkSet())
  430. {
  431. // Link set, the data is a set of link objects, encoded in JSON
  432. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  433. if (!empty($aAttributes[$sAttCode]))
  434. {
  435. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  436. $oObj->Set($sAttCode, $oLinkSet);
  437. // TO DO: detect a real modification, for now always update !!
  438. $bObjectModified = true;
  439. }
  440. }
  441. else if ($oAttDef->GetEditClass() == 'Document')
  442. {
  443. // There should be an uploaded file with the named attr_<attCode>
  444. $oDocument = utils::ReadPostedDocument('file_'.$sAttCode);
  445. if (!$oDocument->IsEmpty())
  446. {
  447. // A new file has been uploaded
  448. $oObj->Set($sAttCode, $oDocument);
  449. $bObjectModified = true;
  450. }
  451. }
  452. else if (!$oAttDef->IsExternalField())
  453. {
  454. $rawValue = utils::ReadPostedParam("attr_$sAttCode", null);
  455. if (!is_null($rawValue))
  456. {
  457. $aAttributes[$sAttCode] = trim($rawValue);
  458. $previousValue = $oObj->Get($sAttCode);
  459. if ($previousValue !== $aAttributes[$sAttCode])
  460. {
  461. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  462. $bObjectModified = true;
  463. }
  464. }
  465. }
  466. }
  467. if (!$bObjectModified)
  468. {
  469. $oP->p("No modification detected. ".MetaModel::GetName(get_class($oObj))." has <strong>not</strong> been updated.\n");
  470. }
  471. else if ($oObj->CheckToUpdate())
  472. {
  473. $oMyChange = MetaModel::NewObject("CMDBChange");
  474. $oMyChange->Set("date", time());
  475. if (UserRights::GetUser() != UserRights::GetRealUser())
  476. {
  477. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  478. }
  479. else
  480. {
  481. $sUserString = UserRights::GetUser();
  482. }
  483. $oMyChange->Set("userinfo", $sUserString);
  484. $iChangeId = $oMyChange->DBInsert();
  485. $oObj->DBUpdateTracked($oMyChange);
  486. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  487. }
  488. else
  489. {
  490. $oP->p("<strong>Error: object can not be updated!</strong>\n");
  491. //$oObj->Reload(); // restore default values!
  492. }
  493. }
  494. else
  495. {
  496. $oP->set_title("iTop - Error");
  497. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  498. }
  499. }
  500. $oObj->DisplayDetails($oP);
  501. break;
  502. case 'delete':
  503. case 'delete_confirmed':
  504. $sClass = utils::ReadParam('class', '');
  505. $sClassLabel = MetaModel::GetName($sClass);
  506. $id = utils::ReadParam('id', '');
  507. $oObj = $oContext->GetObject($sClass, $id);
  508. $sName = $oObj->GetName();
  509. if (!UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oObj)))
  510. {
  511. throw new SecurityException('You are not allowed to do delete objects of class '.$sClass);
  512. }
  513. // Evaluate the impact on the DB integrity
  514. //
  515. list ($aDeletedObjs, $aResetedObjs) = $oObj->GetDeletionScheme();
  516. // Evaluate feasibility (user access control)
  517. //
  518. $bFoundManual = false;
  519. $bFoundStopper = false;
  520. $iTotalDelete = 0; // count of object that must be deleted
  521. $iTotalReset = 0; // count of object for which an ext key will be reset (to 0)
  522. foreach ($aDeletedObjs as $sRemoteClass => $aDeletes)
  523. {
  524. $iTotalDelete += count($aDeletes);
  525. foreach ($aDeletes as $iId => $aData)
  526. {
  527. $oToDelete = $aData['to_delete'];
  528. $bDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, DBObjectSet::FromObject($oToDelete));
  529. if (!$bDeleteAllowed)
  530. {
  531. $aDeletedObjs[$sRemoteClass][$iId]['issue'] = 'not allowed to delete this object';
  532. $bFoundStopper = true;
  533. }
  534. $bAutoDel = $aData['auto_delete'];
  535. if (!$bAutoDel)
  536. {
  537. $bFoundManual = true;
  538. }
  539. }
  540. }
  541. foreach ($aResetedObjs as $sRemoteClass => $aToReset)
  542. {
  543. $iTotalReset += count($aToReset);
  544. foreach ($aToReset as $iId => $aData)
  545. {
  546. $oToReset = $aData['to_reset'];
  547. $aExtKeyLabels = array();
  548. $aForbiddenKeys = array(); // keys on which the current user is not allowed to write
  549. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  550. {
  551. $bUpdateAllowed = UserRights::IsActionAllowedOnAttribute($sClass, $sRemoteExtKey, UR_ACTION_MODIFY, DBObjectSet::FromObject($oToReset));
  552. if (!$bUpdateAllowed)
  553. {
  554. $bFoundStopper = true;
  555. $aForbiddenKeys[] = $aRemoteAttDef->GetLabel();
  556. }
  557. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  558. }
  559. $aResetedObjs[$sRemoteClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  560. if (count($aForbiddenKeys) > 0)
  561. {
  562. $aResetedObjs[$sRemoteClass][$iId]['issue'] = 'you are not allowed to update some fields: '.implode(', ', $aForbiddenKeys);
  563. }
  564. }
  565. }
  566. // Count of dependent objects (+ the current one)
  567. $iTotalTargets = $iTotalDelete + $iTotalReset;
  568. if ($operation == 'delete_confirmed')
  569. {
  570. $oP->add("<h1>Deletion of ".$oObj->GetName()."</h1>\n");
  571. // Security - do not allow the user to force a forbidden delete by the mean of page arguments...
  572. if ($bFoundStopper)
  573. {
  574. throw new SecurityException('This object could not be deleted because the current user do not have sufficient rights');
  575. }
  576. if ($bFoundManual)
  577. {
  578. throw new SecurityException('This object could not be deleted because some manual operations must be performed prior to that');
  579. }
  580. // Prepare the change reporting
  581. //
  582. $oMyChange = MetaModel::NewObject("CMDBChange");
  583. $oMyChange->Set("date", time());
  584. if (UserRights::GetUser() != UserRights::GetRealUser())
  585. {
  586. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  587. }
  588. else
  589. {
  590. $sUserString = UserRights::GetUser();
  591. }
  592. $oMyChange->Set("userinfo", $sUserString);
  593. $oMyChange->DBInsert();
  594. // Delete dependencies
  595. //
  596. $aDisplayData = array();
  597. foreach ($aDeletedObjs as $sRemoteClass => $aDeletes)
  598. {
  599. foreach ($aDeletes as $iId => $aData)
  600. {
  601. $oToDelete = $aData['to_delete'];
  602. $aDisplayData[] = array(
  603. 'class' => MetaModel::GetName(get_class($oToDelete)),
  604. 'object' => $oToDelete->GetHyperLink(),
  605. 'consequence' => 'automatically deleted',
  606. );
  607. $oToDelete->DBDeleteTracked($oMyChange);
  608. }
  609. }
  610. // Update dependencies
  611. //
  612. foreach ($aResetedObjs as $sRemoteClass => $aToReset)
  613. {
  614. foreach ($aToReset as $iId => $aData)
  615. {
  616. $oToReset = $aData['to_reset'];
  617. $aDisplayData[] = array(
  618. 'class' => MetaModel::GetName(get_class($oToReset)),
  619. 'object' => $oToReset->GetHyperLink(),
  620. 'consequence' => 'automatic reset of: '.$aData['attributes_list'],
  621. );
  622. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  623. {
  624. $oToReset->Set($sRemoteExtKey, 0);
  625. $oToReset->DBUpdateTracked($oMyChange);
  626. }
  627. }
  628. }
  629. // Report automatic jobs
  630. //
  631. if ($iTotalTargets > 0)
  632. {
  633. $oP->p('Cleaning up any reference to '.$oObj->GetName().'...');
  634. $aDisplayConfig = array();
  635. $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
  636. $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
  637. $aDisplayConfig['consequence'] = array('label' => 'Done', 'description' => 'What has been done');
  638. $oP->table($aDisplayConfig, $aDisplayData);
  639. }
  640. $oObj->DBDeleteTracked($oMyChange);
  641. $oP->add("<h1>".$sName." - $sClassLabel deleted</h1>\n");
  642. }
  643. else
  644. {
  645. $oP->add("<h1>Deletion of ".$oObj->GetHyperLink()."</h1>\n");
  646. // Explain what should be done
  647. //
  648. $aDisplayData = array();
  649. foreach ($aDeletedObjs as $sRemoteClass => $aDeletes)
  650. {
  651. foreach ($aDeletes as $iId => $aData)
  652. {
  653. $oToDelete = $aData['to_delete'];
  654. $bAutoDel = $aData['auto_delete'];
  655. if (array_key_exists('issue', $aData))
  656. {
  657. if ($bAutoDel)
  658. {
  659. $sConsequence = 'Should be automaticaly deleted, but you are not allowed to do so';
  660. }
  661. else
  662. {
  663. $sConsequence = 'Must be deleted manually - you are not allowed to delete this object, please contact your application admin';
  664. }
  665. }
  666. else
  667. {
  668. if ($bAutoDel)
  669. {
  670. $sConsequence = 'Will be automaticaly deleted';
  671. }
  672. else
  673. {
  674. $sConsequence = 'Must be deleted manually';
  675. }
  676. }
  677. $aDisplayData[] = array(
  678. 'class' => MetaModel::GetName(get_class($oToDelete)),
  679. 'object' => $oToDelete->GetHyperLink(),
  680. 'consequence' => $sConsequence,
  681. );
  682. }
  683. }
  684. foreach ($aResetedObjs as $sRemoteClass => $aToReset)
  685. {
  686. foreach ($aToReset as $iId => $aData)
  687. {
  688. $oToReset = $aData['to_reset'];
  689. if (array_key_exists('issue', $aData))
  690. {
  691. $sConsequence = "Should be automatically updated, but: ".$aData['issue'];
  692. }
  693. else
  694. {
  695. $sConsequence = "will be automaticaly updated (reset: ".$aData['attributes_list'].")";
  696. }
  697. $aDisplayData[] = array(
  698. 'class' => MetaModel::GetName(get_class($oToReset)),
  699. 'object' => $oToReset->GetHyperLink(),
  700. 'consequence' => $sConsequence,
  701. );
  702. }
  703. }
  704. if ($iTotalTargets > 0)
  705. {
  706. $oP->p("$iTotalTargets objects/links are referencing ".$oObj->GetName());
  707. $oP->p('To ensure Database integrity, any reference should be further eliminated');
  708. $aDisplayConfig = array();
  709. $aDisplayConfig['class'] = array('label' => 'Class', 'description' => '');
  710. $aDisplayConfig['object'] = array('label' => 'Object', 'description' => '');
  711. $aDisplayConfig['consequence'] = array('label' => 'Consequence', 'description' => 'What will happen to this object');
  712. $oP->table($aDisplayConfig, $aDisplayData);
  713. }
  714. if ($iTotalTargets > 0 && ($bFoundManual || $bFoundStopper))
  715. {
  716. if ($bFoundStopper)
  717. {
  718. $oP->p("Sorry, you are not allowed to delete this object, please see detailed explanations above");
  719. }
  720. elseif ($bFoundManual)
  721. {
  722. $oP->p("Please do the manual operations requested above prior to requesting the deletion of this object");
  723. }
  724. $oP->add("<form method=\"post\">\n");
  725. $oP->add("<input DISABLED type=\"submit\" name=\"\" value=\" Delete! \">\n");
  726. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\" Cancel \">\n");
  727. $oP->add("</form>\n");
  728. }
  729. else
  730. {
  731. $oP->p("Please confirm that you want to delete ".$oObj->GetHyperLink());
  732. $oP->add("<form method=\"post\">\n");
  733. $oP->add("<input type=\"hidden\" name=\"menu\" value=\"$iActiveNodeId\">\n");
  734. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"delete_confirmed\">\n");
  735. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  736. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  737. $oP->add("<input type=\"submit\" name=\"\" value=\" Delete! \">\n");
  738. $oP->add("<input type=\"button\" onclick=\"window.history.back();\" value=\" Cancel \">\n");
  739. $oP->add("</form>\n");
  740. }
  741. }
  742. break;
  743. case 'apply_new':
  744. $oP->p('Creation of the object');
  745. $oP->p('Obsolete, should now go through the wizard...');
  746. break;
  747. case 'apply_clone':
  748. $sClass = utils::ReadPostedParam('class', '');
  749. $sClassLabel = MetaModel::GetName($sClass);
  750. $iCloneId = utils::ReadPostedParam('clone_id', '');
  751. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  752. if (!utils::IsTransactionValid($sTransactionId))
  753. {
  754. $oP->p("<strong>Error: object has already be cloned!</strong>\n");
  755. }
  756. else
  757. {
  758. $oObj = $oContext->GetObject($sClass, $iCloneId);
  759. $oMyChange = MetaModel::NewObject("CMDBChange");
  760. $oMyChange->Set("date", time());
  761. if (UserRights::GetUser() != UserRights::GetRealUser())
  762. {
  763. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  764. }
  765. else
  766. {
  767. $sUserString = UserRights::GetUser();
  768. }
  769. $oMyChange->Set("userinfo", $sUserString);
  770. $iChangeId = $oMyChange->DBInsert();
  771. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  772. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  773. {
  774. if ( ('finalclass' != $sAttCode) && // finalclass is a reserved word, hardcoded !
  775. ($sStateAttCode != $sAttCode) &&
  776. (!$oAttDef->IsExternalField()) )
  777. {
  778. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  779. $oObj->Set($sAttCode, $value);
  780. }
  781. }
  782. $oObj->DBCloneTracked($oMyChange);
  783. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  784. $oObj->DisplayDetails($oP);
  785. }
  786. break;
  787. case 'wizard_apply_new':
  788. $sJson = utils::ReadPostedParam('json_obj', '');
  789. $oWizardHelper = WizardHelper::FromJSON($sJson);
  790. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  791. if (!utils::IsTransactionValid($sTransactionId))
  792. {
  793. $oP->p("<strong>Error: object has already be created!</strong>\n");
  794. }
  795. else
  796. {
  797. $oObj = $oWizardHelper->GetTargetObject(true /* read uploaded files */);
  798. if (is_object($oObj))
  799. {
  800. $sClass = get_class($oObj);
  801. $sClassLabel = MetaModel::GetName($sClass);
  802. $oMyChange = MetaModel::NewObject("CMDBChange");
  803. $oMyChange->Set("date", time());
  804. if (UserRights::GetUser() != UserRights::GetRealUser())
  805. {
  806. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  807. }
  808. else
  809. {
  810. $sUserString = UserRights::GetUser();
  811. }
  812. $oMyChange->Set("userinfo", $sUserString);
  813. $iChangeId = $oMyChange->DBInsert();
  814. $oObj->DBInsertTracked($oMyChange);
  815. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel created");
  816. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  817. $oObj->DisplayDetails($oP);
  818. }
  819. }
  820. break;
  821. case 'stimulus':
  822. $sClass = utils::ReadParam('class', '');
  823. $id = utils::ReadParam('id', '');
  824. $sStimulus = utils::ReadParam('stimulus', '');
  825. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  826. {
  827. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  828. }
  829. else
  830. {
  831. $oObj = $oContext->GetObject($sClass, $id);
  832. if ($oObj != null)
  833. {
  834. $aTransitions = $oObj->EnumTransitions();
  835. $aStimuli = MetaModel::EnumStimuli($sClass);
  836. if (!isset($aTransitions[$sStimulus]))
  837. {
  838. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  839. }
  840. else
  841. {
  842. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  843. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  844. $aTransition = $aTransitions[$sStimulus];
  845. $sTargetState = $aTransition['target_state'];
  846. $aTargetStates = MetaModel::EnumStates($sClass);
  847. $oP->add("<div class=\"page_header\">\n");
  848. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  849. //$oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  850. $oP->add("</div>\n");
  851. $oObj->DisplayBareDetails($oP);
  852. $aTargetState = $aTargetStates[$sTargetState];
  853. //print_r($aTransitions[$sStimulus]);
  854. //print_r($aTargetState);
  855. $aExpectedAttributes = $aTargetState['attribute_list'];
  856. $oP->add("<div class=\"wizHeader\">\n");
  857. $oP->add("<h1>$sActionDetails</h1>\n");
  858. $oP->add("<div class=\"wizContainer\">\n");
  859. $oP->add("<form method=\"post\">\n");
  860. $aDetails = array();
  861. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  862. {
  863. // Prompt for an attribute if
  864. // - the attribute must be changed or must be displayed to the user for confirmation
  865. // - or the field is mandatory and currently empty
  866. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  867. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  868. {
  869. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  870. $oAttDef = $aAttributesDef[$sAttCode];
  871. $aArgs = array('this' => $oObj);
  872. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetDisplayValue($sAttCode), '', '', $iExpectCode, $aArgs);
  873. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  874. }
  875. }
  876. $oP->details($aDetails);
  877. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  878. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  879. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  880. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  881. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  882. $oP->add($oAppContext->GetForForm());
  883. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  884. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  885. $oP->add("</form>\n");
  886. $oP->add("</div>\n");
  887. $oP->add("</div>\n");
  888. }
  889. }
  890. else
  891. {
  892. $oP->set_title("iTop - Error");
  893. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  894. }
  895. }
  896. break;
  897. case 'apply_stimulus':
  898. $sClass = utils::ReadPostedParam('class', '');
  899. $id = utils::ReadPostedParam('id', '');
  900. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  901. $sStimulus = utils::ReadPostedParam('stimulus', '');
  902. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  903. {
  904. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  905. }
  906. else
  907. {
  908. $oObj = $oContext->GetObject($sClass, $id);
  909. if ($oObj != null)
  910. {
  911. $aTransitions = $oObj->EnumTransitions();
  912. $aStimuli = MetaModel::EnumStimuli($sClass);
  913. if (!isset($aTransitions[$sStimulus]))
  914. {
  915. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  916. }
  917. else if (!utils::IsTransactionValid($sTransactionId))
  918. {
  919. $oP->p("<strong>Error: object has already been updated!</strong>\n");
  920. }
  921. else
  922. {
  923. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  924. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  925. $aTransition = $aTransitions[$sStimulus];
  926. $sTargetState = $aTransition['target_state'];
  927. $aTargetStates = MetaModel::EnumStates($sClass);
  928. $oP->add("<div class=\"page_header\">\n");
  929. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  930. $oP->add("<p>$sActionDetails</p>\n");
  931. $oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  932. $oP->add("</div>\n");
  933. $aTargetState = $aTargetStates[$sTargetState];
  934. //print_r($aTransitions[$sStimulus]);
  935. //print_r($aTargetState);
  936. $aExpectedAttributes = $aTargetState['attribute_list'];
  937. $aDetails = array();
  938. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  939. {
  940. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  941. {
  942. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  943. $oObj->Set($sAttCode, $paramValue);
  944. }
  945. }
  946. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  947. {
  948. $oMyChange = MetaModel::NewObject("CMDBChange");
  949. $oMyChange->Set("date", time());
  950. if (UserRights::GetUser() != UserRights::GetRealUser())
  951. {
  952. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  953. }
  954. else
  955. {
  956. $sUserString = UserRights::GetUser();
  957. }
  958. $oMyChange->Set("userinfo", $sUserString);
  959. $iChangeId = $oMyChange->DBInsert();
  960. $oObj->DBUpdateTracked($oMyChange);
  961. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  962. }
  963. $oObj->DisplayDetails($oP);
  964. }
  965. }
  966. else
  967. {
  968. $oP->set_title("iTop - Error");
  969. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  970. }
  971. }
  972. break;
  973. case 'modify_links':
  974. $sClass = utils::ReadParam('class', '');
  975. $sLinkAttr = utils::ReadParam('link_attr', '');
  976. $sTargetClass = utils::ReadParam('target_class', '');
  977. $id = utils::ReadParam('id', '');
  978. $bAddObjects = utils::ReadParam('addObjects', false);
  979. if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
  980. {
  981. $oP->set_title("iTop - Error");
  982. $oP->add("<p>4 parameters are mandatory for this operation: class, id, target_class and link_attr.</p>\n");
  983. }
  984. else
  985. {
  986. require_once('../application/uilinkswizard.class.inc.php');
  987. $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
  988. $oWizard->Display($oP, $oContext, array('StartWithAdd' => $bAddObjects));
  989. }
  990. break;
  991. case 'do_modify_links':
  992. $aLinks = utils::ReadParam('linkId', array(), 'post');
  993. $sLinksToRemove = trim(utils::ReadParam('linksToRemove', '', 'post'));
  994. $aLinksToRemove = array();
  995. if (!empty($sLinksToRemove))
  996. {
  997. $aLinksToRemove = explode(' ', trim($sLinksToRemove));
  998. }
  999. $sClass = utils::ReadParam('class', '', 'post');
  1000. $sLinkageAtt = utils::ReadParam('linkage', '', 'post');
  1001. $iObjectId = utils::ReadParam('object_id', '', 'post');
  1002. $sLinkingAttCode = utils::ReadParam('linking_attcode', '', 'post');
  1003. $oMyChange = MetaModel::NewObject("CMDBChange");
  1004. $oMyChange->Set("date", time());
  1005. if (UserRights::GetUser() != UserRights::GetRealUser())
  1006. {
  1007. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  1008. }
  1009. else
  1010. {
  1011. $sUserString = UserRights::GetUser();
  1012. }
  1013. $oMyChange->Set("userinfo", $sUserString);
  1014. $iChangeId = $oMyChange->DBInsert();
  1015. // Delete links that are to be deleted
  1016. foreach($aLinksToRemove as $iLinkId)
  1017. {
  1018. if ($iLinkId > 0) // Negative IDs are objects that were not even created
  1019. {
  1020. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1021. $oLink->DBDeleteTracked($oMyChange);
  1022. }
  1023. }
  1024. $aEditableFields = array();
  1025. $aData = array();
  1026. foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
  1027. {
  1028. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1029. if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  1030. {
  1031. $aEditableFields[] = $sAttCode;
  1032. $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
  1033. }
  1034. }
  1035. // Update existing links or create new links
  1036. foreach($aLinks as $iLinkId)
  1037. {
  1038. if ($iLinkId > 0)
  1039. {
  1040. // This is an existing link to be modified
  1041. $oLink = $oContext->GetObject($sClass, $iLinkId);
  1042. // Update all the attributes of the link
  1043. foreach($aEditableFields as $sAttCode)
  1044. {
  1045. $value = $aData[$sAttCode][$iLinkId];
  1046. $oLink->Set($sAttCode, $value);
  1047. }
  1048. if ($oLink->IsModified())
  1049. {
  1050. $oLink->DBUpdateTracked($oMyChange);
  1051. }
  1052. //echo "Updated link:<br/>\n";
  1053. //var_dump($oLink);
  1054. }
  1055. else
  1056. {
  1057. // A new link must be created
  1058. $oLink = MetaModel::NewObject($sClass);
  1059. $oLinkedObjectId = -$iLinkId;
  1060. // Set all the attributes of the link
  1061. foreach($aEditableFields as $sAttCode)
  1062. {
  1063. $value = $aData[$sAttCode][$iLinkId];
  1064. $oLink->Set($sAttCode, $value);
  1065. }
  1066. // And the two external keys
  1067. $oLink->Set($sLinkageAtt, $iObjectId);
  1068. $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
  1069. // then save it
  1070. //echo "Created link:<br/>\n";
  1071. //var_dump($oLink);
  1072. $oLink->DBInsertTracked($oMyChange);
  1073. }
  1074. }
  1075. // Display again the details of the linked object
  1076. $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
  1077. $sTargetClass = $oAttDef->GetTargetClass();
  1078. $oObj = $oContext->GetObject($sTargetClass, $iObjectId);
  1079. $oSearch = $oContext->NewFilter(get_class($oObj));
  1080. $oBlock = new DisplayBlock($oSearch, 'search', false);
  1081. $oBlock->Display($oP, 0);
  1082. $oObj->DisplayDetails($oP);
  1083. break;
  1084. default:
  1085. if (is_object($oActiveNode))
  1086. {
  1087. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  1088. $oP->set_title($oActiveNode->Get('label'));
  1089. }
  1090. }
  1091. ////MetaModel::ShowQueryTrace();
  1092. $oP->output();
  1093. }
  1094. catch(Exception $e)
  1095. {
  1096. require_once('../setup/setuppage.class.inc.php');
  1097. $oP = new SetupWebPage('iTop - fatal error');
  1098. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1099. $oP->error("Error: '".$e->getMessage()."'");
  1100. $oP->output();
  1101. }
  1102. catch(CoreException $e)
  1103. {
  1104. require_once('../setup/setuppage.class.inc.php');
  1105. $oP = new SetupWebPage('iTop - fatal error');
  1106. $oP->add("<h1>Fatal Error, iTop cannot continue</h1>\n");
  1107. $oP->error("Error: '".$e->getHtmlDesc()."'");
  1108. $oP->output();
  1109. }
  1110. ?>