index.php 43 KB

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