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