index.php 43 KB

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