UI.php 38 KB

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