index.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. // Enable the button to upload images (or cut and paste of images)
  556. $oP->add_ready_script(InlineImage::EnableCKEditorImageUpload($oRequest, utils::GetUploadTempId($oP->GetTransactionId())));
  557. }
  558. else
  559. {
  560. // User not authorized to use this service ?
  561. //ShowOngoingTickets($oP);
  562. }
  563. }
  564. /**
  565. * Validate the parameters and create the ticket object (based on the page's POSTed parameters)
  566. * @param WebPage $oP The current web page for the output
  567. * @param Organization $oUserOrg The organization of the current user
  568. * @return void
  569. */
  570. function DoCreateRequest($oP, $oUserOrg)
  571. {
  572. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS.',template_id');
  573. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  574. if (!utils::IsTransactionValid($sTransactionId))
  575. {
  576. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  577. //ShowOngoingTickets($oP);
  578. return;
  579. }
  580. // Validate the parameters
  581. // 1) ServiceCategory
  582. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  583. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  584. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  585. if ($oSet->Count() != 1)
  586. {
  587. // Invalid service for the current user !
  588. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  589. }
  590. $oServiceCategory = $oSet->Fetch();
  591. // 2) Service Subcategory
  592. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  593. RestrictSubcategories($oSearch);
  594. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  595. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  596. if ($oSet->Count() != 1)
  597. {
  598. // Invalid subcategory
  599. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  600. }
  601. $oServiceSubCategory = $oSet->Fetch();
  602. $sClass = ComputeClass($oServiceSubCategory->GetKey());
  603. $oRequest = MetaModel::NewObject($sClass);
  604. $aAttList = array_merge(explode(',', GetConstant($sClass, 'FORM_ATTRIBUTES')), array('service_id', 'servicesubcategory_id'));
  605. $oRequest->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList);
  606. $oRequest->Set('org_id', $oUserOrg->GetKey());
  607. $oRequest->Set('caller_id', UserRights::GetContactId());
  608. if (isset($aParameters['moreinfo']))
  609. {
  610. // There is a template, insert it into the description
  611. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  612. $oRequest->Set($sLogAttCode, $aParameters['moreinfo']);
  613. }
  614. $sTypeAttCode = GetConstant($sClass, 'TYPE');
  615. if (($sTypeAttCode != '') && (PORTAL_SET_TYPE_FROM != ''))
  616. {
  617. $oRequest->Set($sTypeAttCode, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  618. }
  619. if (MetaModel::IsValidAttCode($sClass, 'origin'))
  620. {
  621. $oRequest->Set('origin', 'portal');
  622. }
  623. $oAttPlugin = new AttachmentPlugIn();
  624. $oAttPlugin->OnFormSubmit($oRequest);
  625. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  626. if ($bRes)
  627. {
  628. if (isset($aParameters['template_id']))
  629. {
  630. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id']);
  631. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  632. $oRequest->Set($sLogAttCode, $oTemplate->GetPostedValuesAsText($oRequest)."\n");
  633. $oRequest->DBInsertNoReload();
  634. $oTemplate->RecordExtraDataFromPostedForm($oRequest);
  635. }
  636. else
  637. {
  638. $oRequest->DBInsertNoReload();
  639. }
  640. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName($sClass))."</h1>\n");
  641. //DisplayObject($oP, $oRequest, $oUserOrg);
  642. ShowOngoingTickets($oP);
  643. }
  644. else
  645. {
  646. RequestCreationForm($oP, $oUserOrg);
  647. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  648. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  649. }
  650. }
  651. /**
  652. * Prompts the user for creating a new request
  653. * @param WebPage $oP The current web page
  654. * @return void
  655. */
  656. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  657. {
  658. switch($oP->GetWizardStep())
  659. {
  660. case 0:
  661. default:
  662. SelectServiceCategory($oP, $oUserOrg);
  663. break;
  664. case 1:
  665. SelectServiceSubCategory($oP, $oUserOrg);
  666. break;
  667. case 2:
  668. SelectRequestTemplate($oP, $oUserOrg);
  669. break;
  670. case 3:
  671. RequestCreationForm($oP, $oUserOrg);
  672. break;
  673. case 4:
  674. DoCreateRequest($oP, $oUserOrg);
  675. break;
  676. }
  677. }
  678. /**
  679. * Helper to display lists (UserRequest, Incident, etc.)
  680. * Adjust the presentation depending on the following cases:
  681. * - no item at all
  682. * - items of one class only
  683. * - items of several classes
  684. */
  685. function DisplayRequestLists(WebPage $oP, $aClassToSet)
  686. {
  687. $iNotEmpty = 0; // Count of types for which there are some items to display
  688. foreach ($aClassToSet as $sClass => $oSet)
  689. {
  690. if ($oSet->Count() > 0)
  691. {
  692. $iNotEmpty++;
  693. }
  694. }
  695. if ($iNotEmpty == 0)
  696. {
  697. $oP->p(Dict::S('Portal:NoOpenRequest'));
  698. }
  699. else
  700. {
  701. foreach ($aClassToSet as $sClass => $oSet)
  702. {
  703. if ($iNotEmpty > 1)
  704. {
  705. // Differentiate the sublists
  706. $oP->add("<h2>".MetaModel::GetName($sClass)."</h2>\n");
  707. }
  708. if ($oSet->Count() > 0)
  709. {
  710. $sZList = GetConstant($sClass, 'LIST_ZLIST');
  711. $aZList = explode(',', $sZList);
  712. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  713. }
  714. }
  715. }
  716. }
  717. /**
  718. * Lists all the currently opened User Requests for the current user
  719. * @param WebPage $oP The current web page
  720. * @return void
  721. */
  722. function ListOpenRequests(WebPage $oP)
  723. {
  724. $oUserOrg = GetUserOrg();
  725. $aClassToSet = array();
  726. $iUser = UserRights::GetContactId();
  727. $oContact = MetaModel::GetObject('Contact', UserRights::GetContactId());
  728. foreach (GetTicketClasses() as $sClass)
  729. {
  730. if (IsPowerUser())
  731. {
  732. $sValidationDefine = 'PORTAL_'.strtoupper($sClass).'_DISPLAY_POWERUSER_QUERY';
  733. }
  734. else
  735. {
  736. $sValidationDefine = 'PORTAL_'.strtoupper($sClass).'_DISPLAY_QUERY';
  737. }
  738. if (defined($sValidationDefine))
  739. {
  740. $sOQL = constant($sValidationDefine);
  741. $oSearch = DBObjectSearch::FromOQL($sOQL);
  742. $sOQLCondition = $oSearch->GetClassAlias().".status NOT IN ('closed', 'resolved')";
  743. $oExpr = Expression::FromOQL($sOQLCondition);
  744. $oSearch->AddConditionExpression($oExpr);
  745. }
  746. else
  747. {
  748. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status NOT IN ('closed', 'resolved')";
  749. $oSearch = DBObjectSearch::FromOQL($sOQL);
  750. if ($iUser > 0 && !IsPowerUser())
  751. {
  752. $oSearch->AddCondition('caller_id', $iUser);
  753. }
  754. }
  755. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey(), 'caller_id' => $iUser, 'contact' => $oContact));
  756. }
  757. DisplayRequestLists($oP, $aClassToSet);
  758. }
  759. /**
  760. * Lists all the currently resolved (not yet closed) User Requests for the current user
  761. * @param WebPage $oP The current web page
  762. * @return void
  763. */
  764. function ListResolvedRequests(WebPage $oP)
  765. {
  766. $oUserOrg = GetUserOrg();
  767. $aClassToSet = array();
  768. foreach (GetTicketClasses() as $sClass)
  769. {
  770. $sOQL = "SELECT $sClass WHERE org_id = :org_id AND status = 'resolved'";
  771. $oSearch = DBObjectSearch::FromOQL($sOQL);
  772. $iUser = UserRights::GetContactId();
  773. if ($iUser > 0 && !IsPowerUser())
  774. {
  775. $oSearch->AddCondition('caller_id', $iUser);
  776. }
  777. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  778. }
  779. DisplayRequestLists($oP, $aClassToSet);
  780. }
  781. /**
  782. * Lists all the currently closed tickets
  783. * @param WebPage $oP The current web page
  784. * @return void
  785. */
  786. function ListClosedTickets(WebPage $oP)
  787. {
  788. $aAttSpecs = explode(',', PORTAL_TICKETS_SEARCH_CRITERIA);
  789. // Remove the caller_id form the search criteria if the user is not a Portal Power User
  790. // since the user is only allowed to see her/his own tickets
  791. foreach($aAttSpecs as $idx => $sAttCode)
  792. {
  793. if (($sAttCode == 'caller_id') && !IsPowerUser())
  794. {
  795. unset($aAttSpecs[$idx]);
  796. }
  797. }
  798. $aClasses = GetTicketClasses();
  799. $sMainClass = reset($aClasses);
  800. $oP->DisplaySearchForm($sMainClass, $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  801. $oUserOrg = GetUserOrg();
  802. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  803. $aClassToSet = array();
  804. foreach (GetTicketClasses() as $sClass)
  805. {
  806. $oSearch = $oP->PostedParamsToFilter($sClass, $aAttSpecs, 'search_');
  807. if(is_null($oSearch))
  808. {
  809. $oSearch = new DBObjectSearch($sClass);
  810. }
  811. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  812. $oSearch->AddCondition('status', 'closed');
  813. $iUser = UserRights::GetContactId();
  814. if ($iUser > 0 && !IsPowerUser())
  815. {
  816. $oSearch->AddCondition('caller_id', $iUser);
  817. }
  818. $aClassToSet[$sClass] = new CMDBObjectSet($oSearch);
  819. }
  820. DisplayRequestLists($oP, $aClassToSet);
  821. }
  822. /**
  823. * Display an object - to be customized
  824. * @param WebPage $oP The current web page
  825. * @param Object $oObj Any kind of object
  826. * @param Object $oUserOrg The organization of the logged in user
  827. * @return void
  828. */
  829. function DisplayObject($oP, $oObj, $oUserOrg)
  830. {
  831. if (in_array(get_class($oObj), GetTicketClasses()))
  832. {
  833. ShowDetailsRequest($oP, $oObj);
  834. }
  835. else
  836. {
  837. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  838. }
  839. }
  840. /**
  841. * Displays the details of a request
  842. * @param WebPage $oP The current web page
  843. * @param Object $oObj The target object
  844. * @return void
  845. */
  846. function ShowDetailsRequest(WebPage $oP, $oObj)
  847. {
  848. $sClass = get_class($oObj);
  849. $sLogAttCode = GetConstant($sClass, 'PUBLIC_LOG');
  850. $sUserCommentAttCode = GetConstant($sClass, 'USER_COMMENT');
  851. $bIsReopenButton = false;
  852. $bIsCloseButton = false;
  853. $bIsEscalateButton = false;
  854. $bEditAttachments = false;
  855. $aEditAtt = array(); // List of attributes editable in the main form
  856. if (!MetaModel::DBIsReadOnly())
  857. {
  858. switch($oObj->GetState())
  859. {
  860. case 'resolved':
  861. $aEditAtt = array();
  862. $aTransitions = $oObj->EnumTransitions();
  863. $oSet = DBObjectSet::FromObject($oObj);
  864. // Add the "Reopen" button if this is valid action
  865. if (array_key_exists('ev_reopen', $aTransitions) && UserRights::IsStimulusAllowed($sClass, 'ev_reopen', $oSet))
  866. {
  867. $bIsReopenButton = true;
  868. MakeStimulusForm($oP, $oObj, 'ev_reopen', array($sLogAttCode));
  869. }
  870. // Add the "Close" button if this is valid action
  871. if (array_key_exists('ev_close', $aTransitions) && UserRights::IsStimulusAllowed($sClass, 'ev_close', $oSet))
  872. {
  873. $bIsCloseButton = true;
  874. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', $sUserCommentAttCode));
  875. }
  876. break;
  877. case 'closed':
  878. // By convention 'closed' is the final state of a ticket and nothing can be done in such a state
  879. break;
  880. default:
  881. // In all other states, the only possible action is to update the ticket (both the case log and the attachments)
  882. // This update is possible only if the case log field is not read-only or hidden in the current state
  883. $iFlags = $oObj->GetAttributeFlags($sLogAttCode);
  884. $bReadOnly = (($iFlags & (OPT_ATT_READONLY | OPT_ATT_HIDDEN)) != 0);
  885. if ($bReadOnly)
  886. {
  887. $aEditAtt = array();
  888. $bEditAttachments = false;
  889. }
  890. else
  891. {
  892. $aEditAtt = array(
  893. $sLogAttCode => '????'
  894. );
  895. $bEditAttachments = true;
  896. }
  897. break;
  898. }
  899. }
  900. // REFACTORISER LA MISE EN FORME
  901. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  902. $aAttList = json_decode(GetConstant($sClass, 'DETAILS_ZLIST'), true);
  903. switch($oObj->GetState())
  904. {
  905. case 'closed':
  906. $aAttList['centered'][] = 'user_satisfaction';
  907. $aAttList['centered'][] = $sUserCommentAttCode;
  908. }
  909. // Remove the edited attribute from the shown attributes
  910. //
  911. foreach($aEditAtt as $sAttCode => $foo)
  912. {
  913. foreach($aAttList as $col => $aColumn)
  914. {
  915. if (in_array($sAttCode, $aColumn))
  916. {
  917. if(($index = array_search($sAttCode, $aColumn)) !== false)
  918. {
  919. unset($aAttList[$col][$index]);
  920. }
  921. }
  922. }
  923. }
  924. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  925. $oP->WizardFormStart('request_form', null);
  926. $oP->add('<div id="request_details">');
  927. $oP->add('<table id="request_details_table">');
  928. $oP->add('<tr>');
  929. $oP->add('<td style="vertical-align:top;">');
  930. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  931. $oP->add('</td>');
  932. $oP->add('<td style="vertical-align:top;">');
  933. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  934. $oP->add('</td>');
  935. $oP->add('</tr>');
  936. if (array_key_exists('centered', $aAttList))
  937. {
  938. $oP->add('<tr>');
  939. $oP->add('<td style="vertical-align:top;" colspan="2">');
  940. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  941. $oP->add('</td>');
  942. $oP->add('</tr>');
  943. }
  944. // REFACTORISER
  945. $oP->add('<tr>');
  946. $oP->add('<td colspan="2" style="vertical-align:top;">');
  947. $oAttPlugin = new AttachmentPlugIn();
  948. if ($bEditAttachments)
  949. {
  950. $oAttPlugin->EnableDelete(false);
  951. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  952. }
  953. else
  954. {
  955. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  956. }
  957. $oP->add('</td>');
  958. $oP->add('</tr>');
  959. $oP->add('<tr>');
  960. $oP->add('<td colspan="2" style="vertical-align:top;">');
  961. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  962. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  963. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  964. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  965. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  966. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  967. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  968. $oP->add_script(
  969. <<<EOF
  970. function SetStimulusToApply(sStimulusCode)
  971. {
  972. $('#stimulus_to_apply').val(sStimulusCode);
  973. }
  974. EOF
  975. );
  976. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  977. foreach($aEditAtt as $sAttCode => $foo)
  978. {
  979. $sValue = $oObj->Get($sAttCode);
  980. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  981. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  982. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  983. $sInputId = 'input_'.$sAttCode;
  984. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  985. $aEditFields[$sAttCode] = array(
  986. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  987. 'value' => $sHTMLValue
  988. );
  989. }
  990. foreach($aEditFields as $sAttCode => $aFieldSpec)
  991. {
  992. if ($sAttCode == $sLogAttCode)
  993. {
  994. // Skip, the public log will be displayed below the buttons
  995. continue;
  996. }
  997. $oP->add("<div class=\"edit_item\">");
  998. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  999. $oP->add($aFieldSpec['value']);
  1000. $oP->add('</div>');
  1001. }
  1002. if($bIsReopenButton)
  1003. {
  1004. $sStimulusCode = 'ev_reopen';
  1005. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  1006. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  1007. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  1008. }
  1009. if($bIsCloseButton)
  1010. {
  1011. $sStimulusCode = 'ev_close';
  1012. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  1013. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  1014. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  1015. }
  1016. elseif (count($aEditAtt) > 0)
  1017. {
  1018. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  1019. }
  1020. if ($bIsEscalateButton)
  1021. {
  1022. $sStimulusCode = 'ev_timeout';
  1023. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  1024. }
  1025. $oP->add('</td>');
  1026. $oP->add('</tr>');
  1027. $oP->add('<tr>');
  1028. $oP->add('<td colspan="2" style="vertical-align:top;">');
  1029. if (isset($aEditFields[$sLogAttCode]))
  1030. {
  1031. $oP->add("<div class=\"edit_item\">");
  1032. $oP->add('<h1>'.$aEditFields[$sLogAttCode]['label'].'</h1>');
  1033. $oP->add($aEditFields[$sLogAttCode]['value']);
  1034. $oP->add('</div>');
  1035. // Replace the text area with CKEditor
  1036. // To change the default settings of the editor,
  1037. // a) edit the file /js/ckeditor/config.js
  1038. // b) or override some of the configuration settings, using the second parameter of ckeditor()
  1039. $aConfig = array();
  1040. $sLanguage = strtolower(trim(UserRights::GetUserLanguage()));
  1041. $aConfig['language'] = $sLanguage;
  1042. $aConfig['contentsLanguage'] = $sLanguage;
  1043. $aConfig['extraPlugins'] = 'disabler';
  1044. $aConfig['placeholder'] = Dict::S('UI:CaseLogTypeYourTextHere');
  1045. $sConfigJS = json_encode($aConfig);
  1046. $oP->add_ready_script("$('#input_$sLogAttCode').ckeditor(function() { /* callback code */ }, $sConfigJS);"); // Transform $iId into a CKEdit
  1047. }
  1048. else
  1049. {
  1050. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sLogAttCode).'</h1>');
  1051. $oP->add($oObj->GetAsHTML($sLogAttCode));
  1052. }
  1053. $oP->add('</td>');
  1054. $oP->add('</tr>');
  1055. $oP->add('</table>');
  1056. $oP->add('</div>');
  1057. // Enable the button to upload images (or cut and paste of images)
  1058. $oP->add_ready_script(InlineImage::EnableCKEditorImageUpload($oObj, utils::GetUploadTempId($oP->GetTransactionId())));
  1059. $oP->WizardFormEnd();
  1060. $oP->add('</div>');
  1061. }
  1062. /**
  1063. * Create form to apply a stimulus
  1064. * @param WebPage $oP The current web page
  1065. * @param Object $oObj The target object
  1066. * @param String $sStimulusCode Stimulus that will be applied
  1067. * @param Array $aEditAtt List of attributes to edit
  1068. * @return void
  1069. */
  1070. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  1071. {
  1072. static $bHasStimulusForm = false;
  1073. $sDialogId = $sStimulusCode."_dialog";
  1074. $sFormId = $sStimulusCode."_form";
  1075. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  1076. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  1077. $sClass = get_class($oObj);
  1078. $oP->add('<form id="'.$sFormId.'" method="post">');
  1079. $sTransactionId = utils::GetNewTransactionId();
  1080. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  1081. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  1082. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  1083. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  1084. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  1085. $aTransitions = $oObj->EnumTransitions();
  1086. $aStimuli = MetaModel::EnumStimuli($sClass);
  1087. if (!isset($aTransitions[$sStimulusCode]))
  1088. {
  1089. // Invalid stimulus
  1090. throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulusCode, $oObj->GetName(), $oObj->GetStateLabel()));
  1091. }
  1092. // Compute the attribute flags in the target state
  1093. $aTransition = $aTransitions[$sStimulusCode];
  1094. $sTargetState = $aTransition['target_state'];
  1095. $aTargetStates = MetaModel::EnumStates($sClass);
  1096. $aTargetState = $aTargetStates[$sTargetState];
  1097. $aExpectedAttributes = $aTargetState['attribute_list'];
  1098. foreach($aEditAtt as $sAttCode)
  1099. {
  1100. $sValue = $oObj->Get($sAttCode);
  1101. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  1102. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  1103. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1104. $sInputId = 'input_'.$sAttCode;
  1105. $iFlags = array_key_exists($sAttCode, $aExpectedAttributes) ? $aExpectedAttributes[$sAttCode] : 0;
  1106. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1107. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  1108. $oP->add($sHTMLValue);
  1109. }
  1110. $oP->add('</form>');
  1111. $oP->add('</div>');
  1112. if (!$bHasStimulusForm)
  1113. {
  1114. $bHasStimulusForm = true;
  1115. $oP->add_script(
  1116. <<<EOF
  1117. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  1118. {
  1119. var sWidth = 'auto';
  1120. if (sStimulusCode == 'ev_reopen')
  1121. {
  1122. // Avoid having a dialog spanning the complete width of the window
  1123. // just because it contains a CaseLog entry
  1124. sWidth = '80%';
  1125. }
  1126. $('#'+sStimulusCode+'_dialog').dialog({
  1127. height: 'auto',
  1128. width: sWidth,
  1129. modal: true,
  1130. title: sTitle,
  1131. buttons: [
  1132. { text: sOkButtonLabel, click: function() {
  1133. $(this).find('#'+sStimulusCode+'_form').submit();
  1134. } },
  1135. { text: "$sCancelButtonLabel", click: function() {
  1136. $(this).dialog( "close" );
  1137. } }
  1138. ]
  1139. });
  1140. // Start the validation
  1141. CheckFields(sStimulusCode+'_form', false);
  1142. $('#'+sStimulusCode+'_form').submit( function() {
  1143. return OnSubmit(sStimulusCode+'_form');
  1144. });
  1145. }
  1146. EOF
  1147. );
  1148. }
  1149. }
  1150. /**
  1151. * Get The organization of the current user (i.e. the organization of its contact)
  1152. * @param WebPage $oP The current page, for errors output
  1153. * @return Organization The user's org or null in case of problem...
  1154. */
  1155. function GetUserOrg()
  1156. {
  1157. $oOrg = null;
  1158. $iContactId = UserRights::GetContactId();
  1159. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  1160. if (is_object($oContact))
  1161. {
  1162. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  1163. }
  1164. else
  1165. {
  1166. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  1167. }
  1168. return $oOrg;
  1169. }
  1170. /**
  1171. * Determine if the current user can be considered as being a portal power user
  1172. */
  1173. function IsPowerUSer()
  1174. {
  1175. $iUserID = UserRights::GetUserId();
  1176. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  1177. $oProfileSet = new DBObjectSet(
  1178. DBObjectSearch::FromOQL($sOQLprofile),
  1179. array(),
  1180. array(
  1181. 'user' => $iUserID,
  1182. 'profile' => PORTAL_POWER_USER_PROFILE,
  1183. )
  1184. );
  1185. $bRes = ($oProfileSet->count() > 0);
  1186. return $bRes;
  1187. }
  1188. ///////////////////////////////////////////////////////////////////////////////
  1189. //
  1190. // Main program
  1191. //
  1192. ///////////////////////////////////////////////////////////////////////////////
  1193. try
  1194. {
  1195. require_once(APPROOT.'/application/startup.inc.php');
  1196. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  1197. $oAppContext = new ApplicationContext();
  1198. $sOperation = utils::ReadParam('operation', '');
  1199. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  1200. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  1201. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  1202. utils::InitArchiveMode();
  1203. $aClasses = explode(',', MetaModel::GetConfig()->Get('portal_tickets'));
  1204. $sMainClass = trim(reset($aClasses));
  1205. if (!class_exists($sMainClass))
  1206. {
  1207. $oP = new WebPage(Dict::S('Portal:Title'));
  1208. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  1209. }
  1210. else
  1211. {
  1212. $oUserOrg = GetUserOrg();
  1213. $sCode = $oUserOrg->Get('code');
  1214. $sAlternateStylesheet = '';
  1215. if (@file_exists("./$sCode/portal.css"))
  1216. {
  1217. $sAlternateStylesheet = "$sCode";
  1218. }
  1219. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  1220. $oP->EnableDisconnectButton(utils::CanLogOff());
  1221. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  1222. if (is_object($oUserOrg))
  1223. {
  1224. switch($sOperation)
  1225. {
  1226. case 'show_closed':
  1227. $oP->set_title(Dict::S('Portal:ShowClosed'));
  1228. DisplayMainMenu($oP);
  1229. ShowClosedTickets($oP);
  1230. break;
  1231. case 'create_request':
  1232. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  1233. DisplayMainMenu($oP);
  1234. if (!MetaModel::DBIsReadOnly())
  1235. {
  1236. CreateRequest($oP, $oUserOrg);
  1237. }
  1238. break;
  1239. case 'details':
  1240. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1241. DisplayMainMenu($oP);
  1242. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1243. ValidateObject($oObj);
  1244. DisplayObject($oP, $oObj, $oUserOrg);
  1245. break;
  1246. case 'update_request':
  1247. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  1248. DisplayMainMenu($oP);
  1249. if (!MetaModel::DBIsReadOnly())
  1250. {
  1251. $oObj = $oP->FindObjectFromArgs(GetTicketClasses());
  1252. ValidateObject($oObj);
  1253. $aAttList = array(
  1254. GetConstant(get_class($oObj), 'PUBLIC_LOG'),
  1255. 'user_satisfaction',
  1256. GetConstant(get_class($oObj), 'USER_COMMENT')
  1257. );
  1258. try
  1259. {
  1260. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  1261. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  1262. }
  1263. catch(TransactionException $e)
  1264. {
  1265. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  1266. }
  1267. DisplayObject($oP, $oObj, $oUserOrg);
  1268. }
  1269. break;
  1270. case 'show_ongoing':
  1271. default:
  1272. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  1273. DisplayMainMenu($oP);
  1274. ShowOngoingTickets($oP);
  1275. }
  1276. }
  1277. }
  1278. $oP->output();
  1279. }
  1280. catch(CoreException $e)
  1281. {
  1282. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1283. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1284. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1285. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  1286. //$oP->p($e->getTraceAsString());
  1287. $oP->output();
  1288. if (MetaModel::IsLogEnabledIssue())
  1289. {
  1290. if (MetaModel::IsValidClass('EventIssue'))
  1291. {
  1292. try
  1293. {
  1294. $oLog = new EventIssue();
  1295. $oLog->Set('message', $e->getMessage());
  1296. $oLog->Set('userinfo', '');
  1297. $oLog->Set('issue', $e->GetIssue());
  1298. $oLog->Set('impact', 'Page could not be displayed');
  1299. $oLog->Set('callstack', $e->getTrace());
  1300. $oLog->Set('data', $e->getContextData());
  1301. $oLog->DBInsertNoReload();
  1302. }
  1303. catch(Exception $e)
  1304. {
  1305. IssueLog::Error("Failed to log issue into the DB");
  1306. }
  1307. }
  1308. IssueLog::Error($e->getMessage());
  1309. }
  1310. // For debugging only
  1311. //throw $e;
  1312. }
  1313. catch(Exception $e)
  1314. {
  1315. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  1316. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  1317. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  1318. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  1319. //$oP->p($e->getTraceAsString());
  1320. $oP->output();
  1321. if (MetaModel::IsLogEnabledIssue())
  1322. {
  1323. if (MetaModel::IsValidClass('EventIssue'))
  1324. {
  1325. try
  1326. {
  1327. $oLog = new EventIssue();
  1328. $oLog->Set('message', $e->getMessage());
  1329. $oLog->Set('userinfo', '');
  1330. $oLog->Set('issue', 'PHP Exception');
  1331. $oLog->Set('impact', 'Page could not be displayed');
  1332. $oLog->Set('callstack', $e->getTrace());
  1333. $oLog->Set('data', array());
  1334. $oLog->DBInsertNoReload();
  1335. }
  1336. catch(Exception $e)
  1337. {
  1338. IssueLog::Error("Failed to log issue into the DB");
  1339. }
  1340. }
  1341. IssueLog::Error($e->getMessage());
  1342. }
  1343. }
  1344. ?>