index.php 33 KB

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