nicewebpage.class.inc.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Class NiceWebPage
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT."/application/webpage.class.inc.php");
  25. /**
  26. * Web page with some associated CSS and scripts (jquery) for a fancier display
  27. */
  28. class NiceWebPage extends WebPage
  29. {
  30. var $m_aReadyScripts;
  31. var $m_sRootUrl;
  32. public function __construct($s_title)
  33. {
  34. parent::__construct($s_title);
  35. $this->m_aReadyScripts = array();
  36. $this->add_linked_script("../js/jquery-1.10.0.min.js");
  37. // TODO: use the minified version once debugging is complete and the compatibility is fully tested
  38. $this->add_linked_script("../js/jquery-migrate-1.2.1.js"); // Provides backward compatibility and warns about the use of obsolete features...
  39. //$this->add_linked_script("../js/jquery-migrate-1.2.1.min.js"); // Needed since many other plugins still rely on oldies like $.browser
  40. $this->add_linked_stylesheet('../css/ui-lightness/jquery-ui-1.10.3.custom.min.css');
  41. $this->add_linked_script('../js/jquery-ui-1.10.3.custom.min.js');
  42. $this->add_linked_script("../js/hovertip.js");
  43. // table sorting
  44. $this->add_linked_script("../js/jquery.tablesorter.js");
  45. $this->add_linked_script("../js/jquery.tablesorter.pager.js");
  46. $this->add_linked_script("../js/jquery.tablehover.js");
  47. $this->add_linked_script('../js/field_sorter.js');
  48. $this->add_linked_script('../js/datatable.js');
  49. $this->add_linked_script("../js/jquery.positionBy.js");
  50. $this->add_linked_script("../js/jquery.popupmenu.js");
  51. $this->add_ready_script(
  52. <<< EOF
  53. //add new widget called TruncatedList to properly display truncated lists when they are sorted
  54. $.tablesorter.addWidget({
  55. // give the widget a id
  56. id: "truncatedList",
  57. // format is called when the on init and when a sorting has finished
  58. format: function(table)
  59. {
  60. // Check if there is a "truncated" line
  61. this.truncatedList = false;
  62. if ($("tr td.truncated",table).length > 0)
  63. {
  64. this.truncatedList = true;
  65. }
  66. if (this.truncatedList)
  67. {
  68. $("tr td",table).removeClass('truncated');
  69. $("tr:last td",table).addClass('truncated');
  70. }
  71. }
  72. });
  73. $.tablesorter.addWidget({
  74. // give the widget a id
  75. id: "myZebra",
  76. // format is called when the on init and when a sorting has finished
  77. format: function(table)
  78. {
  79. // Replace the 'red even' lines by 'red_even' since most browser do not support 2 classes selector in CSS, etc..
  80. $("tbody tr:even",table).addClass('even');
  81. $("tbody tr.red:even",table).removeClass('red').removeClass('even').addClass('red_even');
  82. $("tbody tr.orange:even",table).removeClass('orange').removeClass('even').addClass('orange_even');
  83. $("tbody tr.green:even",table).removeClass('green').removeClass('even').addClass('green_even');
  84. // In case we sort again the table, we need to remove the added 'even' classes on odd rows
  85. $("tbody tr:odd",table).removeClass('even');
  86. $("tbody tr.red_even:odd",table).removeClass('even').removeClass('red_even').addClass('red');
  87. $("tbody tr.orange_even:odd",table).removeClass('even').removeClass('orange_even').addClass('orange');
  88. $("tbody tr.green_even:odd",table).removeClass('even').removeClass('green_even').addClass('green');
  89. }
  90. });
  91. $("table.listResults").tableHover(); // hover tables
  92. EOF
  93. );
  94. $this->add_linked_stylesheet("../css/light-grey.css");
  95. $this->m_sRootUrl = $this->GetAbsoluteUrlAppRoot();
  96. $sAbsURLAppRoot = addslashes($this->m_sRootUrl);
  97. $sAbsURLModulesRoot = addslashes($this->GetAbsoluteUrlModulesRoot());
  98. $sAppContext = addslashes($this->GetApplicationContext());
  99. $this->add_script(
  100. <<<EOF
  101. function GetAbsoluteUrlAppRoot()
  102. {
  103. return '$sAbsURLAppRoot';
  104. }
  105. function GetAbsoluteUrlModulesRoot()
  106. {
  107. return '$sAbsURLModulesRoot';
  108. }
  109. function AddAppContext(sURL)
  110. {
  111. var sContext = '$sAppContext';
  112. if (sContext.length > 0)
  113. {
  114. if (sURL.indexOf('?') == -1)
  115. {
  116. return sURL+'?'+sContext;
  117. }
  118. return sURL+'&'+sContext;
  119. }
  120. return sURL;
  121. }
  122. EOF
  123. );
  124. }
  125. public function SetRootUrl($sRootUrl)
  126. {
  127. $this->m_sRootUrl = $sRootUrl;
  128. }
  129. public function small_p($sText)
  130. {
  131. $this->add("<p style=\"font-size:smaller\">$sText</p>\n");
  132. }
  133. public function GetAbsoluteUrlAppRoot()
  134. {
  135. return utils::GetAbsoluteUrlAppRoot();
  136. }
  137. public function GetAbsoluteUrlModulesRoot()
  138. {
  139. return utils::GetAbsoluteUrlModulesRoot();
  140. }
  141. function GetApplicationContext()
  142. {
  143. $oAppContext = new ApplicationContext();
  144. return $oAppContext->GetForLink();
  145. }
  146. // By Rom, used by CSVImport and Advanced search
  147. public function MakeClassesSelect($sName, $sDefaultValue, $iWidthPx, $iActionCode = null)
  148. {
  149. // $aTopLevelClasses = array('bizService', 'bizContact', 'logInfra', 'bizDocument');
  150. // These are classes wich root class is cmdbAbstractObject !
  151. $this->add("<select id=\"select_$sName\" name=\"$sName\">");
  152. $aValidClasses = array();
  153. foreach(MetaModel::GetClasses('bizmodel') as $sClassName)
  154. {
  155. if (is_null($iActionCode) || UserRights::IsActionAllowed($sClassName, $iActionCode))
  156. {
  157. $sSelected = ($sClassName == $sDefaultValue) ? " SELECTED" : "";
  158. $sDescription = MetaModel::GetClassDescription($sClassName);
  159. $sDisplayName = MetaModel::GetName($sClassName);
  160. $aValidClasses[$sDisplayName] = "<option style=\"width: ".$iWidthPx." px;\" title=\"$sDescription\" value=\"$sClassName\"$sSelected>$sDisplayName</option>";
  161. }
  162. }
  163. ksort($aValidClasses);
  164. $this->add(implode("\n", $aValidClasses));
  165. $this->add("</select>");
  166. }
  167. // By Rom, used by Advanced search
  168. public function add_select($aChoices, $sName, $sDefaultValue, $iWidthPx)
  169. {
  170. $this->add("<select id=\"select_$sName\" name=\"$sName\">");
  171. foreach($aChoices as $sKey => $sValue)
  172. {
  173. $sSelected = ($sKey == $sDefaultValue) ? " SELECTED" : "";
  174. $this->add("<option style=\"width: ".$iWidthPx." px;\" value=\"".htmlspecialchars($sKey)."\"$sSelected>".htmlentities($sValue, ENT_QUOTES, 'UTF-8')."</option>");
  175. }
  176. $this->add("</select>");
  177. }
  178. public function add_ready_script($sScript)
  179. {
  180. $this->m_aReadyScripts[] = $sScript;
  181. }
  182. /**
  183. * Outputs (via some echo) the complete HTML page by assembling all its elements
  184. */
  185. public function output()
  186. {
  187. $this->set_base($this->m_sRootUrl.'pages/');
  188. if (count($this->m_aReadyScripts)>0)
  189. {
  190. $this->add_script("\$(document).ready(function() {\n".implode("\n", $this->m_aReadyScripts)."\n});");
  191. }
  192. parent::output();
  193. }
  194. }
  195. ?>