portalwebpage.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. $("table.listResults").tableHover(); // hover tables
  105. $(".listResults").tablesorter( { widgets: ['myZebra', 'truncatedList']} ); // sortable and zebra tables
  106. $(".date-pick").datepicker({
  107. showOn: 'button',
  108. buttonImage: '../images/calendar.png',
  109. buttonImageOnly: true,
  110. dateFormat: 'yy-mm-dd',
  111. constrainInput: false,
  112. changeMonth: true,
  113. changeYear: true
  114. });
  115. $('.resizable').resizable(); // Make resizable everything that claims to be resizable !
  116. $('.caselog_header').click( function () { $(this).toggleClass('open').next('.caselog_entry').toggle(); });
  117. }
  118. catch(err)
  119. {
  120. // Do something with the error !
  121. alert(err);
  122. }
  123. EOF
  124. );
  125. $this->add_script(
  126. <<<EOF
  127. function CheckSelection(sMessage)
  128. {
  129. var bResult = ($('input:checked').length > 0);
  130. if (!bResult)
  131. {
  132. alert(sMessage);
  133. }
  134. return bResult;
  135. }
  136. function GoBack()
  137. {
  138. var form = $('#request_form');
  139. var step = $('input[name=step]');
  140. form.unbind('submit'); // De-activate validation
  141. step.val(step.val() -2); // To go Back one step: next step is x, current step is x-1, previous step is x-2
  142. form.submit(); // Go
  143. }
  144. EOF
  145. );
  146. }
  147. /**
  148. * Add a button to the portal's main menu
  149. */
  150. public function AddMenuButton($sId, $sLabel, $sHyperlink)
  151. {
  152. $this->m_aMenuButtons[] = array('id' => $sId, 'label' => $sLabel, 'hyperlink' => $sHyperlink);
  153. }
  154. public function output()
  155. {
  156. $sMenu = '';
  157. $this->AddMenuButton('logoff', 'Portal:Disconnect', '../pages/logoff.php'); // This menu is always present and is the last one
  158. foreach($this->m_aMenuButtons as $aMenuItem)
  159. {
  160. $sMenu .= "<a class=\"button\" id=\"{$aMenuItem['id']}\" href=\"{$aMenuItem['hyperlink']}\"><span>".Dict::S($aMenuItem['label'])."</span></a>";
  161. }
  162. $this->s_content = '<div id="portal"><div id="banner"><div id="logo"></div>'.$sMenu.'</div><div id="content">'.$this->s_content.'</div></div>';
  163. parent::output();
  164. }
  165. /**
  166. * Displays a list of objects, without any hyperlink (except for the object's details)
  167. * @param DBObjectSet $oSet The set of objects to display
  168. * @param Array $aZList The ZList (list of field codes) to use for the tabular display
  169. * @param String $sEmptyListMessage Message displayed whenever the list is empty
  170. * @return string The HTML text representing the list
  171. */
  172. function DisplaySet($oSet, $aZList, $sEmptyListMessage = '')
  173. {
  174. if ($oSet->Count() > 0)
  175. {
  176. $sClass = $oSet->GetClass();
  177. if (is_subclass_of($sClass, 'cmdbAbstractObject'))
  178. {
  179. // Home-made and very limited display of an object set
  180. //
  181. //$oSet->Seek(0);// juste pour que le warning soit moins crado
  182. //$oSet->Fetch();// juste pour que le warning soit moins crado
  183. //
  184. $this->add("<div id=\"listOf$sClass\">\n");
  185. cmdbAbstractObject::DisplaySet($this, $oSet, array('currentId' => "listOf$sClass", 'menu' => false, 'zlist' => false, 'extra_fields' => implode(',', $aZList)));
  186. $this->add("</div>\n");
  187. }
  188. else
  189. {
  190. // Home-made and very limited display of an object set
  191. $aAttribs = array();
  192. $aValues = array();
  193. $aAttribs['key'] = array('label' => MetaModel::GetName($sClass), 'description' => '');
  194. foreach($aZList as $sAttCode)
  195. {
  196. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  197. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  198. }
  199. while($oObj = $oSet->Fetch())
  200. {
  201. $aRow = array();
  202. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  203. $sHilightClass = $oObj->GetHilightClass();
  204. if ($sHilightClass != '')
  205. {
  206. $aRow['@class'] = $sHilightClass;
  207. }
  208. foreach($aZList as $sAttCode)
  209. {
  210. $aRow[$sAttCode] = GetFieldAsHtml($oObj, $sAttCode);
  211. }
  212. $aValues[$oObj->GetKey()] = $aRow;
  213. }
  214. $this->table($aAttribs, $aValues);
  215. }
  216. }
  217. elseif (strlen($sEmptyListMessage) > 0)
  218. {
  219. $this->add($sEmptyListMessage);
  220. }
  221. }
  222. /**
  223. * Display the attributes of an object (no title, no form)
  224. * @param Object $oObj Any kind of object
  225. * @param aAttList The list of attributes to display
  226. * @return void
  227. */
  228. function DisplayObjectDetails($oObj, $aAttList)
  229. {
  230. $sClass = get_class($oObj);
  231. $aDetails = array();
  232. foreach($aAttList as $sAttCode)
  233. {
  234. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  235. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  236. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  237. {
  238. // Don't display linked set and non-visible attributes (in this state)
  239. $sDisplayValue = GetFieldAsHtml($oObj, $sAttCode);
  240. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  241. }
  242. }
  243. if (false) // Attachements !!!!!
  244. {
  245. $sAttachements = '<table>';
  246. while($oDoc = $oDocSet->Fetch())
  247. {
  248. $sAttachements .= '<tr><td>'.$oDoc->GetAsHtml('contents').'</td></tr>';
  249. }
  250. $sAttachements .= '</table>';
  251. $aDetails[] = array('label' => Dict::S('Portal:Attachments'), 'value' => $sAttachements);
  252. }
  253. $this->details($aDetails);
  254. }
  255. /**
  256. * xxxx
  257. * @param Object $oObj Any kind of object
  258. * @param $sLinkSetAttCode The attribute code of the link set attribute to display
  259. * @param $sRemoteAttCode The external key on the linked class, pointing to the remote objects
  260. * @param $aZList The list of attribute of the remote object
  261. * @return void
  262. */
  263. function DisplayObjectLinkset($oObj, $sLinkSetAttCode, $sRemoteAttCode, $aZList, $sEmptyListMessage = '')
  264. {
  265. if (empty($sEmptyListMessage))
  266. {
  267. $sEmptyListMessage = Dict::S('UI:Search:NoObjectFound');
  268. }
  269. $oSet = $oObj->Get($sLinkSetAttCode);
  270. if ($oSet->Count() > 0)
  271. {
  272. $sClass = $oSet->GetClass();
  273. $oExtKeyToRemote = MetaModel::GetAttributeDef($sClass, $sRemoteAttCode);
  274. $sRemoteClass = $oExtKeyToRemote->GetTargetClass();
  275. $aAttribs = array();
  276. $aValues = array();
  277. $aAttribs['key'] = array('label' => MetaModel::GetName($sRemoteClass), 'description' => '');
  278. foreach($aZList as $sAttCode)
  279. {
  280. $oAttDef = MetaModel::GetAttributeDef($sRemoteClass, $sAttCode);
  281. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  282. }
  283. while($oLink = $oSet->Fetch())
  284. {
  285. $aRow = array();
  286. $oObj = MetaModel::GetObject($sRemoteClass, $oLink->Get($sRemoteAttCode));
  287. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  288. $sHilightClass = $oObj->GetHilightClass();
  289. if ($sHilightClass != '')
  290. {
  291. $aRow['@class'] = $sHilightClass;
  292. }
  293. foreach($aZList as $sAttCode)
  294. {
  295. $aRow[$sAttCode] = GetFieldAsHtml($oObj, $sAttCode);
  296. }
  297. $aValues[$oObj->GetKey()] = $aRow;
  298. }
  299. $this->Table($aAttribs, $aValues);
  300. }
  301. elseif (strlen($sEmptyListMessage) > 0)
  302. {
  303. $this->add($sEmptyListMessage);
  304. }
  305. }
  306. protected function DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix, $sFieldName = null)
  307. {
  308. if (is_null($sFieldName))
  309. {
  310. $sFieldName = str_replace('->', '_x_', $sAttSpec);
  311. }
  312. $iPos = strpos($sAttSpec, '->');
  313. if ($iPos !== false)
  314. {
  315. $sAttCode = substr($sAttSpec, 0, $iPos);
  316. $sSubSpec = substr($sAttSpec, $iPos + 2);
  317. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  318. {
  319. throw new Exception("Invalid attribute code '$sClass/$sAttCode' in search specification '$sAttSpec'");
  320. }
  321. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  322. if ($oAttDef->IsLinkSet())
  323. {
  324. $sTargetClass = $oAttDef->GetLinkedClass();
  325. }
  326. elseif ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  327. {
  328. $sTargetClass = $oAttDef->GetTargetClass(EXTKEY_ABSOLUTE);
  329. }
  330. else
  331. {
  332. throw new Exception("Attribute specification '$sAttSpec', '$sAttCode' should be either a link set or an external key");
  333. }
  334. $this->DisplaySearchField($sTargetClass, $sSubSpec, $aExtraParams, $sPrefix, $sFieldName);
  335. }
  336. else
  337. {
  338. // $sAttSpec is an attribute code
  339. //
  340. $this->add('<span style="white-space: nowrap;padding:5px;display:inline-block;">');
  341. $sFilterValue = '';
  342. $sFilterValue = utils::ReadParam($sPrefix.$sFieldName, '');
  343. $sFilterOpCode = null; // Use the default 'loose' OpCode
  344. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttSpec);
  345. if ($oAttDef->IsExternalKey())
  346. {
  347. $sTargetClass = $oAttDef->GetTargetClass();
  348. $oAllowedValues = new DBObjectSet(new DBObjectSearch($sTargetClass));
  349. $iFieldSize = $oAttDef->GetMaxSize();
  350. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  351. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;");
  352. //$oWidget = UIExtKeyWidget::DIsplayFromAttCode($sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, '', $sPrefix, '');
  353. //$this->add($oWidget->Display($this, $aExtraParams, true /* bSearchMode */));
  354. $aExtKeyParams = $aExtraParams;
  355. $aExtKeyParams['iFieldSize'] = $oAttDef->GetMaxSize();
  356. $aExtKeyParams['iMinChars'] = $oAttDef->GetMinAutoCompleteChars();
  357. // DisplayFromAttCode($this, $sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName = '', $sFormPrefix = '', $aArgs, $bSearchMode = false)
  358. $sHtml = UIExtKeyWidget::DisplayFromAttCode($this, $sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, $sPrefix.$sFieldName, $sPrefix, $aExtKeyParams, true);
  359. $this->add($sHtml);
  360. }
  361. else
  362. {
  363. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClass, $sAttSpec, $aExtraParams);
  364. if (is_null($aAllowedValues))
  365. {
  366. // Any value is possible, display an input box
  367. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;<input class=\"textSearch\" name=\"$sPrefix$sFieldName\" value=\"$sFilterValue\"/>\n");
  368. }
  369. else
  370. {
  371. //Enum field or external key, display a combo
  372. $sValue = "<select name=\"$sPrefix$sFieldName\">\n";
  373. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  374. foreach($aAllowedValues as $key => $value)
  375. {
  376. if ($sFilterValue == $key)
  377. {
  378. $sSelected = ' selected';
  379. }
  380. else
  381. {
  382. $sSelected = '';
  383. }
  384. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  385. }
  386. $sValue .= "</select>\n";
  387. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;$sValue\n");
  388. }
  389. }
  390. unset($aExtraParams[$sFieldName]);
  391. $this->add('</span> ');
  392. }
  393. }
  394. public function DisplaySearchForm($sClass, $aAttList, $aExtraParams, $sPrefix)
  395. {
  396. $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)
  397. // $this->add("<h2>".Dict::Format('UI:SearchFor_Class_Objects', 'xxxxxx')."</h2>\n");
  398. $this->add("<p>\n");
  399. foreach($aAttList as $sAttSpec)
  400. {
  401. //$oAppContext->Reset($sAttSpec); // Make sure the same parameter will not be passed twice
  402. $this->DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix);
  403. }
  404. $this->add("</p>\n");
  405. $this->add("<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n");
  406. foreach($aExtraParams as $sName => $sValue)
  407. {
  408. $this->add("<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n");
  409. }
  410. // $this->add($oAppContext->GetForForm());
  411. $this->add("</form>\n");
  412. }
  413. public function PostedParamsToFilter($sClass, $aAttList, $sPrefix)
  414. {
  415. $oFilter = new DBObjectSearch($sClass);
  416. $iCountParams = 0;
  417. foreach($aAttList as $sAttSpec)
  418. {
  419. $sFieldName = str_replace('->', '_x_', $sAttSpec);
  420. $value = utils::ReadPostedParam($sPrefix.$sFieldName, null);
  421. if (!is_null($value) && strlen($value) > 0)
  422. {
  423. $oFilter->AddConditionAdvanced($sAttSpec, $value);
  424. $iCountParams++;
  425. }
  426. }
  427. if ($iCountParams == 0)
  428. {
  429. return null;
  430. }
  431. else
  432. {
  433. return $oFilter;
  434. }
  435. }
  436. }
  437. ?>