index.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * iTop User Portal main page
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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. * Get the list of parameters (i.e. attribute codes) to be handled while creating a new UserRequest object
  30. * @return Array The list of attribute codes
  31. */
  32. function GetParamsList()
  33. {
  34. return array('org_id', 'caller_id', 'service_id', 'servicesubcategory_id', 'request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  35. }
  36. /**
  37. * Outputs a list of parameters as hidden field into the current page
  38. * (must be called when inside a form)
  39. * @param WebPage $oP The current web page
  40. * @param Array $aInteractive The list of parameters that are handled intractively and thus should not be output as hidden fields
  41. * @param Hash $aParameters Array name => value for the parameters
  42. * @return void
  43. */
  44. function DumpHiddenParams($oP, $aInteractive, $aParameters)
  45. {
  46. foreach($aParameters as $sAttCode => $value)
  47. {
  48. if (!in_array($sAttCode, $aInteractive))
  49. {
  50. $oP->Add("<input type=\"hidden\" name=\"attr_$sAttCode\" value=\"$value\">");
  51. }
  52. }
  53. }
  54. /**
  55. * Read all the parameters of the page for building a UserRequest
  56. * Parameters that were absent from the page's parameters are not set in the resulting hash array
  57. * @input string $sMethod Either get or post
  58. * @return Hash Array of name => value corresponding to the parameters that were passed to the page
  59. */
  60. function ReadAllParams($sMethod = 'get')
  61. {
  62. $aParams = GetParamsList();
  63. $aValues = array();
  64. foreach($aParams as $sName)
  65. {
  66. $value = utils::ReadParam('attr_'.$sName, null, $sMethod);
  67. if (!is_null($value))
  68. {
  69. $aValues[$sName] = $value;
  70. }
  71. }
  72. return $aValues;
  73. }
  74. /**
  75. * Displays the portal main menu
  76. * @param WebPage $oP The current web page
  77. * @return void
  78. */
  79. function DisplayMainMenu(WebPage $oP)
  80. {
  81. $oP->AddMenuButton('refresh', 'Portal:Refresh', './index.php?operation=welcome');
  82. $oP->AddMenuButton('create', 'Portal:CreateNewRequest', './index.php?operation=create_request');
  83. $oP->AddMenuButton('change_pwd', 'Portal:ChangeMyPassword', './index.php?loginop=change_pwd');
  84. $oP->add("<div id=\"#div_resolved_requests\">\n");
  85. $oP->add("<h1 id=\"#open_requests\">".Dict::S('Portal:OpenRequests')."</h1>\n");
  86. ListOpenRequests($oP);
  87. $oP->add("</div>\n");
  88. $oP->add("<div id=\"#div_resolved_requests\">\n");
  89. $oP->add("<h1 id=\"#resolved_requests\">".Dict::S('Portal:ResolvedRequests')."</h1>\n");
  90. ListResolvedRequests($oP);
  91. $oP->add("</div>\n");
  92. }
  93. /**
  94. * Displays the form to select a Service Id (among the valid ones for the specified user Organization)
  95. * @param WebPage $oP Web page for the form output
  96. * @param Organization $oUserOrg The organization of the current user
  97. * @return void
  98. */
  99. function SelectService($oP, $oUserOrg)
  100. {
  101. // Init forms parameters
  102. $aParameters = ReadAllParams();
  103. $oSearch = DBObjectSearch::FromOQL('SELECT Service AS s JOIN SLA AS sla ON sla.service_id=s.id JOIN lnkContractToSLA AS ln ON ln.sla_id=sla.id JOIN CustomerContract AS cc ON ln.contract_id=cc.id WHERE cc.org_id = :org_id');
  104. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey()));
  105. $oP->add("<div class=\"wizContainer\" id=\"form_select_service\">\n");
  106. $oP->add("<h1 id=\"select_subcategory\">".Dict::S('Portal:SelectService')."</h1>\n");
  107. $oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"get\">\n");
  108. $oP->add("<table>\n");
  109. while($oService = $oSet->Fetch())
  110. {
  111. $id = $oService->GetKey();
  112. $sChecked = "";
  113. if (isset($aParameters['service_id']) && ($id == $aParameters['service_id']))
  114. {
  115. $sChecked = "checked";
  116. }
  117. $oP->p("<tr><td style=\"vertical-align:top\"><p><input name=\"attr_service_id\" $sChecked type=\"radio\" id=\"svc_$id\" value=\"$id\"></p></td><td style=\"vertical-align:top\"><p><b><label for=\"svc_$id\">".htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8')."</label></b></p>");
  118. $oP->p("<p>".htmlentities($oService->Get('description'), ENT_QUOTES, 'UTF-8')."</p></td></tr>");
  119. }
  120. $oP->add("</table>\n");
  121. DumpHiddenParams($oP, array('service_id'), $aParameters);
  122. $oP->add("<input type=\"hidden\" name=\"step\" value=\"1\">");
  123. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  124. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">");
  125. $oP->add("</form>");
  126. $oP->add("</div class=\"wizContainer\">\n");
  127. $sMessage = Dict::S('Portal:PleaseSelectOneService');
  128. $oP->add_ready_script(
  129. <<<EOF
  130. $('#request_form').submit(function() {
  131. return CheckSelection('$sMessage');
  132. });
  133. EOF
  134. );
  135. }
  136. /**
  137. * Displays the form to select a Service Subcategory Id (among the valid ones for the specified user Organization)
  138. * and based on the page's parameter 'service_id'
  139. * @param WebPage $oP Web page for the form output
  140. * @param Organization $oUserOrg The organization of the current user
  141. * @return void
  142. */
  143. function SelectSubService($oP, $oUserOrg)
  144. {
  145. // Init forms parameters
  146. $aParameters = ReadAllParams();
  147. $iSvcId = $aParameters['service_id'];
  148. $iDefaultSubSvcId = isset($aParameters['servicesubcategory_id']) ? $aParameters['servicesubcategory_id'] : 0;
  149. $oSearch = DBObjectSearch::FromOQL('SELECT ServiceSubcategory AS ss WHERE ss.service_id = :svc_id');
  150. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $iSvcId));
  151. $oService = MetaModel::GetObject('Service', $iSvcId, false);
  152. if (is_object($oService))
  153. {
  154. $oP->add("<div class=\"wizContainer\" id=\"form_select_servicesubcategory\">\n");
  155. $oP->add("<h1 id=\"select_subcategory\">".Dict::Format('Portal:SelectSubcategoryFrom_Service', htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8'))."</h1>\n");
  156. $oP->add("<form id=\"request_form\" method=\"get\">\n");
  157. $oP->add("<table>\n");
  158. while($oSubService = $oSet->Fetch())
  159. {
  160. $id = $oSubService->GetKey();
  161. $sChecked = "";
  162. if ($id == $iDefaultSubSvcId)
  163. {
  164. $sChecked = "checked";
  165. }
  166. $oP->p("<tr><td style=\"vertical-align:top\"><p><input name=\"attr_servicesubcategory_id\" $sChecked type=\"radio\" id=\"subsvc_$id\" value=\"$id\"></p></td><td style=\"vertical-align:top\"><p><b><label for=\"subsvc_$id\">".htmlentities($oSubService->GetName(), ENT_QUOTES, 'UTF-8')."</label></b></p>");
  167. $oP->p("<p>".htmlentities($oSubService->Get('description'), ENT_QUOTES, 'UTF-8')."</p></td></tr>");
  168. }
  169. $sMessage = Dict::S('Portal:PleaseSelectAServiceSubCategory');
  170. $oP->add_ready_script(
  171. <<<EOF
  172. $('#request_form').submit(function() {
  173. return CheckSelection('$sMessage');
  174. });
  175. EOF
  176. );
  177. $oP->add("</table>\n");
  178. DumpHiddenParams($oP, array('servicesubcategory_id'), $aParameters);
  179. $oP->add("<input type=\"hidden\" name=\"step\" value=\"2\">");
  180. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  181. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoBack();\">&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">");
  182. $oP->add("</form>");
  183. $oP->add("</div>\n");
  184. }
  185. else
  186. {
  187. $oP->p("Error: Invalid Service: id = $iSvcId");
  188. }
  189. }
  190. /**
  191. * Displays the form for the final step of the UserRequest creation
  192. * @param WebPage $oP The current web page for the form output
  193. * @param Organization $oUserOrg The organization of the current user
  194. * @return void
  195. */
  196. function RequestCreationForm($oP, $oUserOrg)
  197. {
  198. $aList = array('request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  199. $aParameters = ReadAllParams();
  200. $oService = MetaModel::GetObject('Service', $aParameters['service_id'], false);
  201. $oSubService = MetaModel::GetObject('ServiceSubcategory', $aParameters['servicesubcategory_id'], false);
  202. if (is_object($oService) && is_object($oSubService))
  203. {
  204. $oRequest = new UserRequest();
  205. $oRequest->Set('org_id', $oUserOrg->GetKey());
  206. $oRequest->Set('caller_id', UserRights::GetContactId());
  207. $oRequest->Set('service_id', $aParameters['service_id']);
  208. $oRequest->Set('servicesubcategory_id', $aParameters['servicesubcategory_id']);
  209. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'service_id');
  210. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => htmlentities($oService->GetName(), ENT_QUOTES, 'UTF-8'));
  211. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'servicesubcategory_id');
  212. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => htmlentities($oSubService->GetName(), ENT_QUOTES, 'UTF-8'));
  213. $iFlags = 0;
  214. foreach($aList as $sAttCode)
  215. {
  216. $value = '';
  217. if (isset($aParameters[$sAttCode]))
  218. {
  219. $value = $aParameters[$sAttCode];
  220. $oRequest->Set($sAttCode, $value);
  221. }
  222. }
  223. $aFieldsMap = array();
  224. foreach($aList as $sAttCode)
  225. {
  226. $value = '';
  227. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  228. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  229. if (isset($aParameters[$sAttCode]))
  230. {
  231. $value = $aParameters[$sAttCode];
  232. }
  233. $aArgs = array('this' => $oRequest);
  234. $aFieldsMap[$sAttCode] = 'attr_'.$sAttCode;
  235. $sValue = $oRequest->GetFormElementForField($oP, get_class($oRequest), $sAttCode, $oAttDef, $value, '', 'attr_'.$sAttCode, '', $iFlags, $aArgs);
  236. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sValue);
  237. }
  238. $aDetails[] = array('label' => Dict::S('Portal:Attachments'), 'value' => '&nbsp;');
  239. $aDetails[] = array('label' => '&nbsp;', 'value' => '<div id="attachments"></div><p><button type="button" onClick="AddAttachment();">'.Dict::S('Portal:AddAttachment').'</button/></p>');
  240. $oP->add_linked_script("../js/json.js");
  241. $oP->add_linked_script("../js/forms-json-utils.js");
  242. $oP->add_linked_script("../js/wizardhelper.js");
  243. $oP->add_linked_script("../js/wizard.utils.js");
  244. $oP->add_linked_script("../js/linkswidget.js");
  245. $oP->add_linked_script("../js/extkeywidget.js");
  246. $oP->add_linked_script("../js/jquery.blockUI.js");
  247. $oP->add("<div class=\"wizContainer\" id=\"form_request_description\">\n");
  248. $oP->add("<h1 id=\"title_request_form\">".Dict::S('Portal:DescriptionOfTheRequest')."</h1>\n");
  249. $oP->add("<form action=\"../portal/index.php\" enctype=\"multipart/form-data\" id=\"request_form\" method=\"post\">\n");
  250. $oP->add("<table>\n");
  251. $oP->details($aDetails);
  252. DumpHiddenParams($oP, $aList, $aParameters);
  253. $oP->add("<input type=\"hidden\" name=\"step\" value=\"3\">");
  254. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"create_request\">");
  255. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  256. $oP->p("<input type=\"submit\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoBack();\">&nbsp;<input type=\"submit\" value=\"".Dict::S('UI:Button:Finish')."\">");
  257. $oP->add("</form>");
  258. $oP->add("</div>\n");
  259. $iFieldsCount = count($aFieldsMap);
  260. $sJsonFieldsMap = json_encode($aFieldsMap);
  261. $oP->add_ready_script(
  262. <<<EOF
  263. // Create the object once at the beginning of the page... no state specified => new
  264. var oWizardHelper = new WizardHelper('UserRequest', '');
  265. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  266. oWizardHelper.SetFieldsCount($iFieldsCount);
  267. // Starts the validation when the page is ready
  268. CheckFields('request_form', false);
  269. $('#request_form').submit( function() {
  270. return CheckFields('request_form', true);
  271. });
  272. EOF
  273. );
  274. $sBtnLabel = Dict::S('Portal:RemoveAttachment');
  275. $oP->add_script(
  276. <<<EOF
  277. var index = 0;
  278. function AddAttachment()
  279. {
  280. $('#attachments').append('<p id="attachment_'+index+'"><input type="file" name="attachement_'+index+'"/>&nbsp;<button type="button" onClick="RemoveAttachment('+index+')">{$sBtnLabel}</button/></p>');
  281. index++;
  282. }
  283. function RemoveAttachment(id_attachment)
  284. {
  285. $('#attachment_'+id_attachment).remove();
  286. }
  287. EOF
  288. );
  289. }
  290. else
  291. {
  292. // User not authorized to use this service ?
  293. DisplayMainMenu($oP);
  294. }
  295. }
  296. /**
  297. * Validate the parameters and create the UserRequest object (based on the page's POSTed parameters)
  298. * @param WebPage $oP The current web page for the output
  299. * @param Organization $oUserOrg The organization of the current user
  300. * @return void
  301. */
  302. function DoCreateRequest($oP, $oUserOrg)
  303. {
  304. $aParameters = ReadAllParams();
  305. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  306. if (!utils::IsTransactionValid($sTransactionId))
  307. {
  308. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  309. DisplayMainMenu($oP);
  310. return;
  311. }
  312. // Validate the parameters
  313. // 1) Service
  314. $oSearch = DBObjectSearch::FromOQL('SELECT Service AS s JOIN SLA AS sla ON sla.service_id=s.id JOIN lnkContractToSLA AS ln ON ln.sla_id=sla.id JOIN CustomerContract AS cc ON ln.contract_id=cc.id WHERE cc.org_id = :org_id AND s.id = :svc_id');
  315. $oSet = new CMDBObjectSet($oSearch, array(), array('org_id' => $oUserOrg->GetKey(), 'svc_id' => $aParameters['service_id']));
  316. if ($oSet->Count() != 1)
  317. {
  318. // Invalid service for the current user !
  319. throw new Exception("Invalid Service: id={$aParameters['servicesubcategory_id']} for the current user (org_id=".$oUserOrg->GetKey().").");
  320. }
  321. $oService = $oSet->Fetch();
  322. // 2) Service Subcategory
  323. $oSearch = DBObjectSearch::FromOQL('SELECT ServiceSubcategory AS sc WHERE sc.id = :subcategory_id AND sc.service_id = :svc_id');
  324. $oSet = new CMDBObjectSet($oSearch, array(), array('svc_id' => $aParameters['service_id'], 'subcategory_id' =>$aParameters['servicesubcategory_id'] ));
  325. if ($oSet->Count() != 1)
  326. {
  327. // Invalid subcategory
  328. throw new Exception("Invalid ServiceSubcategory: id={$aParameters['servicesubcategory_id']} for service ".$oService->GetName()."({$aParameters['service_id']})");
  329. }
  330. $oRequest = new UserRequest();
  331. $oRequest->Set('org_id', $oUserOrg->GetKey());
  332. $oRequest->Set('caller_id', UserRights::GetContactId());
  333. $aList = array('service_id', 'servicesubcategory_id', 'request_type', 'title', 'description', 'impact', 'urgency', 'workgroup_id');
  334. foreach($aList as $sAttCode)
  335. {
  336. $oRequest->Set($sAttCode, $aParameters[$sAttCode]);
  337. }
  338. list($bRes, $aIssues) = $oRequest->CheckToWrite();
  339. if ($bRes)
  340. {
  341. $oMyChange = MetaModel::NewObject("CMDBChange");
  342. $oMyChange->Set("date", time());
  343. $sUserString = CMDBChange::GetCurrentUserName();
  344. $oMyChange->Set("userinfo", $sUserString);
  345. $iChangeId = $oMyChange->DBInsert();
  346. $oRequest->DBInsertTracked($oMyChange);
  347. $oP->add("<h1>".Dict::Format('UI:Title:Object_Of_Class_Created', $oRequest->GetName(), MetaModel::GetName(get_class($oRequest)))."</h1>\n");
  348. // Now process the attachements (if any)
  349. $index = 0;
  350. foreach($_FILES as $sName => $void)
  351. {
  352. $oAttachment = utils::ReadPostedDocument($sName);
  353. if (!$oAttachment->IsEmpty())
  354. {
  355. $index++;
  356. // Create a document and attach it to the created ticket
  357. $oDoc = new FileDoc();
  358. $oDoc->Set('name', Dict::Format('Portal:Attachment_No_To_Ticket_Name', $index, $oRequest->GetName(), $oAttachment->GetFileName()));
  359. $oDoc->Set('org_id', $oUserOrg->GetKey());
  360. $oDoc->Set('description', $oAttachment->GetFileName());
  361. $oDoc->Set('contents', $oAttachment);
  362. $oDoc->DBInsertTracked($oMyChange);
  363. // Link the document to the ticket
  364. $oLink = new lnkTicketToDoc();
  365. $oLink->Set('ticket_id', $oRequest->GetKey());
  366. $oLink->Set('document_id', $oDoc->GetKey());
  367. $oLink->DBInsertTracked($oMyChange);
  368. }
  369. }
  370. DisplayMainMenu($oP);
  371. }
  372. else
  373. {
  374. RequestCreationForm($oP, $oUserOrg);
  375. $sIssueDesc = Dict::Format('UI:ObjectCouldNotBeWritten', implode(', ', $aIssues));
  376. $oP->add_ready_script("alert('".addslashes($sIssueDesc)."');");
  377. }
  378. }
  379. /**
  380. * Prompts the user for creating a new request
  381. * @param WebPage $oP The current web page
  382. * @return void
  383. */
  384. function CreateRequest(WebPage $oP, Organization $oUserOrg)
  385. {
  386. $iStep = utils::ReadParam('step', 0);
  387. switch($iStep)
  388. {
  389. case 0:
  390. default:
  391. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  392. SelectService($oP, $oUserOrg);
  393. break;
  394. case 1:
  395. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  396. SelectSubService($oP, $oUserOrg);
  397. break;
  398. case 2:
  399. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  400. RequestCreationForm($oP, $oUserOrg);
  401. break;
  402. case 3:
  403. DoCreateRequest($oP, $oUserOrg);
  404. break;
  405. }
  406. }
  407. /**
  408. * Displays the value of the given field, in HTML, without any hyperlink to other objects
  409. * @param DBObject $oObj The object to use
  410. * @param string $sAttCode Code of the attribute to display
  411. * @return string HTML text representing the value of this field
  412. */
  413. function GetFieldAsHtml($oObj, $sAttCode)
  414. {
  415. $sValue = '';
  416. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  417. if ($oAttDef->IsExternalKey())
  418. {
  419. // Special processing for external keys: don't display any hyperlink
  420. $oTargetObj = MetaModel::GetObject($oAttDef->GetTargetClass(), $oObj->Get($sAttCode), false);
  421. if (is_object($oTargetObj))
  422. {
  423. $sValue = $oTargetObj->GetName();
  424. }
  425. else
  426. {
  427. $sValue = Dict::S('UI:UndefinedObject');
  428. }
  429. }
  430. else
  431. {
  432. $sValue = $oObj->GetAsHTML($sAttCode);
  433. }
  434. return $sValue;
  435. }
  436. /**
  437. * Displays a list of objects, without any hyperlink (except for the object's details)
  438. * @param WebPage $oP The web page for the output
  439. * @param DBObjectSet $oSet The set of objects to display
  440. * @param Array $aZList The ZList (list of field codes) to use for the tabular display
  441. * @return string The HTML text representing the list
  442. */
  443. function DisplaySet($oP, $oSet, $aZList)
  444. {
  445. if ($oSet->Count() > 0)
  446. {
  447. $aAttribs = array();
  448. $aValues = array();
  449. $oAttDef = MetaModel::GetAttributeDef('UserRequest', 'ref');
  450. $aAttribs['key'] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  451. foreach($aZList as $sAttCode)
  452. {
  453. $oAttDef = MetaModel::GetAttributeDef('UserRequest', $sAttCode);
  454. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  455. }
  456. while($oRequest = $oSet->Fetch())
  457. {
  458. $aRow = array();
  459. $aRow['key'] = '<a href="./index.php?operation=details&id='.$oRequest->GetKey().'">'.$oRequest->Get('ref').'</a>';
  460. $sHilightClass = $oRequest->GetHilightClass();
  461. if ($sHilightClass != '')
  462. {
  463. $aRow['@class'] = $sHilightClass;
  464. }
  465. foreach($aZList as $sAttCode)
  466. {
  467. $aRow[$sAttCode] = GetFieldAsHtml($oRequest, $sAttCode);
  468. }
  469. $aValues[$oRequest->GetKey()] = $aRow;
  470. }
  471. $oP->Table($aAttribs, $aValues);
  472. }
  473. else
  474. {
  475. $oP->add(Dict::S('Portal:NoOpenRequest'));
  476. }
  477. }
  478. /**
  479. * Lists all the currently opened User Requests for the current user
  480. * @param WebPage $oP The current web page
  481. * @return void
  482. */
  483. function ListOpenRequests(WebPage $oP)
  484. {
  485. $iContactId = UserRights::GetContactId();
  486. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  487. if (is_object($oContact))
  488. {
  489. $sOQL = 'SELECT UserRequest WHERE caller_id = :contact_id AND status NOT IN ("resolved", "closed")';
  490. $oSearch = DBObjectSearch::FromOQL($sOQL);
  491. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId));
  492. $aZList = array('title', 'start_date', 'status', 'service_id', 'priority', 'workgroup_id', 'agent_id');
  493. DisplaySet($oP, $oSet, $aZList);
  494. }
  495. }
  496. /**
  497. * Lists all the currently Resolved (not "Closed")User Requests for the current user
  498. * @param WebPage $oP The current web page
  499. * @return void
  500. */
  501. function ListResolvedRequests(WebPage $oP)
  502. {
  503. $iContactId = UserRights::GetContactId();
  504. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  505. if (is_object($oContact))
  506. {
  507. $sOQL = 'SELECT UserRequest WHERE caller_id = :contact_id AND status ="resolved"';
  508. $oSearch = DBObjectSearch::FromOQL($sOQL);
  509. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId));
  510. $aZList = array('title', 'start_date', 'status', 'service_id', 'priority', 'workgroup_id', 'agent_id');
  511. DisplaySet($oP, $oSet, $aZList);
  512. }
  513. }
  514. /**
  515. * Displays the details of the specified UserRequest object
  516. * @param WebPage $oP The current web page for the output
  517. * @param UserRequest $oRequest The object to display
  518. * @return void
  519. */
  520. function DisplayRequestDetails($oP, UserRequest $oRequest)
  521. {
  522. $aList = array('ref', 'status', 'title', 'description', 'request_type','ticket_log', 'start_date', 'tto_escalation_deadline', 'ttr_escalation_deadline', 'caller_id', 'service_id', 'servicesubcategory_id', 'product', 'impact', 'urgency', 'priority', 'workgroup_id', 'agent_id', 'related_problem_id', 'related_change_id', 'close_date', 'last_update', 'assignment_date', 'closure_deadline', 'resolution_code', 'solution', 'user_satisfaction', 'user_commment', 'freeze_reason');
  523. $aDetails = array();
  524. foreach($aList as $sAttCode)
  525. {
  526. $iFlags = $oRequest->GetAttributeFlags($sAttCode);
  527. $oAttDef = MetaModel::GetAttributeDef(get_class($oRequest), $sAttCode);
  528. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  529. {
  530. // Don't display linked set and non-visible attributes (in this state)
  531. $sDisplayValue = GetFieldAsHtml($oRequest, $sAttCode);
  532. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription('UserRequest', $sAttCode).'">'.MetaModel::GetLabel('UserRequest', $sAttCode).'</span>', 'value' => $sDisplayValue);
  533. }
  534. }
  535. $oP->add('<div id="request_details">');
  536. $sOQL = 'SELECT FileDoc AS Doc JOIN lnkTicketToDoc AS L ON L.document_id = Doc.id WHERE L.ticket_id = :request_id';
  537. $oSearch = DBObjectSearch::FromOQL($sOQL);
  538. $oSet = new CMDBObjectSet($oSearch, array(), array('request_id' => $oRequest->GetKey()));
  539. if ($oSet->Count() > 0)
  540. {
  541. $sAttachements = '<table>';
  542. while($oDoc = $oSet->Fetch())
  543. {
  544. $sAttachements .= '<tr><td>'.$oDoc->GetAsHtml('contents').'</td></tr>';
  545. }
  546. $sAttachements .= '</table>';
  547. $aDetails[] = array('label' => Dict::S('Portal:Attachments'), 'value' => $sAttachements);
  548. }
  549. $oP->details($aDetails);
  550. $oP->add('</div>');
  551. }
  552. /**
  553. * Displays a form for the user to provide feedback about a 'resolved' UserRequest and then close the request
  554. * @param WebPage $oP The current web page
  555. * @param UserRequest $oRequest The object to display
  556. * @return void
  557. */
  558. function DisplayResolvedRequestForm($oP, UserRequest $oRequest)
  559. {
  560. $oP->add("<div class=\"wizContainer\" id=\"form_close_request\">\n");
  561. $oP->add("<form action=\"../portal/index.php\" id=\"request_form\" method=\"post\">\n");
  562. $oP->add('<table id="close_form_table"><tr><td style="vertical-align:top;">');
  563. $oP->add("<h1 id=\"title_request_details\">".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oRequest->GetName())."</h1>\n");
  564. DisplayRequestDetails($oP, $oRequest);
  565. $oP->add('</td><td style="vertical-align:top;">');
  566. $aArgs = array('this' => $oRequest);
  567. $sClass = get_class($oRequest);
  568. $aDetails = array();
  569. $aTargetStates = MetaModel::EnumStates($sClass);
  570. $aTargetState = $aTargetStates['closed'];
  571. $aExpectedAttributes = $aTargetState['attribute_list'];
  572. $iFieldIndex = 0;
  573. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  574. {
  575. // Prompt for an attribute if
  576. // - the attribute must be changed or must be displayed to the user for confirmation
  577. // - or the field is mandatory and currently empty
  578. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  579. (($iExpectCode & OPT_ATT_MANDATORY) && ($oRequest->Get($sAttCode) == '')) )
  580. {
  581. $aAttributesDef = MetaModel::ListAttributeDefs($sClass);
  582. $oAttDef = $aAttributesDef[$sAttCode];
  583. $aArgs = array('this' => $oRequest);
  584. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oP, $sClass, $sAttCode, $oAttDef, $oRequest->Get($sAttCode), $oRequest->GetEditValue($sAttCode), 'att_'.$iFieldIndex, '', $iExpectCode, $aArgs);
  585. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => "<span id=\"field_att_$iFieldIndex\">$sHTMLValue</span>");
  586. $aFieldsMap[$sAttCode] = 'att_'.$iFieldIndex;
  587. $iFieldIndex++;
  588. }
  589. }
  590. $aStimuli = MetaModel::EnumStimuli($sClass);
  591. $oP->add("<h1>".Dict::S('Portal:EnterYourCommentsOnTicket')."</h1>");
  592. $oP->details($aDetails);
  593. $oP->add("<input type=\"hidden\" name=\"id\" value=\"".$oRequest->GetKey()."\">");
  594. $oP->add("<input type=\"hidden\" name=\"step\" value=\"2\">");
  595. $oP->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  596. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"details\">");
  597. $oP->p("<input type=\"submit\" value=\"".Dict::S('Portal:Button:CloseTicket')."\">");
  598. $oP->add('</td></tr></table>');
  599. $oP->add("</form>");
  600. $oP->add("</div>\n");
  601. $oP->add_ready_script(
  602. <<<EOF
  603. // Starts the validation when the page is ready
  604. CheckFields('request_form', false);
  605. $('#request_form').submit( function() {
  606. return CheckFields('request_form', true);
  607. });
  608. EOF
  609. );
  610. }
  611. /**
  612. * Actually close the request and saves the user's feedback
  613. * @param WebPage $oP The current web page
  614. * @param UserRequest $oRequest The object to close
  615. * @return void
  616. */
  617. function DoCloseRequest($oP, UserRequest $oRequest)
  618. {
  619. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  620. if (!utils::IsTransactionValid($sTransactionId))
  621. {
  622. $oP->add("<h1>".Dict::S('UI:Error:ObjectAlreadyCreated')."</h1>\n");
  623. DisplayMainMenu($oP);
  624. return;
  625. }
  626. $sClass = get_class($oRequest);
  627. $aDetails = array();
  628. $aTargetStates = MetaModel::EnumStates($sClass);
  629. $aTargetState = $aTargetStates['closed'];
  630. $aExpectedAttributes = $aTargetState['attribute_list'];
  631. $iFieldIndex = 0;
  632. foreach($aExpectedAttributes as $sAttCode => $iExpectCode)
  633. {
  634. // Prompt for an attribute if
  635. // - the attribute must be changed or must be displayed to the user for confirmation
  636. // - or the field is mandatory and currently empty
  637. if ( ($iExpectCode & (OPT_ATT_MUSTCHANGE | OPT_ATT_MUSTPROMPT)) ||
  638. (($iExpectCode & OPT_ATT_MANDATORY) && ($oRequest->Get($sAttCode) == '')) )
  639. {
  640. $value = utils::ReadPostedParam('attr_'.$sAttCode, null);
  641. if (!is_null($value))
  642. {
  643. $oRequest->Set($sAttCode, $value);
  644. }
  645. }
  646. }
  647. if ($oRequest->ApplyStimulus('ev_close'))
  648. {
  649. $oMyChange = MetaModel::NewObject("CMDBChange");
  650. $oMyChange->Set("date", time());
  651. $sUserString = CMDBChange::GetCurrentUserName();
  652. $oMyChange->Set("userinfo", $sUserString);
  653. $iChangeId = $oMyChange->DBInsert();
  654. $oRequest->DBUpdateTracked($oMyChange);
  655. $oP->p("<h1>".Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oRequest)), $oRequest->GetName())."</h1>\n");
  656. DisplayMainMenu($oP);
  657. }
  658. else
  659. {
  660. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  661. $oP->add('Error: cannot close the request - '.$oRequest->GetName());
  662. }
  663. }
  664. /**
  665. * Find the UserRequest object of the specified ID. Make sure that it the caller is the current user
  666. * @param integer $id The ID of the request to find
  667. * @return UserRequert The found object, or null in case of failure (object does not exist, user has no rights to see it...)
  668. */
  669. function FindRequest($id)
  670. {
  671. $oRequest = null;
  672. $iContactId = UserRights::GetContactId();
  673. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  674. if (is_object($oContact))
  675. {
  676. $sOQL = "SELECT UserRequest WHERE caller_id = :contact_id AND id = :request_id";
  677. $oSearch = DBObjectSearch::FromOQL($sOQL);
  678. $oSet = new CMDBObjectSet($oSearch, array(), array('contact_id' => $iContactId, 'request_id' => $id));
  679. if ($oSet->Count() > 0)
  680. {
  681. $oRequest = $oSet->Fetch();
  682. }
  683. }
  684. else
  685. {
  686. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  687. $oP->add("<p class=\"error\">".Dict::S('Portal:ErrorNoContactForThisUser')."</p>");
  688. }
  689. return $oRequest;
  690. }
  691. /**
  692. * Displays the details of a request
  693. * @param WebPage $oP The current web page
  694. * @return void
  695. */
  696. function RequestDetails(WebPage $oP, $id)
  697. {
  698. $oRequest = FindRequest($id);
  699. if (!is_object($oRequest))
  700. {
  701. DisplayMainMenu($oP);
  702. return;
  703. }
  704. $iDefaultStep = 0;
  705. if ($oRequest->GetState() == 'resolved')
  706. {
  707. // The current ticket is in 'resolved' state, prompt to close it
  708. $iDefaultStep = 1;
  709. }
  710. $iStep = utils::ReadParam('step', $iDefaultStep);
  711. switch($iStep)
  712. {
  713. case 0:
  714. $oP->AddMenuButton('back', 'Portal:Back', './index.php?operation=welcome');
  715. $oP->add("<h1 id=\"title_request_details\">".$oRequest->GetIcon()."&nbsp;".Dict::Format('Portal:TitleRequestDetailsFor_Request', $oRequest->GetName())."</h1>\n");
  716. DisplayRequestDetails($oP, $oRequest);
  717. break;
  718. case 1:
  719. $oP->AddMenuButton('cancel', 'UI:Button:Cancel', './index.php?operation=welcome');
  720. DisplayResolvedRequestForm($oP, $oRequest);
  721. break;
  722. case 2:
  723. DoCloseRequest($oP, $oRequest);
  724. break;
  725. }
  726. }
  727. /**
  728. * Get The organization of the current user (i.e. the organization of its contact)
  729. * @param WebPage $oP The current page, for errors output
  730. * @return Organization The user's org or null in case of problem...
  731. */
  732. function GetUserOrg()
  733. {
  734. $oOrg = null;
  735. $iContactId = UserRights::GetContactId();
  736. $oContact = MetaModel::GetObject('Contact', $iContactId, false); // false => Can fail
  737. if (is_object($oContact))
  738. {
  739. $oOrg = MetaModel::GetObject('Organization', $oContact->Get('org_id'), false); // false => can fail
  740. }
  741. else
  742. {
  743. throw new Exception(Dict::S('Portal:ErrorNoContactForThisUser'));
  744. }
  745. return $oOrg;
  746. }
  747. try
  748. {
  749. require_once(APPROOT.'/application/startup.inc.php');
  750. require_once(APPROOT.'/application/portalwebpage.class.inc.php');
  751. $oAppContext = new ApplicationContext();
  752. $sOperation = utils::ReadParam('operation', '');
  753. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  754. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  755. $oUserOrg = GetUserOrg();
  756. $sCode = $oUserOrg->Get('code');
  757. $sAlternateStylesheet = '';
  758. if (@file_exists("./$sCode/portal.css"))
  759. {
  760. $sAlternateStylesheet = "$sCode";
  761. }
  762. $oP = new PortalWebPage(Dict::S('Portal:Title'), $sAlternateStylesheet);
  763. $oP->add($sAlternateStylesheet);
  764. if (is_object($oUserOrg))
  765. {
  766. switch($sOperation)
  767. {
  768. case 'create_request':
  769. CreateRequest($oP, $oUserOrg);
  770. break;
  771. case 'details':
  772. $iRequestId = utils::ReadParam('id', 0);
  773. RequestDetails($oP, $iRequestId);
  774. break;
  775. case 'welcome':
  776. default:
  777. DisplayMainMenu($oP);
  778. }
  779. }
  780. $oP->output();
  781. }
  782. catch(CoreException $e)
  783. {
  784. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  785. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  786. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  787. $oP->error(Dict::Format('UI:Error_Details', $e->getHtmlDesc()));
  788. $oP->output();
  789. if (MetaModel::IsLogEnabledIssue())
  790. {
  791. if (MetaModel::IsValidClass('EventIssue'))
  792. {
  793. try
  794. {
  795. $oLog = new EventIssue();
  796. $oLog->Set('message', $e->getMessage());
  797. $oLog->Set('userinfo', '');
  798. $oLog->Set('issue', $e->GetIssue());
  799. $oLog->Set('impact', 'Page could not be displayed');
  800. $oLog->Set('callstack', $e->getTrace());
  801. $oLog->Set('data', $e->getContextData());
  802. $oLog->DBInsertNoReload();
  803. }
  804. catch(Exception $e)
  805. {
  806. IssueLog::Error("Failed to log issue into the DB");
  807. }
  808. }
  809. IssueLog::Error($e->getMessage());
  810. }
  811. // For debugging only
  812. //throw $e;
  813. }
  814. catch(Exception $e)
  815. {
  816. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  817. $oP = new SetupWebPage(Dict::S('UI:PageTitle:FatalError'));
  818. $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
  819. $oP->error(Dict::Format('UI:Error_Details', $e->getMessage()));
  820. $oP->output();
  821. if (MetaModel::IsLogEnabledIssue())
  822. {
  823. if (MetaModel::IsValidClass('EventIssue'))
  824. {
  825. try
  826. {
  827. $oLog = new EventIssue();
  828. $oLog->Set('message', $e->getMessage());
  829. $oLog->Set('userinfo', '');
  830. $oLog->Set('issue', 'PHP Exception');
  831. $oLog->Set('impact', 'Page could not be displayed');
  832. $oLog->Set('callstack', $e->getTrace());
  833. $oLog->Set('data', array());
  834. $oLog->DBInsertNoReload();
  835. }
  836. catch(Exception $e)
  837. {
  838. IssueLog::Error("Failed to log issue into the DB");
  839. }
  840. }
  841. IssueLog::Error($e->getMessage());
  842. }
  843. }
  844. ?>