index.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * iTop User Portal main page
  20. *
  21. * @copyright Copyright (C) 2010-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 ($iSvcId != null)
  315. {
  316. $aParameters['service_id'] = $iSvcId;
  317. }
  318. if ($iSubSvcId != null)
  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']);
  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 ($iSvcId != null)
  410. {
  411. $aParameters['service_id'] = $iSvcId;
  412. }
  413. if ($iSubSvcId != null)
  414. {
  415. $aParameters['servicesubcategory_id'] = $iSubSvcId;
  416. }
  417. if ($iTemplateId != null)
  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. if (!in_array($sAttCode, $aList))
  489. {
  490. $sValue = $oField->GetFormElement($oP, $sClass);
  491. if ($oField->Get('input_type') == 'hidden')
  492. {
  493. $aHidden[] = $sValue;
  494. }
  495. else
  496. {
  497. $aDetails[] = array('label' => $oField->GetAsHTML('label'), 'value' => $sValue);
  498. }
  499. }
  500. }
  501. }
  502. $oP->add_script(
  503. <<<EOF
  504. // Create the object once at the beginning of the page...
  505. var oWizardHelper = new WizardHelper('$sClass', '');
  506. EOF
  507. );
  508. $oP->add_linked_script("../js/json.js");
  509. $oP->add_linked_script("../js/forms-json-utils.js");
  510. $oP->add_linked_script("../js/wizardhelper.js");
  511. $oP->add_linked_script("../js/wizard.utils.js");
  512. $oP->add_linked_script("../js/linkswidget.js");
  513. $oP->add_linked_script("../js/extkeywidget.js");
  514. $oP->add_linked_script("../js/jquery.blockUI.js");
  515. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  516. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  517. $oP->WizardFormStart('request_form', 4);
  518. $oP->details($aDetails);
  519. // Add hidden fields for known values, enabling dependant attributes to be computed correctly
  520. //
  521. foreach($oRequest->ListChanges() as $sAttCode => $value)
  522. {
  523. if (!in_array($sAttCode, $aList))
  524. {
  525. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  526. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  527. {
  528. $sValue = htmlentities($oRequest->Get($sAttCode), ENT_QUOTES, 'UTF-8');
  529. $oP->add("<input type=\"hidden\" id=\"attr_$sAttCode\" name=\"attr_$sAttCode\" value=\"$sValue\">");
  530. $aFieldsMap[$sAttCode] = 'attr_'.$sAttCode;
  531. }
  532. }
  533. }
  534. if (isset($aParameters['template_id']) && ($aParameters['template_id'] != 0))
  535. {
  536. $oP->add("<input type=\"hidden\" name=\"attr_template_id\" value=\"{$aParameters['template_id']}\">");
  537. }
  538. $oP->add(implode("\n", $aHidden));
  539. $oAttPlugin = new AttachmentPlugIn();
  540. $oAttPlugin->OnDisplayRelations($oRequest, $oP, true /* edit */);
  541. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  542. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_FINISH | BUTTON_CANCEL); //Back button automatically discarded if on the first page
  543. $oP->WizardFormEnd();
  544. $oP->add("</div>\n");
  545. $iFieldsCount = count($aFieldsMap);
  546. $sJsonFieldsMap = json_encode($aFieldsMap);
  547. $oP->add_ready_script(
  548. <<<EOF
  549. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  550. oWizardHelper.SetFieldsCount($iFieldsCount);
  551. // Starts the validation when the page is ready
  552. CheckFields('request_form', false);
  553. $('#request_form').submit( function() {
  554. return OnSubmit('request_form');
  555. });
  556. EOF
  557. );
  558. }
  559. else
  560. {
  561. // User not authorized to use this service ?
  562. //ShowOngoingTickets($oP);
  563. }
  564. }
  565. /**
  566. * Validate the parameters and create the ticket object (based on the page's POSTed parameters)
  567. * @param WebPage $oP The current web page for the output
  568. * @param Organization $oUserOrg The organization of the current user
  569. * @return void
  570. */
  571. function DoCreateRequest($oP, $oUserOrg)
  572. {
  573. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  574. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  575. if (!utils::IsTransactionValid($sTransactionId))
  576. {
  577. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  578. //ShowOngoingTickets($oP);
  579. return;
  580. }
  581. // Validate the parameters
  582. // 1) ServiceCategory
  583. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  584. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  585. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  586. if ($oSet->Count() != 1)
  587. {
  588. // Invalid service for the current user !
  589. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  590. }
  591. $oServiceCategory = $oSet->Fetch();
  592. // 2) Service Subcategory
  593. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  594. RestrictSubcategories($oSearch);
  595. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  596. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  597. if ($oSet->Count() != 1)
  598. {
  599. // Invalid subcategory
  600. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  601. }
  602. $oServiceSubCategory = $oSet->Fetch();
  603. $sClass = ComputeClass($oServiceSubCategory->GetKey());
  604. $oRequest = MetaModel::NewObject($sClass);
  605. $aAttList = array_merge(explode(',', GetConstant($sClass, 'FORM_ATTRIBUTES')), array('service_id', 'servicesubcategory_id'));
  606. $oRequest->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList);
  607. $oRequest->Set('org_id', $oUserOrg->GetKey());
  608. $oRequest->Set('caller_id', UserRights::GetContactId());
  609. if (isset($aParameters['moreinfo']))
  610. {
  611. // There is a template, insert it into the description
  612. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  613. $oRequest->Set($sLogAttCode, $aParameters['moreinfo']);
  614. }
  615. $sTypeAttCode = GetConstant($sClass, 'TYPE');
  616. if (($sTypeAttCode != '') && (PORTAL_SET_TYPE_FROM != ''))
  617. {
  618. $oRequest->Set($sTypeAttCode, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  619. }
  620. if (MetaModel::IsValidAttCode($sClass, 'origin'))
  621. {
  622. $oRequest->Set('origin', 'portal');
  623. }
  624. $oAttPlugin = new AttachmentPlugIn();
  625. $oAttPlugin->OnFormSubmit($oRequest);
  626. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  627. if ($bRes)
  628. {
  629. if (isset($aParameters['template_id']))
  630. {
  631. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id']);
  632. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  633. $oRequest->Set($sLogAttCode, $oTemplate->GetPostedValuesAsText($oRequest)."\n");
  634. $oRequest->DBInsertNoReload();
  635. $oTemplate->RecordExtraDataFromPostedForm($oRequest);
  636. }
  637. else
  638. {
  639. $oRequest->DBInsertNoReload();
  640. }
  641. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName($sClass))."</h1>\n");
  642. //DisplayObject($oP, $oRequest, $oUserOrg);
  643. ShowOngoingTickets($oP);
  644. }
  645. else
  646. {
  647. RequestCreationForm($oP, $oUserOrg);
  648. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  649. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  650. }
  651. }
  652. /**
  653. * Prompts the user for creating a new request
  654. * @param WebPage $oP The current web page
  655. * @return void
  656. */
  657. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  658. {
  659. switch($oP->GetWizardStep())
  660. {
  661. case 0:
  662. default:
  663. SelectServiceCategory($oP, $oUserOrg);
  664. break;
  665. case 1:
  666. SelectServiceSubCategory($oP, $oUserOrg);
  667. break;
  668. case 2:
  669. SelectRequestTemplate($oP, $oUserOrg);
  670. break;
  671. case 3:
  672. RequestCreationForm($oP, $oUserOrg);
  673. break;
  674. case 4:
  675. DoCreateRequest($oP, $oUserOrg);
  676. break;
  677. }
  678. }
  679. /**
  680. * Helper to display lists (UserRequest, Incident, etc.)
  681. * Adjust the presentation depending on the following cases:
  682. * - no item at all
  683. * - items of one class only
  684. * - items of several classes
  685. */
  686. function DisplayRequestLists(WebPage $oP, $aClassToSet)
  687. {
  688. $iNotEmpty = 0; // Count of types for which there are some items to display
  689. foreach ($aClassToSet as $sClass => $oSet)
  690. {
  691. if ($oSet->Count() > 0)
  692. {
  693. $iNotEmpty++;
  694. }
  695. }
  696. if ($iNotEmpty == 0)
  697. {
  698. $oP->p(Dict::S('Portal:NoOpenRequest'));
  699. }
  700. else
  701. {
  702. foreach ($aClassToSet as $sClass => $oSet)
  703. {
  704. if ($iNotEmpty > 1)
  705. {
  706. // Differentiate the sublists
  707. $oP->add("<h2>".MetaModel::GetName($sClass)."</h2>\n");
  708. }
  709. if ($oSet->Count() > 0)
  710. {
  711. $sZList = GetConstant($sClass, 'LIST_ZLIST');
  712. $aZList = explode(',', $sZList);
  713. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  714. }
  715. }
  716. }
  717. }
  718. /**
  719. * Lists all the currently opened User Requests for the current user
  720. * @param WebPage $oP The current web page
  721. * @return void
  722. */
  723. function ListOpenRequests(WebPage $oP)
  724. {
  725. $oUserOrg = GetUserOrg();
  726. $aClassToSet = array();
  727. foreach (GetTicketClasses() as $sClass)
  728. {
  729. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status NOT IN ('closed', 'resolved')";
  730. $oSearch = DBObjectSearch::FromOQL($sOQL);
  731. $iUser = UserRights::GetContactId();
  732. if ($iUser > 0 && !IsPowerUser())
  733. {
  734. $oSearch->AddCondition('caller_id', $iUser);
  735. }
  736. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  737. }
  738. DisplayRequestLists($oP, $aClassToSet);
  739. }
  740. /**
  741. * Lists all the currently resolved (not yet closed) User Requests for the current user
  742. * @param WebPage $oP The current web page
  743. * @return void
  744. */
  745. function ListResolvedRequests(WebPage $oP)
  746. {
  747. $oUserOrg = GetUserOrg();
  748. $aClassToSet = array();
  749. foreach (GetTicketClasses() as $sClass)
  750. {
  751. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status = 'resolved'";
  752. $oSearch = DBObjectSearch::FromOQL($sOQL);
  753. $iUser = UserRights::GetContactId();
  754. if ($iUser > 0 && !IsPowerUser())
  755. {
  756. $oSearch->AddCondition('caller_id', $iUser);
  757. }
  758. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  759. }
  760. DisplayRequestLists($oP, $aClassToSet);
  761. }
  762. /**
  763. * Lists all the currently closed tickets
  764. * @param WebPage $oP The current web page
  765. * @return void
  766. */
  767. function ListClosedTickets(WebPage $oP)
  768. {
  769. $aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
  770. // Remove the caller_id form the search criteria if the user is not a Portal Power User
  771. // since the user is only allowed to see her/his own tickets
  772. foreach($aAttSpecs as $idx => $sAttCode)
  773. {
  774. if (($sAttCode == 'caller_id') && !IsPowerUser())
  775. {
  776. unset($aAttSpecs[$idx]);
  777. }
  778. }
  779. $aClasses = GetTicketClasses();
  780. $sMainClass = reset($aClasses);
  781. $oP->DisplaySearchForm($sMainClass, $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  782. $oUserOrg = GetUserOrg();
  783. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  784. $aClassToSet = array();
  785. foreach (GetTicketClasses() as $sClass)
  786. {
  787. $oSearch = $oP->PostedParamsToFilter($sClass, $aAttSpecs, 'search_');
  788. if(is_null($oSearch))
  789. {
  790. $oSearch = new DBObjectSearch($sClass);
  791. }
  792. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  793. $oSearch->AddCondition('status', 'closed');
  794. $iUser = UserRights::GetContactId();
  795. if ($iUser > 0 && !IsPowerUser())
  796. {
  797. $oSearch->AddCondition('caller_id', $iUser);
  798. }
  799. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch);
  800. }
  801. DisplayRequestLists($oP, $aClassToSet);
  802. }
  803. /**
  804. * Display an object - to be customized
  805. * @param WebPage $oP The current web page
  806. * @param Object $oObj Any kind of object
  807. * @param Object $oUserOrg The organization of the logged in user
  808. * @return void
  809. */
  810. function DisplayObject($oP, $oObj, $oUserOrg)
  811. {
  812. if (in_array(get_class($oObj), GetTicketClasses()))
  813. {
  814. ShowDetailsRequest($oP, $oObj);
  815. }
  816. else
  817. {
  818. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  819. }
  820. }
  821. /**
  822. * Displays the details of a request
  823. * @param WebPage $oP The current web page
  824. * @param Object $oObj The target object
  825. * @return void
  826. */
  827. function ShowDetailsRequest(WebPage $oP, $oObj)
  828. {
  829. $sClass = get_class($oObj);
  830. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  831. $sUserCommentAttCode = GetConstant($sClass, 'USER_COMMENT');
  832. $bIsEscalateButton = false;
  833. $bIsReopenButton = false;
  834. $bIsCloseButton = false;
  835. $bEditAttachments = false;
  836. $aEditAtt = array(); // List of attributes editable in the main form
  837. if (!MetaModel::DBIsReadOnly())
  838. {
  839. switch($oObj->GetState())
  840. {
  841. case 'new':
  842. case 'assigned':
  843. case 'frozen':
  844. case 'pending':
  845. $aEditAtt = array(
  846. $sLogAttCode => '????'
  847. );
  848. $bEditAttachments = true;
  849. // disabled - $bIsEscalateButton = true;
  850. break;
  851. case 'escalated_tto':
  852. case 'escalated_ttr':
  853. $aEditAtt = array(
  854. $sLogAttCode => '????'
  855. );
  856. $bEditAttachments = true;
  857. break;
  858. case 'resolved':
  859. $aEditAtt = array();
  860. if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
  861. {
  862. $bIsReopenButton = true;
  863. MakeStimulusForm($oP, $oObj, 'ev_reopen', array($sLogAttCode));
  864. }
  865. $bIsCloseButton = true;
  866. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', $sUserCommentAttCode));
  867. break;
  868. case 'closed':
  869. case 'closure_requested':
  870. default:
  871. break;
  872. }
  873. }
  874. // REFACTORISER LA MISE EN FORME
  875. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  876. $aAttList = json_decode(GetConstant($sClass, 'DETAILS_ZLIST'), true);
  877. switch($oObj->GetState())
  878. {
  879. case 'closed':
  880. $aAttList['centered'][] = 'user_satisfaction';
  881. $aAttList['centered'][] = $sUserCommentAttCode;
  882. }
  883. // Remove the edited attribute from the shown attributes
  884. //
  885. foreach($aEditAtt as $sAttCode => $foo)
  886. {
  887. foreach($aAttList as $col => $aColumn)
  888. {
  889. if (in_array($sAttCode, $aColumn))
  890. {
  891. if(($index = array_search($sAttCode, $aColumn)) !== false)
  892. {
  893. unset($aAttList[$col][$index]);
  894. }
  895. }
  896. }
  897. }
  898. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  899. $oP->WizardFormStart('request_form', null);
  900. $oP->add('<div id="request_details">');
  901. $oP->add('<table id="request_details_table">');
  902. $oP->add('<tr>');
  903. $oP->add('<td style="vertical-align:top;">');
  904. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  905. $oP->add('</td>');
  906. $oP->add('<td style="vertical-align:top;">');
  907. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  908. $oP->add('</td>');
  909. $oP->add('</tr>');
  910. if (array_key_exists('centered', $aAttList))
  911. {
  912. $oP->add('<tr>');
  913. $oP->add('<td style="vertical-align:top;" colspan="2">');
  914. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  915. $oP->add('</td>');
  916. $oP->add('</tr>');
  917. }
  918. // REFACTORISER
  919. $oP->add('<tr>');
  920. $oP->add('<td colspan="2" style="vertical-align:top;">');
  921. $oAttPlugin = new AttachmentPlugIn();
  922. if ($bEditAttachments)
  923. {
  924. $oAttPlugin->EnableDelete(false);
  925. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  926. }
  927. else
  928. {
  929. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  930. }
  931. $oP->add('</td>');
  932. $oP->add('</tr>');
  933. $oP->add('<tr>');
  934. $oP->add('<td colspan="2" style="vertical-align:top;">');
  935. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  936. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  937. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  938. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  939. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  940. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  941. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  942. $oP->add_script(
  943. <<<EOF
  944. function SetStimulusToApply(sStimulusCode)
  945. {
  946. $('#stimulus_to_apply').val(sStimulusCode);
  947. }
  948. EOF
  949. );
  950. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  951. foreach($aEditAtt as $sAttCode => $foo)
  952. {
  953. $sValue = $oObj->Get($sAttCode);
  954. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  955. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  956. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  957. $sInputId = 'input_'.$sAttCode;
  958. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  959. $aEditFields[$sAttCode] = array(
  960. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  961. 'value' => $sHTMLValue
  962. );
  963. }
  964. foreach($aEditFields as $sAttCode => $aFieldSpec)
  965. {
  966. if ($sAttCode == $sLogAttCode)
  967. {
  968. // Skip, the public log will be displayed below the buttons
  969. continue;
  970. }
  971. $oP->add("<div class=\"edit_item\">");
  972. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  973. $oP->add($aFieldSpec['value']);
  974. $oP->add('</div>');
  975. }
  976. if($bIsReopenButton)
  977. {
  978. $sStimulusCode = 'ev_reopen';
  979. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  980. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  981. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  982. }
  983. if($bIsCloseButton)
  984. {
  985. $sStimulusCode = 'ev_close';
  986. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  987. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  988. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  989. }
  990. elseif (count($aEditAtt) > 0)
  991. {
  992. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  993. }
  994. if ($bIsEscalateButton)
  995. {
  996. $sStimulusCode = 'ev_timeout';
  997. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  998. }
  999. $oP->add('</td>');
  1000. $oP->add('</tr>');
  1001. $oP->add('<tr>');
  1002. $oP->add('<td colspan="2" style="vertical-align:top;">');
  1003. if (isset($aEditFields[$sLogAttCode]))
  1004. {
  1005. $oP->add("<div class=\"edit_item\">");
  1006. $oP->add('<h1>'.$aEditFields[$sLogAttCode]['label'].'</h1>');
  1007. $oP->add($aEditFields[$sLogAttCode]['value']);
  1008. $oP->add('</div>');
  1009. }
  1010. else
  1011. {
  1012. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sLogAttCode).'</h1>');
  1013. $oP->add($oObj->GetAsHTML($sLogAttCode));
  1014. }
  1015. $oP->add('</td>');
  1016. $oP->add('</tr>');
  1017. $oP->add('</table>');
  1018. $oP->add('</div>');
  1019. $oP->WizardFormEnd();
  1020. $oP->add('</div>');
  1021. }
  1022. /**
  1023. * Create form to apply a stimulus
  1024. * @param WebPage $oP The current web page
  1025. * @param Object $oObj The target object
  1026. * @param String $sStimulusCode Stimulus that will be applied
  1027. * @param Array $aEditAtt List of attributes to edit
  1028. * @return void
  1029. */
  1030. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  1031. {
  1032. static $bHasStimulusForm = false;
  1033. $sDialogId = $sStimulusCode."_dialog";
  1034. $sFormId = $sStimulusCode."_form";
  1035. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  1036. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  1037. $sClass = get_class($oObj);
  1038. $oP->add('<form id="'.$sFormId.'" method="post">');
  1039. $sTransactionId = utils::GetNewTransactionId();
  1040. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  1041. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  1042. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  1043. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  1044. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  1045. foreach($aEditAtt as $sAttCode)
  1046. {
  1047. $sValue = $oObj->Get($sAttCode);
  1048. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  1049. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  1050. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1051. $sInputId = 'input_'.$sAttCode;
  1052. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  1053. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  1054. $oP->add($sHTMLValue);
  1055. }
  1056. $oP->add('</form>');
  1057. $oP->add('</div>');
  1058. if (!$bHasStimulusForm)
  1059. {
  1060. $bHasStimulusForm = true;
  1061. $oP->add_script(
  1062. <<<EOF
  1063. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  1064. {
  1065. $('#'+sStimulusCode+'_dialog').dialog({
  1066. height: 'auto',
  1067. width: 'auto',
  1068. modal: true,
  1069. title: sTitle,
  1070. buttons: [
  1071. { text: sOkButtonLabel, click: function() {
  1072. $(this).find('#'+sStimulusCode+'_form').submit();
  1073. } },
  1074. { text: "$sCancelButtonLabel", click: function() {
  1075. $(this).dialog( "close" );
  1076. } },
  1077. ],
  1078. });
  1079. }
  1080. EOF
  1081. );
  1082. }
  1083. }
  1084. /**
  1085. * Get The organization of the current user (i.e. the organization of its contact)
  1086. * @param WebPage $oP The current page, for errors output
  1087. * @return Organization The user's org or null in case of problem...
  1088. */
  1089. function GetUserOrg()
  1090. {
  1091. $oOrg = null;
  1092. $iContactId = UserRights::GetContactId();
  1093. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  1094. if (is_object($oContact))
  1095. {
  1096. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  1097. }
  1098. else
  1099. {
  1100. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  1101. }
  1102. return $oOrg;
  1103. }
  1104. /**
  1105. * Determine if the current user can be considered as being a portal power user
  1106. */
  1107. function IsPowerUSer()
  1108. {
  1109. $iUserID = UserRights::GetUserId();
  1110. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  1111. $oProfileSet = new DBObjectSet(
  1112. DBObjectSearch::FromOQL($sOQLprofile),
  1113. array(),
  1114. array(
  1115. 'user' => $iUserID,
  1116. 'profile' => PORTAL_POWER_USER_PROFILE,
  1117. )
  1118. );
  1119. $bRes = ($oProfileSet->count() > 0);
  1120. return $bRes;
  1121. }
  1122. ///////////////////////////////////////////////////////////////////////////////
  1123. //
  1124. // Main program
  1125. //
  1126. ///////////////////////////////////////////////////////////////////////////////
  1127. try
  1128. {
  1129. require_once(APPROOT.'/application/startup.inc.php');
  1130. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  1131. $oAppContext = new ApplicationContext();
  1132. $sOperation = utils::ReadParam('operation', '');
  1133. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  1134. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  1135. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  1136. $aClasses = explode(',', MetaModel::GetConfig()->Get('portal_tickets'));
  1137. $sMainClass = trim(reset($aClasses));
  1138. if (!class_exists($sMainClass))
  1139. {
  1140. $oP = new WebPage(Dict::S('Portal:Title'));
  1141. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  1142. }
  1143. else
  1144. {
  1145. $oUserOrg = GetUserOrg();
  1146. $sCode = $oUserOrg->Get('code');
  1147. $sAlternateStylesheet = '';
  1148. if (@file_exists("./$sCode/portal.css"))
  1149. {
  1150. $sAlternateStylesheet = "$sCode";
  1151. }
  1152. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  1153. $oP->EnableDisconnectButton(utils::CanLogOff());
  1154. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  1155. if (is_object($oUserOrg))
  1156. {
  1157. switch($sOperation)
  1158. {
  1159. case 'show_closed':
  1160. $oP->set_title(Dict::S('Portal:ShowClosed'));
  1161. DisplayMainMenu($oP);
  1162. ShowClosedTickets($oP);
  1163. break;
  1164. case 'create_request':
  1165. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  1166. DisplayMainMenu($oP);
  1167. if (!MetaModel::DBIsReadOnly())
  1168. {
  1169. CreateRequest($oP, $oUserOrg);
  1170. }
  1171. break;
  1172. case 'details':
  1173. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1174. DisplayMainMenu($oP);
  1175. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1176. ValidateObject($oObj);
  1177. DisplayObject($oP, $oObj, $oUserOrg);
  1178. break;
  1179. case 'update_request':
  1180. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1181. DisplayMainMenu($oP);
  1182. if (!MetaModel::DBIsReadOnly())
  1183. {
  1184. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1185. ValidateObject($oObj);
  1186. $aAttList = array(
  1187. GetConstant(get_class($oObj), 'PUBLIC_LOG'),
  1188. 'user_satisfaction',
  1189. GetConstant(get_class($oObj), 'USER_COMMENT')
  1190. );
  1191. try
  1192. {
  1193. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  1194. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  1195. }
  1196. catch(TransactionException $e)
  1197. {
  1198. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  1199. }
  1200. DisplayObject($oP, $oObj, $oUserOrg);
  1201. }
  1202. break;
  1203. case 'show_ongoing':
  1204. default:
  1205. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  1206. DisplayMainMenu($oP);
  1207. ShowOngoingTickets($oP);
  1208. }
  1209. }
  1210. }
  1211. $oP->output();
  1212. }
  1213. catch(CoreException $e)
  1214. {
  1215. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1216. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1217. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1218. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  1219. //$oP->p($e->getTraceAsString());
  1220. $oP->output();
  1221. if (MetaModel::IsLogEnabledIssue())
  1222. {
  1223. if (MetaModel::IsValidClass('EventIssue'))
  1224. {
  1225. try
  1226. {
  1227. $oLog = new EventIssue();
  1228. $oLog->Set('message', $e->getMessage());
  1229. $oLog->Set('userinfo', '');
  1230. $oLog->Set('issue', $e->GetIssue());
  1231. $oLog->Set('impact', 'Page could not be displayed');
  1232. $oLog->Set('callstack', $e->getTrace());
  1233. $oLog->Set('data', $e->getContextData());
  1234. $oLog->DBInsertNoReload();
  1235. }
  1236. catch(Exception $e)
  1237. {
  1238. IssueLog::Error("Failed to log issue into the DB");
  1239. }
  1240. }
  1241. IssueLog::Error($e->getMessage());
  1242. }
  1243. // For debugging only
  1244. //throw $e;
  1245. }
  1246. catch(Exception $e)
  1247. {
  1248. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1249. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1250. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1251. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  1252. //$oP->p($e->getTraceAsString());
  1253. $oP->output();
  1254. if (MetaModel::IsLogEnabledIssue())
  1255. {
  1256. if (MetaModel::IsValidClass('EventIssue'))
  1257. {
  1258. try
  1259. {
  1260. $oLog = new EventIssue();
  1261. $oLog->Set('message', $e->getMessage());
  1262. $oLog->Set('userinfo', '');
  1263. $oLog->Set('issue', 'PHP Exception');
  1264. $oLog->Set('impact', 'Page could not be displayed');
  1265. $oLog->Set('callstack', $e->getTrace());
  1266. $oLog->Set('data', array());
  1267. $oLog->DBInsertNoReload();
  1268. }
  1269. catch(Exception $e)
  1270. {
  1271. IssueLog::Error("Failed to log issue into the DB");
  1272. }
  1273. }
  1274. IssueLog::Error($e->getMessage());
  1275. }
  1276. }
  1277. ?>