portalwebpage.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. * Class PortalWebPage
  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."/application/nicewebpage.class.inc.php");
  25. require_once(APPROOT."/application/applicationcontext.class.inc.php");
  26. require_once(APPROOT."/application/user.preferences.class.inc.php");
  27. /**
  28. * Web page with some associated CSS and scripts (jquery) for a fancier display
  29. * of the Portal web page
  30. */
  31. class PortalWebPage extends NiceWebPage
  32. {
  33. /**
  34. * Portal menu
  35. */
  36. protected $m_aMenuButtons;
  37. public function __construct($sTitle, $sAlternateStyleSheet = '')
  38. {
  39. $this->m_aMenuButtons = array();
  40. parent::__construct($sTitle);
  41. $this->add_header("Content-type: text/html; charset=utf-8");
  42. $this->add_header("Cache-control: no-cache");
  43. $this->add_linked_stylesheet("../css/jquery.treeview.css");
  44. $this->add_linked_stylesheet("../css/jquery.autocomplete.css");
  45. if ($sAlternateStyleSheet != '')
  46. {
  47. $this->add_linked_stylesheet("../portal/$sAlternateStyleSheet/portal.css");
  48. }
  49. else
  50. {
  51. $this->add_linked_stylesheet("../portal/portal.css");
  52. }
  53. $this->add_linked_script('../js/jquery.layout.min.js');
  54. $this->add_linked_script('../js/jquery.ba-bbq.min.js');
  55. $this->add_linked_script("../js/jquery.tablehover.js");
  56. $this->add_linked_script("../js/jquery.treeview.js");
  57. $this->add_linked_script("../js/jquery.autocomplete.js");
  58. $this->add_linked_script("../js/jquery.positionBy.js");
  59. $this->add_linked_script("../js/jquery.popupmenu.js");
  60. $this->add_linked_script("../js/date.js");
  61. $this->add_linked_script("../js/jquery.tablesorter.min.js");
  62. $this->add_linked_script("../js/jquery.tablesorter.pager.js");
  63. $this->add_linked_script("../js/jquery.blockUI.js");
  64. $this->add_linked_script("../js/utils.js");
  65. $this->add_linked_script("../js/forms-json-utils.js");
  66. $this->add_linked_script("../js/swfobject.js");
  67. $this->add_ready_script(
  68. <<<EOF
  69. try
  70. {
  71. //add new widget called TruncatedList to properly display truncated lists when they are sorted
  72. $.tablesorter.addWidget({
  73. // give the widget a id
  74. id: "truncatedList",
  75. // format is called when the on init and when a sorting has finished
  76. format: function(table)
  77. {
  78. // Check if there is a "truncated" line
  79. this.truncatedList = false;
  80. if ($("tr td.truncated",table).length > 0)
  81. {
  82. this.truncatedList = true;
  83. }
  84. if (this.truncatedList)
  85. {
  86. $("tr td",table).removeClass('truncated');
  87. $("tr:last td",table).addClass('truncated');
  88. }
  89. }
  90. });
  91. $.tablesorter.addWidget({
  92. // give the widget a id
  93. id: "myZebra",
  94. // format is called when the on init and when a sorting has finished
  95. format: function(table)
  96. {
  97. // Replace the 'red even' lines by 'red_even' since most browser do not support 2 classes selector in CSS, etc..
  98. $("tbody tr:even",table).addClass('even');
  99. $("tbody tr.red:even",table).removeClass('red').removeClass('even').addClass('red_even');
  100. $("tbody tr.orange:even",table).removeClass('orange').removeClass('even').addClass('orange_even');
  101. $("tbody tr.green:even",table).removeClass('green').removeClass('even').addClass('green_even');
  102. }
  103. });
  104. $(".date-pick").datepicker({
  105. showOn: 'button',
  106. buttonImage: '../images/calendar.png',
  107. buttonImageOnly: true,
  108. dateFormat: 'yy-mm-dd',
  109. constrainInput: false,
  110. changeMonth: true,
  111. changeYear: true
  112. });
  113. $('.resizable').resizable(); // Make resizable everything that claims to be resizable !
  114. $('.caselog_header').click( function () { $(this).toggleClass('open').next('.caselog_entry').toggle(); });
  115. }
  116. catch(err)
  117. {
  118. // Do something with the error !
  119. alert(err);
  120. }
  121. EOF
  122. );
  123. $this->add_script(
  124. <<<EOF
  125. function CheckSelection(sMessage)
  126. {
  127. var bResult = ($('input:checked').length > 0);
  128. if (!bResult)
  129. {
  130. alert(sMessage);
  131. }
  132. return bResult;
  133. }
  134. function GoBack()
  135. {
  136. var form = $('#request_form');
  137. var step = $('input[name=step]');
  138. form.unbind('submit'); // De-activate validation
  139. step.val(step.val() -2); // To go Back one step: next step is x, current step is x-1, previous step is x-2
  140. form.submit(); // Go
  141. }
  142. EOF
  143. );
  144. }
  145. /**
  146. * Add a button to the portal's main menu
  147. */
  148. public function AddMenuButton($sId, $sLabel, $sHyperlink)
  149. {
  150. $this->m_aMenuButtons[] = array('id' => $sId, 'label' => $sLabel, 'hyperlink' => $sHyperlink);
  151. }
  152. public function output()
  153. {
  154. $sMenu = '';
  155. $this->AddMenuButton('logoff', 'Portal:Disconnect', '../pages/logoff.php'); // This menu is always present and is the last one
  156. foreach($this->m_aMenuButtons as $aMenuItem)
  157. {
  158. $sMenu .= "<a class=\"button\" id=\"{$aMenuItem['id']}\" href=\"{$aMenuItem['hyperlink']}\"><span>".Dict::S($aMenuItem['label'])."</span></a>";
  159. }
  160. $this->s_content = '<div id="portal"><div id="banner"><div id="logo"></div>'.$sMenu.'</div><div id="content">'.$this->s_content.'</div></div>';
  161. parent::output();
  162. }
  163. /**
  164. * Displays a list of objects, without any hyperlink (except for the object's details)
  165. * @param DBObjectSet $oSet The set of objects to display
  166. * @param Array $aZList The ZList (list of field codes) to use for the tabular display
  167. * @param String $sEmptyListMessage Message displayed whenever the list is empty
  168. * @return string The HTML text representing the list
  169. */
  170. function DisplaySet($oSet, $aZList, $sEmptyListMessage = '')
  171. {
  172. if ($oSet->Count() > 0)
  173. {
  174. $sClass = $oSet->GetClass();
  175. if (is_subclass_of($sClass, 'cmdbAbstractObject'))
  176. {
  177. // Home-made and very limited display of an object set
  178. //
  179. //$oSet->Seek(0);// juste pour que le warning soit moins crado
  180. //$oSet->Fetch();// juste pour que le warning soit moins crado
  181. //
  182. $this->add("<div id=\"listOf$sClass\">\n");
  183. cmdbAbstractObject::DisplaySet($this, $oSet, array('currentId' => "listOf$sClass", 'menu' => false, 'zlist' => false, 'extra_fields' => implode(',', $aZList)));
  184. $this->add("</div>\n");
  185. }
  186. else
  187. {
  188. // Home-made and very limited display of an object set
  189. $aAttribs = array();
  190. $aValues = array();
  191. $aAttribs['key'] = array('label' => MetaModel::GetName($sClass), 'description' => '');
  192. foreach($aZList as $sAttCode)
  193. {
  194. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  195. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  196. }
  197. while($oObj = $oSet->Fetch())
  198. {
  199. $aRow = array();
  200. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  201. $sHilightClass = $oObj->GetHilightClass();
  202. if ($sHilightClass != '')
  203. {
  204. $aRow['@class'] = $sHilightClass;
  205. }
  206. foreach($aZList as $sAttCode)
  207. {
  208. $aRow[$sAttCode] = GetFieldAsHtml($oObj, $sAttCode);
  209. }
  210. $aValues[$oObj->GetKey()] = $aRow;
  211. }
  212. $this->table($aAttribs, $aValues);
  213. }
  214. }
  215. elseif (strlen($sEmptyListMessage) > 0)
  216. {
  217. $this->add($sEmptyListMessage);
  218. }
  219. }
  220. /**
  221. * Display the attributes of an object (no title, no form)
  222. * @param Object $oObj Any kind of object
  223. * @param aAttList The list of attributes to display
  224. * @return void
  225. */
  226. function DisplayObjectDetails($oObj, $aAttList)
  227. {
  228. $sClass = get_class($oObj);
  229. $aDetails = array();
  230. foreach($aAttList as $sAttCode)
  231. {
  232. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  233. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  234. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  235. {
  236. // Don't display linked set and non-visible attributes (in this state)
  237. $sDisplayValue = GetFieldAsHtml($oObj, $sAttCode);
  238. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  239. }
  240. }
  241. if (false) // Attachements !!!!!
  242. {
  243. $sAttachements = '<table>';
  244. while($oDoc = $oDocSet->Fetch())
  245. {
  246. $sAttachements .= '<tr><td>'.$oDoc->GetAsHtml('contents').'</td></tr>';
  247. }
  248. $sAttachements .= '</table>';
  249. $aDetails[] = array('label' => Dict::S('Portal:Attachments'), 'value' => $sAttachements);
  250. }
  251. $this->details($aDetails);
  252. }
  253. /**
  254. * xxxx
  255. * @param Object $oObj Any kind of object
  256. * @param $sLinkSetAttCode The attribute code of the link set attribute to display
  257. * @param $sRemoteAttCode The external key on the linked class, pointing to the remote objects
  258. * @param $aZList The list of attribute of the remote object
  259. * @return void
  260. */
  261. function DisplayObjectLinkset($oObj, $sLinkSetAttCode, $sRemoteAttCode, $aZList, $sEmptyListMessage = '')
  262. {
  263. if (empty($sEmptyListMessage))
  264. {
  265. $sEmptyListMessage = Dict::S('UI:Search:NoObjectFound');
  266. }
  267. $oSet = $oObj->Get($sLinkSetAttCode);
  268. if ($oSet->Count() > 0)
  269. {
  270. $sClass = $oSet->GetClass();
  271. $oExtKeyToRemote = MetaModel::GetAttributeDef($sClass, $sRemoteAttCode);
  272. $sRemoteClass = $oExtKeyToRemote->GetTargetClass();
  273. $aAttribs = array();
  274. $aValues = array();
  275. $aAttribs['key'] = array('label' => MetaModel::GetName($sRemoteClass), 'description' => '');
  276. foreach($aZList as $sAttCode)
  277. {
  278. $oAttDef = MetaModel::GetAttributeDef($sRemoteClass, $sAttCode);
  279. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  280. }
  281. while($oLink = $oSet->Fetch())
  282. {
  283. $aRow = array();
  284. $oObj = MetaModel::GetObject($sRemoteClass, $oLink->Get($sRemoteAttCode));
  285. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  286. $sHilightClass = $oObj->GetHilightClass();
  287. if ($sHilightClass != '')
  288. {
  289. $aRow['@class'] = $sHilightClass;
  290. }
  291. foreach($aZList as $sAttCode)
  292. {
  293. $aRow[$sAttCode] = GetFieldAsHtml($oObj, $sAttCode);
  294. }
  295. $aValues[$oObj->GetKey()] = $aRow;
  296. }
  297. $this->Table($aAttribs, $aValues);
  298. }
  299. elseif (strlen($sEmptyListMessage) > 0)
  300. {
  301. $this->add($sEmptyListMessage);
  302. }
  303. }
  304. protected function DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix, $sFieldName = null)
  305. {
  306. if (is_null($sFieldName))
  307. {
  308. $sFieldName = str_replace('->', '_x_', $sAttSpec);
  309. }
  310. $iPos = strpos($sAttSpec, '->');
  311. if ($iPos !== false)
  312. {
  313. $sAttCode = substr($sAttSpec, 0, $iPos);
  314. $sSubSpec = substr($sAttSpec, $iPos + 2);
  315. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  316. {
  317. throw new Exception("Invalid attribute code '$sClass/$sAttCode' in search specification '$sAttSpec'");
  318. }
  319. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  320. if ($oAttDef->IsLinkSet())
  321. {
  322. $sTargetClass = $oAttDef->GetLinkedClass();
  323. }
  324. elseif ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  325. {
  326. $sTargetClass = $oAttDef->GetTargetClass(EXTKEY_ABSOLUTE);
  327. }
  328. else
  329. {
  330. throw new Exception("Attribute specification '$sAttSpec', '$sAttCode' should be either a link set or an external key");
  331. }
  332. $this->DisplaySearchField($sTargetClass, $sSubSpec, $aExtraParams, $sPrefix, $sFieldName);
  333. }
  334. else
  335. {
  336. // $sAttSpec is an attribute code
  337. //
  338. $this->add('<span style="white-space: nowrap;padding:5px;display:inline-block;">');
  339. $sFilterValue = '';
  340. $sFilterValue = utils::ReadParam($sPrefix.$sFieldName, '');
  341. $sFilterOpCode = null; // Use the default 'loose' OpCode
  342. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttSpec);
  343. if ($oAttDef->IsExternalKey())
  344. {
  345. $sTargetClass = $oAttDef->GetTargetClass();
  346. $oAllowedValues = new DBObjectSet(new DBObjectSearch($sTargetClass));
  347. $iFieldSize = $oAttDef->GetMaxSize();
  348. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  349. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;");
  350. //$oWidget = UIExtKeyWidget::DIsplayFromAttCode($sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, '', $sPrefix, '');
  351. //$this->add($oWidget->Display($this, $aExtraParams, true /* bSearchMode */));
  352. $aExtKeyParams = $aExtraParams;
  353. $aExtKeyParams['iFieldSize'] = $oAttDef->GetMaxSize();
  354. $aExtKeyParams['iMinChars'] = $oAttDef->GetMinAutoCompleteChars();
  355. // DisplayFromAttCode($this, $sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName = '', $sFormPrefix = '', $aArgs, $bSearchMode = false)
  356. $sHtml = UIExtKeyWidget::DisplayFromAttCode($this, $sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, $sPrefix.$sFieldName, $sPrefix, $aExtKeyParams, true);
  357. $this->add($sHtml);
  358. }
  359. else
  360. {
  361. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClass, $sAttSpec, $aExtraParams);
  362. if (is_null($aAllowedValues))
  363. {
  364. // Any value is possible, display an input box
  365. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;<input class=\"textSearch\" name=\"$sPrefix$sFieldName\" value=\"$sFilterValue\"/>\n");
  366. }
  367. else
  368. {
  369. //Enum field or external key, display a combo
  370. $sValue = "<select name=\"$sPrefix$sFieldName\">\n";
  371. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  372. foreach($aAllowedValues as $key => $value)
  373. {
  374. if ($sFilterValue == $key)
  375. {
  376. $sSelected = ' selected';
  377. }
  378. else
  379. {
  380. $sSelected = '';
  381. }
  382. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  383. }
  384. $sValue .= "</select>\n";
  385. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;$sValue\n");
  386. }
  387. }
  388. unset($aExtraParams[$sFieldName]);
  389. $this->add('</span> ');
  390. }
  391. }
  392. public function DisplaySearchForm($sClass, $aAttList, $aExtraParams, $sPrefix)
  393. {
  394. $this->add("<form id=\"search_$sClass\" action=\"\" method=\"post\">\n"); // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
  395. // $this->add("<h2>".Dict::Format('UI:SearchFor_Class_Objects', 'xxxxxx')."</h2>\n");
  396. $this->add("<p>\n");
  397. foreach($aAttList as $sAttSpec)
  398. {
  399. //$oAppContext->Reset($sAttSpec); // Make sure the same parameter will not be passed twice
  400. $this->DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix);
  401. }
  402. $this->add("</p>\n");
  403. $this->add("<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n");
  404. foreach($aExtraParams as $sName => $sValue)
  405. {
  406. $this->add("<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n");
  407. }
  408. // $this->add($oAppContext->GetForForm());
  409. $this->add("</form>\n");
  410. }
  411. public function PostedParamsToFilter($sClass, $aAttList, $sPrefix)
  412. {
  413. $oFilter = new DBObjectSearch($sClass);
  414. $iCountParams = 0;
  415. foreach($aAttList as $sAttSpec)
  416. {
  417. $sFieldName = str_replace('->', '_x_', $sAttSpec);
  418. $value = utils::ReadPostedParam($sPrefix.$sFieldName, null);
  419. if (!is_null($value) && strlen($value) > 0)
  420. {
  421. $oFilter->AddConditionAdvanced($sAttSpec, $value);
  422. $iCountParams++;
  423. }
  424. }
  425. if ($iCountParams == 0)
  426. {
  427. return null;
  428. }
  429. else
  430. {
  431. return $oFilter;
  432. }
  433. }
  434. }
  435. ?>