UI.php 37 KB

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