index.php 46 KB

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