UI.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. $sClassLabel = MetaModel::GetName($sClass);
  233. $oP->p("<h2>Creation of a new $sClassLabel</h2>");
  234. if (!empty($sStateCode))
  235. {
  236. $aStates = MetaModel::EnumStates($sClass);
  237. $sStateLabel = $aStates[$sStateCode]['label'];
  238. }
  239. $aWizardSteps = $oWizard->GetWizardStructure();
  240. // Display the structure of the wizard
  241. $iStepIndex = 1;
  242. $iMaxInputId = 0;
  243. $aFieldsMap = array();
  244. foreach($aWizardSteps['mandatory'] as $aSteps)
  245. {
  246. $oP->SetCurrentTab("Step $iStepIndex *");
  247. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap);
  248. //$oP->add("</div>\n");
  249. $iStepIndex++;
  250. }
  251. foreach($aWizardSteps['optional'] as $aSteps)
  252. {
  253. $oP->SetCurrentTab("Step $iStepIndex *");
  254. $oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true); // true means enable the finish button
  255. //$oP->add("</div>\n");
  256. $iStepIndex++;
  257. }
  258. $oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);
  259. $oAppContext = new ApplicationContext();
  260. $oContext = new UserContext();
  261. $oObj = null;
  262. if (!empty($id))
  263. {
  264. $oObj = $oContext->GetObject($sClass, $id);
  265. }
  266. if (!is_object($oObj))
  267. {
  268. // new object or or that can't be retrieved (corrupted id or object not allowed to this user)
  269. $id = '';
  270. $oObj = MetaModel::NewObject($sClass);
  271. }
  272. $oP->add("<script>
  273. // Fill the map between the fields of the form and the attributes of the object\n");
  274. $aNewFieldsMap = array();
  275. foreach($aFieldsMap as $id => $sFieldCode)
  276. {
  277. $aNewFieldsMap[$sFieldCode] = $id;
  278. }
  279. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  280. $oP->add("
  281. // Initializes the object once at the beginning of the page...
  282. var oWizardHelper = new WizardHelper('$sClass');
  283. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  284. ActivateStep(1);
  285. </script>\n");
  286. }
  287. break;
  288. case 'apply_modify':
  289. $sClass = utils::ReadPostedParam('class', '');
  290. $id = utils::ReadPostedParam('id', '');
  291. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  292. if ( empty($sClass) || empty($id)) // TO DO: check that the class name is valid !
  293. {
  294. $oP->add("<p>'class' and 'id' parameters must be specifed for this operation.</p>\n");
  295. }
  296. else if (!utils::IsTransactionValid($sTransactionId))
  297. {
  298. $oP->p("<strong>Error: object has already be updated!</strong>\n");
  299. }
  300. else
  301. {
  302. $oObj = $oContext->GetObject($sClass, $id);
  303. if ($oObj != null)
  304. {
  305. $oP->set_title("iTop - ".$oObj->GetName()." - $sClass modification");
  306. $oP->add("<h1>".$oObj->GetName()." - $sClass modification</h1>\n");
  307. $bObjectModified = false;
  308. foreach(MetaModel::ListAttributeDefs(get_class($oObj)) as $sAttCode=>$oAttDef)
  309. {
  310. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  311. if ($iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
  312. {
  313. // Non-visible, or read-only attribute, do nothing
  314. }
  315. else if ($oAttDef->IsLinkSet())
  316. {
  317. // Link set, the data is a set of link objects, encoded in JSON
  318. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  319. if (!empty($aAttributes[$sAttCode]))
  320. {
  321. $oLinkSet = WizardHelper::ParseJsonSet($oObj, $oAttDef->GetLinkedClass(), $oAttDef->GetExtKeyToMe(), $aAttributes[$sAttCode]);
  322. $oObj->Set($sAttCode, $oLinkSet);
  323. // TO DO: detect a real modification, for now always update !!
  324. $bObjectModified = true;
  325. }
  326. }
  327. else if (!$oAttDef->IsExternalField())
  328. {
  329. $aAttributes[$sAttCode] = trim(utils::ReadPostedParam("attr_$sAttCode", ''));
  330. $previousValue = $oObj->Get($sAttCode);
  331. if (!empty($aAttributes[$sAttCode]) && ($previousValue != $aAttributes[$sAttCode]))
  332. {
  333. $oObj->Set($sAttCode, $aAttributes[$sAttCode]);
  334. $bObjectModified = true;
  335. }
  336. }
  337. }
  338. if (!$bObjectModified)
  339. {
  340. $oP->p("No modification detected. ".get_class($oObj)." has <strong>not</strong> been updated.\n");
  341. }
  342. else if ($oObj->CheckToUpdate())
  343. {
  344. $oMyChange = MetaModel::NewObject("CMDBChange");
  345. $oMyChange->Set("date", time());
  346. if (UserRights::GetUser() != UserRights::GetRealUser())
  347. {
  348. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  349. }
  350. else
  351. {
  352. $sUserString = UserRights::GetUser();
  353. }
  354. $oMyChange->Set("userinfo", $sUserString);
  355. $iChangeId = $oMyChange->DBInsert();
  356. $oObj->DBUpdateTracked($oMyChange);
  357. $oP->p(get_class($oObj)." updated.\n");
  358. }
  359. else
  360. {
  361. $oP->p("<strong>Error: object can not be updated!</strong>\n");
  362. //$oObj->Reload(); // restore default values!
  363. }
  364. }
  365. else
  366. {
  367. $oP->set_title("iTop - Error");
  368. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  369. }
  370. }
  371. $oObj->DisplayDetails($oP);
  372. break;
  373. case 'delete':
  374. $sClass = utils::ReadParam('class', '');
  375. $id = utils::ReadParam('id', '');
  376. $oObj = $oContext->GetObject($sClass, $id);
  377. $sName = $oObj->GetName();
  378. $oMyChange = MetaModel::NewObject("CMDBChange");
  379. $oMyChange->Set("date", time());
  380. if (UserRights::GetUser() != UserRights::GetRealUser())
  381. {
  382. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  383. }
  384. else
  385. {
  386. $sUserString = UserRights::GetUser();
  387. }
  388. $oMyChange->Set("userinfo", $sUserString);
  389. $oMyChange->DBInsert();
  390. $oObj->DBDeleteTracked($oMyChange);
  391. $oP->add("<h1>".$sName." - $sClass deleted</h1>\n");
  392. break;
  393. case 'apply_new':
  394. $oP->p('Creation of the object');
  395. $oP->p('Obsolete, should now go through the wizard...');
  396. break;
  397. case 'apply_clone':
  398. $sClass = utils::ReadPostedParam('class', '');
  399. $iCloneId = utils::ReadPostedParam('clone_id', '');
  400. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  401. if (!utils::IsTransactionValid($sTransactionId))
  402. {
  403. $oP->p("<strong>Error: object has already be cloned!</strong>\n");
  404. }
  405. else
  406. {
  407. $oObj = $oContext->GetObject($sClass, $iCloneId);
  408. $oMyChange = MetaModel::NewObject("CMDBChange");
  409. $oMyChange->Set("date", time());
  410. if (UserRights::GetUser() != UserRights::GetRealUser())
  411. {
  412. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  413. }
  414. else
  415. {
  416. $sUserString = UserRights::GetUser();
  417. }
  418. $oMyChange->Set("userinfo", $sUserString);
  419. $iChangeId = $oMyChange->DBInsert();
  420. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObj));
  421. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  422. {
  423. if ( ('finalclass' != $sAttCode) && // finalclass is a reserved word, hardcoded !
  424. ($sStateAttCode != $sAttCode) &&
  425. (!$oAttDef->IsExternalField()) )
  426. {
  427. $value = utils::ReadPostedParam('attr_'.$sAttCode, '');
  428. $oObj->Set($sAttCode, $value);
  429. }
  430. }
  431. $oObj->DBCloneTracked($oMyChange);
  432. $oP->add("<h1>".$oObj->GetName()." - $sClass created</h1>\n");
  433. $oObj->DisplayDetails($oP);
  434. }
  435. break;
  436. case 'wizard_apply_new':
  437. $sJson = utils::ReadPostedParam('json_obj', '');
  438. $oWizardHelper = WizardHelper::FromJSON($sJson);
  439. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  440. if (!utils::IsTransactionValid($sTransactionId))
  441. {
  442. $oP->p("<strong>Error: object has already be created!</strong>\n");
  443. }
  444. else
  445. {
  446. $oObj = $oWizardHelper->GetTargetObject();
  447. if (is_object($oObj))
  448. {
  449. $sClass = get_class($oObj);
  450. $oMyChange = MetaModel::NewObject("CMDBChange");
  451. $oMyChange->Set("date", time());
  452. if (UserRights::GetUser() != UserRights::GetRealUser())
  453. {
  454. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  455. }
  456. else
  457. {
  458. $sUserString = UserRights::GetUser();
  459. }
  460. $oMyChange->Set("userinfo", $sUserString);
  461. $iChangeId = $oMyChange->DBInsert();
  462. $oObj->DBInsertTracked($oMyChange);
  463. $oP->set_title("iTop - ".$oObj->GetName()." - $sClass created");
  464. $oP->add("<h1>".$oObj->GetName()." - $sClass created</h1>\n");
  465. $oObj->DisplayDetails($oP);
  466. }
  467. }
  468. break;
  469. case 'stimulus':
  470. $sClass = utils::ReadParam('class', '');
  471. $id = utils::ReadParam('id', '');
  472. $sStimulus = utils::ReadParam('stimulus', '');
  473. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  474. {
  475. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  476. }
  477. else
  478. {
  479. $oObj = $oContext->GetObject($sClass, $id);
  480. if ($oObj != null)
  481. {
  482. $aTransitions = $oObj->EnumTransitions();
  483. $aStimuli = MetaModel::EnumStimuli($sClass);
  484. if (!isset($aTransitions[$sStimulus]))
  485. {
  486. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  487. }
  488. else
  489. {
  490. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  491. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  492. $aTransition = $aTransitions[$sStimulus];
  493. $sTargetState = $aTransition['target_state'];
  494. $aTargetStates = MetaModel::EnumStates($sClass);
  495. $oP->add("<div class=\"page_header\">\n");
  496. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  497. //$oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  498. $oP->add("</div>\n");
  499. $oObj->DisplayBareDetails($oP);
  500. $aTargetState = $aTargetStates[$sTargetState];
  501. //print_r($aTransitions[$sStimulus]);
  502. //print_r($aTargetState);
  503. $aExpectedAttributes = $aTargetState['attribute_list'];
  504. $oP->add("<div class=\"wizHeader\">\n");
  505. $oP->add("<h1>$sActionDetails</h1>\n");
  506. $oP->add("<div class=\"wizContainer\">\n");
  507. $oP->add("<form method=\"post\">\n");
  508. $aDetails = array();
  509. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  510. {
  511. // Prompt for an attribute if
  512. // - the attribute must be changed or must be displayed to the user for confirmation
  513. // - or the field is mandatory and currently empty
  514. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  515. (($iExpectCode & OPT_ATT_MANDATORY) && ($oObj->Get($sAttCode) == '')) )
  516. {
  517. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  518. $oAttDef = $aAttributesDef[$sAttCode];
  519. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oObj->Get($sAttCode), $oObj->GetDisplayValue($sAttCode));
  520. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  521. }
  522. }
  523. $oP->details($aDetails);
  524. $oP->add("<input type=\"hidden\" name=\"id\" value=\"$id\">\n");
  525. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  526. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"apply_stimulus\">\n");
  527. $oP->add("<input type=\"hidden\" name=\"stimulus\" value=\"$sStimulus\">\n");
  528. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  529. $oP->add($oAppContext->GetForForm());
  530. $oP->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  531. $oP->add("<button type=\"submit\" class=\"action\"><span>$sActionLabel</span></button>\n");
  532. $oP->add("</form>\n");
  533. $oP->add("</div>\n");
  534. $oP->add("</div>\n");
  535. }
  536. }
  537. else
  538. {
  539. $oP->set_title("iTop - Error");
  540. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  541. }
  542. }
  543. break;
  544. case 'apply_stimulus':
  545. $sClass = utils::ReadPostedParam('class', '');
  546. $id = utils::ReadPostedParam('id', '');
  547. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  548. $sStimulus = utils::ReadPostedParam('stimulus', '');
  549. if ( empty($sClass) || empty($id) || empty($sStimulus) ) // TO DO: check that the class name is valid !
  550. {
  551. $oP->add("<p>'class', 'id' and 'stimulus' parameters must be specifed for this operation.</p>\n");
  552. }
  553. else
  554. {
  555. $oObj = $oContext->GetObject($sClass, $id);
  556. if ($oObj != null)
  557. {
  558. $aTransitions = $oObj->EnumTransitions();
  559. $aStimuli = MetaModel::EnumStimuli($sClass);
  560. if (!isset($aTransitions[$sStimulus]))
  561. {
  562. $oP->add("<p><strong>Error:</strong> Invalid stimulus: '$sStimulus' on object: {$oObj->GetName()} in state {$oObj->GetState()}.</p>\n");
  563. }
  564. else if (!utils::IsTransactionValid($sTransactionId))
  565. {
  566. $oP->p("<strong>Error: object has already be updated!</strong>\n");
  567. }
  568. else
  569. {
  570. $sActionLabel = $aStimuli[$sStimulus]->Get('label');
  571. $sActionDetails = $aStimuli[$sStimulus]->Get('description');
  572. $aTransition = $aTransitions[$sStimulus];
  573. $sTargetState = $aTransition['target_state'];
  574. $aTargetStates = MetaModel::EnumStates($sClass);
  575. $oP->add("<div class=\"page_header\">\n");
  576. $oP->add("<h1>$sActionLabel - <span class=\"hilite\">{$oObj->GetName()}</span></h1>\n");
  577. $oP->add("<p>$sActionDetails</p>\n");
  578. $oP->add("<p>Applying '$sActionLabel' on object: {$oObj->GetName()} in state {$oObj->GetState()} to target state: $sTargetState.</p>\n");
  579. $oP->add("</div>\n");
  580. $aTargetState = $aTargetStates[$sTargetState];
  581. //print_r($aTransitions[$sStimulus]);
  582. //print_r($aTargetState);
  583. $aExpectedAttributes = $aTargetState['attribute_list'];
  584. $aDetails = array();
  585. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  586. {
  587. if (($iExpectCode & OPT_ATT_MUSTCHANGE) || ($oObj->Get($sAttCode) == '') )
  588. {
  589. $paramValue = utils::ReadPostedParam("attr_$sAttCode", '');
  590. $oObj->Set($sAttCode, $paramValue);
  591. }
  592. }
  593. if ($oObj->ApplyStimulus($sStimulus) && $oObj->CheckToUpdate())
  594. {
  595. $oMyChange = MetaModel::NewObject("CMDBChange");
  596. $oMyChange->Set("date", time());
  597. if (UserRights::GetUser() != UserRights::GetRealUser())
  598. {
  599. $sUserString = UserRights::GetRealUser()." on behalf of ".UserRights::GetUser();
  600. }
  601. else
  602. {
  603. $sUserString = UserRights::GetUser();
  604. }
  605. $oMyChange->Set("userinfo", $sUserString);
  606. $iChangeId = $oMyChange->DBInsert();
  607. $oObj->DBUpdateTracked($oMyChange);
  608. $oP->p(get_class($oObj)." updated.\n");
  609. }
  610. $oObj->DisplayDetails($oP);
  611. }
  612. }
  613. else
  614. {
  615. $oP->set_title("iTop - Error");
  616. $oP->add("<p>Sorry this object does not exist (or you are not allowed to edit it).</p>\n");
  617. }
  618. }
  619. break;
  620. default:
  621. $oActiveNode->RenderContent($oP, $oAppContext->GetAsHash());
  622. }
  623. $oP->output();
  624. ?>