UI.php 38 KB

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