index.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. <?php
  2. // Copyright (C) 2010-2014 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-2013 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. * Helper to determine the supported types of tickets
  30. */
  31. function GetTicketClasses()
  32. {
  33. $aClasses = array();
  34. foreach (explode(',', MetaModel::GetConfig()->Get('portal_tickets')) as $sRawClass)
  35. {
  36. $sRawClass = trim($sRawClass);
  37. if (!MetaModel::IsValidClass($sRawClass))
  38. {
  39. throw new Exception("Class '$sRawClass' is not a valid class, please review your configuration (portal_tickets)");
  40. }
  41. if (!MetaModel::IsParentClass('Ticket', $sRawClass))
  42. {
  43. throw new Exception("Class '$sRawClass' does not inherit from Ticket, please review your configuration (portal_tickets)");
  44. }
  45. $aClasses[] = $sRawClass;
  46. }
  47. return $aClasses;
  48. }
  49. /**
  50. * Helper to protect the portal against malicious usages
  51. * Throws an exception if the current user is not allowed to view the object details
  52. */
  53. function ValidateObject($oObject)
  54. {
  55. if (IsPowerUser())
  56. {
  57. $sValidationDefine = 'PORTAL_'.strtoupper(get_class($oObject)).'_DISPLAY_POWERUSER_QUERY';
  58. }
  59. else
  60. {
  61. $sValidationDefine = 'PORTAL_'.strtoupper(get_class($oObject)).'_DISPLAY_QUERY';
  62. }
  63. if (defined($sValidationDefine))
  64. {
  65. $sValidationOql = constant($sValidationDefine);
  66. $oSearch = DBObjectSearch::FromOQL($sValidationOql);
  67. $oSearch->AddCondition('id', $oObject->GetKey());
  68. if ($iUser = UserRights::GetContactId())
  69. {
  70. $oContact = MetaModel::GetObject('Contact', $iUser);
  71. $aArgs = $oContact->ToArgs('contact');
  72. }
  73. else
  74. {
  75. $aArgs = array();
  76. }
  77. $oSet = new DBObjectSet($oSearch, array(), $aArgs);
  78. if ($oSet->Count() == 0)
  79. {
  80. throw new SecurityException('You are not allowed to access the object '.get_class($oObject).'::'.$oObject->GetKey());
  81. }
  82. }
  83. }
  84. /**
  85. * Helper to get the relevant constant
  86. */
  87. function GetConstant($sClass, $sName)
  88. {
  89. $sConstName = 'PORTAL_'.strtoupper($sClass).'_'.$sName;
  90. if (defined($sConstName))
  91. {
  92. return constant($sConstName);
  93. }
  94. else
  95. {
  96. throw new Exception("Missing portal constant '$sConstName'");
  97. }
  98. }
  99. /**
  100. * Helper to determine the ticket class given the service subcategory
  101. */
  102. function ComputeClass($iSubSvcId)
  103. {
  104. $aClasses = GetTicketClasses();
  105. if ((PORTAL_SET_TYPE_FROM == '') || (PORTAL_TYPE_TO_CLASS == ''))
  106. {
  107. // return the first enabled class
  108. $sClass = reset($aClasses);
  109. }
  110. else
  111. {
  112. $oServiceSubcat = MetaModel::GetObject('ServiceSubcategory', $iSubSvcId, true, true /* allow all data*/);
  113. $sTicketType = $oServiceSubcat->Get(PORTAL_SET_TYPE_FROM);
  114. $aMapping = json_decode(PORTAL_TYPE_TO_CLASS, true);
  115. if (!array_key_exists($sTicketType, $aMapping))
  116. {
  117. throw new Exception("Ticket type '$sTicketType' not found in the mapping (".implode(', ', array_keys($aMapping))."). Please contact your administrator.");
  118. }
  119. $sClass = $aMapping[$sTicketType];
  120. if (!in_array($sClass, $aClasses))
  121. {
  122. throw new Exception("Service subcategory #$iSubSvcId has a ticket type ($sClass) that is not known by the portal, please contact your administrator.");
  123. }
  124. }
  125. return $sClass;
  126. }
  127. /**
  128. * Helper to limit the service categories depending on the current settings
  129. */
  130. function RestrictSubcategories(&$oSearch)
  131. {
  132. $aMapping = (PORTAL_TYPE_TO_CLASS == '') ? array() : json_decode(PORTAL_TYPE_TO_CLASS, true);
  133. foreach($aMapping as $sTicketType => $sClass)
  134. {
  135. if (!in_array($sClass, GetTicketClasses()))
  136. {
  137. // Exclude this value for the result set
  138. $oSearch->AddCondition(PORTAL_SET_TYPE_FROM, $sTicketType, '!=');
  139. }
  140. }
  141. }
  142. /**
  143. * Displays the portal main menu
  144. * @param WebPage $oP The current web page
  145. * @return void
  146. */
  147. function DisplayMainMenu(WebPage $oP)
  148. {
  149. $oP->AddMenuButton('showongoing', 'Portal:ShowOngoing', '../portal/index.php?operation=show_ongoing');
  150. $oP->AddMenuButton('newrequest', 'Portal:CreateNewRequest', '../portal/index.php?operation=create_request');
  151. $oP->AddMenuButton('showclosed', 'Portal:ShowClosed', '../portal/index.php?operation=show_closed');
  152. if (UserRights::CanChangePassword())
  153. {
  154. $oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', '../portal/index.php?loginop=change_pwd');
  155. }
  156. }
  157. /**
  158. * Displays the current tickets
  159. * @param WebPage $oP The current web page
  160. * @return void
  161. */
  162. function ShowOngoingTickets(WebPage $oP)
  163. {
  164. $oP->add("<div id=\"open_requests\">\n");
  165. $oP->add("<h1 id=\"title_open_requests\">".Dict::S('Portal:OpenRequests')."</h1>\n");
  166. ListOpenRequests($oP);
  167. $oP->add("</div>\n");
  168. $oP->add("<div id=\"#div_resolved_requests\">\n");
  169. $oP->add("<h1 id=\"#resolved_requests\">".Dict::S('Portal:ResolvedRequests')."</h1>\n");
  170. ListResolvedRequests($oP);
  171. $oP->add("</div>\n");
  172. }
  173. /**
  174. * Displays the closed tickets
  175. * @param WebPage $oP The current web page
  176. * @return void
  177. */
  178. function ShowClosedTickets(WebPage $oP)
  179. {
  180. $oP->add("<div id=\"#closed_tickets\">\n");
  181. //$oP->add("<h1 id=\"#closed_tickets\">".Dict::S('Portal:ListClosedTickets')."</h1>\n");
  182. ListClosedTickets($oP);
  183. $oP->add("</div>\n");
  184. }
  185. /**
  186. * Displays the form to select a Service Category Id (among the valid ones for the specified user Organization)
  187. * @param WebPage $oP Web page for the form output
  188. * @param Organization $oUserOrg The organization of the current user
  189. * @return void
  190. */
  191. function SelectServiceCategory($oP, $oUserOrg)
  192. {
  193. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  194. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICECATEGORY_QUERY);
  195. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  196. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  197. if ($oSet->Count() == 1)
  198. {
  199. $oService = $oSet->Fetch();
  200. $iSvcCategory = $oService->GetKey();
  201. // Only one Category, skip this step in the wizard
  202. SelectServiceSubCategory($oP, $oUserOrg, $iSvcCategory);
  203. }
  204. else
  205. {
  206. $oP->add("<div class=\"wizContainer\" id=\"form_select_service\">\n");
  207. $oP->WizardFormStart('request_wizard', 1);
  208. $oP->add("<h1 id=\"select_category\">".Dict::S('Portal:SelectService')."</h1>\n");
  209. $oP->add("<table>\n");
  210. while($oService = $oSet->Fetch())
  211. {
  212. $id = $oService->GetKey();
  213. $sChecked = "";
  214. if (isset($aParameters['service_id']) && ($id == $aParameters['service_id']))
  215. {
  216. $sChecked = "checked";
  217. }
  218. $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>");
  219. $oP->p("<p>".$oService->GetAsHTML('description')."</p></td></tr>");
  220. }
  221. $oP->add("</table>\n");
  222. $oP->DumpHiddenParams($aParameters, array('service_id'));
  223. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  224. $oP->WizardFormButtons(BUTTON_NEXT | BUTTON_CANCEL); // NO back button since it's the first step of the Wizard
  225. $oP->WizardFormEnd();
  226. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectOneService'));
  227. $oP->add("</div>\n");
  228. }
  229. }
  230. /**
  231. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  232. * and based on the page's parameter 'service_id'
  233. * @param WebPage $oP Web page for the form output
  234. * @param Organization $oUserOrg The organization of the current user
  235. * @param $iSvcId Id of the selected service in case of pass-through (when there is only one service)
  236. * @return void
  237. */
  238. function SelectServiceSubCategory($oP, $oUserOrg, $iSvcId = null)
  239. {
  240. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  241. if ($iSvcId == null)
  242. {
  243. $iSvcId = $aParameters['service_id'];
  244. }
  245. else
  246. {
  247. $aParameters['service_id'] = $iSvcId;
  248. }
  249. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  250. $iDefaultWizNext = 2;
  251. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICE_SUBCATEGORY_QUERY);
  252. RestrictSubcategories($oSearch);
  253. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  254. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId, 'org_id' => $oUserOrg->GetKey()));
  255. if ($oSet->Count() == 1)
  256. {
  257. // Only one sub service, skip this step of the wizard
  258. $oSubService = $oSet->Fetch();
  259. $iSubSvdId = $oSubService->GetKey();
  260. SelectRequestTemplate($oP, $oUserOrg, $iSvcId, $iSubSvdId);
  261. }
  262. else
  263. {
  264. $oServiceCategory = MetaModel::GetObject('Service', $iSvcId, false, true /* allow all data*/);
  265. if (is_object($oServiceCategory))
  266. {
  267. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  268. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', $oServiceCategory->GetName())."</h1>\n");
  269. $oP->WizardFormStart('request_wizard', $iDefaultWizNext);
  270. $oP->add("<table>\n");
  271. while($oSubService = $oSet->Fetch())
  272. {
  273. $id = $oSubService->GetKey();
  274. $sChecked = "";
  275. if ($id == $iDefaultSubSvcId)
  276. {
  277. $sChecked = "checked";
  278. }
  279. $oP->add("<tr>");
  280. $oP->add("<td style=\"vertical-align:top\">");
  281. $oP->add("<p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"servicesubcategory_$id\" value=\"$id\"></p>");
  282. $oP->add("</td>");
  283. $oP->add("<td style=\"vertical-align:top\">");
  284. $oP->add("<p><b><label for=\"servicesubcategory_$id\">".$oSubService->GetName()."</label></b></p>");
  285. $oP->add("<p>".$oSubService->GetAsHTML('description')."</p>");
  286. $oP->add("</td>");
  287. $oP->add("</tr>");
  288. }
  289. $oP->add("</table>\n");
  290. $oP->DumpHiddenParams($aParameters, array('servicesubcategory_id'));
  291. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  292. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL); //Back button automatically discarded if on the first page
  293. $oP->WizardFormEnd();
  294. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectAServiceSubCategory'));
  295. $oP->add("</div>\n");
  296. }
  297. else
  298. {
  299. $oP->p("Error: Invalid Service: id = $iSvcId");
  300. }
  301. }
  302. }
  303. /**
  304. * Displays the form to select a Template
  305. * @param WebPage $oP Web page for the form output
  306. * @param Organization $oUserOrg The organization of the current user
  307. * @param $iSvcId Id of the selected service in case of pass-through (when there is only one service)
  308. * @param integer $iSubSvcId The identifier of the sub-service (fall through when there is only one sub-service)
  309. * @return void
  310. */
  311. function SelectRequestTemplate($oP, $oUserOrg, $iSvcId = null, $iSubSvcId = null)
  312. {
  313. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  314. if (!is_null($iSvcId))
  315. {
  316. $aParameters['service_id'] = $iSvcId;
  317. }
  318. if (!is_null($iSubSvcId))
  319. {
  320. $aParameters['servicesubcategory_id'] = $iSubSvcId;
  321. }
  322. $iDefaultTemplate = isset($aParameters['template_id']) ? $aParameters['template_id'] : 0;
  323. if (MetaModel::IsValidClass('Template'))
  324. {
  325. $sClass = ComputeClass($aParameters['servicesubcategory_id']);
  326. try
  327. {
  328. $sOql = GetConstant($sClass, 'TEMPLATE_QUERY');
  329. }
  330. catch(Exception $e)
  331. {
  332. // Backward compatibility
  333. $sOql = REQUEST_TEMPLATE_QUERY;
  334. }
  335. $oSearch = DBObjectSearch::FromOQL($sOql);
  336. $oSearch->AllowAllData();
  337. $oSet = new CMDBObjectSet($oSearch, array(), array(
  338. 'service_id' => $aParameters['service_id'],
  339. 'servicesubcategory_id' => $aParameters['servicesubcategory_id']
  340. ));
  341. if ($oSet->Count() == 0)
  342. {
  343. RequestCreationForm($oP, $oUserOrg, $aParameters['service_id'], $aParameters['servicesubcategory_id'], 0);
  344. return;
  345. }
  346. elseif ($oSet->Count() == 1)
  347. {
  348. $oTemplate = $oSet->Fetch();
  349. $iTemplateId = $oTemplate->GetKey();
  350. RequestCreationForm($oP, $oUserOrg, $aParameters['service_id'], $aParameters['servicesubcategory_id'], $iTemplateId);
  351. return;
  352. }
  353. $oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false);
  354. if (is_object($oServiceSubCategory))
  355. {
  356. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  357. $oP->add("<h1 id=\"select_template\">".Dict::Format('Portal:SelectRequestTemplate', $oServiceSubCategory->GetName())."</h1>\n");
  358. $oP->WizardFormStart('request_wizard', 3);
  359. $oP->add("<table>\n");
  360. while($oTemplate = $oSet->Fetch())
  361. {
  362. $id = $oTemplate->GetKey();
  363. $sChecked = "";
  364. if ($id == $iDefaultTemplate)
  365. {
  366. $sChecked = "checked";
  367. }
  368. $oP->add("<tr>");
  369. $oP->add("<td style=\"vertical-align:top\">");
  370. $oP->p("<input name=\"attr_template_id\" $sChecked type=\"radio\" id=\"template_$id\" value=\"$id\">");
  371. $oP->add("</td>");
  372. $oP->add("<td style=\"vertical-align:top\">");
  373. $oP->p("<b><label for=\"template_$id\">".$oTemplate->GetAsHTML('label')."</label></b>");
  374. $oP->p($oTemplate->GetAsHTML('description'));
  375. $oP->add("</td>");
  376. $oP->add("</tr>");
  377. }
  378. $oP->add("</table>\n");
  379. $oP->DumpHiddenParams($aParameters, array('template_id'));
  380. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  381. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL); //Back button automatically discarded if on the first page
  382. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectATemplate'));
  383. $oP->WizardFormEnd();
  384. $oP->add("</div>\n");
  385. }
  386. else
  387. {
  388. $oP->p("Error: Invalid servicesubcategory_id = ".$aParameters['servicesubcategory_id']);
  389. }
  390. }
  391. else
  392. {
  393. RequestCreationForm($oP, $oUserOrg, $aParameters['service_id'], $aParameters['servicesubcategory_id']);
  394. return;
  395. }
  396. }
  397. /**
  398. * Displays the form for the final step of the ticket creation
  399. * @param WebPage $oP The current web page for the form output
  400. * @param Organization $oUserOrg The organization of the current user
  401. * @param integer $iSvcId The identifier of the service (fall through when there is only one service)
  402. * @param integer $iSubSvcId The identifier of the sub-service (fall through when there is only one sub-service)
  403. * @param integer $iTemplateId The identifier of the template (fall through when there is only one template)
  404. * @return void
  405. */
  406. function RequestCreationForm($oP, $oUserOrg, $iSvcId = null, $iSubSvcId = null, $iTemplateId = null)
  407. {
  408. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  409. if (!is_null($iSvcId))
  410. {
  411. $aParameters['service_id'] = $iSvcId;
  412. }
  413. if (!is_null($iSubSvcId))
  414. {
  415. $aParameters['servicesubcategory_id'] = $iSubSvcId;
  416. }
  417. if (!is_null($iTemplateId))
  418. {
  419. $aParameters['template_id'] = $iTemplateId;
  420. }
  421. $sDescription = '';
  422. if (isset($aParameters['template_id']) && ($aParameters['template_id'] != 0))
  423. {
  424. $aTemplateFields = array();
  425. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id'], false);
  426. if (is_object($oTemplate))
  427. {
  428. $oFieldSearch = DBObjectSearch::FromOQL('SELECT TemplateField WHERE template_id = :template_id');
  429. $oFieldSearch->AllowAllData();
  430. $oFieldSet = new DBObjectSet($oFieldSearch, array('order' => true), array('template_id' => $oTemplate->GetKey()));
  431. while($oField = $oFieldSet->Fetch())
  432. {
  433. $sAttCode = $oField->Get('code');
  434. if (isset($aParameters[$sAttCode]))
  435. {
  436. $oField->Set('initial_value', $aParameters[$sAttCode]);
  437. }
  438. $aTemplateFields[$sAttCode] = $oField;
  439. }
  440. }
  441. }
  442. $oServiceCategory = MetaModel::GetObject('Service', $aParameters['service_id'], false, true /* allow all data*/);
  443. $oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false, true /* allow all data*/);
  444. if (is_object($oServiceCategory) && is_object($oServiceSubCategory))
  445. {
  446. $sClass = ComputeClass($oServiceSubCategory->GetKey());
  447. $oRequest = MetaModel::NewObject($sClass);
  448. $oRequest->Set('org_id', $oUserOrg->GetKey());
  449. $oRequest->Set('caller_id', UserRights::GetContactId());
  450. $oRequest->Set('service_id', $aParameters['service_id']);
  451. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  452. $oAttDef = MetaModel::GetAttributeDef($sClass, 'service_id');
  453. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceCategory->GetName());
  454. $oAttDef = MetaModel::GetAttributeDef($sClass, 'servicesubcategory_id');
  455. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceSubCategory->GetName());
  456. $aList = explode(',', GetConstant($sClass, 'FORM_ATTRIBUTES'));
  457. $iFlags = 0;
  458. foreach($aList as $sAttCode)
  459. {
  460. $value = '';
  461. if (isset($aParameters[$sAttCode]))
  462. {
  463. $value = $aParameters[$sAttCode];
  464. $oRequest->Set($sAttCode, $value);
  465. }
  466. }
  467. $aFieldsMap = array();
  468. foreach($aList as $sAttCode)
  469. {
  470. $value = '';
  471. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  472. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  473. if (isset($aParameters[$sAttCode]))
  474. {
  475. $value = $aParameters[$sAttCode];
  476. }
  477. $aArgs = array('this' => $oRequest);
  478. $sInputId = 'attr_'.$sAttCode;
  479. $aFieldsMap[$sAttCode] = $sInputId;
  480. $sValue = "<span id=\"field_{$sInputId}\">".$oRequest->GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs).'</span>';
  481. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  482. }
  483. $aHidden = array();
  484. if (!empty($aTemplateFields))
  485. {
  486. foreach ($aTemplateFields as $sAttCode => $oField)
  487. {
  488. $sValue = $oField->GetFormElement($oP, $sClass);
  489. if ($oField->Get('input_type') == 'hidden')
  490. {
  491. $aHidden[] = $sValue;
  492. }
  493. else
  494. {
  495. $aDetails[] = array('label' => $oField->GetAsHTML('label'), 'value' => $sValue);
  496. }
  497. }
  498. }
  499. $oP->add_script(
  500. <<<EOF
  501. // Create the object once at the beginning of the page...
  502. var oWizardHelper = new WizardHelper('$sClass', '');
  503. EOF
  504. );
  505. $oP->add_linked_script("../js/json.js");
  506. $oP->add_linked_script("../js/forms-json-utils.js");
  507. $oP->add_linked_script("../js/wizardhelper.js");
  508. $oP->add_linked_script("../js/wizard.utils.js");
  509. $oP->add_linked_script("../js/linkswidget.js");
  510. $oP->add_linked_script("../js/extkeywidget.js");
  511. $oP->add_linked_script("../js/jquery.blockUI.js");
  512. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  513. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  514. $oP->WizardFormStart('request_form', 4);
  515. $oP->details($aDetails);
  516. // Add hidden fields for known values, enabling dependant attributes to be computed correctly
  517. //
  518. foreach($oRequest->ListChanges() as $sAttCode => $value)
  519. {
  520. if (!in_array($sAttCode, $aList))
  521. {
  522. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  523. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  524. {
  525. $sValue = htmlentities($oRequest->Get($sAttCode), ENT_QUOTES, 'UTF-8');
  526. $oP->add("<input type=\"hidden\" id=\"attr_$sAttCode\" name=\"attr_$sAttCode\" value=\"$sValue\">");
  527. $aFieldsMap[$sAttCode] = 'attr_'.$sAttCode;
  528. }
  529. }
  530. }
  531. if (isset($aParameters['template_id']) && ($aParameters['template_id'] != 0))
  532. {
  533. $oP->add("<input type=\"hidden\" name=\"attr_template_id\" value=\"{$aParameters['template_id']}\">");
  534. }
  535. $oP->add(implode("\n", $aHidden));
  536. $oAttPlugin = new AttachmentPlugIn();
  537. $oAttPlugin->OnDisplayRelations($oRequest, $oP, true /* edit */);
  538. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  539. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_FINISH | BUTTON_CANCEL); //Back button automatically discarded if on the first page
  540. $oP->WizardFormEnd();
  541. $oP->add("</div>\n");
  542. $iFieldsCount = count($aFieldsMap);
  543. $sJsonFieldsMap = json_encode($aFieldsMap);
  544. $oP->add_ready_script(
  545. <<<EOF
  546. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  547. oWizardHelper.SetFieldsCount($iFieldsCount);
  548. // Starts the validation when the page is ready
  549. CheckFields('request_form', false);
  550. $('#request_form').submit( function() {
  551. return OnSubmit('request_form');
  552. });
  553. EOF
  554. );
  555. }
  556. else
  557. {
  558. // User not authorized to use this service ?
  559. //ShowOngoingTickets($oP);
  560. }
  561. }
  562. /**
  563. * Validate the parameters and create the ticket object (based on the page's POSTed parameters)
  564. * @param WebPage $oP The current web page for the output
  565. * @param Organization $oUserOrg The organization of the current user
  566. * @return void
  567. */
  568. function DoCreateRequest($oP, $oUserOrg)
  569. {
  570. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  571. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  572. if (!utils::IsTransactionValid($sTransactionId))
  573. {
  574. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  575. //ShowOngoingTickets($oP);
  576. return;
  577. }
  578. // Validate the parameters
  579. // 1) ServiceCategory
  580. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  581. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  582. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  583. if ($oSet->Count() != 1)
  584. {
  585. // Invalid service for the current user !
  586. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  587. }
  588. $oServiceCategory = $oSet->Fetch();
  589. // 2) Service Subcategory
  590. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  591. RestrictSubcategories($oSearch);
  592. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  593. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  594. if ($oSet->Count() != 1)
  595. {
  596. // Invalid subcategory
  597. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  598. }
  599. $oServiceSubCategory = $oSet->Fetch();
  600. $sClass = ComputeClass($oServiceSubCategory->GetKey());
  601. $oRequest = MetaModel::NewObject($sClass);
  602. $aAttList = array_merge(explode(',', GetConstant($sClass, 'FORM_ATTRIBUTES')), array('service_id', 'servicesubcategory_id'));
  603. $oRequest->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList);
  604. $oRequest->Set('org_id', $oUserOrg->GetKey());
  605. $oRequest->Set('caller_id', UserRights::GetContactId());
  606. if (isset($aParameters['moreinfo']))
  607. {
  608. // There is a template, insert it into the description
  609. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  610. $oRequest->Set($sLogAttCode, $aParameters['moreinfo']);
  611. }
  612. $sTypeAttCode = GetConstant($sClass, 'TYPE');
  613. if (($sTypeAttCode != '') && (PORTAL_SET_TYPE_FROM != ''))
  614. {
  615. $oRequest->Set($sTypeAttCode, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  616. }
  617. if (MetaModel::IsValidAttCode($sClass, 'origin'))
  618. {
  619. $oRequest->Set('origin', 'portal');
  620. }
  621. $oAttPlugin = new AttachmentPlugIn();
  622. $oAttPlugin->OnFormSubmit($oRequest);
  623. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  624. if ($bRes)
  625. {
  626. if (isset($aParameters['template_id']))
  627. {
  628. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id']);
  629. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  630. $oRequest->Set($sLogAttCode, $oTemplate->GetPostedValuesAsText($oRequest)."\n");
  631. $oRequest->DBInsertNoReload();
  632. $oTemplate->RecordExtraDataFromPostedForm($oRequest);
  633. }
  634. else
  635. {
  636. $oRequest->DBInsertNoReload();
  637. }
  638. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName($sClass))."</h1>\n");
  639. //DisplayObject($oP, $oRequest, $oUserOrg);
  640. ShowOngoingTickets($oP);
  641. }
  642. else
  643. {
  644. RequestCreationForm($oP, $oUserOrg);
  645. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  646. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  647. }
  648. }
  649. /**
  650. * Prompts the user for creating a new request
  651. * @param WebPage $oP The current web page
  652. * @return void
  653. */
  654. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  655. {
  656. switch($oP->GetWizardStep())
  657. {
  658. case 0:
  659. default:
  660. SelectServiceCategory($oP, $oUserOrg);
  661. break;
  662. case 1:
  663. SelectServiceSubCategory($oP, $oUserOrg);
  664. break;
  665. case 2:
  666. SelectRequestTemplate($oP, $oUserOrg);
  667. break;
  668. case 3:
  669. RequestCreationForm($oP, $oUserOrg);
  670. break;
  671. case 4:
  672. DoCreateRequest($oP, $oUserOrg);
  673. break;
  674. }
  675. }
  676. /**
  677. * Helper to display lists (UserRequest, Incident, etc.)
  678. * Adjust the presentation depending on the following cases:
  679. * - no item at all
  680. * - items of one class only
  681. * - items of several classes
  682. */
  683. function DisplayRequestLists(WebPage $oP, $aClassToSet)
  684. {
  685. $iNotEmpty = 0; // Count of types for which there are some items to display
  686. foreach ($aClassToSet as $sClass => $oSet)
  687. {
  688. if ($oSet->Count() > 0)
  689. {
  690. $iNotEmpty++;
  691. }
  692. }
  693. if ($iNotEmpty == 0)
  694. {
  695. $oP->p(Dict::S('Portal:NoOpenRequest'));
  696. }
  697. else
  698. {
  699. foreach ($aClassToSet as $sClass => $oSet)
  700. {
  701. if ($iNotEmpty > 1)
  702. {
  703. // Differentiate the sublists
  704. $oP->add("<h2>".MetaModel::GetName($sClass)."</h2>\n");
  705. }
  706. if ($oSet->Count() > 0)
  707. {
  708. $sZList = GetConstant($sClass, 'LIST_ZLIST');
  709. $aZList = explode(',', $sZList);
  710. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  711. }
  712. }
  713. }
  714. }
  715. /**
  716. * Lists all the currently opened User Requests for the current user
  717. * @param WebPage $oP The current web page
  718. * @return void
  719. */
  720. function ListOpenRequests(WebPage $oP)
  721. {
  722. $oUserOrg = GetUserOrg();
  723. $aClassToSet = array();
  724. $iUser = UserRights::GetContactId();
  725. $oContact = MetaModel::GetObject('Contact', UserRights::GetContactId());
  726. foreach (GetTicketClasses() as $sClass)
  727. {
  728. if (IsPowerUser())
  729. {
  730. $sValidationDefine = 'PORTAL_'.strtoupper($sClass).'_DISPLAY_POWERUSER_QUERY';
  731. }
  732. else
  733. {
  734. $sValidationDefine = 'PORTAL_'.strtoupper($sClass).'_DISPLAY_QUERY';
  735. }
  736. if (defined($sValidationDefine))
  737. {
  738. $sOQL = constant($sValidationDefine);
  739. $oSearch = DBObjectSearch::FromOQL($sOQL);
  740. $sOQLCondition = $oSearch->GetClassAlias().".status NOT IN ('closed', 'resolved')";
  741. $oExpr = Expression::FromOQL($sOQLCondition);
  742. $oSearch->AddConditionExpression($oExpr);
  743. }
  744. else
  745. {
  746. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status NOT IN ('closed', 'resolved')";
  747. $oSearch = DBObjectSearch::FromOQL($sOQL);
  748. if ($iUser > 0 && !IsPowerUser())
  749. {
  750. $oSearch->AddCondition('caller_id', $iUser);
  751. }
  752. }
  753. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey(), 'caller_id' => $iUser, 'contact' => $oContact));
  754. }
  755. DisplayRequestLists($oP, $aClassToSet);
  756. }
  757. /**
  758. * Lists all the currently resolved (not yet closed) User Requests for the current user
  759. * @param WebPage $oP The current web page
  760. * @return void
  761. */
  762. function ListResolvedRequests(WebPage $oP)
  763. {
  764. $oUserOrg = GetUserOrg();
  765. $aClassToSet = array();
  766. foreach (GetTicketClasses() as $sClass)
  767. {
  768. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status = 'resolved'";
  769. $oSearch = DBObjectSearch::FromOQL($sOQL);
  770. $iUser = UserRights::GetContactId();
  771. if ($iUser > 0 && !IsPowerUser())
  772. {
  773. $oSearch->AddCondition('caller_id', $iUser);
  774. }
  775. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  776. }
  777. DisplayRequestLists($oP, $aClassToSet);
  778. }
  779. /**
  780. * Lists all the currently closed tickets
  781. * @param WebPage $oP The current web page
  782. * @return void
  783. */
  784. function ListClosedTickets(WebPage $oP)
  785. {
  786. $aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
  787. // Remove the caller_id form the search criteria if the user is not a Portal Power User
  788. // since the user is only allowed to see her/his own tickets
  789. foreach($aAttSpecs as $idx => $sAttCode)
  790. {
  791. if (($sAttCode == 'caller_id') && !IsPowerUser())
  792. {
  793. unset($aAttSpecs[$idx]);
  794. }
  795. }
  796. $aClasses = GetTicketClasses();
  797. $sMainClass = reset($aClasses);
  798. $oP->DisplaySearchForm($sMainClass, $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  799. $oUserOrg = GetUserOrg();
  800. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  801. $aClassToSet = array();
  802. foreach (GetTicketClasses() as $sClass)
  803. {
  804. $oSearch = $oP->PostedParamsToFilter($sClass, $aAttSpecs, 'search_');
  805. if(is_null($oSearch))
  806. {
  807. $oSearch = new DBObjectSearch($sClass);
  808. }
  809. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  810. $oSearch->AddCondition('status', 'closed');
  811. $iUser = UserRights::GetContactId();
  812. if ($iUser > 0 && !IsPowerUser())
  813. {
  814. $oSearch->AddCondition('caller_id', $iUser);
  815. }
  816. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch);
  817. }
  818. DisplayRequestLists($oP, $aClassToSet);
  819. }
  820. /**
  821. * Display an object - to be customized
  822. * @param WebPage $oP The current web page
  823. * @param Object $oObj Any kind of object
  824. * @param Object $oUserOrg The organization of the logged in user
  825. * @return void
  826. */
  827. function DisplayObject($oP, $oObj, $oUserOrg)
  828. {
  829. if (in_array(get_class($oObj), GetTicketClasses()))
  830. {
  831. ShowDetailsRequest($oP, $oObj);
  832. }
  833. else
  834. {
  835. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  836. }
  837. }
  838. /**
  839. * Displays the details of a request
  840. * @param WebPage $oP The current web page
  841. * @param Object $oObj The target object
  842. * @return void
  843. */
  844. function ShowDetailsRequest(WebPage $oP, $oObj)
  845. {
  846. $sClass = get_class($oObj);
  847. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  848. $sUserCommentAttCode = GetConstant($sClass, 'USER_COMMENT');
  849. $bIsReopenButton = false;
  850. $bIsCloseButton = false;
  851. $bIsEscalateButton = false;
  852. $bEditAttachments = false;
  853. $aEditAtt = array(); // List of attributes editable in the main form
  854. if (!MetaModel::DBIsReadOnly())
  855. {
  856. switch($oObj->GetState())
  857. {
  858. case 'resolved':
  859. $aEditAtt = array();
  860. $aTransitions = $oObj->EnumTransitions();
  861. $oSet = DBObjectSet::FromObject($oObj);
  862. // Add the "Reopen" button if this is valid action
  863. if (array_key_exists('ev_reopen', $aTransitions) && UserRights::IsStimulusAllowed($sClass, 'ev_reopen', $oSet))
  864. {
  865. $bIsReopenButton = true;
  866. MakeStimulusForm($oP, $oObj, 'ev_reopen', array($sLogAttCode));
  867. }
  868. // Add the "Close" button if this is valid action
  869. if (array_key_exists('ev_close', $aTransitions) && UserRights::IsStimulusAllowed($sClass, 'ev_close', $oSet))
  870. {
  871. $bIsCloseButton = true;
  872. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', $sUserCommentAttCode));
  873. }
  874. break;
  875. case 'closed':
  876. // By convention 'closed' is the final state of a ticket and nothing can be done in such a state
  877. break;
  878. default:
  879. // In all other states, the only possible action is to update the ticket (both the case log and the attachments)
  880. // This update is possible only if the case log field is not read-only or hidden in the current state
  881. $iFlags = $oObj->GetAttributeFlags($sLogAttCode);
  882. $bReadOnly = (($iFlags & (OPT_ATT_READONLY | OPT_ATT_HIDDEN)) != 0);
  883. if ($bReadOnly)
  884. {
  885. $aEditAtt = array();
  886. $bEditAttachments = false;
  887. }
  888. else
  889. {
  890. $aEditAtt = array(
  891. $sLogAttCode => '????'
  892. );
  893. $bEditAttachments = true;
  894. }
  895. break;
  896. }
  897. }
  898. // REFACTORISER LA MISE EN FORME
  899. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  900. $aAttList = json_decode(GetConstant($sClass, 'DETAILS_ZLIST'), true);
  901. switch($oObj->GetState())
  902. {
  903. case 'closed':
  904. $aAttList['centered'][] = 'user_satisfaction';
  905. $aAttList['centered'][] = $sUserCommentAttCode;
  906. }
  907. // Remove the edited attribute from the shown attributes
  908. //
  909. foreach($aEditAtt as $sAttCode => $foo)
  910. {
  911. foreach($aAttList as $col => $aColumn)
  912. {
  913. if (in_array($sAttCode, $aColumn))
  914. {
  915. if(($index = array_search($sAttCode, $aColumn)) !== false)
  916. {
  917. unset($aAttList[$col][$index]);
  918. }
  919. }
  920. }
  921. }
  922. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  923. $oP->WizardFormStart('request_form', null);
  924. $oP->add('<div id="request_details">');
  925. $oP->add('<table id="request_details_table">');
  926. $oP->add('<tr>');
  927. $oP->add('<td style="vertical-align:top;">');
  928. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  929. $oP->add('</td>');
  930. $oP->add('<td style="vertical-align:top;">');
  931. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  932. $oP->add('</td>');
  933. $oP->add('</tr>');
  934. if (array_key_exists('centered', $aAttList))
  935. {
  936. $oP->add('<tr>');
  937. $oP->add('<td style="vertical-align:top;" colspan="2">');
  938. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  939. $oP->add('</td>');
  940. $oP->add('</tr>');
  941. }
  942. // REFACTORISER
  943. $oP->add('<tr>');
  944. $oP->add('<td colspan="2" style="vertical-align:top;">');
  945. $oAttPlugin = new AttachmentPlugIn();
  946. if ($bEditAttachments)
  947. {
  948. $oAttPlugin->EnableDelete(false);
  949. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  950. }
  951. else
  952. {
  953. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  954. }
  955. $oP->add('</td>');
  956. $oP->add('</tr>');
  957. $oP->add('<tr>');
  958. $oP->add('<td colspan="2" style="vertical-align:top;">');
  959. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  960. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  961. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  962. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  963. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  964. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  965. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  966. $oP->add_script(
  967. <<<EOF
  968. function SetStimulusToApply(sStimulusCode)
  969. {
  970. $('#stimulus_to_apply').val(sStimulusCode);
  971. }
  972. EOF
  973. );
  974. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  975. foreach($aEditAtt as $sAttCode => $foo)
  976. {
  977. $sValue = $oObj->Get($sAttCode);
  978. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  979. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  980. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  981. $sInputId = 'input_'.$sAttCode;
  982. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  983. $aEditFields[$sAttCode] = array(
  984. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  985. 'value' => $sHTMLValue
  986. );
  987. }
  988. foreach($aEditFields as $sAttCode => $aFieldSpec)
  989. {
  990. if ($sAttCode == $sLogAttCode)
  991. {
  992. // Skip, the public log will be displayed below the buttons
  993. continue;
  994. }
  995. $oP->add("<div class=\"edit_item\">");
  996. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  997. $oP->add($aFieldSpec['value']);
  998. $oP->add('</div>');
  999. }
  1000. if($bIsReopenButton)
  1001. {
  1002. $sStimulusCode = 'ev_reopen';
  1003. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  1004. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  1005. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  1006. }
  1007. if($bIsCloseButton)
  1008. {
  1009. $sStimulusCode = 'ev_close';
  1010. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  1011. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  1012. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  1013. }
  1014. elseif (count($aEditAtt) > 0)
  1015. {
  1016. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  1017. }
  1018. if ($bIsEscalateButton)
  1019. {
  1020. $sStimulusCode = 'ev_timeout';
  1021. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  1022. }
  1023. $oP->add('</td>');
  1024. $oP->add('</tr>');
  1025. $oP->add('<tr>');
  1026. $oP->add('<td colspan="2" style="vertical-align:top;">');
  1027. if (isset($aEditFields[$sLogAttCode]))
  1028. {
  1029. $oP->add("<div class=\"edit_item\">");
  1030. $oP->add('<h1>'.$aEditFields[$sLogAttCode]['label'].'</h1>');
  1031. $oP->add($aEditFields[$sLogAttCode]['value']);
  1032. $oP->add('</div>');
  1033. }
  1034. else
  1035. {
  1036. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sLogAttCode).'</h1>');
  1037. $oP->add($oObj->GetAsHTML($sLogAttCode));
  1038. }
  1039. $oP->add('</td>');
  1040. $oP->add('</tr>');
  1041. $oP->add('</table>');
  1042. $oP->add('</div>');
  1043. $oP->WizardFormEnd();
  1044. $oP->add('</div>');
  1045. }
  1046. /**
  1047. * Create form to apply a stimulus
  1048. * @param WebPage $oP The current web page
  1049. * @param Object $oObj The target object
  1050. * @param String $sStimulusCode Stimulus that will be applied
  1051. * @param Array $aEditAtt List of attributes to edit
  1052. * @return void
  1053. */
  1054. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  1055. {
  1056. static $bHasStimulusForm = false;
  1057. $sDialogId = $sStimulusCode."_dialog";
  1058. $sFormId = $sStimulusCode."_form";
  1059. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  1060. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  1061. $sClass = get_class($oObj);
  1062. $oP->add('<form id="'.$sFormId.'" method="post">');
  1063. $sTransactionId = utils::GetNewTransactionId();
  1064. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  1065. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  1066. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  1067. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  1068. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  1069. $aTransitions = $oObj->EnumTransitions();
  1070. $aStimuli = MetaModel::EnumStimuli($sClass);
  1071. if (!isset($aTransitions[$sStimulusCode]))
  1072. {
  1073. // Invalid stimulus
  1074. throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulusCode, $oObj->GetName(), $oObj->GetStateLabel()));
  1075. }
  1076. // Compute the attribute flags in the target state
  1077. $aTransition = $aTransitions[$sStimulusCode];
  1078. $sTargetState = $aTransition['target_state'];
  1079. $aTargetStates = MetaModel::EnumStates($sClass);
  1080. $aTargetState = $aTargetStates[$sTargetState];
  1081. $aExpectedAttributes = $aTargetState['attribute_list'];
  1082. foreach($aEditAtt as $sAttCode)
  1083. {
  1084. $sValue = $oObj->Get($sAttCode);
  1085. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  1086. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  1087. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1088. $sInputId = 'input_'.$sAttCode;
  1089. $iFlags = array_key_exists($sAttCode, $aExpectedAttributes) ? $aExpectedAttributes[$sAttCode] : 0;
  1090. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1091. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  1092. $oP->add($sHTMLValue);
  1093. }
  1094. $oP->add('</form>');
  1095. $oP->add('</div>');
  1096. if (!$bHasStimulusForm)
  1097. {
  1098. $bHasStimulusForm = true;
  1099. $oP->add_script(
  1100. <<<EOF
  1101. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  1102. {
  1103. var sWidth = 'auto';
  1104. if (sStimulusCode == 'ev_reopen')
  1105. {
  1106. // Avoid having a dialog spanning the complete width of the window
  1107. // just because it contains a CaseLog entry
  1108. sWidth = '80%';
  1109. }
  1110. $('#'+sStimulusCode+'_dialog').dialog({
  1111. height: 'auto',
  1112. width: sWidth,
  1113. modal: true,
  1114. title: sTitle,
  1115. buttons: [
  1116. { text: sOkButtonLabel, click: function() {
  1117. $(this).find('#'+sStimulusCode+'_form').submit();
  1118. } },
  1119. { text: "$sCancelButtonLabel", click: function() {
  1120. $(this).dialog( "close" );
  1121. } }
  1122. ]
  1123. });
  1124. // Start the validation
  1125. CheckFields(sStimulusCode+'_form', false);
  1126. $('#'+sStimulusCode+'_form').submit( function() {
  1127. return OnSubmit(sStimulusCode+'_form');
  1128. });
  1129. }
  1130. EOF
  1131. );
  1132. }
  1133. }
  1134. /**
  1135. * Get The organization of the current user (i.e. the organization of its contact)
  1136. * @param WebPage $oP The current page, for errors output
  1137. * @return Organization The user's org or null in case of problem...
  1138. */
  1139. function GetUserOrg()
  1140. {
  1141. $oOrg = null;
  1142. $iContactId = UserRights::GetContactId();
  1143. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  1144. if (is_object($oContact))
  1145. {
  1146. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  1147. }
  1148. else
  1149. {
  1150. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  1151. }
  1152. return $oOrg;
  1153. }
  1154. /**
  1155. * Determine if the current user can be considered as being a portal power user
  1156. */
  1157. function IsPowerUSer()
  1158. {
  1159. $iUserID = UserRights::GetUserId();
  1160. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  1161. $oProfileSet = new DBObjectSet(
  1162. DBObjectSearch::FromOQL($sOQLprofile),
  1163. array(),
  1164. array(
  1165. 'user' => $iUserID,
  1166. 'profile' => PORTAL_POWER_USER_PROFILE,
  1167. )
  1168. );
  1169. $bRes = ($oProfileSet->count() > 0);
  1170. return $bRes;
  1171. }
  1172. ///////////////////////////////////////////////////////////////////////////////
  1173. //
  1174. // Main program
  1175. //
  1176. ///////////////////////////////////////////////////////////////////////////////
  1177. try
  1178. {
  1179. require_once(APPROOT.'/application/startup.inc.php');
  1180. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  1181. $oAppContext = new ApplicationContext();
  1182. $sOperation = utils::ReadParam('operation', '');
  1183. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  1184. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  1185. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  1186. $aClasses = explode(',', MetaModel::GetConfig()->Get('portal_tickets'));
  1187. $sMainClass = trim(reset($aClasses));
  1188. if (!class_exists($sMainClass))
  1189. {
  1190. $oP = new WebPage(Dict::S('Portal:Title'));
  1191. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  1192. }
  1193. else
  1194. {
  1195. $oUserOrg = GetUserOrg();
  1196. $sCode = $oUserOrg->Get('code');
  1197. $sAlternateStylesheet = '';
  1198. if (@file_exists("./$sCode/portal.css"))
  1199. {
  1200. $sAlternateStylesheet = "$sCode";
  1201. }
  1202. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  1203. $oP->EnableDisconnectButton(utils::CanLogOff());
  1204. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  1205. if (is_object($oUserOrg))
  1206. {
  1207. switch($sOperation)
  1208. {
  1209. case 'show_closed':
  1210. $oP->set_title(Dict::S('Portal:ShowClosed'));
  1211. DisplayMainMenu($oP);
  1212. ShowClosedTickets($oP);
  1213. break;
  1214. case 'create_request':
  1215. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  1216. DisplayMainMenu($oP);
  1217. if (!MetaModel::DBIsReadOnly())
  1218. {
  1219. CreateRequest($oP, $oUserOrg);
  1220. }
  1221. break;
  1222. case 'details':
  1223. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1224. DisplayMainMenu($oP);
  1225. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1226. ValidateObject($oObj);
  1227. DisplayObject($oP, $oObj, $oUserOrg);
  1228. break;
  1229. case 'update_request':
  1230. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1231. DisplayMainMenu($oP);
  1232. if (!MetaModel::DBIsReadOnly())
  1233. {
  1234. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1235. ValidateObject($oObj);
  1236. $aAttList = array(
  1237. GetConstant(get_class($oObj), 'PUBLIC_LOG'),
  1238. 'user_satisfaction',
  1239. GetConstant(get_class($oObj), 'USER_COMMENT')
  1240. );
  1241. try
  1242. {
  1243. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  1244. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  1245. }
  1246. catch(TransactionException $e)
  1247. {
  1248. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  1249. }
  1250. DisplayObject($oP, $oObj, $oUserOrg);
  1251. }
  1252. break;
  1253. case 'show_ongoing':
  1254. default:
  1255. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  1256. DisplayMainMenu($oP);
  1257. ShowOngoingTickets($oP);
  1258. }
  1259. }
  1260. }
  1261. $oP->output();
  1262. }
  1263. catch(CoreException $e)
  1264. {
  1265. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1266. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1267. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1268. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  1269. //$oP->p($e->getTraceAsString());
  1270. $oP->output();
  1271. if (MetaModel::IsLogEnabledIssue())
  1272. {
  1273. if (MetaModel::IsValidClass('EventIssue'))
  1274. {
  1275. try
  1276. {
  1277. $oLog = new EventIssue();
  1278. $oLog->Set('message', $e->getMessage());
  1279. $oLog->Set('userinfo', '');
  1280. $oLog->Set('issue', $e->GetIssue());
  1281. $oLog->Set('impact', 'Page could not be displayed');
  1282. $oLog->Set('callstack', $e->getTrace());
  1283. $oLog->Set('data', $e->getContextData());
  1284. $oLog->DBInsertNoReload();
  1285. }
  1286. catch(Exception $e)
  1287. {
  1288. IssueLog::Error("Failed to log issue into the DB");
  1289. }
  1290. }
  1291. IssueLog::Error($e->getMessage());
  1292. }
  1293. // For debugging only
  1294. //throw $e;
  1295. }
  1296. catch(Exception $e)
  1297. {
  1298. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1299. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1300. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1301. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  1302. //$oP->p($e->getTraceAsString());
  1303. $oP->output();
  1304. if (MetaModel::IsLogEnabledIssue())
  1305. {
  1306. if (MetaModel::IsValidClass('EventIssue'))
  1307. {
  1308. try
  1309. {
  1310. $oLog = new EventIssue();
  1311. $oLog->Set('message', $e->getMessage());
  1312. $oLog->Set('userinfo', '');
  1313. $oLog->Set('issue', 'PHP Exception');
  1314. $oLog->Set('impact', 'Page could not be displayed');
  1315. $oLog->Set('callstack', $e->getTrace());
  1316. $oLog->Set('data', array());
  1317. $oLog->DBInsertNoReload();
  1318. }
  1319. catch(Exception $e)
  1320. {
  1321. IssueLog::Error("Failed to log issue into the DB");
  1322. }
  1323. }
  1324. IssueLog::Error($e->getMessage());
  1325. }
  1326. }
  1327. ?>