index.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * iTop User Portal main page
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/nicewebpage.class.inc.php');
  27. require_once(APPROOT.'/application/wizardhelper.class.inc.php');
  28. /**
  29. * Get the list of parameters (i.e. attribute codes) to be handled while creating a new UserRequest object
  30. * @return Array The list of attribute codes
  31. */
  32. function GetParamsList()
  33. {
  34. return array('org_id', 'caller_id', 'service_id', 'servicesubcategory_id', 'request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  35. }
  36. /**
  37. * Outputs a list of parameters as hidden field into the current page
  38. * (must be called when inside a form)
  39. * @param WebPage $oP The current web page
  40. * @param Array $aInteractive The list of parameters that are handled intractively and thus should not be output as hidden fields
  41. * @param Hash $aParameters Array name => value for the parameters
  42. * @return void
  43. */
  44. function DumpHiddenParams($oP, $aInteractive, $aParameters)
  45. {
  46. foreach($aParameters as $sAttCode => $value)
  47. {
  48. if (!in_array($sAttCode, $aInteractive))
  49. {
  50. $oP->Add("<input type=\"hidden\" name=\"attr_$sAttCode\" value=\"$value\">");
  51. }
  52. }
  53. }
  54. /**
  55. * Read all the parameters of the page for building a UserRequest
  56. * Parameters that were absent from the page's parameters are not set in the resulting hash array
  57. * @input string $sMethod Either get or post
  58. * @return Hash Array of name => value corresponding to the parameters that were passed to the page
  59. */
  60. function ReadAllParams($sMethod = 'get')
  61. {
  62. $aParams = GetParamsList();
  63. $aValues = array();
  64. foreach($aParams as $sName)
  65. {
  66. $value = utils::ReadParam('attr_'.$sName, null, $sMethod);
  67. if (!is_null($value))
  68. {
  69. $aValues[$sName] = $value;
  70. }
  71. }
  72. return $aValues;
  73. }
  74. /**
  75. * Displays the portal main menu
  76. * @param WebPage $oP The current web page
  77. * @return void
  78. */
  79. function DisplayMainMenu(WebPage $oP)
  80. {
  81. $oP->AddMenuButton('refresh', 'Portal:Refresh', './index.php?operation=welcome');
  82. $oP->AddMenuButton('create', 'Portal:CreateNewRequest', './index.php?operation=create_request');
  83. $oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', './index.php?loginop=change_pwd');
  84. $oP->add("<div id=\"#div_resolved_requests\">\n");
  85. $oP->add("<h1 id=\"#open_requests\">".Dict::S('Portal:OpenRequests')."</h1>\n");
  86. ListOpenRequests($oP);
  87. $oP->add("</div>\n");
  88. $oP->add("<div id=\"#div_resolved_requests\">\n");
  89. $oP->add("<h1 id=\"#resolved_requests\">".Dict::S('Portal:ResolvedRequests')."</h1>\n");
  90. ListResolvedRequests($oP);
  91. $oP->add("</div>\n");
  92. }
  93. /**
  94. * Displays the form to select a Service Id (among the valid ones for the specified user Organization)
  95. * @param WebPage $oP Web page for the form output
  96. * @param Organization $oUserOrg The organization of the current user
  97. * @return void
  98. */
  99. function SelectService($oP, $oUserOrg)
  100. {
  101. // Init forms parameters
  102. $aParameters = ReadAllParams();
  103. $oSearch = DBObjectSearch::FromOQL('SELECT Service AS s JOIN SLA AS sla ON sla.service_id=s.id JOIN lnkContractToSLA AS ln ON ln.sla_id=sla.id JOIN CustomerContract AS cc ON ln.contract_id=cc.id WHERE cc.org_id = :org_id');
  104. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  105. $oP->add("<div class=\"wizContainer\" id=\"form_select_service\">\n");
  106. $oP->add("<h1 id=\"select_subcategory\">".Dict::S('Portal:SelectService')."</h1>\n");
  107. $oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"get\">\n");
  108. $oP->add("<table>\n");
  109. while($oService = $oSet->Fetch())
  110. {
  111. $id = $oService->GetKey();
  112. $sChecked = "";
  113. if ($id == $aParameters['service_id'])
  114. {
  115. $sChecked = "checked";
  116. }
  117. $oP->p("<tr><td style=\"vertical-align:top\"><p><input name=\"attr_service_id\" $sChecked type=\"radio\" id=\"svc_$id\" value=\"$id\"></p></td><td style=\"vertical-align:top\"><p><b><label for=\"svc_$id\">".htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8')."</label></b></p>");
  118. $oP->p("<p>".htmlentities($oService->Get('description'), ENT_QUOTES, 'UTF-8')."</p></td></tr>");
  119. }
  120. $oP->add("</table>\n");
  121. DumpHiddenParams($oP, array('service_id'), $aParameters);
  122. $oP->add("<input type=\"hidden\" name=\"step\" value=\"1\">");
  123. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  124. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">");
  125. $oP->add("</form>");
  126. $oP->add("</div class=\"wizContainer\">\n");
  127. $sMessage = Dict::S('Portal:PleaseSelectOneService');
  128. $oP->add_ready_script(
  129. <<<EOF
  130. $('#request_form').submit(function() {
  131. return CheckSelection('$sMessage');
  132. });
  133. EOF
  134. );
  135. }
  136. /**
  137. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  138. * and based on the page's parameter 'service_id'
  139. * @param WebPage $oP Web page for the form output
  140. * @param Organization $oUserOrg The organization of the current user
  141. * @return void
  142. */
  143. function SelectSubService($oP, $oUserOrg)
  144. {
  145. // Init forms parameters
  146. $aParameters = ReadAllParams();
  147. $iSvcId = $aParameters['service_id'];
  148. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  149. $oSearch = DBObjectSearch::FromOQL('SELECT ServiceSubcategory AS ss WHERE ss.service_id = :svc_id');
  150. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId));
  151. $oService = MetaModel::GetObject('Service', $iSvcId, false);
  152. if (is_object($oService))
  153. {
  154. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  155. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8'))."</h1>\n");
  156. $oP->add("<form id=\"request_form\" method=\"get\">\n");
  157. $oP->add("<table>\n");
  158. while($oSubService = $oSet->Fetch())
  159. {
  160. $id = $oSubService->GetKey();
  161. $sChecked = "";
  162. if ($id == $iDefaultSubSvcId)
  163. {
  164. $sChecked = "checked";
  165. }
  166. $oP->p("<tr><td style=\"vertical-align:top\"><p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"subsvc_$id\" value=\"$id\"></p></td><td style=\"vertical-align:top\"><p><b><label for=\"subsvc_$id\">".htmlentities($oSubService->GetName(), ENT_QUOTES, 'UTF-8')."</label></b></p>");
  167. $oP->p("<p>".htmlentities($oSubService->Get('description'), ENT_QUOTES, 'UTF-8')."</p></td></tr>");
  168. }
  169. $sMessage = Dict::S('Portal:PleaseSelectAServiceSubCategory');
  170. $oP->add_ready_script(
  171. <<<EOF
  172. $('#request_form').submit(function() {
  173. return CheckSelection('$sMessage');
  174. });
  175. EOF
  176. );
  177. $oP->add("</table>\n");
  178. DumpHiddenParams($oP, array('servicesubcategory_id'), $aParameters);
  179. $oP->add("<input type=\"hidden\" name=\"step\" value=\"2\">");
  180. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  181. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoBack();\">&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">");
  182. $oP->add("</form>");
  183. $oP->add("</div>\n");
  184. }
  185. else
  186. {
  187. $oP->p("Error: Invalid Service: id = $iSvcId");
  188. }
  189. }
  190. /**
  191. * Displays the form for the final step of the UserRequest creation
  192. * @param WebPage $oP The current web page for the form output
  193. * @param Organization $oUserOrg The organization of the current user
  194. * @return void
  195. */
  196. function RequestCreationForm($oP, $oUserOrg)
  197. {
  198. $aList = array('request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  199. $aParameters = ReadAllParams();
  200. $oService = MetaModel::GetObject('Service', $aParameters['service_id'], false);
  201. $oSubService = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false);
  202. if (is_object($oService) && is_object($oSubService))
  203. {
  204. $oRequest = new UserRequest();
  205. $oRequest->Set('org_id', $oUserOrg->GetKey());
  206. $oRequest->Set('caller_id', UserRights::GetContactId());
  207. $oRequest->Set('service_id', $aParameters['service_id']);
  208. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  209. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'service_id');
  210. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8'));
  211. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'servicesubcategory_id');
  212. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => htmlentities($oSubService->GetName(), ENT_QUOTES, 'UTF-8'));
  213. $iFlags = 0;
  214. foreach($aList as $sAttCode)
  215. {
  216. $value = '';
  217. if (isset($aParameters[$sAttCode]))
  218. {
  219. $value = $aParameters[$sAttCode];
  220. $oRequest->Set($sAttCode, $value);
  221. }
  222. }
  223. foreach($aList as $sAttCode)
  224. {
  225. $value = '';
  226. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  227. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  228. if (isset($aParameters[$sAttCode]))
  229. {
  230. $value = $aParameters[$sAttCode];
  231. }
  232. $aArgs = array('this' => $oRequest);
  233. $sValue = $oRequest->GetFormElementForField($oP, get_class($oRequest), $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs);
  234. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  235. }
  236. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  237. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  238. $oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  239. $oP->add("<table>\n");
  240. $oP->details($aDetails);
  241. DumpHiddenParams($oP, $aList, $aParameters);
  242. $oP->add("<input type=\"hidden\" name=\"step\" value=\"3\">");
  243. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  244. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  245. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoBack();\">&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Finish')."\">");
  246. $oP->add("</form>");
  247. $oP->add("</div>\n");
  248. $oP->add_ready_script(
  249. <<<EOF
  250. // Starts the validation when the page is ready
  251. CheckFields('request_form', false);
  252. $('#request_form').submit( function() {
  253. return CheckFields('request_form', true);
  254. });
  255. EOF
  256. );
  257. }
  258. else
  259. {
  260. // User not authorized to use this service ?
  261. DisplayMainMenu($oP);
  262. }
  263. }
  264. /**
  265. * Validate the parameters and create the UserRequest object (based on the page's POSTed parameters)
  266. * @param WebPage $oP The current web page for the output
  267. * @param Organization $oUserOrg The organization of the current user
  268. * @return void
  269. */
  270. function DoCreateRequest($oP, $oUserOrg)
  271. {
  272. $aParameters = ReadAllParams();
  273. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  274. if (!utils::IsTransactionValid($sTransactionId))
  275. {
  276. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  277. DisplayMainMenu($oP);
  278. return;
  279. }
  280. // Validate the parameters
  281. // 1) Service
  282. $oSearch = DBObjectSearch::FromOQL('SELECT Service AS s JOIN SLA AS sla ON sla.service_id=s.id JOIN lnkContractToSLA AS ln ON ln.sla_id=sla.id JOIN CustomerContract AS cc ON ln.contract_id=cc.id WHERE cc.org_id = :org_id AND s.id = :svc_id');
  283. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey(), 'svc_id' => $aParameters['service_id']));
  284. if ($oSet->Count() != 1)
  285. {
  286. // Invalid service for the current user !
  287. throw new Exception("Invalid Service: id={$aParameters['servicesubcategory_id']} for the current user (org_id=".$oUserOrg->GetKey().").");
  288. }
  289. $oService = $oSet->Fetch();
  290. // 2) Service Subcategory
  291. $oSearch = DBObjectSearch::FromOQL('SELECT ServiceSubcategory AS sc WHERE sc.id = :subcategory_id AND sc.service_id = :svc_id');
  292. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $aParameters['service_id'], 'subcategory_id' =>$aParameters['servicesubcategory_id'] ));
  293. if ($oSet->Count() != 1)
  294. {
  295. // Invalid subcategory
  296. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service ".$oService->GetName()."({$aParameters['service_id']})");
  297. }
  298. $oRequest = new UserRequest();
  299. $oRequest->Set('org_id', $oUserOrg->GetKey());
  300. $oRequest->Set('caller_id', UserRights::GetContactId());
  301. $aList = array('service_id', 'servicesubcategory_id', 'request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  302. foreach($aList as $sAttCode)
  303. {
  304. $oRequest->Set($sAttCode, $aParameters[$sAttCode]);
  305. }
  306. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  307. if ($bRes)
  308. {
  309. $oMyChange = MetaModel::NewObject("CMDBChange");
  310. $oMyChange->Set("date", time());
  311. if (UserRights::IsImpersonated())
  312. {
  313. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  314. }
  315. else
  316. {
  317. $sUserString = UserRights::GetUser();
  318. }
  319. $oMyChange->Set("userinfo", $sUserString);
  320. $iChangeId = $oMyChange->DBInsert();
  321. $oRequest->DBInsertTracked($oMyChange);
  322. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName(get_class($oRequest)))."</h1>\n");
  323. DisplayMainMenu($oP);
  324. }
  325. else
  326. {
  327. RequestCreationForm($oP, $oUserOrg);
  328. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  329. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  330. }
  331. }
  332. /**
  333. * Prompts the user for creating a new request
  334. * @param WebPage $oP The current web page
  335. * @return void
  336. */
  337. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  338. {
  339. $iStep = utils::ReadParam('step', 0);
  340. switch($iStep)
  341. {
  342. case 0:
  343. default:
  344. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  345. SelectService($oP, $oUserOrg);
  346. break;
  347. case 1:
  348. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  349. SelectSubService($oP, $oUserOrg);
  350. break;
  351. case 2:
  352. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  353. RequestCreationForm($oP, $oUserOrg);
  354. break;
  355. case 3:
  356. DoCreateRequest($oP, $oUserOrg);
  357. break;
  358. }
  359. }
  360. /**
  361. * Displays the value of the given field, in HTML, without any hyperlink to other objects
  362. * @param DBObject $oObj The object to use
  363. * @param string $sAttCode Code of the attribute to display
  364. * @return string HTML text representing the value of this field
  365. */
  366. function GetFieldAsHtml($oObj, $sAttCode)
  367. {
  368. $sValue = '';
  369. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  370. if ($oAttDef->IsExternalKey())
  371. {
  372. // Special processing for external keys: don't display any hyperlink
  373. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $oObj->Get($sAttCode), false);
  374. if (is_object($oTargetObj))
  375. {
  376. $sValue = $oTargetObj->GetName();
  377. }
  378. else
  379. {
  380. $sValue = Dict::S('UI:UndefinedObject');
  381. }
  382. }
  383. else
  384. {
  385. $sValue = $oObj->GetAsHTML($sAttCode);
  386. }
  387. return $sValue;
  388. }
  389. /**
  390. * Displays a list of objects, without any hyperlink (except for the object's details)
  391. * @param WebPage $oP The web page for the output
  392. * @param DBObjectSet $oSet The set of objects to display
  393. * @param Array $aZList The ZList (list of field codes) to use for the tabular display
  394. * @return string The HTML text representing the list
  395. */
  396. function DisplaySet($oP, $oSet, $aZList)
  397. {
  398. if ($oSet->Count() > 0)
  399. {
  400. $aAttribs = array();
  401. $aValues = array();
  402. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'ref');
  403. $aAttribs['key'] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  404. foreach($aZList as $sAttCode)
  405. {
  406. $oAttDef = MetaModel::GetAttributeDef('UserRequest', $sAttCode);
  407. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  408. }
  409. while($oRequest = $oSet->Fetch())
  410. {
  411. $aRow = array();
  412. $aRow['key'] = '<a href="./index.php?operation=details&id='.$oRequest->GetKey().'">'.$oRequest->Get('ref').'</a>';
  413. $sHilightClass = $oRequest->GetHilightClass();
  414. if ($sHilightClass != '')
  415. {
  416. $aRow['@class'] = $sHilightClass;
  417. }
  418. foreach($aZList as $sAttCode)
  419. {
  420. $aRow[$sAttCode] = GetFieldAsHtml($oRequest, $sAttCode);
  421. }
  422. $aValues[$oRequest->GetKey()] = $aRow;
  423. }
  424. $oP->Table($aAttribs, $aValues);
  425. }
  426. else
  427. {
  428. $oP->add(Dict::S('Portal:NoOpenRequest'));
  429. }
  430. }
  431. /**
  432. * Lists all the currently opened User Requests for the current user
  433. * @param WebPage $oP The current web page
  434. * @return void
  435. */
  436. function ListOpenRequests(WebPage $oP)
  437. {
  438. $iContactId = UserRights::GetContactId();
  439. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  440. if (is_object($oContact))
  441. {
  442. $sOQL = 'SELECT UserRequest WHERE caller_id = :contact_id AND status NOT IN ("resolved", "closed")';
  443. $oSearch = DBObjectSearch::FromOQL($sOQL);
  444. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId));
  445. $aZList = array('title', 'start_date', 'status', 'service_id', 'priority', 'workgroup_id', 'agent_id');
  446. DisplaySet($oP, $oSet, $aZList);
  447. }
  448. }
  449. /**
  450. * Lists all the currently Resolved (not "Closed")User Requests for the current user
  451. * @param WebPage $oP The current web page
  452. * @return void
  453. */
  454. function ListResolvedRequests(WebPage $oP)
  455. {
  456. $iContactId = UserRights::GetContactId();
  457. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  458. if (is_object($oContact))
  459. {
  460. $sOQL = 'SELECT UserRequest WHERE caller_id = :contact_id AND status ="resolved"';
  461. $oSearch = DBObjectSearch::FromOQL($sOQL);
  462. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId));
  463. $aZList = array('title', 'start_date', 'status', 'service_id', 'priority', 'workgroup_id', 'agent_id');
  464. DisplaySet($oP, $oSet, $aZList);
  465. }
  466. }
  467. /**
  468. * Displays the details of the specified UserRequest object
  469. * @param WebPage $oP The current web page for the output
  470. * @param UserRequest $oRequest The object to display
  471. * @return void
  472. */
  473. function DisplayRequestDetails($oP, UserRequest $oRequest)
  474. {
  475. $aList = array('ref', 'status', 'title', 'description', 'request_type','ticket_log', 'start_date', 'tto_escalation_deadline', 'ttr_escalation_deadline', 'caller_id', 'service_id', 'servicesubcategory_id', 'product', 'impact', 'urgency', 'priority', 'workgroup_id', 'agent_id', 'related_problem_id', 'related_change_id', 'close_date', 'last_update', 'assignment_date', 'closure_deadline', 'resolution_code', 'solution', 'user_satisfaction', 'user_commment', 'freeze_reason');
  476. $aDetails = array();
  477. foreach($aList as $sAttCode)
  478. {
  479. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  480. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  481. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  482. {
  483. // Don't display linked set and non-visible attributes (in this state)
  484. $sDisplayValue = GetFieldAsHtml($oRequest, $sAttCode);
  485. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription('UserRequest', $sAttCode).'">'.MetaModel::GetLabel('UserRequest', $sAttCode).'</span>', 'value' => $sDisplayValue);
  486. }
  487. }
  488. $oP->add('<div id="request_details">');
  489. $oP->details($aDetails);
  490. $oP->add('</div>');
  491. }
  492. /**
  493. * Displays a form for the user to provide feedback about a 'resolved' UserRequest and then close the request
  494. * @param WebPage $oP The current web page
  495. * @param UserRequest $oRequest The object to display
  496. * @return void
  497. */
  498. function DisplayResolvedRequestForm($oP, UserRequest $oRequest)
  499. {
  500. $oP->add("<div class=\"wizContainer\" id=\"form_close_request\">\n");
  501. $oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  502. $oP->add('<table id="close_form_table"><tr><td style="vertical-align:top;">');
  503. $oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oRequest->GetName())."</h1>\n");
  504. DisplayRequestDetails($oP, $oRequest);
  505. $oP->add('</td><td style="vertical-align:top;">');
  506. $aArgs = array('this' => $oRequest);
  507. $sClass = get_class($oRequest);
  508. $aDetails = array();
  509. $aTargetStates = MetaModel::EnumStates($sClass);
  510. $aTargetState = $aTargetStates['closed'];
  511. $aExpectedAttributes = $aTargetState['attribute_list'];
  512. $iFieldIndex = 0;
  513. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  514. {
  515. // Prompt for an attribute if
  516. // - the attribute must be changed or must be displayed to the user for confirmation
  517. // - or the field is mandatory and currently empty
  518. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  519. (($iExpectCode & OPT_ATT_MANDATORY) && ($oRequest->Get($sAttCode) == '')) )
  520. {
  521. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  522. $oAttDef = $aAttributesDef[$sAttCode];
  523. $aArgs = array('this' => $oRequest);
  524. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oRequest->Get($sAttCode), $oRequest->GetEditValue($sAttCode), 'att_'.$iFieldIndex, '', $iExpectCode, $aArgs);
  525. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => "<span id=\"field_att_$iFieldIndex\">$sHTMLValue</span>");
  526. $aFieldsMap[$sAttCode] = 'att_'.$iFieldIndex;
  527. $iFieldIndex++;
  528. }
  529. }
  530. $aStimuli = MetaModel::EnumStimuli($sClass);
  531. $oP->add("<h1>".Dict::S('Portal:EnterYourCommentsOnTicket')."</h1>");
  532. $oP->details($aDetails);
  533. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oRequest->GetKey()."\">");
  534. $oP->add("<input type=\"hidden\" name=\"step\" value=\"2\">");
  535. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  536. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"details\">");
  537. $oP->p("<input type=\"submit\" value=\"".Dict::S('Portal:Button:CloseTicket')."\">");
  538. $oP->add('</td></tr></table>');
  539. $oP->add("</form>");
  540. $oP->add("</div>\n");
  541. $oP->add_ready_script(
  542. <<<EOF
  543. // Starts the validation when the page is ready
  544. CheckFields('request_form', false);
  545. $('#request_form').submit( function() {
  546. return CheckFields('request_form', true);
  547. });
  548. EOF
  549. );
  550. }
  551. /**
  552. * Actually close the request and saves the user's feedback
  553. * @param WebPage $oP The current web page
  554. * @param UserRequest $oRequest The object to close
  555. * @return void
  556. */
  557. function DoCloseRequest($oP, UserRequest $oRequest)
  558. {
  559. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  560. if (!utils::IsTransactionValid($sTransactionId))
  561. {
  562. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  563. DisplayMainMenu($oP);
  564. return;
  565. }
  566. $sClass = get_class($oRequest);
  567. $aDetails = array();
  568. $aTargetStates = MetaModel::EnumStates($sClass);
  569. $aTargetState = $aTargetStates['closed'];
  570. $aExpectedAttributes = $aTargetState['attribute_list'];
  571. $iFieldIndex = 0;
  572. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  573. {
  574. // Prompt for an attribute if
  575. // - the attribute must be changed or must be displayed to the user for confirmation
  576. // - or the field is mandatory and currently empty
  577. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  578. (($iExpectCode & OPT_ATT_MANDATORY) && ($oRequest->Get($sAttCode) == '')) )
  579. {
  580. $value = utils::ReadPostedParam('attr_'.$sAttCode, null);
  581. if (!is_null($value))
  582. {
  583. $oRequest->Set($sAttCode, $value);
  584. }
  585. }
  586. }
  587. if ($oRequest->ApplyStimulus('ev_close'))
  588. {
  589. $oMyChange = MetaModel::NewObject("CMDBChange");
  590. $oMyChange->Set("date", time());
  591. if (UserRights::IsImpersonated())
  592. {
  593. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  594. }
  595. else
  596. {
  597. $sUserString = UserRights::GetUser();
  598. }
  599. $oMyChange->Set("userinfo", $sUserString);
  600. $iChangeId = $oMyChange->DBInsert();
  601. $oRequest->DBUpdateTracked($oMyChange);
  602. $oP->p("<h1>".Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oRequest)), $oRequest->GetName())."</h1>\n");
  603. DisplayMainMenu($oP);
  604. }
  605. else
  606. {
  607. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  608. $oP->add('Error: cannot close the request - '.$oRequest->GetName());
  609. }
  610. }
  611. /**
  612. * Find the UserRequest object of the specified ID. Make sure that it the caller is the current user
  613. * @param integer $id The ID of the request to find
  614. * @return UserRequert The found object, or null in case of failure (object does not exist, user has no rights to see it...)
  615. */
  616. function FindRequest($id)
  617. {
  618. $oRequest = null;
  619. $iContactId = UserRights::GetContactId();
  620. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  621. if (is_object($oContact))
  622. {
  623. $sOQL = "SELECT UserRequest WHERE caller_id = :contact_id AND id = :request_id";
  624. $oSearch = DBObjectSearch::FromOQL($sOQL);
  625. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId, 'request_id' => $id));
  626. if ($oSet->Count() > 0)
  627. {
  628. $oRequest = $oSet->Fetch();
  629. }
  630. }
  631. else
  632. {
  633. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  634. $oP->add("<p class=\"error\">".Dict::S('Portal:ErrorNoContactForThisUser')."</p>");
  635. }
  636. return $oRequest;
  637. }
  638. /**
  639. * Displays the details of a request
  640. * @param WebPage $oP The current web page
  641. * @return void
  642. */
  643. function RequestDetails(WebPage $oP, $id)
  644. {
  645. $oRequest = FindRequest($id);
  646. if (!is_object($oRequest))
  647. {
  648. DisplayMainMenu($oP);
  649. return;
  650. }
  651. $iDefaultStep = 0;
  652. if ($oRequest->GetState() == 'resolved')
  653. {
  654. // The current ticket is in 'resolved' state, prompt to close it
  655. $iDefaultStep = 1;
  656. }
  657. $iStep = utils::ReadParam('step', $iDefaultStep);
  658. switch($iStep)
  659. {
  660. case 0:
  661. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  662. $oP->add("<h1 id=\"title_request_details\">".$oRequest->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oRequest->GetName())."</h1>\n");
  663. DisplayRequestDetails($oP, $oRequest);
  664. break;
  665. case 1:
  666. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  667. DisplayResolvedRequestForm($oP, $oRequest);
  668. break;
  669. case 2:
  670. DoCloseRequest($oP, $oRequest);
  671. break;
  672. }
  673. }
  674. /**
  675. * Get The organization of the current user (i.e. the organization of its contact)
  676. * @param WebPage $oP The current page, for errors output
  677. * @return Organization The user's org or null in case of problem...
  678. */
  679. function GetUserOrg()
  680. {
  681. $oOrg = null;
  682. $iContactId = UserRights::GetContactId();
  683. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  684. if (is_object($oContact))
  685. {
  686. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  687. }
  688. else
  689. {
  690. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  691. }
  692. return $oOrg;
  693. }
  694. try
  695. {
  696. require_once(APPROOT.'/application/startup.inc.php');
  697. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  698. $oAppContext = new ApplicationContext();
  699. $sOperation = utils::ReadParam('operation', '');
  700. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  701. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  702. $oUserOrg = GetUserOrg();
  703. $sCode = $oUserOrg->Get('code');
  704. $sAlternateStylesheet = '';
  705. if (@file_exists("./$sCode/portal.css"))
  706. {
  707. $sAlternateStylesheet = "$sCode";
  708. }
  709. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  710. $oP->add($sAlternateStylesheet);
  711. if (is_object($oUserOrg))
  712. {
  713. switch($sOperation)
  714. {
  715. case 'create_request':
  716. CreateRequest($oP, $oUserOrg);
  717. break;
  718. case 'details':
  719. $iRequestId = utils::ReadParam('id', 0);
  720. RequestDetails($oP, $iRequestId);
  721. break;
  722. case 'welcome':
  723. default:
  724. DisplayMainMenu($oP);
  725. }
  726. }
  727. $oP->output();
  728. }
  729. catch(CoreException $e)
  730. {
  731. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  732. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  733. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  734. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  735. $oP->output();
  736. if (MetaModel::IsLogEnabledIssue())
  737. {
  738. if (MetaModel::IsValidClass('EventIssue'))
  739. {
  740. try
  741. {
  742. $oLog = new EventIssue();
  743. $oLog->Set('message', $e->getMessage());
  744. $oLog->Set('userinfo', '');
  745. $oLog->Set('issue', $e->GetIssue());
  746. $oLog->Set('impact', 'Page could not be displayed');
  747. $oLog->Set('callstack', $e->getTrace());
  748. $oLog->Set('data', $e->getContextData());
  749. $oLog->DBInsertNoReload();
  750. }
  751. catch(Exception $e)
  752. {
  753. IssueLog::Error("Failed to log issue into the DB");
  754. }
  755. }
  756. IssueLog::Error($e->getMessage());
  757. }
  758. // For debugging only
  759. //throw $e;
  760. }
  761. catch(Exception $e)
  762. {
  763. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  764. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  765. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  766. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  767. $oP->output();
  768. if (MetaModel::IsLogEnabledIssue())
  769. {
  770. if (MetaModel::IsValidClass('EventIssue'))
  771. {
  772. try
  773. {
  774. $oLog = new EventIssue();
  775. $oLog->Set('message', $e->getMessage());
  776. $oLog->Set('userinfo', '');
  777. $oLog->Set('issue', 'PHP Exception');
  778. $oLog->Set('impact', 'Page could not be displayed');
  779. $oLog->Set('callstack', $e->getTrace());
  780. $oLog->Set('data', array());
  781. $oLog->DBInsertNoReload();
  782. }
  783. catch(Exception $e)
  784. {
  785. IssueLog::Error("Failed to log issue into the DB");
  786. }
  787. }
  788. IssueLog::Error($e->getMessage());
  789. }
  790. }
  791. ?>