UI.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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, 0);
  96. }
  97. else
  98. {
  99. $oBlock = new DisplayBlock($oFilter, 'list', false);
  100. $oBlock->Display($oP, 0);
  101. }
  102. }
  103. catch(CoreException $e)
  104. {
  105. $oFilter = new DBObjectSearch($sOQLClass); // To Do: Make sure we don't bypass security
  106. $oSet = new DBObjectSet($oFilter);
  107. if ($bSearchForm)
  108. {
  109. $oBlock = new DisplayBlock($oFilter, 'search', false);
  110. $oBlock->Display($oP, 0);
  111. }
  112. $oP->P("<b>Error incorrect OQL query:</b>");
  113. $oP->P($e->getHtmlDesc());
  114. }
  115. catch(Exception $e)
  116. {
  117. $oP->p('<b>An error occured while running the query:</b>');
  118. $oP->p($e->getMessage());
  119. }
  120. }
  121. break;
  122. case 'search':
  123. $sFilter = utils::ReadParam('filter', '');
  124. $sFormat = utils::ReadParam('format', '');
  125. $bSearchForm = utils::ReadParam('search_form', true);
  126. if (empty($sFilter))
  127. {
  128. $oP->set_title("iTop - Error");
  129. $oP->add("<p>'filter' must be specifed for this operation.</p>\n");
  130. }
  131. else
  132. {
  133. $oP->set_title("iTop - Search results");
  134. // TO DO: limit the search filter by the user context
  135. $oFilter = CMDBSearchFilter::unserialize($sFilter); // TO DO : check that the filter is valid
  136. $oSet = new DBObjectSet($oFilter);
  137. if ($bSearchForm)
  138. {
  139. $oBlock = new DisplayBlock($oFilter, 'search', false);
  140. $oBlock->Display($oP, 0);
  141. }
  142. if (strtolower($sFormat) == 'csv')
  143. {
  144. $oBlock = new DisplayBlock($oFilter, 'csv', false);
  145. $oBlock->Display($oP, 0);
  146. }
  147. else
  148. {
  149. $oBlock = new DisplayBlock($oFilter, 'list', false);
  150. $oBlock->Display($oP, 0);
  151. }
  152. }
  153. break;
  154. case 'full_text':
  155. $sFullText = trim(utils::ReadParam('text', ''));
  156. if (empty($sFullText))
  157. {
  158. $oP->p('Nothing to search.');
  159. }
  160. else
  161. {
  162. $oP->p("<h2>Results for '$sFullText':</h2>\n");
  163. $iCount = 0;
  164. // Search in full text mode in all the classes
  165. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  166. {
  167. $oFilter = new DBObjectSearch($sClassName);
  168. $oFilter->AddCondition_FullText($sFullText);
  169. $oSet = new DBObjectSet($oFilter);
  170. if ($oSet->Count() > 0)
  171. {
  172. $aLeafs = array();
  173. while($oObj = $oSet->Fetch())
  174. {
  175. if (get_class($oObj) == $sClassName)
  176. {
  177. $aLeafs[] = $oObj->GetKey();
  178. }
  179. }
  180. $oLeafsFilter = new DBObjectSearch($sClassName);
  181. if (count($aLeafs) > 0)
  182. {
  183. $iCount += count($aLeafs);
  184. $oP->add("<div class=\"page_header\">\n");
  185. $oP->add("<h1><span class=\"hilite\">".Metamodel::GetName($sClassName).":</span> ".count($aLeafs)." object(s) found.</h1>\n");
  186. $oP->add("</div>\n");
  187. $oLeafsFilter->AddCondition('pkey', $aLeafs, 'IN');
  188. $oBlock = new DisplayBlock($oLeafsFilter, 'list', false);
  189. $oBlock->Display($oP, 0);
  190. }
  191. }
  192. }
  193. if ($iCount == 0)
  194. {
  195. $oP->p('No object found.');
  196. }
  197. }
  198. break;
  199. case 'modify':
  200. $oP->add_linked_script("../js/json.js");
  201. $oP->add_linked_script("../js/forms-json-utils.js");
  202. $oP->add_linked_script("../js/wizardhelper.js");
  203. $oP->add_linked_script("../js/wizard.utils.js");
  204. $oP->add_linked_script("../js/linkswidget.js");
  205. $oP->add_linked_script("../js/jquery.blockUI.js");
  206. $sClass = utils::ReadParam('class', '');
  207. $sClassLabel = MetaModel::GetName($sClass);
  208. $id = utils::ReadParam('id', '');
  209. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  210. {
  211. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  212. }
  213. else
  214. {
  215. // Check if the user can modify this object
  216. $oSearch = new DBObjectSearch($sClass);
  217. $oSearch->AddCondition('pkey', $id, '=');
  218. $oSet = new CMDBObjectSet($oSearch);
  219. if ($oSet->Count() > 0)
  220. {
  221. $oObj = $oSet->Fetch();
  222. }
  223. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  224. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  225. if( ($oObj != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  226. {
  227. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel modification");
  228. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel modification</h1>\n");
  229. $oObj->DisplayModifyForm($oP);
  230. }
  231. else
  232. {
  233. $oP->set_title("iTop - Error");
  234. $oP->add("<p>Sorry this object does not exist (or you are not allowed to view it).</p>\n");
  235. }
  236. }
  237. break;
  238. case 'clone':
  239. $sClass = utils::ReadParam('class', '');
  240. $sClassLabel = MetaModel::GetName($sClass);
  241. $id = utils::ReadParam('id', '');
  242. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  243. {
  244. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  245. }
  246. else
  247. {
  248. // Check if the user can modify this object
  249. $oSearch = new DBObjectSearch($sClass);
  250. $oSearch->AddCondition('pkey', $id, '=');
  251. $oSet = new CMDBObjectSet($oSearch);
  252. if ($oSet->Count() > 0)
  253. {
  254. $oObjToClone = $oSet->Fetch();
  255. }
  256. $bIsModifiedAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  257. $bIsReadAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES);
  258. if( ($oObjToClone != null) && ($bIsModifiedAllowed) && ($bIsReadAllowed))
  259. {
  260. $oP->set_title("iTop - ".$oObjToClone->GetName()." - $sClassLabel clone");
  261. $oP->add("<h1>".$oObjToClone->GetName()." - $sClassLabel clone</h1>\n");
  262. cmdbAbstractObject::DisplayCreationForm($oP, $sClass, $oObjToClone);
  263. }
  264. else
  265. {
  266. $oP->set_title("iTop - Error");
  267. $oP->add("<p>Sorry this object does not exist (or you are not allowed to view it).</p>\n");
  268. }
  269. }
  270. break;
  271. case 'new':
  272. $sClass = utils::ReadParam('class', '');
  273. $sStateCode = utils::ReadParam('state', '');
  274. if ( empty($sClass) )
  275. {
  276. $oP->p("The class must be specified for this operation!");
  277. }
  278. else
  279. {
  280. $oP->add_linked_script("../js/json.js");
  281. $oP->add_linked_script("../js/forms-json-utils.js");
  282. $oP->add_linked_script("../js/wizardhelper.js");
  283. $oP->add_linked_script("../js/wizard.utils.js");
  284. $oP->add_linked_script("../js/linkswidget.js");
  285. $oP->add_linked_script("../js/jquery.blockUI.js");
  286. $oWizard = new UIWizard($oP, $sClass, $sStateCode);
  287. $sStateCode = $oWizard->GetTargetState(); // Will computes the default state if none was supplied
  288. $sClassLabel = MetaModel::GetName($sClass);
  289. $oP->p("<h2>Creation of a new $sClassLabel</h2>");
  290. if (!empty($sStateCode))
  291. {
  292. $aStates = MetaModel::EnumStates($sClass);
  293. $sStateLabel = $aStates[$sStateCode]['label'];
  294. }
  295. $aWizardSteps = $oWizard->GetWizardStructure();
  296. // Display the structure of the wizard
  297. $iStepIndex = 1;
  298. $iMaxInputId = 0;
  299. $aFieldsMap = array();
  300. foreach($aWizardSteps['mandatory'] as $aSteps)
  301. {
  302. $oP->SetCurrentTab("Step $iStepIndex *");
  303. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap);
  304. //$oP->add("</div>\n");
  305. $iStepIndex++;
  306. }
  307. foreach($aWizardSteps['optional'] as $aSteps)
  308. {
  309. $oP->SetCurrentTab("Step $iStepIndex *");
  310. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true); // true means enable the finish button
  311. //$oP->add("</div>\n");
  312. $iStepIndex++;
  313. }
  314. $oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);
  315. $oAppContext = new ApplicationContext();
  316. $oContext = new UserContext();
  317. $oObj = null;
  318. if (!empty($id))
  319. {
  320. $oObj = $oContext->GetObject($sClass, $id);
  321. }
  322. if (!is_object($oObj))
  323. {
  324. // new object or that can't be retrieved (corrupted id or object not allowed to this user)
  325. $id = '';
  326. $oObj = MetaModel::NewObject($sClass);
  327. }
  328. $oP->add("<script>
  329. // Fill the map between the fields of the form and the attributes of the object\n");
  330. $aNewFieldsMap = array();
  331. foreach($aFieldsMap as $id => $sFieldCode)
  332. {
  333. $aNewFieldsMap[$sFieldCode] = $id;
  334. }
  335. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  336. $oP->add("
  337. // Initializes the object once at the beginning of the page...
  338. var oWizardHelper = new WizardHelper('$sClass');
  339. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  340. ActivateStep(1);
  341. </script>\n");
  342. }
  343. break;
  344. case 'apply_modify':
  345. $sClass = utils::ReadPostedParam('class', '');
  346. $sClassLabel = MetaModel::GetName($sClass);
  347. $id = utils::ReadPostedParam('id', '');
  348. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  349. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  350. {
  351. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  352. }
  353. else if (!utils::IsTransactionValid($sTransactionId))
  354. {
  355. $oP->p("<strong>Error: object has already be updated!</strong>\n");
  356. }
  357. else
  358. {
  359. $oObj = $oContext->GetObject($sClass, $id);
  360. if ($oObj != null)
  361. {
  362. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel modification");
  363. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel modification</h1>\n");
  364. $bObjectModified = false;
  365. foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
  366. {
  367. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  368. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  369. {
  370. // Non-visible, or read-only attribute, do nothing
  371. }
  372. else if ($oAttDef->IsLinkSet())
  373. {
  374. // Link set, the data is a set of link objects, encoded in JSON
  375. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  376. if (!empty($aAttributes[$sAttCode]))
  377. {
  378. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  379. $oObj->Set($sAttCode, $oLinkSet);
  380. // TO DO: detect a real modification, for now always update !!
  381. $bObjectModified = true;
  382. }
  383. }
  384. else if (!$oAttDef->IsExternalField())
  385. {
  386. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  387. $previousValue = $oObj->Get($sAttCode);
  388. if (!empty($aAttributes[$sAttCode]) && ($previousValue != $aAttributes[$sAttCode]))
  389. {
  390. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  391. $bObjectModified = true;
  392. }
  393. }
  394. }
  395. if (!$bObjectModified)
  396. {
  397. $oP->p("No modification detected. ".MetaModel::GetName(get_class($oObj))." has <strong>not</strong> been updated.\n");
  398. }
  399. else if ($oObj->CheckToUpdate())
  400. {
  401. $oMyChange = MetaModel::NewObject("CMDBChange");
  402. $oMyChange->Set("date", time());
  403. if (UserRights::GetUser() != UserRights::GetRealUser())
  404. {
  405. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  406. }
  407. else
  408. {
  409. $sUserString = UserRights::GetUser();
  410. }
  411. $oMyChange->Set("userinfo", $sUserString);
  412. $iChangeId = $oMyChange->DBInsert();
  413. $oObj->DBUpdateTracked($oMyChange);
  414. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  415. }
  416. else
  417. {
  418. $oP->p("<strong>Error: object can not be updated!</strong>\n");
  419. //$oObj->Reload(); // restore default values!
  420. }
  421. }
  422. else
  423. {
  424. $oP->set_title("iTop - Error");
  425. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  426. }
  427. }
  428. $oObj->DisplayDetails($oP);
  429. break;
  430. case 'delete':
  431. $sClass = utils::ReadParam('class', '');
  432. $sClassLabel = MetaModel::GetName($sClass);
  433. $id = utils::ReadParam('id', '');
  434. $oObj = $oContext->GetObject($sClass, $id);
  435. $sName = $oObj->GetName();
  436. $oMyChange = MetaModel::NewObject("CMDBChange");
  437. $oMyChange->Set("date", time());
  438. if (UserRights::GetUser() != UserRights::GetRealUser())
  439. {
  440. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  441. }
  442. else
  443. {
  444. $sUserString = UserRights::GetUser();
  445. }
  446. $oMyChange->Set("userinfo", $sUserString);
  447. $oMyChange->DBInsert();
  448. $oObj->DBDeleteTracked($oMyChange);
  449. $oP->add("<h1>".$sName." - $sClassLabel deleted</h1>\n");
  450. break;
  451. case 'apply_new':
  452. $oP->p('Creation of the object');
  453. $oP->p('Obsolete, should now go through the wizard...');
  454. break;
  455. case 'apply_clone':
  456. $sClass = utils::ReadPostedParam('class', '');
  457. $sClassLabel = MetaModel::GetName($sClass);
  458. $iCloneId = utils::ReadPostedParam('clone_id', '');
  459. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  460. if (!utils::IsTransactionValid($sTransactionId))
  461. {
  462. $oP->p("<strong>Error: object has already be cloned!</strong>\n");
  463. }
  464. else
  465. {
  466. $oObj = $oContext->GetObject($sClass, $iCloneId);
  467. $oMyChange = MetaModel::NewObject("CMDBChange");
  468. $oMyChange->Set("date", time());
  469. if (UserRights::GetUser() != UserRights::GetRealUser())
  470. {
  471. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  472. }
  473. else
  474. {
  475. $sUserString = UserRights::GetUser();
  476. }
  477. $oMyChange->Set("userinfo", $sUserString);
  478. $iChangeId = $oMyChange->DBInsert();
  479. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  480. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  481. {
  482. if ( ('finalclass' != $sAttCode) && // finalclass is a reserved word, hardcoded !
  483. ($sStateAttCode != $sAttCode) &&
  484. (!$oAttDef->IsExternalField()) )
  485. {
  486. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  487. $oObj->Set($sAttCode, $value);
  488. }
  489. }
  490. $oObj->DBCloneTracked($oMyChange);
  491. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  492. $oObj->DisplayDetails($oP);
  493. }
  494. break;
  495. case 'wizard_apply_new':
  496. $sJson = utils::ReadPostedParam('json_obj', '');
  497. $oWizardHelper = WizardHelper::FromJSON($sJson);
  498. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  499. if (!utils::IsTransactionValid($sTransactionId))
  500. {
  501. $oP->p("<strong>Error: object has already be created!</strong>\n");
  502. }
  503. else
  504. {
  505. $oObj = $oWizardHelper->GetTargetObject();
  506. if (is_object($oObj))
  507. {
  508. $sClass = get_class($oObj);
  509. $sClassLabel = MetaModel::GetName($sClass);
  510. $oMyChange = MetaModel::NewObject("CMDBChange");
  511. $oMyChange->Set("date", time());
  512. if (UserRights::GetUser() != UserRights::GetRealUser())
  513. {
  514. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  515. }
  516. else
  517. {
  518. $sUserString = UserRights::GetUser();
  519. }
  520. $oMyChange->Set("userinfo", $sUserString);
  521. $iChangeId = $oMyChange->DBInsert();
  522. $oObj->DBInsertTracked($oMyChange);
  523. $oP->set_title("iTop - ".$oObj->GetName()." - $sClassLabel created");
  524. $oP->add("<h1>".$oObj->GetName()." - $sClassLabel created</h1>\n");
  525. $oObj->DisplayDetails($oP);
  526. }
  527. }
  528. break;
  529. case 'stimulus':
  530. $sClass = utils::ReadParam('class', '');
  531. $id = utils::ReadParam('id', '');
  532. $sStimulus = utils::ReadParam('stimulus', '');
  533. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  534. {
  535. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  536. }
  537. else
  538. {
  539. $oObj = $oContext->GetObject($sClass, $id);
  540. if ($oObj != null)
  541. {
  542. $aTransitions = $oObj->EnumTransitions();
  543. $aStimuli = MetaModel::EnumStimuli($sClass);
  544. if (!isset($aTransitions[$sStimulus]))
  545. {
  546. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  547. }
  548. else
  549. {
  550. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  551. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  552. $aTransition = $aTransitions[$sStimulus];
  553. $sTargetState = $aTransition['target_state'];
  554. $aTargetStates = MetaModel::EnumStates($sClass);
  555. $oP->add("<div class=\"page_header\">\n");
  556. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  557. //$oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  558. $oP->add("</div>\n");
  559. $oObj->DisplayBareDetails($oP);
  560. $aTargetState = $aTargetStates[$sTargetState];
  561. //print_r($aTransitions[$sStimulus]);
  562. //print_r($aTargetState);
  563. $aExpectedAttributes = $aTargetState['attribute_list'];
  564. $oP->add("<div class=\"wizHeader\">\n");
  565. $oP->add("<h1>$sActionDetails</h1>\n");
  566. $oP->add("<div class=\"wizContainer\">\n");
  567. $oP->add("<form method=\"post\">\n");
  568. $aDetails = array();
  569. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  570. {
  571. // Prompt for an attribute if
  572. // - the attribute must be changed or must be displayed to the user for confirmation
  573. // - or the field is mandatory and currently empty
  574. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  575. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  576. {
  577. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  578. $oAttDef = $aAttributesDef[$sAttCode];
  579. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetDisplayValue($sAttCode));
  580. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  581. }
  582. }
  583. $oP->details($aDetails);
  584. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  585. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  586. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  587. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  588. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  589. $oP->add($oAppContext->GetForForm());
  590. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  591. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  592. $oP->add("</form>\n");
  593. $oP->add("</div>\n");
  594. $oP->add("</div>\n");
  595. }
  596. }
  597. else
  598. {
  599. $oP->set_title("iTop - Error");
  600. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  601. }
  602. }
  603. break;
  604. case 'apply_stimulus':
  605. $sClass = utils::ReadPostedParam('class', '');
  606. $id = utils::ReadPostedParam('id', '');
  607. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  608. $sStimulus = utils::ReadPostedParam('stimulus', '');
  609. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  610. {
  611. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  612. }
  613. else
  614. {
  615. $oObj = $oContext->GetObject($sClass, $id);
  616. if ($oObj != null)
  617. {
  618. $aTransitions = $oObj->EnumTransitions();
  619. $aStimuli = MetaModel::EnumStimuli($sClass);
  620. if (!isset($aTransitions[$sStimulus]))
  621. {
  622. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  623. }
  624. else if (!utils::IsTransactionValid($sTransactionId))
  625. {
  626. $oP->p("<strong>Error: object has already been updated!</strong>\n");
  627. }
  628. else
  629. {
  630. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  631. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  632. $aTransition = $aTransitions[$sStimulus];
  633. $sTargetState = $aTransition['target_state'];
  634. $aTargetStates = MetaModel::EnumStates($sClass);
  635. $oP->add("<div class=\"page_header\">\n");
  636. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  637. $oP->add("<p>$sActionDetails</p>\n");
  638. $oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  639. $oP->add("</div>\n");
  640. $aTargetState = $aTargetStates[$sTargetState];
  641. //print_r($aTransitions[$sStimulus]);
  642. //print_r($aTargetState);
  643. $aExpectedAttributes = $aTargetState['attribute_list'];
  644. $aDetails = array();
  645. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  646. {
  647. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  648. {
  649. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  650. $oObj->Set($sAttCode, $paramValue);
  651. }
  652. }
  653. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  654. {
  655. $oMyChange = MetaModel::NewObject("CMDBChange");
  656. $oMyChange->Set("date", time());
  657. if (UserRights::GetUser() != UserRights::GetRealUser())
  658. {
  659. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  660. }
  661. else
  662. {
  663. $sUserString = UserRights::GetUser();
  664. }
  665. $oMyChange->Set("userinfo", $sUserString);
  666. $iChangeId = $oMyChange->DBInsert();
  667. $oObj->DBUpdateTracked($oMyChange);
  668. $oP->p(MetaModel::GetName(get_class($oObj))." updated.\n");
  669. }
  670. $oObj->DisplayDetails($oP);
  671. }
  672. }
  673. else
  674. {
  675. $oP->set_title("iTop - Error");
  676. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  677. }
  678. }
  679. break;
  680. case 'modify_links':
  681. $sClass = utils::ReadParam('class', '');
  682. $sLinkAttr = utils::ReadParam('link_attr', '');
  683. $sTargetClass = utils::ReadParam('target_class', '');
  684. $id = utils::ReadParam('id', '');
  685. $bAddObjects = utils::ReadParam('addObjects', false);
  686. if ( empty($sClass) || empty($id) || empty($sLinkAttr) || empty($sTargetClass)) // TO DO: check that the class name is valid !
  687. {
  688. $oP->set_title("iTop - Error");
  689. $oP->add("<p>4 parameters are mandatory for this operation: class, id, target_class and link_attr.</p>\n");
  690. }
  691. else
  692. {
  693. require_once('../application/uilinkswizard.class.inc.php');
  694. $oWizard = new UILinksWizard($sClass, $sLinkAttr, $id, $sTargetClass);
  695. $oWizard->Display($oP, $oContext, array('StartWithAdd' => $bAddObjects));
  696. }
  697. break;
  698. case 'do_modify_links':
  699. $aLinks = utils::ReadParam('linkId', array(), 'post');
  700. $sLinksToRemove = trim(utils::ReadParam('linksToRemove', '', 'post'));
  701. $aLinksToRemove = array();
  702. if (!empty($sLinksToRemove))
  703. {
  704. $aLinksToRemove = explode(' ', trim($sLinksToRemove));
  705. }
  706. $sClass = utils::ReadParam('class', '', 'post');
  707. $sLinkageAtt = utils::ReadParam('linkage', '', 'post');
  708. $iObjectId = utils::ReadParam('object_id', '', 'post');
  709. $sLinkingAttCode = utils::ReadParam('linking_attcode', '', 'post');
  710. $oMyChange = MetaModel::NewObject("CMDBChange");
  711. $oMyChange->Set("date", time());
  712. if (UserRights::GetUser() != UserRights::GetRealUser())
  713. {
  714. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  715. }
  716. else
  717. {
  718. $sUserString = UserRights::GetUser();
  719. }
  720. $oMyChange->Set("userinfo", $sUserString);
  721. $iChangeId = $oMyChange->DBInsert();
  722. // Delete links that are to be deleted
  723. foreach($aLinksToRemove as $iLinkId)
  724. {
  725. if ($iLinkId > 0) // Negative IDs are objects that were not even created
  726. {
  727. $oLink = $oContext->GetObject($sClass, $iLinkId);
  728. $oLink->DBDeleteTracked($oMyChange);
  729. }
  730. }
  731. $aEditableFields = array();
  732. $aData = array();
  733. foreach(MetaModel::GetAttributesList($sClass) as $sAttCode)
  734. {
  735. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  736. if ( (!$oAttDef->IsExternalKey()) && (!$oAttDef->IsExternalField()))
  737. {
  738. $aEditableFields[] = $sAttCode;
  739. $aData[$sAttCode] = utils::ReadParam('attr_'.$sAttCode, array(), 'post');
  740. }
  741. }
  742. // Update existing links or create new links
  743. foreach($aLinks as $iLinkId)
  744. {
  745. if ($iLinkId > 0)
  746. {
  747. // This is an existing link to be modified
  748. $oLink = $oContext->GetObject($sClass, $iLinkId);
  749. // Update all the attributes of the link
  750. foreach($aEditableFields as $sAttCode)
  751. {
  752. $value = $aData[$sAttCode][$iLinkId];
  753. $oLink->Set($sAttCode, $value);
  754. }
  755. if ($oLink->IsModified())
  756. {
  757. $oLink->DBUpdateTracked($oMyChange);
  758. }
  759. //echo "Updated link:<br/>\n";
  760. //var_dump($oLink);
  761. }
  762. else
  763. {
  764. // A new link must be created
  765. $oLink = MetaModel::NewObject($sClass);
  766. $oLinkedObjectId = -$iLinkId;
  767. // Set all the attributes of the link
  768. foreach($aEditableFields as $sAttCode)
  769. {
  770. $value = $aData[$sAttCode][$iLinkId];
  771. $oLink->Set($sAttCode, $value);
  772. }
  773. // And the two external keys
  774. $oLink->Set($sLinkageAtt, $iObjectId);
  775. $oLink->Set($sLinkingAttCode, $oLinkedObjectId);
  776. // then save it
  777. //echo "Created link:<br/>\n";
  778. //var_dump($oLink);
  779. $oLink->DBInsertTracked($oMyChange);
  780. }
  781. }
  782. // Display again the details of the linked object
  783. $oAttDef = MetaModel::GetAttributeDef($sClass, $sLinkageAtt);
  784. $sTargetClass = $oAttDef->GetTargetClass();
  785. $oObj = $oContext->GetObject($sTargetClass, $iObjectId);
  786. $oSearch = $oContext->NewFilter(get_class($oObj));
  787. $oBlock = new DisplayBlock($oSearch, 'search', false);
  788. $oBlock->Display($oP, 0);
  789. $oObj->DisplayDetails($oP);
  790. break;
  791. default:
  792. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  793. }
  794. ////MetaModel::ShowQueryTrace();
  795. $oP->output();
  796. ?>