UI.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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->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. }
  1087. }
  1088. ////MetaModel::ShowQueryTrace();
  1089. $oP->output();
  1090. ?>