UI.php 22 KB

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