index.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * iTop User Portal main page
  20. *
  21. * @copyright Copyright (C) 2010-2012 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. * Displays the portal main menu
  30. * @param WebPage $oP The current web page
  31. * @return void
  32. */
  33. function DisplayMainMenu(WebPage $oP)
  34. {
  35. $oP->AddMenuButton('showongoing', 'Portal:ShowOngoing', '../portal/index.php?operation=show_ongoing');
  36. $oP->AddMenuButton('newrequest', 'Portal:CreateNewRequest', '../portal/index.php?operation=create_request');
  37. $oP->AddMenuButton('showclosed', 'Portal:ShowClosed', '../portal/index.php?operation=show_closed');
  38. if (UserRights::CanChangePassword())
  39. {
  40. $oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', '../portal/index.php?loginop=change_pwd');
  41. }
  42. }
  43. /**
  44. * Displays the current tickets
  45. * @param WebPage $oP The current web page
  46. * @return void
  47. */
  48. function ShowOngoingTickets(WebPage $oP)
  49. {
  50. $oP->add("<div id=\"open_requests\">\n");
  51. $oP->add("<h1 id=\"title_open_requests\">".Dict::S('Portal:OpenRequests')."</h1>\n");
  52. ListOpenRequests($oP);
  53. $oP->add("</div>\n");
  54. $oP->add("<div id=\"#div_resolved_requests\">\n");
  55. $oP->add("<h1 id=\"#resolved_requests\">".Dict::S('Portal:ResolvedRequests')."</h1>\n");
  56. ListResolvedRequests($oP);
  57. $oP->add("</div>\n");
  58. }
  59. /**
  60. * Displays the closed tickets
  61. * @param WebPage $oP The current web page
  62. * @return void
  63. */
  64. function ShowClosedTickets(WebPage $oP)
  65. {
  66. $oP->add("<div id=\"#closed_tickets\">\n");
  67. //$oP->add("<h1 id=\"#closed_tickets\">".Dict::S('Portal:ListClosedTickets')."</h1>\n");
  68. ListClosedTickets($oP);
  69. $oP->add("</div>\n");
  70. }
  71. /**
  72. * Displays the form to select a Service Category Id (among the valid ones for the specified user Organization)
  73. * @param WebPage $oP Web page for the form output
  74. * @param Organization $oUserOrg The organization of the current user
  75. * @return void
  76. */
  77. function SelectServiceCategory($oP, $oUserOrg)
  78. {
  79. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  80. $oP->add("<div class=\"wizContainer\" id=\"form_select_service\">\n");
  81. $oP->WizardFormStart('request_wizard', 1);
  82. $oP->add("<h1 id=\"select_category\">".Dict::S('Portal:SelectService')."</h1>\n");
  83. $oP->add("<table>\n");
  84. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICECATEGORY_QUERY);
  85. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  86. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  87. while($oService = $oSet->Fetch())
  88. {
  89. $id = $oService->GetKey();
  90. $sChecked = "";
  91. if (isset($aParameters['service_id']) && ($id == $aParameters['service_id']))
  92. {
  93. $sChecked = "checked";
  94. }
  95. $oP->add("<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>");
  96. $oP->add("".$oService->GetAsHTML('description')."</td></tr>");
  97. }
  98. $oP->add("</table>\n");
  99. $oP->DumpHiddenParams($aParameters, array('service_id'));
  100. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  101. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL);
  102. $oP->WizardFormEnd();
  103. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectOneService'));
  104. $oP->add("</div>\n");
  105. }
  106. /**
  107. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  108. * and based on the page's parameter 'service_id'
  109. * @param WebPage $oP Web page for the form output
  110. * @param Organization $oUserOrg The organization of the current user
  111. * @return void
  112. */
  113. function SelectServiceSubCategory($oP, $oUserOrg)
  114. {
  115. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  116. $iSvcId = $aParameters['service_id'];
  117. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  118. $iDefaultWizNext = 2;
  119. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICE_SUBCATEGORY_QUERY);
  120. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  121. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId, 'org_id' => $oUserOrg->GetKey()));
  122. $oServiceCategory = MetaModel::GetObject('Service', $iSvcId, false, true /* allow all data*/);
  123. if (is_object($oServiceCategory))
  124. {
  125. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  126. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', $oServiceCategory->GetName())."</h1>\n");
  127. $oP->WizardFormStart('request_wizard', $iDefaultWizNext);
  128. $oP->add("<table>\n");
  129. while($oSubService = $oSet->Fetch())
  130. {
  131. $id = $oSubService->GetKey();
  132. $sChecked = "";
  133. if ($id == $iDefaultSubSvcId)
  134. {
  135. $sChecked = "checked";
  136. }
  137. $oP->add("<tr>");
  138. $oP->add("<td style=\"vertical-align:top\">");
  139. $oP->add("<p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"servicesubcategory_$id\" value=\"$id\"></p>");
  140. $oP->add("</td>");
  141. $oP->add("<td style=\"vertical-align:top\">");
  142. $oP->add("<p><b><label for=\"servicesubcategory_$id\">".$oSubService->GetName()."</label></b></p>");
  143. $oP->add("<p>".$oSubService->GetAsHTML('description')."</p>");
  144. $oP->add("</td>");
  145. $oP->add("</tr>");
  146. }
  147. $oP->add("</table>\n");
  148. $oP->DumpHiddenParams($aParameters, array('servicesubcategory_id'));
  149. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  150. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL);
  151. $oP->WizardFormEnd();
  152. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectAServiceSubCategory'));
  153. $oP->add("</div>\n");
  154. }
  155. else
  156. {
  157. $oP->p("Error: Invalid Service: id = $iSvcId");
  158. }
  159. }
  160. /**
  161. * Displays the form for the final step of the UserRequest creation
  162. * @param WebPage $oP The current web page for the form output
  163. * @param Organization $oUserOrg The organization of the current user
  164. * @return void
  165. */
  166. function RequestCreationForm($oP, $oUserOrg)
  167. {
  168. $oP->add_script(
  169. <<<EOF
  170. // Create the object once at the beginning of the page...
  171. var oWizardHelper = new WizardHelper('UserRequest', '');
  172. EOF
  173. );
  174. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  175. // Example: $aList = array('title', 'description', 'impact', 'emergency');
  176. $aList = explode(',', PORTAL_REQUEST_FORM_ATTRIBUTES);
  177. $sDescription = '';
  178. if (isset($aParameters['template_id']))
  179. {
  180. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id'], false);
  181. if (is_object($oTemplate))
  182. {
  183. $sDescription = htmlentities($oTemplate->Get('template'), ENT_QUOTES, 'UTF-8');
  184. }
  185. }
  186. $oServiceCategory = MetaModel::GetObject('Service', $aParameters['service_id'], false, true /* allow all data*/);
  187. $oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false, true /* allow all data*/);
  188. if (is_object($oServiceCategory) && is_object($oServiceSubCategory))
  189. {
  190. $oRequest = new UserRequest();
  191. $oRequest->Set('org_id', $oUserOrg->GetKey());
  192. $oRequest->Set('caller_id', UserRights::GetContactId());
  193. $oRequest->Set('service_id', $aParameters['service_id']);
  194. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  195. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'service_id');
  196. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceCategory->GetName());
  197. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'servicesubcategory_id');
  198. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceSubCategory->GetName());
  199. $iFlags = 0;
  200. foreach($aList as $sAttCode)
  201. {
  202. $value = '';
  203. if (isset($aParameters[$sAttCode]))
  204. {
  205. $value = $aParameters[$sAttCode];
  206. $oRequest->Set($sAttCode, $value);
  207. }
  208. }
  209. $aFieldsMap = array();
  210. foreach($aList as $sAttCode)
  211. {
  212. $value = '';
  213. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  214. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  215. if (isset($aParameters[$sAttCode]))
  216. {
  217. $value = $aParameters[$sAttCode];
  218. }
  219. $aArgs = array('this' => $oRequest);
  220. $sInputId = 'attr_'.$sAttCode;
  221. $aFieldsMap[$sAttCode] = $sInputId;
  222. $sValue = "<span id=\"field_{$sInputId}\">".$oRequest->GetFormElementForField($oP, get_class($oRequest), $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs).'</span>';
  223. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  224. }
  225. $aDetails[] = array('label' => MetaModel::GetLabel('UserRequest', PORTAL_ATTCODE_LOG), 'value' => '<textarea id="attr_moreinfo" class="resizable ui-resizable" cols="40" rows="8" name="attr_moreinfo" title="" style="margin: 0px; resize: none; position: static; display: block; height: 145px; width: 339px;">'.$sDescription.'</textarea>');
  226. $oP->add_linked_script("../js/json.js");
  227. $oP->add_linked_script("../js/forms-json-utils.js");
  228. $oP->add_linked_script("../js/wizardhelper.js");
  229. $oP->add_linked_script("../js/wizard.utils.js");
  230. $oP->add_linked_script("../js/linkswidget.js");
  231. $oP->add_linked_script("../js/extkeywidget.js");
  232. $oP->add_linked_script("../js/jquery.blockUI.js");
  233. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  234. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  235. $oP->WizardFormStart('request_form', 3);
  236. //$oP->add("<table>\n");
  237. $oP->details($aDetails);
  238. $oAttPlugin = new AttachmentPlugIn();
  239. $oAttPlugin->OnDisplayRelations($oRequest, $oP, true /* edit */);
  240. $oP->DumpHiddenParams($aParameters, $aList);
  241. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  242. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_FINISH | BUTTON_CANCEL);
  243. $oP->WizardFormEnd();
  244. $oP->add("</div>\n");
  245. $iFieldsCount = count($aFieldsMap);
  246. $sJsonFieldsMap = json_encode($aFieldsMap);
  247. $oP->add_ready_script(
  248. <<<EOF
  249. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  250. oWizardHelper.SetFieldsCount($iFieldsCount);
  251. // Starts the validation when the page is ready
  252. CheckFields('request_form', false);
  253. $('#request_form').submit( function() {
  254. return OnSubmit('request_form');
  255. });
  256. EOF
  257. );
  258. }
  259. else
  260. {
  261. // User not authorized to use this service ?
  262. //ShowOngoingTickets($oP);
  263. }
  264. }
  265. /**
  266. * Validate the parameters and create the UserRequest object (based on the page's POSTed parameters)
  267. * @param WebPage $oP The current web page for the output
  268. * @param Organization $oUserOrg The organization of the current user
  269. * @return void
  270. */
  271. function DoCreateRequest($oP, $oUserOrg)
  272. {
  273. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  274. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  275. if (!utils::IsTransactionValid($sTransactionId))
  276. {
  277. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  278. //ShowOngoingTickets($oP);
  279. return;
  280. }
  281. // Validate the parameters
  282. // 1) ServiceCategory
  283. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  284. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  285. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  286. if ($oSet->Count() != 1)
  287. {
  288. // Invalid service for the current user !
  289. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  290. }
  291. $oServiceCategory = $oSet->Fetch();
  292. // 2) Service Subcategory
  293. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  294. $oSearch->AllowAllData(); // In case the user has the rights on his org only
  295. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  296. if ($oSet->Count() != 1)
  297. {
  298. // Invalid subcategory
  299. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  300. }
  301. $oServiceSubCategory = $oSet->Fetch();
  302. $oRequest = new UserRequest();
  303. $oRequest->Set('org_id', $oUserOrg->GetKey());
  304. $oRequest->Set('caller_id', UserRights::GetContactId());
  305. $aList = array('service_id', 'servicesubcategory_id', 'title', 'description', 'impact');
  306. $oRequest->UpdateObjectFromPostedForm();
  307. if (isset($aParameters['moreinfo']))
  308. {
  309. // There is a template, insert it into the description
  310. $oRequest->Set(PORTAL_ATTCODE_LOG, $aParameters['moreinfo']);
  311. }
  312. if ((PORTAL_ATTCODE_TYPE != '') && (PORTAL_SET_TYPE_FROM != ''))
  313. {
  314. $oRequest->Set(PORTAL_ATTCODE_TYPE, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  315. }
  316. if (MetaModel::IsValidAttCode('UserRequest', 'origin'))
  317. {
  318. $oRequest->Set('origin', 'portal');
  319. }
  320. /////$oP->DoUpdateObjectFromPostedForm($oObj);
  321. $oAttPlugin = new AttachmentPlugIn();
  322. $oAttPlugin->OnFormSubmit($oRequest);
  323. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  324. if ($bRes)
  325. {
  326. $oRequest->DBInsertNoReload();
  327. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName(get_class($oRequest)))."</h1>\n");
  328. //DisplayObject($oP, $oRequest, $oUserOrg);
  329. ShowOngoingTickets($oP);
  330. }
  331. else
  332. {
  333. RequestCreationForm($oP, $oUserOrg);
  334. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  335. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  336. }
  337. }
  338. /**
  339. * Prompts the user for creating a new request
  340. * @param WebPage $oP The current web page
  341. * @return void
  342. */
  343. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  344. {
  345. switch($oP->GetWizardStep())
  346. {
  347. case 0:
  348. default:
  349. SelectServiceCategory($oP, $oUserOrg);
  350. break;
  351. case 1:
  352. SelectServiceSubCategory($oP, $oUserOrg);
  353. break;
  354. case 2:
  355. RequestCreationForm($oP, $oUserOrg);
  356. break;
  357. case 3:
  358. DoCreateRequest($oP, $oUserOrg);
  359. break;
  360. }
  361. }
  362. /**
  363. * Lists all the currently opened User Requests for the current user
  364. * @param WebPage $oP The current web page
  365. * @return void
  366. */
  367. function ListOpenRequests(WebPage $oP)
  368. {
  369. $oUserOrg = GetUserOrg();
  370. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status NOT IN ("closed", "resolved")';
  371. $oSearch = DBObjectSearch::FromOQL($sOQL);
  372. $iUser = UserRights::GetContactId();
  373. if ($iUser > 0 && !IsPowerUser())
  374. {
  375. $oSearch->AddCondition('caller_id', $iUser);
  376. }
  377. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  378. $aZList = array('finalclass', 'title', 'start_date', 'status', 'servicesubcategory_id', 'priority', 'caller_id');
  379. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  380. }
  381. /**
  382. * Lists all the currently resolved (not yet closed) User Requests for the current user
  383. * @param WebPage $oP The current web page
  384. * @return void
  385. */
  386. function ListResolvedRequests(WebPage $oP)
  387. {
  388. $oUserOrg = GetUserOrg();
  389. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status = "resolved"';
  390. $oSearch = DBObjectSearch::FromOQL($sOQL);
  391. $iUser = UserRights::GetContactId();
  392. if ($iUser > 0 && !IsPowerUser())
  393. {
  394. $oSearch->AddCondition('caller_id', $iUser);
  395. }
  396. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  397. $aZList = array('finalclass', 'title', 'start_date', 'status', 'servicesubcategory_id', 'priority', 'caller_id');
  398. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  399. }
  400. /**
  401. * Lists all the currently closed tickets
  402. * @param WebPage $oP The current web page
  403. * @return void
  404. */
  405. function ListClosedTickets(WebPage $oP)
  406. {
  407. $aAttSpecs = array('ref', 'start_date', 'close_date', 'service_id', 'caller_id');
  408. $aZList = array('title', 'start_date', 'close_date', 'servicesubcategory_id');
  409. $oP->DisplaySearchForm('UserRequest', $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  410. $oUserOrg = GetUserOrg();
  411. // UserRequest
  412. $oSearch = $oP->PostedParamsToFilter('UserRequest', $aAttSpecs, 'search_');
  413. if(is_null($oSearch))
  414. {
  415. $oSearch = new DBObjectSearch('UserRequest');
  416. }
  417. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  418. $oSearch->AddCondition('status', 'closed');
  419. $iUser = UserRights::GetContactId();
  420. if ($iUser > 0 && !IsPowerUser())
  421. {
  422. $oSearch->AddCondition('caller_id', $iUser);
  423. }
  424. $oSet1 = new CMDBObjectSet($oSearch);
  425. $oP->add("<p>\n");
  426. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  427. $oP->DisplaySet($oSet1, $aZList, Dict::S('Portal:NoClosedRequest'));
  428. $oP->add("</p>\n");
  429. }
  430. /**
  431. * Display an object - to be customized
  432. * @param WebPage $oP The current web page
  433. * @param Object $oObj Any kind of object
  434. * @param Object $oUserOrg The organization of the logged in user
  435. * @return void
  436. */
  437. function DisplayObject($oP, $oObj, $oUserOrg)
  438. {
  439. switch(get_class($oObj))
  440. {
  441. case 'UserRequest':
  442. ShowDetailsRequest($oP, $oObj);
  443. break;
  444. default:
  445. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  446. }
  447. }
  448. /**
  449. * Displays the details of a request
  450. * @param WebPage $oP The current web page
  451. * @param Object $oObj The target object
  452. * @return void
  453. */
  454. function ShowDetailsRequest(WebPage $oP, $oObj)
  455. {
  456. $sClass = get_class($oObj);
  457. $bIsEscalateButton = false;
  458. $bIsReopenButton = false;
  459. $bIsCloseButton = false;
  460. $bEditAttachments = false;
  461. $aEditAtt = array(); // List of attributes editable in the main form
  462. if (!MetaModel::DBIsReadOnly())
  463. {
  464. switch($oObj->GetState())
  465. {
  466. case 'new':
  467. case 'assigned':
  468. case 'frozen':
  469. $aEditAtt = array(
  470. PORTAL_ATTCODE_LOG => '????'
  471. );
  472. $bEditAttachments = true;
  473. // disabled - $bIsEscalateButton = true;
  474. break;
  475. case 'escalated_tto':
  476. case 'escalated_ttr':
  477. $aEditAtt = array(
  478. PORTAL_ATTCODE_LOG => '????'
  479. );
  480. $bEditAttachments = true;
  481. break;
  482. case 'resolved':
  483. $aEditAtt = array();
  484. if (array_key_exists('ev_reopen', MetaModel::EnumStimuli($sClass)))
  485. {
  486. $bIsReopenButton = true;
  487. MakeStimulusForm($oP, $oObj, 'ev_reopen', array(PORTAL_ATTCODE_LOG));
  488. }
  489. $bIsCloseButton = true;
  490. MakeStimulusForm($oP, $oObj, 'ev_close', array('user_satisfaction', PORTAL_ATTCODE_COMMENT));
  491. break;
  492. case 'closed':
  493. case 'closure_requested':
  494. default:
  495. break;
  496. }
  497. }
  498. // REFACTORISER LA MISE EN FORME
  499. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  500. switch($sClass)
  501. {
  502. case 'UserRequest':
  503. $aAttList = array('col:left'=> array('ref','caller_id','servicesubcategory_id','title','description'),'col:right'=> array('status','priority','start_date','resolution_date','last_update','agent_id'));
  504. switch($oObj->GetState())
  505. {
  506. case 'closed':
  507. $aAttList['centered'][] = 'user_satisfaction';
  508. $aAttList['centered'][] = PORTAL_ATTCODE_COMMENT;
  509. }
  510. break;
  511. default:
  512. array('col:left'=> array('ref','service_id','servicesubcategory_id','title','description'),'col:right'=> array('status','start_date'));
  513. break;
  514. }
  515. // Remove the edited attribute from the shown attributes
  516. //
  517. foreach($aEditAtt as $sAttCode => $foo)
  518. {
  519. foreach($aAttList as $col => $aColumn)
  520. {
  521. if (in_array($sAttCode, $aColumn))
  522. {
  523. if(($index = array_search($sAttCode, $aColumn)) !== false)
  524. {
  525. unset($aAttList[$col][$index]);
  526. }
  527. }
  528. }
  529. }
  530. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  531. $oP->WizardFormStart('request_form', null);
  532. $oP->add('<div id="request_details">');
  533. $oP->add('<table id="request_details_table">');
  534. $oP->add('<tr>');
  535. $oP->add('<td style="vertical-align:top;">');
  536. $oP->DisplayObjectDetails($oObj, $aAttList['col:left']);
  537. $oP->add('</td>');
  538. $oP->add('<td style="vertical-align:top;">');
  539. $oP->DisplayObjectDetails($oObj, $aAttList['col:right']);
  540. $oP->add('</td>');
  541. $oP->add('</tr>');
  542. if (array_key_exists('centered', $aAttList))
  543. {
  544. $oP->add('<tr>');
  545. $oP->add('<td style="vertical-align:top;" colspan="2">');
  546. $oP->DisplayObjectDetails($oObj, $aAttList['centered']);
  547. $oP->add('</td>');
  548. $oP->add('</tr>');
  549. }
  550. // REFACTORISER
  551. $oP->add('<tr>');
  552. $oP->add('<td colspan="2" style="vertical-align:top;">');
  553. $oAttPlugin = new AttachmentPlugIn();
  554. if ($bEditAttachments)
  555. {
  556. $oAttPlugin->EnableDelete(false);
  557. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  558. }
  559. else
  560. {
  561. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  562. }
  563. $oP->add('</td>');
  564. $oP->add('</tr>');
  565. $oP->add('<tr>');
  566. $oP->add('<td colspan="2" style="vertical-align:top;">');
  567. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  568. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  569. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  570. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  571. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  572. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  573. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  574. $oP->add_script(
  575. <<<EOF
  576. function SetStimulusToApply(sStimulusCode)
  577. {
  578. $('#stimulus_to_apply').val(sStimulusCode);
  579. }
  580. EOF
  581. );
  582. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  583. foreach($aEditAtt as $sAttCode => $foo)
  584. {
  585. $sValue = $oObj->Get($sAttCode);
  586. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  587. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  588. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  589. $sInputId = 'input_'.$sAttCode;
  590. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  591. $aEditFields[$sAttCode] = array(
  592. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  593. 'value' => $sHTMLValue
  594. );
  595. }
  596. foreach($aEditFields as $sAttCode => $aFieldSpec)
  597. {
  598. if ($sAttCode == PORTAL_ATTCODE_LOG)
  599. {
  600. // Skip, the public log will be displayed below the buttons
  601. continue;
  602. }
  603. $oP->add("<div class=\"edit_item\">");
  604. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  605. $oP->add($aFieldSpec['value']);
  606. $oP->add('</div>');
  607. }
  608. if($bIsReopenButton)
  609. {
  610. $sStimulusCode = 'ev_reopen';
  611. $sTitle = addslashes(Dict::S('Portal:Button:ReopenTicket'));
  612. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  613. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  614. }
  615. if($bIsCloseButton)
  616. {
  617. $sStimulusCode = 'ev_close';
  618. $sTitle = addslashes(Dict::S('Portal:Button:CloseTicket'));
  619. $sOk = addslashes(Dict::S('UI:Button:Ok'));
  620. $oP->p('<input type="button" onClick="RunStimulusDialog(\''.$sStimulusCode.'\', \''.$sTitle.'\', \''.$sOk.'\');" value="'.$sTitle.'...">');
  621. }
  622. elseif (count($aEditAtt) > 0)
  623. {
  624. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  625. }
  626. if ($bIsEscalateButton)
  627. {
  628. $sStimulusCode = 'ev_timeout';
  629. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  630. }
  631. $oP->add('</td>');
  632. $oP->add('</tr>');
  633. $oP->add('<tr>');
  634. $oP->add('<td colspan="2" style="vertical-align:top;">');
  635. if (isset($aEditFields[PORTAL_ATTCODE_LOG]))
  636. {
  637. $oP->add("<div class=\"edit_item\">");
  638. $oP->add('<h1>'.$aEditFields[PORTAL_ATTCODE_LOG]['label'].'</h1>');
  639. $oP->add($aEditFields[PORTAL_ATTCODE_LOG]['value']);
  640. $oP->add('</div>');
  641. }
  642. else
  643. {
  644. $oP->add('<h1>'.MetaModel::GetLabel($sClass, PORTAL_ATTCODE_LOG).'</h1>');
  645. $oP->add($oObj->GetAsHTML(PORTAL_ATTCODE_LOG));
  646. }
  647. $oP->add('</td>');
  648. $oP->add('</tr>');
  649. $oP->add('</table>');
  650. $oP->add('</div>');
  651. $oP->WizardFormEnd();
  652. $oP->add('</div>');
  653. }
  654. /**
  655. * Create form to apply a stimulus
  656. * @param WebPage $oP The current web page
  657. * @param Object $oObj The target object
  658. * @param String $sStimulusCode Stimulus that will be applied
  659. * @param Array $aEditAtt List of attributes to edit
  660. * @return void
  661. */
  662. function MakeStimulusForm(WebPage $oP, $oObj, $sStimulusCode, $aEditAtt)
  663. {
  664. static $bHasStimulusForm = false;
  665. $sDialogId = $sStimulusCode."_dialog";
  666. $sFormId = $sStimulusCode."_form";
  667. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  668. $oP->add('<div id="'.$sDialogId.'" style="display: none;">');
  669. $sClass = get_class($oObj);
  670. $oP->add('<form id="'.$sFormId.'" method="post">');
  671. $sTransactionId = utils::GetNewTransactionId();
  672. $oP->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  673. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  674. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  675. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  676. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"$sStimulusCode\">\n");
  677. foreach($aEditAtt as $sAttCode)
  678. {
  679. $sValue = $oObj->Get($sAttCode);
  680. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  681. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  682. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  683. $sInputId = 'input_'.$sAttCode;
  684. $sHTMLValue = "<span id=\"field_{$sStimulusCode}_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  685. $oP->add('<h1>'.MetaModel::GetLabel($sClass, $sAttCode).'</h1>');
  686. $oP->add($sHTMLValue);
  687. }
  688. $oP->add('</form>');
  689. $oP->add('</div>');
  690. if (!$bHasStimulusForm)
  691. {
  692. $bHasStimulusForm = true;
  693. $oP->add_script(
  694. <<<EOF
  695. function RunStimulusDialog(sStimulusCode, sTitle, sOkButtonLabel)
  696. {
  697. $('#'+sStimulusCode+'_dialog').dialog({
  698. height: 'auto',
  699. width: 'auto',
  700. modal: true,
  701. title: sTitle,
  702. buttons: [
  703. { text: sOkButtonLabel, click: function() {
  704. $(this).find('#'+sStimulusCode+'_form').submit();
  705. } },
  706. { text: "$sCancelButtonLabel", click: function() {
  707. $(this).dialog( "close" );
  708. } },
  709. ],
  710. });
  711. }
  712. EOF
  713. );
  714. }
  715. }
  716. /**
  717. * Get The organization of the current user (i.e. the organization of its contact)
  718. * @param WebPage $oP The current page, for errors output
  719. * @return Organization The user's org or null in case of problem...
  720. */
  721. function GetUserOrg()
  722. {
  723. $oOrg = null;
  724. $iContactId = UserRights::GetContactId();
  725. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  726. if (is_object($oContact))
  727. {
  728. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  729. }
  730. else
  731. {
  732. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  733. }
  734. return $oOrg;
  735. }
  736. /**
  737. * Determine if the current user can be considered as being a portal power user
  738. */
  739. function IsPowerUSer()
  740. {
  741. $iUserID = UserRights::GetUserId();
  742. $sOQLprofile = "SELECT URP_Profiles AS p JOIN URP_UserProfile AS up ON up.profileid=p.id WHERE up.userid = :user AND p.name = :profile";
  743. $oProfileSet = new DBObjectSet(
  744. DBObjectSearch::FromOQL($sOQLprofile),
  745. array(),
  746. array(
  747. 'user' => $iUserID,
  748. 'profile' => PORTAL_POWER_USER_PROFILE,
  749. )
  750. );
  751. $bRes = ($oProfileSet->count() > 0);
  752. return $bRes;
  753. }
  754. ///////////////////////////////////////////////////////////////////////////////
  755. //
  756. // Main program
  757. //
  758. ///////////////////////////////////////////////////////////////////////////////
  759. try
  760. {
  761. require_once(APPROOT.'/application/startup.inc.php');
  762. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  763. $oAppContext = new ApplicationContext();
  764. $sOperation = utils::ReadParam('operation', '');
  765. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  766. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  767. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  768. if (!class_exists('UserRequest'))
  769. {
  770. $oP = new WebPage(Dict::S('Portal:Title'));
  771. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  772. }
  773. else
  774. {
  775. $oUserOrg = GetUserOrg();
  776. $sCode = $oUserOrg->Get('code');
  777. $sAlternateStylesheet = '';
  778. if (@file_exists("./$sCode/portal.css"))
  779. {
  780. $sAlternateStylesheet = "$sCode";
  781. }
  782. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  783. $oP->EnableDisconnectButton(utils::CanLogOff());
  784. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  785. if (is_object($oUserOrg))
  786. {
  787. switch($sOperation)
  788. {
  789. case 'show_closed':
  790. $oP->set_title(Dict::S('Portal:ShowClosed'));
  791. DisplayMainMenu($oP);
  792. ShowClosedTickets($oP);
  793. break;
  794. case 'create_request':
  795. $oP->set_title(Dict::S('Portal:CreateNewRequest'));
  796. DisplayMainMenu($oP);
  797. if (!MetaModel::DBIsReadOnly())
  798. {
  799. CreateRequest($oP, $oUserOrg);
  800. }
  801. break;
  802. case 'details':
  803. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  804. DisplayMainMenu($oP);
  805. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  806. DisplayObject($oP, $oObj, $oUserOrg);
  807. break;
  808. case 'update_request':
  809. $oP->set_title(Dict::S('Portal:TitleDetailsFor_Request'));
  810. DisplayMainMenu($oP);
  811. if (!MetaModel::DBIsReadOnly())
  812. {
  813. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  814. switch(get_class($oObj))
  815. {
  816. case 'UserRequest':
  817. $aAttList = array(PORTAL_ATTCODE_LOG, 'user_satisfaction', PORTAL_ATTCODE_COMMENT);
  818. break;
  819. default:
  820. throw new Exception("Implementation issue: unexpected class '".get_class($oObj)."'");
  821. }
  822. try
  823. {
  824. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  825. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  826. }
  827. catch(TransactionException $e)
  828. {
  829. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  830. }
  831. DisplayObject($oP, $oObj, $oUserOrg);
  832. }
  833. break;
  834. case 'show_ongoing':
  835. default:
  836. $oP->set_title(Dict::S('Portal:ShowOngoing'));
  837. DisplayMainMenu($oP);
  838. ShowOngoingTickets($oP);
  839. }
  840. }
  841. }
  842. $oP->output();
  843. }
  844. catch(CoreException $e)
  845. {
  846. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  847. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  848. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  849. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  850. //$oP->p($e->getTraceAsString());
  851. $oP->output();
  852. if (MetaModel::IsLogEnabledIssue())
  853. {
  854. if (MetaModel::IsValidClass('EventIssue'))
  855. {
  856. try
  857. {
  858. $oLog = new EventIssue();
  859. $oLog->Set('message', $e->getMessage());
  860. $oLog->Set('userinfo', '');
  861. $oLog->Set('issue', $e->GetIssue());
  862. $oLog->Set('impact', 'Page could not be displayed');
  863. $oLog->Set('callstack', $e->getTrace());
  864. $oLog->Set('data', $e->getContextData());
  865. $oLog->DBInsertNoReload();
  866. }
  867. catch(Exception $e)
  868. {
  869. IssueLog::Error("Failed to log issue into the DB");
  870. }
  871. }
  872. IssueLog::Error($e->getMessage());
  873. }
  874. // For debugging only
  875. //throw $e;
  876. }
  877. catch(Exception $e)
  878. {
  879. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  880. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  881. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  882. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  883. //$oP->p($e->getTraceAsString());
  884. $oP->output();
  885. if (MetaModel::IsLogEnabledIssue())
  886. {
  887. if (MetaModel::IsValidClass('EventIssue'))
  888. {
  889. try
  890. {
  891. $oLog = new EventIssue();
  892. $oLog->Set('message', $e->getMessage());
  893. $oLog->Set('userinfo', '');
  894. $oLog->Set('issue', 'PHP Exception');
  895. $oLog->Set('impact', 'Page could not be displayed');
  896. $oLog->Set('callstack', $e->getTrace());
  897. $oLog->Set('data', array());
  898. $oLog->DBInsertNoReload();
  899. }
  900. catch(Exception $e)
  901. {
  902. IssueLog::Error("Failed to log issue into the DB");
  903. }
  904. }
  905. IssueLog::Error($e->getMessage());
  906. }
  907. }
  908. ?>