index.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. <?php
  2. // Copyright (C) 2010-2012 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. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  86. while($oService = $oSet->Fetch())
  87. {
  88. $id = $oService->GetKey();
  89. $sChecked = "";
  90. if (isset($aParameters['service_id']) && ($id == $aParameters['service_id']))
  91. {
  92. $sChecked = "checked";
  93. }
  94. $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>");
  95. $oP->p("<p>".$oService->GetAsHTML('description')."</p></td></tr>");
  96. }
  97. $oP->add("</table>\n");
  98. $oP->DumpHiddenParams($aParameters, array('service_id'));
  99. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  100. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL);
  101. $oP->WizardFormEnd();
  102. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectOneService'));
  103. $oP->add("</div>\n");
  104. }
  105. /**
  106. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  107. * and based on the page's parameter 'service_id'
  108. * @param WebPage $oP Web page for the form output
  109. * @param Organization $oUserOrg The organization of the current user
  110. * @return void
  111. */
  112. function SelectServiceSubCategory($oP, $oUserOrg)
  113. {
  114. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  115. $iSvcId = $aParameters['service_id'];
  116. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  117. $iDefaultWizNext = 2;
  118. $oSearch = DBObjectSearch::FromOQL(PORTAL_SERVICE_SUBCATEGORY_QUERY);
  119. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId, 'org_id' => $oUserOrg->GetKey()));
  120. $oServiceCategory = MetaModel::GetObject('Service', $iSvcId, false);
  121. if (is_object($oServiceCategory))
  122. {
  123. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  124. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', $oServiceCategory->GetName())."</h1>\n");
  125. $oP->WizardFormStart('request_wizard', $iDefaultWizNext);
  126. $oP->add("<table>\n");
  127. while($oSubService = $oSet->Fetch())
  128. {
  129. $id = $oSubService->GetKey();
  130. $sChecked = "";
  131. if ($id == $iDefaultSubSvcId)
  132. {
  133. $sChecked = "checked";
  134. }
  135. $oP->add("<tr>");
  136. $oP->add("<td style=\"vertical-align:top\">");
  137. $oP->add("<p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"servicesubcategory_$id\" value=\"$id\"></p>");
  138. $oP->add("</td>");
  139. $oP->add("<td style=\"vertical-align:top\">");
  140. $oP->add("<p><b><label for=\"servicesubcategory_$id\">".$oSubService->GetName()."</label></b></p>");
  141. $oP->add("<p>".$oSubService->GetAsHTML('description')."</p>");
  142. $oP->add("</td>");
  143. $oP->add("</tr>");
  144. }
  145. $oP->add("</table>\n");
  146. $oP->DumpHiddenParams($aParameters, array('servicesubcategory_id'));
  147. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  148. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_NEXT | BUTTON_CANCEL);
  149. $oP->WizardFormEnd();
  150. $oP->WizardCheckSelectionOnSubmit(Dict::S('Portal:PleaseSelectAServiceSubCategory'));
  151. $oP->add("</div>\n");
  152. }
  153. else
  154. {
  155. $oP->p("Error: Invalid Service: id = $iSvcId");
  156. }
  157. }
  158. /**
  159. * Displays the form for the final step of the UserRequest creation
  160. * @param WebPage $oP The current web page for the form output
  161. * @param Organization $oUserOrg The organization of the current user
  162. * @return void
  163. */
  164. function RequestCreationForm($oP, $oUserOrg)
  165. {
  166. $oP->add_script(
  167. <<<EOF
  168. // Create the object once at the beginning of the page...
  169. var oWizardHelper = new WizardHelper('UserRequest', '');
  170. EOF
  171. );
  172. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  173. // Example: $aList = array('title', 'description', 'impact', 'emergency');
  174. $aList = explode(',', PORTAL_REQUEST_FORM_ATTRIBUTES);
  175. $sDescription = '';
  176. if (isset($aParameters['template_id']))
  177. {
  178. $oTemplate = MetaModel::GetObject('Template', $aParameters['template_id'], false);
  179. if (is_object($oTemplate))
  180. {
  181. $sDescription = htmlentities($oTemplate->Get('template'), ENT_QUOTES, 'UTF-8');
  182. }
  183. }
  184. $oServiceCategory = MetaModel::GetObject('Service', $aParameters['service_id'], false);
  185. $oServiceSubCategory = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false);
  186. if (is_object($oServiceCategory) && is_object($oServiceSubCategory))
  187. {
  188. $oRequest = new UserRequest();
  189. $oRequest->Set('org_id', $oUserOrg->GetKey());
  190. $oRequest->Set('caller_id', UserRights::GetContactId());
  191. $oRequest->Set('service_id', $aParameters['service_id']);
  192. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  193. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'service_id');
  194. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceCategory->GetName());
  195. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'servicesubcategory_id');
  196. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $oServiceSubCategory->GetName());
  197. $iFlags = 0;
  198. foreach($aList as $sAttCode)
  199. {
  200. $value = '';
  201. if (isset($aParameters[$sAttCode]))
  202. {
  203. $value = $aParameters[$sAttCode];
  204. $oRequest->Set($sAttCode, $value);
  205. }
  206. }
  207. $aFieldsMap = array();
  208. foreach($aList as $sAttCode)
  209. {
  210. $value = '';
  211. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  212. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  213. if (isset($aParameters[$sAttCode]))
  214. {
  215. $value = $aParameters[$sAttCode];
  216. }
  217. $aArgs = array('this' => $oRequest);
  218. $sInputId = 'attr_'.$sAttCode;
  219. $aFieldsMap[$sAttCode] = $sInputId;
  220. $sValue = "<span id=\"field_{$sInputId}\">".$oRequest->GetFormElementForField($oP, get_class($oRequest), $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs).'</span>';
  221. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  222. }
  223. $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>');
  224. $oP->add_linked_script("../js/json.js");
  225. $oP->add_linked_script("../js/forms-json-utils.js");
  226. $oP->add_linked_script("../js/wizardhelper.js");
  227. $oP->add_linked_script("../js/wizard.utils.js");
  228. $oP->add_linked_script("../js/linkswidget.js");
  229. $oP->add_linked_script("../js/extkeywidget.js");
  230. $oP->add_linked_script("../js/jquery.blockUI.js");
  231. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  232. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  233. $oP->WizardFormStart('request_form', 3);
  234. //$oP->add("<table>\n");
  235. $oP->details($aDetails);
  236. $oAttPlugin = new AttachmentPlugIn();
  237. $oAttPlugin->OnDisplayRelations($oRequest, $oP, true /* edit */);
  238. $oP->DumpHiddenParams($aParameters, $aList);
  239. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  240. $oP->WizardFormButtons(BUTTON_BACK | BUTTON_FINISH | BUTTON_CANCEL);
  241. $oP->WizardFormEnd();
  242. $oP->add("</div>\n");
  243. $iFieldsCount = count($aFieldsMap);
  244. $sJsonFieldsMap = json_encode($aFieldsMap);
  245. $oP->add_ready_script(
  246. <<<EOF
  247. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  248. oWizardHelper.SetFieldsCount($iFieldsCount);
  249. // Starts the validation when the page is ready
  250. CheckFields('request_form', false);
  251. $('#request_form').submit( function() {
  252. return OnSubmit('request_form');
  253. });
  254. EOF
  255. );
  256. }
  257. else
  258. {
  259. // User not authorized to use this service ?
  260. //ShowOngoingTickets($oP);
  261. }
  262. }
  263. /**
  264. * Validate the parameters and create the UserRequest object (based on the page's POSTed parameters)
  265. * @param WebPage $oP The current web page for the output
  266. * @param Organization $oUserOrg The organization of the current user
  267. * @return void
  268. */
  269. function DoCreateRequest($oP, $oUserOrg)
  270. {
  271. $aParameters = $oP->ReadAllParams(PORTAL_ALL_PARAMS);
  272. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  273. if (!utils::IsTransactionValid($sTransactionId))
  274. {
  275. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  276. //ShowOngoingTickets($oP);
  277. return;
  278. }
  279. // Validate the parameters
  280. // 1) ServiceCategory
  281. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICECATEGORY_QUERY);
  282. $oSet = new CMDBObjectSet($oSearch, array(), array('id' => $aParameters['service_id'], 'org_id' => $oUserOrg->GetKey()));
  283. if ($oSet->Count() != 1)
  284. {
  285. // Invalid service for the current user !
  286. throw new Exception("Invalid Service Category: id={$aParameters['service_id']} - count: ".$oSet->Count());
  287. }
  288. $oServiceCategory = $oSet->Fetch();
  289. // 2) Service Subcategory
  290. $oSearch = DBObjectSearch::FromOQL(PORTAL_VALIDATE_SERVICESUBCATEGORY_QUERY);
  291. $oSet = new CMDBObjectSet($oSearch, array(), array('service_id' => $aParameters['service_id'], 'id' =>$aParameters['servicesubcategory_id'],'org_id' => $oUserOrg->GetKey() ));
  292. if ($oSet->Count() != 1)
  293. {
  294. // Invalid subcategory
  295. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service category ".$oServiceCategory->GetName()."({$aParameters['service_id']}) - count: ".$oSet->Count());
  296. }
  297. $oServiceSubCategory = $oSet->Fetch();
  298. $oRequest = new UserRequest();
  299. $oRequest->Set('org_id', $oUserOrg->GetKey());
  300. $oRequest->Set('caller_id', UserRights::GetContactId());
  301. $aList = array('service_id', 'servicesubcategory_id', 'title', 'description', 'impact');
  302. $oRequest->UpdateObjectFromPostedForm();
  303. if (isset($aParameters['moreinfo']))
  304. {
  305. // There is a template, insert it into the description
  306. $oRequest->Set(PORTAL_ATTCODE_LOG, $aParameters['moreinfo']);
  307. }
  308. if ((PORTAL_ATTCODE_TYPE != '') && (PORTAL_SET_TYPE_FROM != ''))
  309. {
  310. $oRequest->Set(PORTAL_ATTCODE_TYPE, $oServiceSubCategory->Get(PORTAL_SET_TYPE_FROM));
  311. }
  312. if (MetaModel::IsValidAttCode('UserRequest', 'origin'))
  313. {
  314. $oRequest->Set('origin', 'portal');
  315. }
  316. /////$oP->DoUpdateObjectFromPostedForm($oObj);
  317. $oAttPlugin = new AttachmentPlugIn();
  318. $oAttPlugin->OnFormSubmit($oRequest);
  319. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  320. if ($bRes)
  321. {
  322. $oRequest->DBInsert();
  323. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName(get_class($oRequest)))."</h1>\n");
  324. //DisplayObject($oP, $oRequest, $oUserOrg);
  325. ShowOngoingTickets($oP);
  326. }
  327. else
  328. {
  329. RequestCreationForm($oP, $oUserOrg);
  330. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  331. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  332. }
  333. }
  334. /**
  335. * Prompts the user for creating a new request
  336. * @param WebPage $oP The current web page
  337. * @return void
  338. */
  339. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  340. {
  341. switch($oP->GetWizardStep())
  342. {
  343. case 0:
  344. default:
  345. SelectServiceCategory($oP, $oUserOrg);
  346. break;
  347. case 1:
  348. SelectServiceSubCategory($oP, $oUserOrg);
  349. break;
  350. case 2:
  351. RequestCreationForm($oP, $oUserOrg);
  352. break;
  353. case 3:
  354. DoCreateRequest($oP, $oUserOrg);
  355. break;
  356. }
  357. }
  358. /**
  359. * Lists all the currently opened User Requests for the current user
  360. * @param WebPage $oP The current web page
  361. * @return void
  362. */
  363. function ListOpenRequests(WebPage $oP)
  364. {
  365. $oUserOrg = GetUserOrg();
  366. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status NOT IN ("closed", "resolved")';
  367. $oSearch = DBObjectSearch::FromOQL($sOQL);
  368. $iUser = UserRights::GetContactId();
  369. if ($iUser > 0)
  370. {
  371. $oSearch->AddCondition('caller_id', $iUser);
  372. }
  373. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  374. $aZList = array('finalclass', 'title', 'start_date', 'status', 'servicesubcategory_id', 'priority', 'caller_id');
  375. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  376. }
  377. /**
  378. * Lists all the currently resolved (not yet closed) User Requests for the current user
  379. * @param WebPage $oP The current web page
  380. * @return void
  381. */
  382. function ListResolvedRequests(WebPage $oP)
  383. {
  384. $oUserOrg = GetUserOrg();
  385. $sOQL = 'SELECT UserRequest WHERE org_id = :org_id AND status = "resolved"';
  386. $oSearch = DBObjectSearch::FromOQL($sOQL);
  387. $iUser = UserRights::GetContactId();
  388. if ($iUser > 0)
  389. {
  390. $oSearch->AddCondition('caller_id', $iUser);
  391. }
  392. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  393. $aZList = array('finalclass', 'title', 'start_date', 'status', 'servicesubcategory_id', 'priority', 'caller_id');
  394. $oP->DisplaySet($oSet, $aZList, Dict::S('Portal:NoOpenRequest'));
  395. }
  396. /**
  397. * Lists all the currently closed tickets
  398. * @param WebPage $oP The current web page
  399. * @return void
  400. */
  401. function ListClosedTickets(WebPage $oP)
  402. {
  403. $aAttSpecs = array('ref', 'start_date', 'close_date', 'service_id', 'caller_id');
  404. $aZList = array('title', 'start_date', 'close_date', 'servicesubcategory_id');
  405. $oP->DisplaySearchForm('UserRequest', $aAttSpecs, array('operation' => 'show_closed'), 'search_', false /* => not closed */);
  406. $oUserOrg = GetUserOrg();
  407. // UserRequest
  408. $oSearch = $oP->PostedParamsToFilter('UserRequest', $aAttSpecs, 'search_');
  409. if(is_null($oSearch))
  410. {
  411. $oSearch = new DBObjectSearch('UserRequest');
  412. }
  413. $oSearch->AddCondition('org_id', $oUserOrg->GetKey());
  414. $oSearch->AddCondition('status', 'closed');
  415. $iUser = UserRights::GetContactId();
  416. if ($iUser > 0)
  417. {
  418. $oSearch->AddCondition('caller_id', $iUser);
  419. }
  420. $oSet1 = new CMDBObjectSet($oSearch);
  421. $oP->add("<p>\n");
  422. $oP->add("<h1>".Dict::S('Portal:ClosedRequests')."</h1>\n");
  423. $oP->DisplaySet($oSet1, $aZList, Dict::S('Portal:NoClosedRequest'));
  424. $oP->add("</p>\n");
  425. }
  426. /**
  427. * Display an object - to be customized
  428. * @param WebPage $oP The current web page
  429. * @param Object $oObj Any kind of object
  430. * @param Object $oUserOrg The organization of the logged in user
  431. * @return void
  432. */
  433. function DisplayObject($oP, $oObj, $oUserOrg)
  434. {
  435. switch(get_class($oObj))
  436. {
  437. case 'UserRequest':
  438. ShowDetailsRequest($oP, $oObj);
  439. break;
  440. default:
  441. throw new Exception("The class ".get_class($oObj)." is not handled through the portal");
  442. }
  443. }
  444. /**
  445. * Displays the details of a request
  446. * @param WebPage $oP The current web page
  447. * @param Object $oObj The target object
  448. * @return void
  449. */
  450. function ShowDetailsRequest(WebPage $oP, $oObj)
  451. {
  452. $sClass = get_class($oObj);
  453. $bIsEscalateButton = false;
  454. $bIsCloseButton = false;
  455. $bEditAttachments = false;
  456. $aEditAtt = array();
  457. if (!MetaModel::DBIsReadOnly())
  458. {
  459. switch($oObj->GetState())
  460. {
  461. case 'new':
  462. case 'assigned':
  463. case 'frozen':
  464. $aEditAtt = array(
  465. PORTAL_ATTCODE_LOG => '????'
  466. );
  467. $bEditAttachments = true;
  468. // disabled - $bIsEscalateButton = true;
  469. break;
  470. case 'escalated_tto':
  471. case 'escalated_ttr':
  472. $aEditAtt = array(
  473. PORTAL_ATTCODE_LOG => '????'
  474. );
  475. $bEditAttachments = true;
  476. break;
  477. case 'resolved':
  478. $aEditAtt = array(
  479. // non, read-only dans cet etat - 'ticket_log' => '????',
  480. 'user_satisfaction' => '????',
  481. PORTAL_ATTCODE_COMMENT => '????',
  482. );
  483. $bIsCloseButton = true;
  484. break;
  485. case 'closed':
  486. case 'closure_requested':
  487. default:
  488. break;
  489. }
  490. }
  491. // REFACTORISER LA MISE EN FORME
  492. $oP->add("<h1 id=\"title_request_details\">".$oObj->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oObj->GetName())."</h1>\n");
  493. switch($sClass)
  494. {
  495. case 'UserIssue':
  496. $aAttList = array('col:0'=> array('ref','caller_id','impact','perimeter','servicesubcategory_id','title','description'),'col:1'=> array('status','priority','start_date','resolution_date','last_update','agent_id'));
  497. break;
  498. case 'UserRequest':
  499. $aAttList = array('col:0'=> array('ref','caller_id','servicesubcategory_id','title','description'),'col:1'=> array('status','priority','start_date','resolution_date','last_update','agent_id'));
  500. break;
  501. default:
  502. array('col:0'=> array('ref','service_id','servicesubcategory_id','title','description'),'col:1'=> array('status','start_date'));
  503. break;
  504. }
  505. // Remove the edited attribute from the shown attributes
  506. //
  507. foreach($aEditAtt as $sAttCode => $foo)
  508. {
  509. foreach($aAttList as $col => $aColumn)
  510. {
  511. if (in_array($sAttCode, $aColumn))
  512. {
  513. if(($index = array_search($sAttCode, $aColumn)) !== false)
  514. {
  515. unset($aAttList[$col][$index]);
  516. }
  517. }
  518. }
  519. }
  520. $oP->add("<div class=\"wizContainer\" id=\"form_commment_request\">\n");
  521. $oP->WizardFormStart('request_form', null);
  522. $oP->add('<div id="request_details">');
  523. $oP->add('<table id="request_details_table">');
  524. $oP->add('<tr>');
  525. $oP->add('<td style="vertical-align:top;">');
  526. $oP->DisplayObjectDetails($oObj, $aAttList['col:0']);
  527. $oP->add('</td>');
  528. $oP->add('<td style="vertical-align:top;">');
  529. $oP->DisplayObjectDetails($oObj, $aAttList['col:1']);
  530. $oP->add('</td>');
  531. $oP->add('</tr>');
  532. // REFACTORISER
  533. $oP->add('<tr>');
  534. $oP->add('<td colspan="2" style="vertical-align:top;">');
  535. $oAttPlugin = new AttachmentPlugIn();
  536. if ($bEditAttachments)
  537. {
  538. $oAttPlugin->EnableDelete(false);
  539. $oAttPlugin->OnDisplayRelations($oObj, $oP, true /* edit */);
  540. }
  541. else
  542. {
  543. $oAttPlugin->OnDisplayRelations($oObj, $oP, false /* read */);
  544. }
  545. $oP->add('</td>');
  546. $oP->add('</tr>');
  547. if (count($aEditAtt) > 0)
  548. {
  549. $oP->add('<tr>');
  550. $oP->add('<td colspan="2" style="vertical-align:top;">');
  551. //$oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  552. //$oP->add('<table id=""><tr><td style="vertical-align:top;">');
  553. //$oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:CommentsFor_Request', $oObj->GetName())."</h1>\n");
  554. $oP->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">");
  555. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oObj->GetKey()."\">");
  556. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"update_request\">");
  557. $oP->add("<input type=\"hidden\" id=\"stimulus_to_apply\" name=\"apply_stimulus\" value=\"\">\n");
  558. $oP->add_script(
  559. <<<EOF
  560. function SetStimulusToApply(sStimulusCode)
  561. {
  562. $('#stimulus_to_apply').val(sStimulusCode);
  563. }
  564. EOF
  565. );
  566. $aEditFields = array(); // Intermediate array to avoid code duplication while splitting btw ticket_log and the rest
  567. foreach($aEditAtt as $sAttCode => $foo)
  568. {
  569. $sValue = $oObj->Get($sAttCode);
  570. $sDisplayValue = $oObj->GetEditValue($sAttCode);
  571. $aArgs = array('this' => $oObj, 'formPrefix' => '');
  572. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  573. $sInputId = 'input_'.$sAttCode;
  574. $sHTMLValue = "<span id=\"field_{$sInputId}\">".cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', 0 /*$iFlags*/, $aArgs).'</span>';
  575. $aEditFields[$sAttCode] = array(
  576. 'label' => MetaModel::GetLabel($sClass, $sAttCode),
  577. 'value' => $sHTMLValue
  578. );
  579. }
  580. foreach($aEditFields as $sAttCode => $aFieldSpec)
  581. {
  582. if ($sAttCode == PORTAL_ATTCODE_LOG)
  583. {
  584. // Skip, the public log will be displayed below the buttons
  585. continue;
  586. }
  587. $oP->add("<div class=\"edit_item\">");
  588. $oP->add('<h1>'.$aFieldSpec['label'].'</h1>');
  589. $oP->add($aFieldSpec['value']);
  590. $oP->add('</div>');
  591. }
  592. // $oP->p('<textarea id="user_request_commment" name="commment"></textarea>');
  593. if($bIsCloseButton)
  594. {
  595. $sStimulusCode = 'ev_close';
  596. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:Button:CloseTicket').'">');
  597. }
  598. else
  599. {
  600. $oP->p('<input type="submit" value="'.Dict::S('Portal:Button:UpdateRequest').'">');
  601. }
  602. if ($bIsEscalateButton)
  603. {
  604. $sStimulusCode = 'ev_timeout';
  605. $oP->p('<input type="submit" onClick="SetStimulusToApply(\''.$sStimulusCode.'\');" value="'.Dict::S('Portal:ButtonEscalate').'">');
  606. }
  607. $oP->add('</td>');
  608. $oP->add('</tr>');
  609. }
  610. $oP->add('<tr>');
  611. $oP->add('<td colspan="2" style="vertical-align:top;">');
  612. if (isset($aEditFields[PORTAL_ATTCODE_LOG]))
  613. {
  614. $oP->add("<div class=\"edit_item\">");
  615. $oP->add('<h1>'.$aEditFields[PORTAL_ATTCODE_LOG]['label'].'</h1>');
  616. $oP->add($aEditFields[PORTAL_ATTCODE_LOG]['value']);
  617. $oP->add('</div>');
  618. }
  619. else
  620. {
  621. $oP->add('<h1>'.MetaModel::GetLabel($sClass, PORTAL_ATTCODE_LOG).'</h1>');
  622. $oP->add($oObj->GetAsHTML(PORTAL_ATTCODE_LOG));
  623. }
  624. $oP->add('</td>');
  625. $oP->add('</tr>');
  626. $oP->add('</table>');
  627. $oP->add('</div>');
  628. $oP->WizardFormEnd();
  629. $oP->add('</div>');
  630. }
  631. /**
  632. * Get The organization of the current user (i.e. the organization of its contact)
  633. * @param WebPage $oP The current page, for errors output
  634. * @return Organization The user's org or null in case of problem...
  635. */
  636. function GetUserOrg()
  637. {
  638. $oOrg = null;
  639. $iContactId = UserRights::GetContactId();
  640. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  641. if (is_object($oContact))
  642. {
  643. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  644. }
  645. else
  646. {
  647. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  648. }
  649. return $oOrg;
  650. }
  651. ///////////////////////////////////////////////////////////////////////////////
  652. //
  653. // Main program
  654. //
  655. ///////////////////////////////////////////////////////////////////////////////
  656. try
  657. {
  658. require_once(APPROOT.'/application/startup.inc.php');
  659. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  660. $oAppContext = new ApplicationContext();
  661. $sOperation = utils::ReadParam('operation', '');
  662. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  663. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  664. ApplicationContext::SetUrlMakerClass('MyPortalURLMaker');
  665. if (!class_exists('UserRequest'))
  666. {
  667. $oP = new WebPage(Dict::S('Portal:Title'));
  668. $oP->p(dict::Format('Portal:NoRequestMgmt', UserRights::GetUserFriendlyName()));
  669. }
  670. else
  671. {
  672. $oUserOrg = GetUserOrg();
  673. $sCode = $oUserOrg->Get('code');
  674. $sAlternateStylesheet = '';
  675. if (@file_exists("./$sCode/portal.css"))
  676. {
  677. $sAlternateStylesheet = "$sCode";
  678. }
  679. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  680. $oP->EnableDisconnectButton(utils::CanLogOff());
  681. $oP->SetWelcomeMessage(Dict::Format('Portal:WelcomeUserOrg', UserRights::GetUserFriendlyName(), $oUserOrg->GetName()));
  682. if (is_object($oUserOrg))
  683. {
  684. switch($sOperation)
  685. {
  686. case 'show_closed':
  687. DisplayMainMenu($oP);
  688. ShowClosedTickets($oP);
  689. break;
  690. case 'create_request':
  691. DisplayMainMenu($oP);
  692. if (!MetaModel::DBIsReadOnly())
  693. {
  694. CreateRequest($oP, $oUserOrg);
  695. }
  696. break;
  697. case 'details':
  698. DisplayMainMenu($oP);
  699. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  700. DisplayObject($oP, $oObj, $oUserOrg);
  701. break;
  702. case 'update_request':
  703. DisplayMainMenu($oP);
  704. if (!MetaModel::DBIsReadOnly())
  705. {
  706. $oObj = $oP->FindObjectFromArgs(array('UserRequest'));
  707. switch(get_class($oObj))
  708. {
  709. case 'UserRequest':
  710. $aAttList = array(PORTAL_ATTCODE_LOG, 'user_satisfaction', PORTAL_ATTCODE_COMMENT);
  711. break;
  712. default:
  713. throw new Exception("Implementation issue: unexpected class '".get_class($oObj)."'");
  714. }
  715. try
  716. {
  717. $oP->DoUpdateObjectFromPostedForm($oObj, $aAttList);
  718. $oObj->Reload(); // Make sure the object is in good shape to be displayed
  719. }
  720. catch(TransactionException $e)
  721. {
  722. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyUpdated')."</h1>\n");
  723. }
  724. DisplayObject($oP, $oObj, $oUserOrg);
  725. }
  726. break;
  727. case 'show_ongoing':
  728. default:
  729. DisplayMainMenu($oP);
  730. ShowOngoingTickets($oP);
  731. }
  732. }
  733. }
  734. $oP->output();
  735. }
  736. catch(CoreException $e)
  737. {
  738. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  739. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  740. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  741. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  742. $oP->output();
  743. if (MetaModel::IsLogEnabledIssue())
  744. {
  745. if (MetaModel::IsValidClass('EventIssue'))
  746. {
  747. try
  748. {
  749. $oLog = new EventIssue();
  750. $oLog->Set('message', $e->getMessage());
  751. $oLog->Set('userinfo', '');
  752. $oLog->Set('issue', $e->GetIssue());
  753. $oLog->Set('impact', 'Page could not be displayed');
  754. $oLog->Set('callstack', $e->getTrace());
  755. $oLog->Set('data', $e->getContextData());
  756. $oLog->DBInsertNoReload();
  757. }
  758. catch(Exception $e)
  759. {
  760. IssueLog::Error("Failed to log issue into the DB");
  761. }
  762. }
  763. IssueLog::Error($e->getMessage());
  764. }
  765. // For debugging only
  766. //throw $e;
  767. }
  768. catch(Exception $e)
  769. {
  770. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  771. $oP = new SetupPage(Dict::S('UI:PageTitle:FatalError'));
  772. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  773. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  774. $oP->output();
  775. if (MetaModel::IsLogEnabledIssue())
  776. {
  777. if (MetaModel::IsValidClass('EventIssue'))
  778. {
  779. try
  780. {
  781. $oLog = new EventIssue();
  782. $oLog->Set('message', $e->getMessage());
  783. $oLog->Set('userinfo', '');
  784. $oLog->Set('issue', 'PHP Exception');
  785. $oLog->Set('impact', 'Page could not be displayed');
  786. $oLog->Set('callstack', $e->getTrace());
  787. $oLog->Set('data', array());
  788. $oLog->DBInsertNoReload();
  789. }
  790. catch(Exception $e)
  791. {
  792. IssueLog::Error("Failed to log issue into the DB");
  793. }
  794. }
  795. IssueLog::Error($e->getMessage());
  796. }
  797. }
  798. ?>