index.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * iTop User Portal main page
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. * Displays the portal main menu
  30. * @param WebPage $oP The current web page
  31. * @return void
  32. */
  33. function DisplayMainMenu(WebPage $oP)
  34. {
  35. $oP->AddMenuButton('showongoing', 'Portal:ShowOngoing', '../portal/index.php?operation=show_ongoing');
  36. $oP->AddMenuButton('newrequest', 'Portal:CreateNewRequest', '../portal/index.php?operation=create_request');
  37. $oP->AddMenuButton('showclosed', 'Portal:ShowClosed', '../portal/index.php?operation=show_closed');
  38. if (UserRights::CanChangePassword())
  39. {
  40. $oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', '../portal/index.php?loginop=change_pwd');
  41. }
  42. }
  43. /**
  44. * Displays the current tickets
  45. * @param WebPage $oP The current web page
  46. * @return void
  47. */
  48. function ShowOngoingTickets(WebPage $oP)
  49. {
  50. $oP->add("<div id=\"open_requests\">\n");
  51. $oP->add("<h1 id=\"title_open_requests\">".Dict::S('Portal:OpenRequests')."</h1>\n");
  52. ListOpenRequests($oP);
  53. $oP->add("</div>\n");
  54. $oP->add("<div id=\"#div_resolved_requests\">\n");
  55. $oP->add("<h1 id=\"#resolved_requests\">".Dict::S('Portal:ResolvedRequests')."</h1>\n");
  56. ListResolvedRequests($oP);
  57. $oP->add("</div>\n");
  58. }
  59. /**
  60. * Displays the closed tickets
  61. * @param WebPage $oP The current web page
  62. * @return void
  63. */
  64. function ShowClosedTickets(WebPage $oP)
  65. {
  66. $oP->add("<div id=\"#closed_tickets\">\n");
  67. //$oP->add("<h1 id=\"#closed_tickets\">".Dict::S('Portal:ListClosedTickets')."</h1>\n");
  68. ListClosedTickets($oP);
  69. $oP->add("</div>\n");
  70. }
  71. /**
  72. * Displays the form to select a Service Category Id (among the valid ones for the specified user Organization)
  73. * @param WebPage $oP Web page for the form output
  74. * @param Organization $oUserOrg The organization of the current user
  75. * @return void
  76. */
  77. function SelectServiceCategory($oP, $oUserOrg)
  78. {
  79. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  80. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICECATEGORY_QUERY);
  81. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  82. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  83. if ($oSet->Count() == 1)
  84. {
  85. $oService = $oSet->Fetch();
  86. $iSvcCategory = $oService->GetKey();
  87. // Only one Category, skip this step in the wizard
  88. SelectServiceSubCategory($oP, $oUserOrg, $iSvcCategory);
  89. }
  90. else
  91. {
  92. $oP->add("<div class=\"wizContainer\" id=\"form_select_service\">\n");
  93. $oP->WizardFormStart('request_wizard', 1);
  94. $oP->add("<h1 id=\"select_category\">".Dict::S('Portal:SelectService')."</h1>\n");
  95. $oP->add("<table>\n");
  96. while($oService = $oSet->Fetch())
  97. {
  98. $id = $oService->GetKey();
  99. $sChecked = "";
  100. if (isset($aParameters['service_id']) && ($id == $aParameters['service_id']))
  101. {
  102. $sChecked = "checked";
  103. }
  104. $oP->p("<tr><td style=\"vertical-align:top\"><p><input name=\"attr_service_id\" $sChecked type=\"radio\" id=\"service_$id\" value=\"$id\"></p></td><td style=\"vertical-align:top\"><p><b><label for=\"service_$id\">".$oService->GetName()."</label></b></p>");
  105. $oP->p("<p>".$oService->GetAsHTML('description')."</p></td></tr>");
  106. }
  107. $oP->add("</table>\n");
  108. $oP->DumpHiddenParams($aParameters, array('service_id'));
  109. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  110. $oP->WizardFormButtons(BUTTON_NEXT | BUTTON_CANCEL); // NO back button since it's the first step of the Wizard
  111. $oP->WizardFormEnd();
  112. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectOneService'));
  113. $oP->add("</div>\n");
  114. }
  115. }
  116. /**
  117. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  118. * and based on the page's parameter 'service_id'
  119. * @param WebPage $oP Web page for the form output
  120. * @param Organization $oUserOrg The organization of the current user
  121. * @param $iSvcId Id of the selected service in case of pass-through (when there is only one service)
  122. * @return void
  123. */
  124. function SelectServiceSubCategory($oP, $oUserOrg, $iSvcId = null)
  125. {
  126. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  127. $iWizardButtons = 0;
  128. if ($iSvcId == null)
  129. {
  130. $iSvcId = $aParameters['service_id'];
  131. }
  132. else
  133. {
  134. $aParameters['service_id'] = $iSvcId;
  135. $iWizardButtons = BUTTON_NEXT;
  136. }
  137. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  138. $iDefaultWizNext = 2;
  139. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICE_SUBCATEGORY_QUERY);
  140. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  141. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId, 'org_id' => $oUserOrg->GetKey()));
  142. if ($oSet->Count() == 1)
  143. {
  144. // Only one sub service, skip this step of the wizard
  145. $oSubService = $oSet->Fetch();
  146. $iSubSvdId = $oSubService->GetKey();
  147. RequestCreationForm($oP, $oUserOrg, $iSvcId, $iSubSvdId);
  148. }
  149. else
  150. {
  151. $oServiceCategory = MetaModel::GetObject('Service', $iSvcId, false, true /* allow all data*/);
  152. if (is_object($oServiceCategory))
  153. {
  154. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  155. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', $oServiceCategory->GetName())."</h1>\n");
  156. $oP->WizardFormStart('request_wizard', $iDefaultWizNext);
  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->add("<tr>");
  167. $oP->add("<td style=\"vertical-align:top\">");
  168. $oP->add("<p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"servicesubcategory_$id\" value=\"$id\"></p>");
  169. $oP->add("</td>");
  170. $oP->add("<td style=\"vertical-align:top\">");
  171. $oP->add("<p><b><label for=\"servicesubcategory_$id\">".$oSubService->GetName()."</label></b></p>");
  172. $oP->add("<p>".$oSubService->GetAsHTML('description')."</p>");
  173. $oP->add("</td>");
  174. $oP->add("</tr>");
  175. }
  176. $oP->add("</table>\n");
  177. $oP->DumpHiddenParams($aParameters, array('servicesubcategory_id'));
  178. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  179. $iWizardButtons |= BUTTON_NEXT | BUTTON_CANCEL;
  180. $oP->WizardFormButtons($iWizardButtons);
  181. $oP->WizardFormEnd();
  182. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectAServiceSubCategory'));
  183. $oP->add("</div>\n");
  184. }
  185. else
  186. {
  187. $oP->p("Error: Invalid Service: id = $iSvcId");
  188. }
  189. }
  190. }
  191. /**
  192. * Displays the form for the final step of the UserRequest creation
  193. * @param WebPage $oP The current web page for the form output
  194. * @param Organization $oUserOrg The organization of the current user
  195. * @param integer $iSvcId The identifier of the service (in cal of fall through, when there is only one service)
  196. * @param integer $iSubSvcId The identifier of the sub-service (in cal of fall through, when there is only one sub-service)
  197. * @return void
  198. */
  199. function RequestCreationForm($oP, $oUserOrg, $iSvcId = null, $iSubSvcId = null)
  200. {
  201. $oP->add_script(
  202. <<<EOF
  203. // Create the object once at the beginning of the page...
  204. var oWizardHelper = new WizardHelper('UserRequest', '');
  205. EOF
  206. );
  207. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  208. if ($iSvcId != null)
  209. {
  210. $aParameters['service_id'] = $iSvcId;
  211. }
  212. if ($iSubSvcId != null)
  213. {
  214. $aParameters['servicesubcategory_id'] = $iSubSvcId;
  215. }
  216. // Example: $aList = array('title', 'description', 'impact', 'emergency');
  217. $aList = explode(',', PORTAL_REQUEST_FORM_ATTRIBUTES);
  218. $sDescription = '';
  219. if (isset($aParameters['template_id']))
  220. {
  221. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id'], false);
  222. if (is_object($oTemplate))
  223. {
  224. $sDescription = htmlentities($oTemplate->Get('template'), ENT_QUOTES, 'UTF-8');
  225. }
  226. }
  227. $oServiceCategory = MetaModel::GetObject('Service', $aParameters['service_id'], false, true /* allow all data*/);
  228. $oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false, true /* allow all data*/);
  229. if (is_object($oServiceCategory) && is_object($oServiceSubCategory))
  230. {
  231. $oRequest = new UserRequest();
  232. $oRequest->Set('org_id', $oUserOrg->GetKey());
  233. $oRequest->Set('caller_id', UserRights::GetContactId());
  234. $oRequest->Set('service_id', $aParameters['service_id']);
  235. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  236. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'service_id');
  237. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceCategory->GetName());
  238. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'servicesubcategory_id');
  239. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceSubCategory->GetName());
  240. $iFlags = 0;
  241. foreach($aList as $sAttCode)
  242. {
  243. $value = '';
  244. if (isset($aParameters[$sAttCode]))
  245. {
  246. $value = $aParameters[$sAttCode];
  247. $oRequest->Set($sAttCode, $value);
  248. }
  249. }
  250. $aFieldsMap = array();
  251. foreach($aList as $sAttCode)
  252. {
  253. $value = '';
  254. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  255. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  256. if (isset($aParameters[$sAttCode]))
  257. {
  258. $value = $aParameters[$sAttCode];
  259. }
  260. $aArgs = array('this' => $oRequest);
  261. $sInputId = 'attr_'.$sAttCode;
  262. $aFieldsMap[$sAttCode] = $sInputId;
  263. $sValue = "<span id=\"field_{$sInputId}\">".$oRequest->GetFormElementForField($oP, get_class($oRequest), $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs).'</span>';
  264. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  265. }
  266. $aDetails[] = array('label' => MetaModel::GetLabel('UserRequest', PORTAL_ATTCODE_LOG), 'value' => '<textarea id="attr_moreinfo" class="resizable ui-resizable" cols="40" rows="8" name="attr_moreinfo" title="" style="margin: 0px; resize: none; position: static; display: block; height: 145px; width: 339px;">'.$sDescription.'</textarea>');
  267. $oP->add_linked_script("../js/json.js");
  268. $oP->add_linked_script("../js/forms-json-utils.js");
  269. $oP->add_linked_script("../js/wizardhelper.js");
  270. $oP->add_linked_script("../js/wizard.utils.js");
  271. $oP->add_linked_script("../js/linkswidget.js");
  272. $oP->add_linked_script("../js/extkeywidget.js");
  273. $oP->add_linked_script("../js/jquery.blockUI.js");
  274. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  275. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  276. $oP->WizardFormStart('request_form', 3);
  277. //$oP->add("<table>\n");
  278. $oP->details($aDetails);
  279. $oAttPlugin = new AttachmentPlugIn();
  280. $oAttPlugin->OnDisplayRelations($oRequest, $oP, true /* edit */);
  281. $oP->DumpHiddenParams($aParameters, $aList);
  282. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  283. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_FINISH | BUTTON_CANCEL);
  284. $oP->WizardFormEnd();
  285. $oP->add("</div>\n");
  286. $iFieldsCount = count($aFieldsMap);
  287. $sJsonFieldsMap = json_encode($aFieldsMap);
  288. $oP->add_ready_script(
  289. <<<EOF
  290. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  291. oWizardHelper.SetFieldsCount($iFieldsCount);
  292. // Starts the validation when the page is ready
  293. CheckFields('request_form', false);
  294. $('#request_form').submit( function() {
  295. return OnSubmit('request_form');
  296. });
  297. EOF
  298. );
  299. }
  300. else
  301. {
  302. // User not authorized to use this service ?
  303. //ShowOngoingTickets($oP);
  304. }
  305. }
  306. /**
  307. * Validate the parameters and create the UserRequest object (based on the page's POSTed parameters)
  308. * @param WebPage $oP The current web page for the output
  309. * @param Organization $oUserOrg The organization of the current user
  310. * @return void
  311. */
  312. function DoCreateRequest($oP, $oUserOrg)
  313. {
  314. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  315. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  316. if (!utils::IsTransactionValid($sTransactionId))
  317. {
  318. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  319. //ShowOngoingTickets($oP);
  320. return;
  321. }
  322. // Validate the parameters
  323. // 1) ServiceCategory
  324. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  325. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  326. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  327. if ($oSet->Count() != 1)
  328. {
  329. // Invalid service for the current user !
  330. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  331. }
  332. $oServiceCategory = $oSet->Fetch();
  333. // 2) Service Subcategory
  334. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  335. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  336. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  337. if ($oSet->Count() != 1)
  338. {
  339. // Invalid subcategory
  340. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  341. }
  342. $oServiceSubCategory = $oSet->Fetch();
  343. $oRequest = new UserRequest();
  344. $oRequest->Set('org_id', $oUserOrg->GetKey());
  345. $oRequest->Set('caller_id', UserRights::GetContactId());
  346. $aList = array('service_id', 'servicesubcategory_id', 'title', 'description', 'impact');
  347. $oRequest->UpdateObjectFromPostedForm();
  348. if (isset($aParameters['moreinfo']))
  349. {
  350. // There is a template, insert it into the description
  351. $oRequest->Set(PORTAL_ATTCODE_LOG, $aParameters['moreinfo']);
  352. }
  353. if ((PORTAL_ATTCODE_TYPE != '') && (PORTAL_SET_TYPE_FROM != ''))
  354. {
  355. $oRequest->Set(PORTAL_ATTCODE_TYPE, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  356. }
  357. if (MetaModel::IsValidAttCode('UserRequest', 'origin'))
  358. {
  359. $oRequest->Set('origin', 'portal');
  360. }
  361. /////$oP->DoUpdateObjectFromPostedForm($oObj);
  362. $oAttPlugin = new AttachmentPlugIn();
  363. $oAttPlugin->OnFormSubmit($oRequest);
  364. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  365. if ($bRes)
  366. {
  367. $oRequest->DBInsertNoReload();
  368. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName(get_class($oRequest)))."</h1>\n");
  369. //DisplayObject($oP, $oRequest, $oUserOrg);
  370. ShowOngoingTickets($oP);
  371. }
  372. else
  373. {
  374. RequestCreationForm($oP, $oUserOrg);
  375. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  376. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  377. }
  378. }
  379. /**
  380. * Prompts the user for creating a new request
  381. * @param WebPage $oP The current web page
  382. * @return void
  383. */
  384. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  385. {
  386. switch($oP->GetWizardStep())
  387. {
  388. case 0:
  389. default:
  390. SelectServiceCategory($oP, $oUserOrg);
  391. break;
  392. case 1:
  393. SelectServiceSubCategory($oP, $oUserOrg);
  394. break;
  395. case 2:
  396. RequestCreationForm($oP, $oUserOrg);
  397. break;
  398. case 3:
  399. DoCreateRequest($oP, $oUserOrg);
  400. break;
  401. }
  402. }
  403. /**
  404. * Lists all the currently opened User Requests for the current user
  405. * @param WebPage $oP The current web page
  406. * @return void
  407. */
  408. function ListOpenRequests(WebPage $oP)
  409. {
  410. $oUserOrg = GetUserOrg();
  411. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status NOT IN ("closed", "resolved")';
  412. $oSearch = DBObjectSearch::FromOQL($sOQL);
  413. $iUser = UserRights::GetContactId();
  414. if ($iUser > 0 && !IsPowerUser())
  415. {
  416. $oSearch->AddCondition('caller_id', $iUser);
  417. }
  418. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  419. $aZList = explode(',', PORTAL_TICKETS_LIST_ZLIST);
  420. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  421. }
  422. /**
  423. * Lists all the currently resolved (not yet closed) User Requests for the current user
  424. * @param WebPage $oP The current web page
  425. * @return void
  426. */
  427. function ListResolvedRequests(WebPage $oP)
  428. {
  429. $oUserOrg = GetUserOrg();
  430. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status = "resolved"';
  431. $oSearch = DBObjectSearch::FromOQL($sOQL);
  432. $iUser = UserRights::GetContactId();
  433. if ($iUser > 0 && !IsPowerUser())
  434. {
  435. $oSearch->AddCondition('caller_id', $iUser);
  436. }
  437. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  438. $aZList = explode(',', PORTAL_TICKETS_LIST_ZLIST);
  439. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  440. }
  441. /**
  442. * Lists all the currently closed tickets
  443. * @param WebPage $oP The current web page
  444. * @return void
  445. */
  446. function ListClosedTickets(WebPage $oP)
  447. {
  448. $aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
  449. $aZList = explode(',', PORTAL_TICKETS_CLOSED_ZLIST);
  450. $oP->DisplaySearchForm('UserRequest', $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  451. $oUserOrg = GetUserOrg();
  452. // UserRequest
  453. $oSearch = $oP->PostedParamsToFilter('UserRequest', $aAttSpecs, 'search_');
  454. if(is_null($oSearch))
  455. {
  456. $oSearch = new DBObjectSearch('UserRequest');
  457. }
  458. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  459. $oSearch->AddCondition('status', 'closed');
  460. $iUser = UserRights::GetContactId();
  461. if ($iUser > 0 && !IsPowerUser())
  462. {
  463. $oSearch->AddCondition('caller_id', $iUser);
  464. }
  465. $oSet1 = new CMDBObjectSet($oSearch);
  466. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  467. $oP->DisplaySet($oSet1, $aZList, Dict::S('Portal:NoClosedRequest'));
  468. }
  469. /**
  470. * Display an object - to be customized
  471. * @param WebPage $oP The current web page
  472. * @param Object $oObj Any kind of object
  473. * @param Object $oUserOrg The organization of the logged in user
  474. * @return void
  475. */
  476. function DisplayObject($oP, $oObj, $oUserOrg)
  477. {
  478. switch(get_class($oObj))
  479. {
  480. case 'UserRequest':
  481. ShowDetailsRequest($oP, $oObj);
  482. break;
  483. default:
  484. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  485. }
  486. }
  487. /**
  488. * Displays the details of a request
  489. * @param WebPage $oP The current web page
  490. * @param Object $oObj The target object
  491. * @return void
  492. */
  493. function ShowDetailsRequest(WebPage $oP, $oObj)
  494. {
  495. $sClass = get_class($oObj);
  496. $bIsEscalateButton = false;
  497. $bIsReopenButton = false;
  498. $bIsCloseButton = false;
  499. $bEditAttachments = false;
  500. $aEditAtt = array(); // List of attributes editable in the main form
  501. if (!MetaModel::DBIsReadOnly())
  502. {
  503. switch($oObj->GetState())
  504. {
  505. case 'new':
  506. case 'assigned':
  507. case 'frozen':
  508. $aEditAtt = array(
  509. PORTAL_ATTCODE_LOG => '????'
  510. );
  511. $bEditAttachments = true;
  512. // disabled - $bIsEscalateButton = true;
  513. break;
  514. case 'escalated_tto':
  515. case 'escalated_ttr':
  516. $aEditAtt = array(
  517. PORTAL_ATTCODE_LOG => '????'
  518. );
  519. $bEditAttachments = true;
  520. break;
  521. case 'resolved':
  522. $aEditAtt = array();
  523. if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
  524. {
  525. $bIsReopenButton = true;
  526. MakeStimulusForm($oP, $oObj, 'ev_reopen', array(PORTAL_ATTCODE_LOG));
  527. }
  528. $bIsCloseButton = true;
  529. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', PORTAL_ATTCODE_COMMENT));
  530. break;
  531. case 'closed':
  532. case 'closure_requested':
  533. default:
  534. break;
  535. }
  536. }
  537. // REFACTORISER LA MISE EN FORME
  538. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  539. switch($sClass)
  540. {
  541. case 'UserRequest':
  542. $aAttList = json_decode(PORTAL_TICKET_DETAILS_ZLIST, true);
  543. switch($oObj->GetState())
  544. {
  545. case 'closed':
  546. $aAttList['centered'][] = 'user_satisfaction';
  547. $aAttList['centered'][] = PORTAL_ATTCODE_COMMENT;
  548. }
  549. break;
  550. default:
  551. array('col:left'=> array('ref','service_id','servicesubcategory_id','title','description'),'col:right'=> array('status','start_date'));
  552. break;
  553. }
  554. // Remove the edited attribute from the shown attributes
  555. //
  556. foreach($aEditAtt as $sAttCode => $foo)
  557. {
  558. foreach($aAttList as $col => $aColumn)
  559. {
  560. if (in_array($sAttCode, $aColumn))
  561. {
  562. if(($index = array_search($sAttCode, $aColumn)) !== false)
  563. {
  564. unset($aAttList[$col][$index]);
  565. }
  566. }
  567. }
  568. }
  569. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  570. $oP->WizardFormStart('request_form', null);
  571. $oP->add('<div id="request_details">');
  572. $oP->add('<table id="request_details_table">');
  573. $oP->add('<tr>');
  574. $oP->add('<td style="vertical-align:top;">');
  575. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  576. $oP->add('</td>');
  577. $oP->add('<td style="vertical-align:top;">');
  578. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  579. $oP->add('</td>');
  580. $oP->add('</tr>');
  581. if (array_key_exists('centered', $aAttList))
  582. {
  583. $oP->add('<tr>');
  584. $oP->add('<td style="vertical-align:top;" colspan="2">');
  585. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  586. $oP->add('</td>');
  587. $oP->add('</tr>');
  588. }
  589. // REFACTORISER
  590. $oP->add('<tr>');
  591. $oP->add('<td colspan="2" style="vertical-align:top;">');
  592. $oAttPlugin = new AttachmentPlugIn();
  593. if ($bEditAttachments)
  594. {
  595. $oAttPlugin->EnableDelete(false);
  596. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  597. }
  598. else
  599. {
  600. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  601. }
  602. $oP->add('</td>');
  603. $oP->add('</tr>');
  604. $oP->add('<tr>');
  605. $oP->add('<td colspan="2" style="vertical-align:top;">');
  606. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  607. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  608. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  609. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  610. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  611. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  612. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  613. $oP->add_script(
  614. <<<EOF
  615. function SetStimulusToApply(sStimulusCode)
  616. {
  617. $('#stimulus_to_apply').val(sStimulusCode);
  618. }
  619. EOF
  620. );
  621. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  622. foreach($aEditAtt as $sAttCode => $foo)
  623. {
  624. $sValue = $oObj->Get($sAttCode);
  625. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  626. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  627. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  628. $sInputId = 'input_'.$sAttCode;
  629. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  630. $aEditFields[$sAttCode] = array(
  631. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  632. 'value' => $sHTMLValue
  633. );
  634. }
  635. foreach($aEditFields as $sAttCode => $aFieldSpec)
  636. {
  637. if ($sAttCode == PORTAL_ATTCODE_LOG)
  638. {
  639. // Skip, the public log will be displayed below the buttons
  640. continue;
  641. }
  642. $oP->add("<div class=\"edit_item\">");
  643. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  644. $oP->add($aFieldSpec['value']);
  645. $oP->add('</div>');
  646. }
  647. if($bIsReopenButton)
  648. {
  649. $sStimulusCode = 'ev_reopen';
  650. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  651. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  652. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  653. }
  654. if($bIsCloseButton)
  655. {
  656. $sStimulusCode = 'ev_close';
  657. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  658. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  659. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  660. }
  661. elseif (count($aEditAtt) > 0)
  662. {
  663. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  664. }
  665. if ($bIsEscalateButton)
  666. {
  667. $sStimulusCode = 'ev_timeout';
  668. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  669. }
  670. $oP->add('</td>');
  671. $oP->add('</tr>');
  672. $oP->add('<tr>');
  673. $oP->add('<td colspan="2" style="vertical-align:top;">');
  674. if (isset($aEditFields[PORTAL_ATTCODE_LOG]))
  675. {
  676. $oP->add("<div class=\"edit_item\">");
  677. $oP->add('<h1>'.$aEditFields[PORTAL_ATTCODE_LOG]['label'].'</h1>');
  678. $oP->add($aEditFields[PORTAL_ATTCODE_LOG]['value']);
  679. $oP->add('</div>');
  680. }
  681. else
  682. {
  683. $oP->add('<h1>'.MetaModel::GetLabel($sClass, PORTAL_ATTCODE_LOG).'</h1>');
  684. $oP->add($oObj->GetAsHTML(PORTAL_ATTCODE_LOG));
  685. }
  686. $oP->add('</td>');
  687. $oP->add('</tr>');
  688. $oP->add('</table>');
  689. $oP->add('</div>');
  690. $oP->WizardFormEnd();
  691. $oP->add('</div>');
  692. }
  693. /**
  694. * Create form to apply a stimulus
  695. * @param WebPage $oP The current web page
  696. * @param Object $oObj The target object
  697. * @param String $sStimulusCode Stimulus that will be applied
  698. * @param Array $aEditAtt List of attributes to edit
  699. * @return void
  700. */
  701. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  702. {
  703. static $bHasStimulusForm = false;
  704. $sDialogId = $sStimulusCode."_dialog";
  705. $sFormId = $sStimulusCode."_form";
  706. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  707. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  708. $sClass = get_class($oObj);
  709. $oP->add('<form id="'.$sFormId.'" method="post">');
  710. $sTransactionId = utils::GetNewTransactionId();
  711. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  712. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  713. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  714. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  715. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  716. foreach($aEditAtt as $sAttCode)
  717. {
  718. $sValue = $oObj->Get($sAttCode);
  719. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  720. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  721. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  722. $sInputId = 'input_'.$sAttCode;
  723. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  724. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  725. $oP->add($sHTMLValue);
  726. }
  727. $oP->add('</form>');
  728. $oP->add('</div>');
  729. if (!$bHasStimulusForm)
  730. {
  731. $bHasStimulusForm = true;
  732. $oP->add_script(
  733. <<<EOF
  734. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  735. {
  736. $('#'+sStimulusCode+'_dialog').dialog({
  737. height: 'auto',
  738. width: 'auto',
  739. modal: true,
  740. title: sTitle,
  741. buttons: [
  742. { text: sOkButtonLabel, click: function() {
  743. $(this).find('#'+sStimulusCode+'_form').submit();
  744. } },
  745. { text: "$sCancelButtonLabel", click: function() {
  746. $(this).dialog( "close" );
  747. } },
  748. ],
  749. });
  750. }
  751. EOF
  752. );
  753. }
  754. }
  755. /**
  756. * Get The organization of the current user (i.e. the organization of its contact)
  757. * @param WebPage $oP The current page, for errors output
  758. * @return Organization The user's org or null in case of problem...
  759. */
  760. function GetUserOrg()
  761. {
  762. $oOrg = null;
  763. $iContactId = UserRights::GetContactId();
  764. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  765. if (is_object($oContact))
  766. {
  767. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  768. }
  769. else
  770. {
  771. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  772. }
  773. return $oOrg;
  774. }
  775. /**
  776. * Determine if the current user can be considered as being a portal power user
  777. */
  778. function IsPowerUSer()
  779. {
  780. $iUserID = UserRights::GetUserId();
  781. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  782. $oProfileSet = new DBObjectSet(
  783. DBObjectSearch::FromOQL($sOQLprofile),
  784. array(),
  785. array(
  786. 'user' => $iUserID,
  787. 'profile' => PORTAL_POWER_USER_PROFILE,
  788. )
  789. );
  790. $bRes = ($oProfileSet->count() > 0);
  791. return $bRes;
  792. }
  793. ///////////////////////////////////////////////////////////////////////////////
  794. //
  795. // Main program
  796. //
  797. ///////////////////////////////////////////////////////////////////////////////
  798. try
  799. {
  800. require_once(APPROOT.'/application/startup.inc.php');
  801. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  802. $oAppContext = new ApplicationContext();
  803. $sOperation = utils::ReadParam('operation', '');
  804. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  805. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  806. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  807. if (!class_exists('UserRequest'))
  808. {
  809. $oP = new WebPage(Dict::S('Portal:Title'));
  810. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  811. }
  812. else
  813. {
  814. $oUserOrg = GetUserOrg();
  815. $sCode = $oUserOrg->Get('code');
  816. $sAlternateStylesheet = '';
  817. if (@file_exists("./$sCode/portal.css"))
  818. {
  819. $sAlternateStylesheet = "$sCode";
  820. }
  821. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  822. $oP->EnableDisconnectButton(utils::CanLogOff());
  823. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  824. if (is_object($oUserOrg))
  825. {
  826. switch($sOperation)
  827. {
  828. case 'show_closed':
  829. $oP->set_title(Dict::S('Portal:ShowClosed'));
  830. DisplayMainMenu($oP);
  831. ShowClosedTickets($oP);
  832. break;
  833. case 'create_request':
  834. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  835. DisplayMainMenu($oP);
  836. if (!MetaModel::DBIsReadOnly())
  837. {
  838. CreateRequest($oP, $oUserOrg);
  839. }
  840. break;
  841. case 'details':
  842. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  843. DisplayMainMenu($oP);
  844. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  845. DisplayObject($oP, $oObj, $oUserOrg);
  846. break;
  847. case 'update_request':
  848. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  849. DisplayMainMenu($oP);
  850. if (!MetaModel::DBIsReadOnly())
  851. {
  852. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  853. switch(get_class($oObj))
  854. {
  855. case 'UserRequest':
  856. $aAttList = array(PORTAL_ATTCODE_LOG, 'user_satisfaction', PORTAL_ATTCODE_COMMENT);
  857. break;
  858. default:
  859. throw new Exception("Implementation issue: unexpected class '".get_class($oObj)."'");
  860. }
  861. try
  862. {
  863. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  864. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  865. }
  866. catch(TransactionException $e)
  867. {
  868. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  869. }
  870. DisplayObject($oP, $oObj, $oUserOrg);
  871. }
  872. break;
  873. case 'show_ongoing':
  874. default:
  875. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  876. DisplayMainMenu($oP);
  877. ShowOngoingTickets($oP);
  878. }
  879. }
  880. }
  881. $oP->output();
  882. }
  883. catch(CoreException $e)
  884. {
  885. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  886. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  887. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  888. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  889. //$oP->p($e->getTraceAsString());
  890. $oP->output();
  891. if (MetaModel::IsLogEnabledIssue())
  892. {
  893. if (MetaModel::IsValidClass('EventIssue'))
  894. {
  895. try
  896. {
  897. $oLog = new EventIssue();
  898. $oLog->Set('message', $e->getMessage());
  899. $oLog->Set('userinfo', '');
  900. $oLog->Set('issue', $e->GetIssue());
  901. $oLog->Set('impact', 'Page could not be displayed');
  902. $oLog->Set('callstack', $e->getTrace());
  903. $oLog->Set('data', $e->getContextData());
  904. $oLog->DBInsertNoReload();
  905. }
  906. catch(Exception $e)
  907. {
  908. IssueLog::Error("Failed to log issue into the DB");
  909. }
  910. }
  911. IssueLog::Error($e->getMessage());
  912. }
  913. // For debugging only
  914. //throw $e;
  915. }
  916. catch(Exception $e)
  917. {
  918. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  919. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  920. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  921. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  922. //$oP->p($e->getTraceAsString());
  923. $oP->output();
  924. if (MetaModel::IsLogEnabledIssue())
  925. {
  926. if (MetaModel::IsValidClass('EventIssue'))
  927. {
  928. try
  929. {
  930. $oLog = new EventIssue();
  931. $oLog->Set('message', $e->getMessage());
  932. $oLog->Set('userinfo', '');
  933. $oLog->Set('issue', 'PHP Exception');
  934. $oLog->Set('impact', 'Page could not be displayed');
  935. $oLog->Set('callstack', $e->getTrace());
  936. $oLog->Set('data', array());
  937. $oLog->DBInsertNoReload();
  938. }
  939. catch(Exception $e)
  940. {
  941. IssueLog::Error("Failed to log issue into the DB");
  942. }
  943. }
  944. IssueLog::Error($e->getMessage());
  945. }
  946. }
  947. ?>