index.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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->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>");
  96. $oP->p("<p>".$oService->GetAsHTML('description')."</p></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("<p>".$oSubService->GetAsHTML('description')."</p>");
  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("<p>\n");
  426. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  427. $oP->DisplaySet($oSet1, $aZList, Dict::S('Portal:NoClosedRequest'));
  428. $oP->add("</p>\n");
  429. }
  430. /**
  431. * Display an object - to be customized
  432. * @param WebPage $oP The current web page
  433. * @param Object $oObj Any kind of object
  434. * @param Object $oUserOrg The organization of the logged in user
  435. * @return void
  436. */
  437. function DisplayObject($oP, $oObj, $oUserOrg)
  438. {
  439. switch(get_class($oObj))
  440. {
  441. case 'UserRequest':
  442. ShowDetailsRequest($oP, $oObj);
  443. break;
  444. default:
  445. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  446. }
  447. }
  448. /**
  449. * Displays the details of a request
  450. * @param WebPage $oP The current web page
  451. * @param Object $oObj The target object
  452. * @return void
  453. */
  454. function ShowDetailsRequest(WebPage $oP, $oObj)
  455. {
  456. $sClass = get_class($oObj);
  457. $bIsEscalateButton = false;
  458. $bIsReopenButton = false;
  459. $bIsCloseButton = false;
  460. $bEditAttachments = false;
  461. $aEditAtt = array(); // List of attributes editable in the main form
  462. if (!MetaModel::DBIsReadOnly())
  463. {
  464. switch($oObj->GetState())
  465. {
  466. case 'new':
  467. case 'assigned':
  468. case 'frozen':
  469. $aEditAtt = array(
  470. PORTAL_ATTCODE_LOG => '????'
  471. );
  472. $bEditAttachments = true;
  473. // disabled - $bIsEscalateButton = true;
  474. break;
  475. case 'escalated_tto':
  476. case 'escalated_ttr':
  477. $aEditAtt = array(
  478. PORTAL_ATTCODE_LOG => '????'
  479. );
  480. $bEditAttachments = true;
  481. break;
  482. case 'resolved':
  483. $aEditAtt = array();
  484. if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
  485. {
  486. $bIsReopenButton = true;
  487. MakeStimulusForm($oP, $oObj, 'ev_reopen', array(PORTAL_ATTCODE_LOG));
  488. }
  489. $bIsCloseButton = true;
  490. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', PORTAL_ATTCODE_COMMENT));
  491. break;
  492. case 'closed':
  493. case 'closure_requested':
  494. default:
  495. break;
  496. }
  497. }
  498. // REFACTORISER LA MISE EN FORME
  499. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  500. switch($sClass)
  501. {
  502. case 'UserRequest':
  503. $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'));
  504. switch($oObj->GetState())
  505. {
  506. case 'closed':
  507. $aAttList['centered'][] = 'user_satisfaction';
  508. $aAttList['centered'][] = PORTAL_ATTCODE_COMMENT;
  509. }
  510. break;
  511. default:
  512. array('col:left'=> array('ref','service_id','servicesubcategory_id','title','description'),'col:right'=> array('status','start_date'));
  513. break;
  514. }
  515. // Remove the edited attribute from the shown attributes
  516. //
  517. foreach($aEditAtt as $sAttCode => $foo)
  518. {
  519. foreach($aAttList as $col => $aColumn)
  520. {
  521. if (in_array($sAttCode, $aColumn))
  522. {
  523. if(($index = array_search($sAttCode, $aColumn)) !== false)
  524. {
  525. unset($aAttList[$col][$index]);
  526. }
  527. }
  528. }
  529. }
  530. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  531. $oP->WizardFormStart('request_form', null);
  532. $oP->add('<div id="request_details">');
  533. $oP->add('<table id="request_details_table">');
  534. $oP->add('<tr>');
  535. $oP->add('<td style="vertical-align:top;">');
  536. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  537. $oP->add('</td>');
  538. $oP->add('<td style="vertical-align:top;">');
  539. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  540. $oP->add('</td>');
  541. $oP->add('</tr>');
  542. if (array_key_exists('centered', $aAttList))
  543. {
  544. $oP->add('<tr>');
  545. $oP->add('<td style="vertical-align:top;" colspan="2">');
  546. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  547. $oP->add('</td>');
  548. $oP->add('</tr>');
  549. }
  550. // REFACTORISER
  551. $oP->add('<tr>');
  552. $oP->add('<td colspan="2" style="vertical-align:top;">');
  553. $oAttPlugin = new AttachmentPlugIn();
  554. if ($bEditAttachments)
  555. {
  556. $oAttPlugin->EnableDelete(false);
  557. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  558. }
  559. else
  560. {
  561. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  562. }
  563. $oP->add('</td>');
  564. $oP->add('</tr>');
  565. $oP->add('<tr>');
  566. $oP->add('<td colspan="2" style="vertical-align:top;">');
  567. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  568. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  569. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  570. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  571. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  572. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  573. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  574. $oP->add_script(
  575. <<<EOF
  576. function SetStimulusToApply(sStimulusCode)
  577. {
  578. $('#stimulus_to_apply').val(sStimulusCode);
  579. }
  580. EOF
  581. );
  582. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  583. foreach($aEditAtt as $sAttCode => $foo)
  584. {
  585. $sValue = $oObj->Get($sAttCode);
  586. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  587. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  588. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  589. $sInputId = 'input_'.$sAttCode;
  590. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  591. $aEditFields[$sAttCode] = array(
  592. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  593. 'value' => $sHTMLValue
  594. );
  595. }
  596. foreach($aEditFields as $sAttCode => $aFieldSpec)
  597. {
  598. if ($sAttCode == PORTAL_ATTCODE_LOG)
  599. {
  600. // Skip, the public log will be displayed below the buttons
  601. continue;
  602. }
  603. $oP->add("<div class=\"edit_item\">");
  604. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  605. $oP->add($aFieldSpec['value']);
  606. $oP->add('</div>');
  607. }
  608. if($bIsReopenButton)
  609. {
  610. $sStimulusCode = 'ev_reopen';
  611. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  612. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  613. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  614. }
  615. if($bIsCloseButton)
  616. {
  617. $sStimulusCode = 'ev_close';
  618. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  619. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  620. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  621. }
  622. elseif (count($aEditAtt) > 0)
  623. {
  624. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  625. }
  626. if ($bIsEscalateButton)
  627. {
  628. $sStimulusCode = 'ev_timeout';
  629. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  630. }
  631. $oP->add('</td>');
  632. $oP->add('</tr>');
  633. $oP->add('<tr>');
  634. $oP->add('<td colspan="2" style="vertical-align:top;">');
  635. if (isset($aEditFields[PORTAL_ATTCODE_LOG]))
  636. {
  637. $oP->add("<div class=\"edit_item\">");
  638. $oP->add('<h1>'.$aEditFields[PORTAL_ATTCODE_LOG]['label'].'</h1>');
  639. $oP->add($aEditFields[PORTAL_ATTCODE_LOG]['value']);
  640. $oP->add('</div>');
  641. }
  642. else
  643. {
  644. $oP->add('<h1>'.MetaModel::GetLabel($sClass, PORTAL_ATTCODE_LOG).'</h1>');
  645. $oP->add($oObj->GetAsHTML(PORTAL_ATTCODE_LOG));
  646. }
  647. $oP->add('</td>');
  648. $oP->add('</tr>');
  649. $oP->add('</table>');
  650. $oP->add('</div>');
  651. $oP->WizardFormEnd();
  652. $oP->add('</div>');
  653. }
  654. /**
  655. * Create form to apply a stimulus
  656. * @param WebPage $oP The current web page
  657. * @param Object $oObj The target object
  658. * @param String $sStimulusCode Stimulus that will be applied
  659. * @param Array $aEditAtt List of attributes to edit
  660. * @return void
  661. */
  662. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  663. {
  664. static $bHasStimulusForm = false;
  665. $sDialogId = $sStimulusCode."_dialog";
  666. $sFormId = $sStimulusCode."_form";
  667. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  668. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  669. $sClass = get_class($oObj);
  670. $oP->add('<form id="'.$sFormId.'" method="post">');
  671. $sTransactionId = utils::GetNewTransactionId();
  672. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  673. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  674. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  675. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  676. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  677. foreach($aEditAtt as $sAttCode)
  678. {
  679. $sValue = $oObj->Get($sAttCode);
  680. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  681. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  682. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  683. $sInputId = 'input_'.$sAttCode;
  684. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  685. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  686. $oP->add($sHTMLValue);
  687. }
  688. $oP->add('</form>');
  689. $oP->add('</div>');
  690. if (!$bHasStimulusForm)
  691. {
  692. $bHasStimulusForm = true;
  693. $oP->add_script(
  694. <<<EOF
  695. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  696. {
  697. $('#'+sStimulusCode+'_dialog').dialog({
  698. height: 'auto',
  699. width: 'auto',
  700. modal: true,
  701. title: sTitle,
  702. buttons: [
  703. { text: sOkButtonLabel, click: function() {
  704. $(this).find('#'+sStimulusCode+'_form').submit();
  705. } },
  706. { text: "$sCancelButtonLabel", click: function() {
  707. $(this).dialog( "close" );
  708. } },
  709. ],
  710. });
  711. }
  712. EOF
  713. );
  714. }
  715. }
  716. /**
  717. * Get The organization of the current user (i.e. the organization of its contact)
  718. * @param WebPage $oP The current page, for errors output
  719. * @return Organization The user's org or null in case of problem...
  720. */
  721. function GetUserOrg()
  722. {
  723. $oOrg = null;
  724. $iContactId = UserRights::GetContactId();
  725. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  726. if (is_object($oContact))
  727. {
  728. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  729. }
  730. else
  731. {
  732. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  733. }
  734. return $oOrg;
  735. }
  736. /**
  737. * Determine if the current user can be considered as being a portal power user
  738. */
  739. function IsPowerUSer()
  740. {
  741. $iUserID = UserRights::GetUserId();
  742. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  743. $oProfileSet = new DBObjectSet(
  744. DBObjectSearch::FromOQL($sOQLprofile),
  745. array(),
  746. array(
  747. 'user' => $iUserID,
  748. 'profile' => PORTAL_POWER_USER_PROFILE,
  749. )
  750. );
  751. $bRes = ($oProfileSet->count() > 0);
  752. return $bRes;
  753. }
  754. ///////////////////////////////////////////////////////////////////////////////
  755. //
  756. // Main program
  757. //
  758. ///////////////////////////////////////////////////////////////////////////////
  759. try
  760. {
  761. require_once(APPROOT.'/application/startup.inc.php');
  762. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  763. $oAppContext = new ApplicationContext();
  764. $sOperation = utils::ReadParam('operation', '');
  765. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  766. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  767. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  768. if (!class_exists('UserRequest'))
  769. {
  770. $oP = new WebPage(Dict::S('Portal:Title'));
  771. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  772. }
  773. else
  774. {
  775. $oUserOrg = GetUserOrg();
  776. $sCode = $oUserOrg->Get('code');
  777. $sAlternateStylesheet = '';
  778. if (@file_exists("./$sCode/portal.css"))
  779. {
  780. $sAlternateStylesheet = "$sCode";
  781. }
  782. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  783. $oP->EnableDisconnectButton(utils::CanLogOff());
  784. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  785. if (is_object($oUserOrg))
  786. {
  787. switch($sOperation)
  788. {
  789. case 'show_closed':
  790. DisplayMainMenu($oP);
  791. ShowClosedTickets($oP);
  792. break;
  793. case 'create_request':
  794. DisplayMainMenu($oP);
  795. if (!MetaModel::DBIsReadOnly())
  796. {
  797. CreateRequest($oP, $oUserOrg);
  798. }
  799. break;
  800. case 'details':
  801. DisplayMainMenu($oP);
  802. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  803. DisplayObject($oP, $oObj, $oUserOrg);
  804. break;
  805. case 'update_request':
  806. DisplayMainMenu($oP);
  807. if (!MetaModel::DBIsReadOnly())
  808. {
  809. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  810. switch(get_class($oObj))
  811. {
  812. case 'UserRequest':
  813. $aAttList = array(PORTAL_ATTCODE_LOG, 'user_satisfaction', PORTAL_ATTCODE_COMMENT);
  814. break;
  815. default:
  816. throw new Exception("Implementation issue: unexpected class '".get_class($oObj)."'");
  817. }
  818. try
  819. {
  820. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  821. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  822. }
  823. catch(TransactionException $e)
  824. {
  825. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  826. }
  827. DisplayObject($oP, $oObj, $oUserOrg);
  828. }
  829. break;
  830. case 'show_ongoing':
  831. default:
  832. DisplayMainMenu($oP);
  833. ShowOngoingTickets($oP);
  834. }
  835. }
  836. }
  837. $oP->output();
  838. }
  839. catch(CoreException $e)
  840. {
  841. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  842. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  843. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  844. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  845. //$oP->p($e->getTraceAsString());
  846. $oP->output();
  847. if (MetaModel::IsLogEnabledIssue())
  848. {
  849. if (MetaModel::IsValidClass('EventIssue'))
  850. {
  851. try
  852. {
  853. $oLog = new EventIssue();
  854. $oLog->Set('message', $e->getMessage());
  855. $oLog->Set('userinfo', '');
  856. $oLog->Set('issue', $e->GetIssue());
  857. $oLog->Set('impact', 'Page could not be displayed');
  858. $oLog->Set('callstack', $e->getTrace());
  859. $oLog->Set('data', $e->getContextData());
  860. $oLog->DBInsertNoReload();
  861. }
  862. catch(Exception $e)
  863. {
  864. IssueLog::Error("Failed to log issue into the DB");
  865. }
  866. }
  867. IssueLog::Error($e->getMessage());
  868. }
  869. // For debugging only
  870. //throw $e;
  871. }
  872. catch(Exception $e)
  873. {
  874. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  875. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  876. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  877. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  878. //$oP->p($e->getTraceAsString());
  879. $oP->output();
  880. if (MetaModel::IsLogEnabledIssue())
  881. {
  882. if (MetaModel::IsValidClass('EventIssue'))
  883. {
  884. try
  885. {
  886. $oLog = new EventIssue();
  887. $oLog->Set('message', $e->getMessage());
  888. $oLog->Set('userinfo', '');
  889. $oLog->Set('issue', 'PHP Exception');
  890. $oLog->Set('impact', 'Page could not be displayed');
  891. $oLog->Set('callstack', $e->getTrace());
  892. $oLog->Set('data', array());
  893. $oLog->DBInsertNoReload();
  894. }
  895. catch(Exception $e)
  896. {
  897. IssueLog::Error("Failed to log issue into the DB");
  898. }
  899. }
  900. IssueLog::Error($e->getMessage());
  901. }
  902. }
  903. ?>