UI.php 24 KB

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