index.php 32 KB

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