index.php 30 KB

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