index.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. $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. foreach (GetTicketClasses() as $sClass)
  725. {
  726. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status NOT IN ('closed', 'resolved')";
  727. $oSearch = DBObjectSearch::FromOQL($sOQL);
  728. $iUser = UserRights::GetContactId();
  729. if ($iUser > 0 && !IsPowerUser())
  730. {
  731. $oSearch->AddCondition('caller_id', $iUser);
  732. }
  733. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  734. }
  735. DisplayRequestLists($oP, $aClassToSet);
  736. }
  737. /**
  738. * Lists all the currently resolved (not yet closed) User Requests for the current user
  739. * @param WebPage $oP The current web page
  740. * @return void
  741. */
  742. function ListResolvedRequests(WebPage $oP)
  743. {
  744. $oUserOrg = GetUserOrg();
  745. $aClassToSet = array();
  746. foreach (GetTicketClasses() as $sClass)
  747. {
  748. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status = 'resolved'";
  749. $oSearch = DBObjectSearch::FromOQL($sOQL);
  750. $iUser = UserRights::GetContactId();
  751. if ($iUser > 0 && !IsPowerUser())
  752. {
  753. $oSearch->AddCondition('caller_id', $iUser);
  754. }
  755. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  756. }
  757. DisplayRequestLists($oP, $aClassToSet);
  758. }
  759. /**
  760. * Lists all the currently closed tickets
  761. * @param WebPage $oP The current web page
  762. * @return void
  763. */
  764. function ListClosedTickets(WebPage $oP)
  765. {
  766. $aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
  767. // Remove the caller_id form the search criteria if the user is not a Portal Power User
  768. // since the user is only allowed to see her/his own tickets
  769. foreach($aAttSpecs as $idx => $sAttCode)
  770. {
  771. if (($sAttCode == 'caller_id') && !IsPowerUser())
  772. {
  773. unset($aAttSpecs[$idx]);
  774. }
  775. }
  776. $aClasses = GetTicketClasses();
  777. $sMainClass = reset($aClasses);
  778. $oP->DisplaySearchForm($sMainClass, $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  779. $oUserOrg = GetUserOrg();
  780. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  781. $aClassToSet = array();
  782. foreach (GetTicketClasses() as $sClass)
  783. {
  784. $oSearch = $oP->PostedParamsToFilter($sClass, $aAttSpecs, 'search_');
  785. if(is_null($oSearch))
  786. {
  787. $oSearch = new DBObjectSearch($sClass);
  788. }
  789. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  790. $oSearch->AddCondition('status', 'closed');
  791. $iUser = UserRights::GetContactId();
  792. if ($iUser > 0 && !IsPowerUser())
  793. {
  794. $oSearch->AddCondition('caller_id', $iUser);
  795. }
  796. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch);
  797. }
  798. DisplayRequestLists($oP, $aClassToSet);
  799. }
  800. /**
  801. * Display an object - to be customized
  802. * @param WebPage $oP The current web page
  803. * @param Object $oObj Any kind of object
  804. * @param Object $oUserOrg The organization of the logged in user
  805. * @return void
  806. */
  807. function DisplayObject($oP, $oObj, $oUserOrg)
  808. {
  809. if (in_array(get_class($oObj), GetTicketClasses()))
  810. {
  811. ShowDetailsRequest($oP, $oObj);
  812. }
  813. else
  814. {
  815. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  816. }
  817. }
  818. /**
  819. * Displays the details of a request
  820. * @param WebPage $oP The current web page
  821. * @param Object $oObj The target object
  822. * @return void
  823. */
  824. function ShowDetailsRequest(WebPage $oP, $oObj)
  825. {
  826. $sClass = get_class($oObj);
  827. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  828. $sUserCommentAttCode = GetConstant($sClass, 'USER_COMMENT');
  829. $bIsEscalateButton = false;
  830. $bIsReopenButton = false;
  831. $bIsCloseButton = false;
  832. $bEditAttachments = false;
  833. $aEditAtt = array(); // List of attributes editable in the main form
  834. if (!MetaModel::DBIsReadOnly())
  835. {
  836. switch($oObj->GetState())
  837. {
  838. case 'new':
  839. case 'assigned':
  840. case 'frozen':
  841. case 'pending':
  842. $aEditAtt = array(
  843. $sLogAttCode => '????'
  844. );
  845. $bEditAttachments = true;
  846. // disabled - $bIsEscalateButton = true;
  847. break;
  848. case 'escalated_tto':
  849. case 'escalated_ttr':
  850. $aEditAtt = array(
  851. $sLogAttCode => '????'
  852. );
  853. $bEditAttachments = true;
  854. break;
  855. case 'resolved':
  856. $aEditAtt = array();
  857. if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
  858. {
  859. $bIsReopenButton = true;
  860. MakeStimulusForm($oP, $oObj, 'ev_reopen', array($sLogAttCode));
  861. }
  862. $bIsCloseButton = true;
  863. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', $sUserCommentAttCode));
  864. break;
  865. case 'closed':
  866. case 'closure_requested':
  867. default:
  868. break;
  869. }
  870. }
  871. // REFACTORISER LA MISE EN FORME
  872. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  873. $aAttList = json_decode(GetConstant($sClass, 'DETAILS_ZLIST'), true);
  874. switch($oObj->GetState())
  875. {
  876. case 'closed':
  877. $aAttList['centered'][] = 'user_satisfaction';
  878. $aAttList['centered'][] = $sUserCommentAttCode;
  879. }
  880. // Remove the edited attribute from the shown attributes
  881. //
  882. foreach($aEditAtt as $sAttCode => $foo)
  883. {
  884. foreach($aAttList as $col => $aColumn)
  885. {
  886. if (in_array($sAttCode, $aColumn))
  887. {
  888. if(($index = array_search($sAttCode, $aColumn)) !== false)
  889. {
  890. unset($aAttList[$col][$index]);
  891. }
  892. }
  893. }
  894. }
  895. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  896. $oP->WizardFormStart('request_form', null);
  897. $oP->add('<div id="request_details">');
  898. $oP->add('<table id="request_details_table">');
  899. $oP->add('<tr>');
  900. $oP->add('<td style="vertical-align:top;">');
  901. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  902. $oP->add('</td>');
  903. $oP->add('<td style="vertical-align:top;">');
  904. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  905. $oP->add('</td>');
  906. $oP->add('</tr>');
  907. if (array_key_exists('centered', $aAttList))
  908. {
  909. $oP->add('<tr>');
  910. $oP->add('<td style="vertical-align:top;" colspan="2">');
  911. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  912. $oP->add('</td>');
  913. $oP->add('</tr>');
  914. }
  915. // REFACTORISER
  916. $oP->add('<tr>');
  917. $oP->add('<td colspan="2" style="vertical-align:top;">');
  918. $oAttPlugin = new AttachmentPlugIn();
  919. if ($bEditAttachments)
  920. {
  921. $oAttPlugin->EnableDelete(false);
  922. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  923. }
  924. else
  925. {
  926. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  927. }
  928. $oP->add('</td>');
  929. $oP->add('</tr>');
  930. $oP->add('<tr>');
  931. $oP->add('<td colspan="2" style="vertical-align:top;">');
  932. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  933. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  934. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  935. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  936. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  937. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  938. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  939. $oP->add_script(
  940. <<<EOF
  941. function SetStimulusToApply(sStimulusCode)
  942. {
  943. $('#stimulus_to_apply').val(sStimulusCode);
  944. }
  945. EOF
  946. );
  947. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  948. foreach($aEditAtt as $sAttCode => $foo)
  949. {
  950. $sValue = $oObj->Get($sAttCode);
  951. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  952. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  953. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  954. $sInputId = 'input_'.$sAttCode;
  955. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  956. $aEditFields[$sAttCode] = array(
  957. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  958. 'value' => $sHTMLValue
  959. );
  960. }
  961. foreach($aEditFields as $sAttCode => $aFieldSpec)
  962. {
  963. if ($sAttCode == $sLogAttCode)
  964. {
  965. // Skip, the public log will be displayed below the buttons
  966. continue;
  967. }
  968. $oP->add("<div class=\"edit_item\">");
  969. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  970. $oP->add($aFieldSpec['value']);
  971. $oP->add('</div>');
  972. }
  973. if($bIsReopenButton)
  974. {
  975. $sStimulusCode = 'ev_reopen';
  976. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  977. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  978. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  979. }
  980. if($bIsCloseButton)
  981. {
  982. $sStimulusCode = 'ev_close';
  983. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  984. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  985. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  986. }
  987. elseif (count($aEditAtt) > 0)
  988. {
  989. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  990. }
  991. if ($bIsEscalateButton)
  992. {
  993. $sStimulusCode = 'ev_timeout';
  994. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  995. }
  996. $oP->add('</td>');
  997. $oP->add('</tr>');
  998. $oP->add('<tr>');
  999. $oP->add('<td colspan="2" style="vertical-align:top;">');
  1000. if (isset($aEditFields[$sLogAttCode]))
  1001. {
  1002. $oP->add("<div class=\"edit_item\">");
  1003. $oP->add('<h1>'.$aEditFields[$sLogAttCode]['label'].'</h1>');
  1004. $oP->add($aEditFields[$sLogAttCode]['value']);
  1005. $oP->add('</div>');
  1006. }
  1007. else
  1008. {
  1009. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sLogAttCode).'</h1>');
  1010. $oP->add($oObj->GetAsHTML($sLogAttCode));
  1011. }
  1012. $oP->add('</td>');
  1013. $oP->add('</tr>');
  1014. $oP->add('</table>');
  1015. $oP->add('</div>');
  1016. $oP->WizardFormEnd();
  1017. $oP->add('</div>');
  1018. }
  1019. /**
  1020. * Create form to apply a stimulus
  1021. * @param WebPage $oP The current web page
  1022. * @param Object $oObj The target object
  1023. * @param String $sStimulusCode Stimulus that will be applied
  1024. * @param Array $aEditAtt List of attributes to edit
  1025. * @return void
  1026. */
  1027. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  1028. {
  1029. static $bHasStimulusForm = false;
  1030. $sDialogId = $sStimulusCode."_dialog";
  1031. $sFormId = $sStimulusCode."_form";
  1032. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  1033. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  1034. $sClass = get_class($oObj);
  1035. $oP->add('<form id="'.$sFormId.'" method="post">');
  1036. $sTransactionId = utils::GetNewTransactionId();
  1037. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  1038. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  1039. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  1040. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  1041. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  1042. foreach($aEditAtt as $sAttCode)
  1043. {
  1044. $sValue = $oObj->Get($sAttCode);
  1045. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  1046. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  1047. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1048. $sInputId = 'input_'.$sAttCode;
  1049. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  1050. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  1051. $oP->add($sHTMLValue);
  1052. }
  1053. $oP->add('</form>');
  1054. $oP->add('</div>');
  1055. if (!$bHasStimulusForm)
  1056. {
  1057. $bHasStimulusForm = true;
  1058. $oP->add_script(
  1059. <<<EOF
  1060. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  1061. {
  1062. $('#'+sStimulusCode+'_dialog').dialog({
  1063. height: 'auto',
  1064. width: 'auto',
  1065. modal: true,
  1066. title: sTitle,
  1067. buttons: [
  1068. { text: sOkButtonLabel, click: function() {
  1069. $(this).find('#'+sStimulusCode+'_form').submit();
  1070. } },
  1071. { text: "$sCancelButtonLabel", click: function() {
  1072. $(this).dialog( "close" );
  1073. } },
  1074. ],
  1075. });
  1076. }
  1077. EOF
  1078. );
  1079. }
  1080. }
  1081. /**
  1082. * Get The organization of the current user (i.e. the organization of its contact)
  1083. * @param WebPage $oP The current page, for errors output
  1084. * @return Organization The user's org or null in case of problem...
  1085. */
  1086. function GetUserOrg()
  1087. {
  1088. $oOrg = null;
  1089. $iContactId = UserRights::GetContactId();
  1090. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  1091. if (is_object($oContact))
  1092. {
  1093. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  1094. }
  1095. else
  1096. {
  1097. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  1098. }
  1099. return $oOrg;
  1100. }
  1101. /**
  1102. * Determine if the current user can be considered as being a portal power user
  1103. */
  1104. function IsPowerUSer()
  1105. {
  1106. $iUserID = UserRights::GetUserId();
  1107. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  1108. $oProfileSet = new DBObjectSet(
  1109. DBObjectSearch::FromOQL($sOQLprofile),
  1110. array(),
  1111. array(
  1112. 'user' => $iUserID,
  1113. 'profile' => PORTAL_POWER_USER_PROFILE,
  1114. )
  1115. );
  1116. $bRes = ($oProfileSet->count() > 0);
  1117. return $bRes;
  1118. }
  1119. ///////////////////////////////////////////////////////////////////////////////
  1120. //
  1121. // Main program
  1122. //
  1123. ///////////////////////////////////////////////////////////////////////////////
  1124. try
  1125. {
  1126. require_once(APPROOT.'/application/startup.inc.php');
  1127. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  1128. $oAppContext = new ApplicationContext();
  1129. $sOperation = utils::ReadParam('operation', '');
  1130. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  1131. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  1132. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  1133. $aClasses = explode(',', MetaModel::GetConfig()->Get('portal_tickets'));
  1134. $sMainClass = trim(reset($aClasses));
  1135. if (!class_exists($sMainClass))
  1136. {
  1137. $oP = new WebPage(Dict::S('Portal:Title'));
  1138. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  1139. }
  1140. else
  1141. {
  1142. $oUserOrg = GetUserOrg();
  1143. $sCode = $oUserOrg->Get('code');
  1144. $sAlternateStylesheet = '';
  1145. if (@file_exists("./$sCode/portal.css"))
  1146. {
  1147. $sAlternateStylesheet = "$sCode";
  1148. }
  1149. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  1150. $oP->EnableDisconnectButton(utils::CanLogOff());
  1151. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  1152. if (is_object($oUserOrg))
  1153. {
  1154. switch($sOperation)
  1155. {
  1156. case 'show_closed':
  1157. $oP->set_title(Dict::S('Portal:ShowClosed'));
  1158. DisplayMainMenu($oP);
  1159. ShowClosedTickets($oP);
  1160. break;
  1161. case 'create_request':
  1162. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  1163. DisplayMainMenu($oP);
  1164. if (!MetaModel::DBIsReadOnly())
  1165. {
  1166. CreateRequest($oP, $oUserOrg);
  1167. }
  1168. break;
  1169. case 'details':
  1170. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1171. DisplayMainMenu($oP);
  1172. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1173. ValidateObject($oObj);
  1174. DisplayObject($oP, $oObj, $oUserOrg);
  1175. break;
  1176. case 'update_request':
  1177. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1178. DisplayMainMenu($oP);
  1179. if (!MetaModel::DBIsReadOnly())
  1180. {
  1181. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1182. ValidateObject($oObj);
  1183. $aAttList = array(
  1184. GetConstant(get_class($oObj), 'PUBLIC_LOG'),
  1185. 'user_satisfaction',
  1186. GetConstant(get_class($oObj), 'USER_COMMENT')
  1187. );
  1188. try
  1189. {
  1190. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  1191. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  1192. }
  1193. catch(TransactionException $e)
  1194. {
  1195. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  1196. }
  1197. DisplayObject($oP, $oObj, $oUserOrg);
  1198. }
  1199. break;
  1200. case 'show_ongoing':
  1201. default:
  1202. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  1203. DisplayMainMenu($oP);
  1204. ShowOngoingTickets($oP);
  1205. }
  1206. }
  1207. }
  1208. $oP->output();
  1209. }
  1210. catch(CoreException $e)
  1211. {
  1212. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1213. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1214. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1215. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  1216. //$oP->p($e->getTraceAsString());
  1217. $oP->output();
  1218. if (MetaModel::IsLogEnabledIssue())
  1219. {
  1220. if (MetaModel::IsValidClass('EventIssue'))
  1221. {
  1222. try
  1223. {
  1224. $oLog = new EventIssue();
  1225. $oLog->Set('message', $e->getMessage());
  1226. $oLog->Set('userinfo', '');
  1227. $oLog->Set('issue', $e->GetIssue());
  1228. $oLog->Set('impact', 'Page could not be displayed');
  1229. $oLog->Set('callstack', $e->getTrace());
  1230. $oLog->Set('data', $e->getContextData());
  1231. $oLog->DBInsertNoReload();
  1232. }
  1233. catch(Exception $e)
  1234. {
  1235. IssueLog::Error("Failed to log issue into the DB");
  1236. }
  1237. }
  1238. IssueLog::Error($e->getMessage());
  1239. }
  1240. // For debugging only
  1241. //throw $e;
  1242. }
  1243. catch(Exception $e)
  1244. {
  1245. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1246. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1247. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1248. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  1249. //$oP->p($e->getTraceAsString());
  1250. $oP->output();
  1251. if (MetaModel::IsLogEnabledIssue())
  1252. {
  1253. if (MetaModel::IsValidClass('EventIssue'))
  1254. {
  1255. try
  1256. {
  1257. $oLog = new EventIssue();
  1258. $oLog->Set('message', $e->getMessage());
  1259. $oLog->Set('userinfo', '');
  1260. $oLog->Set('issue', 'PHP Exception');
  1261. $oLog->Set('impact', 'Page could not be displayed');
  1262. $oLog->Set('callstack', $e->getTrace());
  1263. $oLog->Set('data', array());
  1264. $oLog->DBInsertNoReload();
  1265. }
  1266. catch(Exception $e)
  1267. {
  1268. IssueLog::Error("Failed to log issue into the DB");
  1269. }
  1270. }
  1271. IssueLog::Error($e->getMessage());
  1272. }
  1273. }
  1274. ?>