UI.php 31 KB

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