dashboardlayout.class.inc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * Dashboard presentation
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. abstract class DashboardLayout
  25. {
  26. public function __construct()
  27. {
  28. }
  29. abstract public function Render($oPage, $aDashlets, $bEditMode = false);
  30. static public function GetInfo()
  31. {
  32. return array(
  33. 'label' => '',
  34. 'icon' => '',
  35. 'description' => '',
  36. );
  37. }
  38. }
  39. abstract class DashboardLayoutMultiCol extends DashboardLayout
  40. {
  41. protected $iNbCols;
  42. public function __construct()
  43. {
  44. $this->iNbCols = 1;
  45. }
  46. protected function TrimCell($aDashlets)
  47. {
  48. $aKeys = array_reverse(array_keys($aDashlets));
  49. $idx = 0;
  50. $bNoVisibleFound = true;
  51. while($idx < count($aKeys) && $bNoVisibleFound)
  52. {
  53. $oDashlet = $aDashlets[$aKeys[$idx]];
  54. if ($oDashlet->IsVisible())
  55. {
  56. $bNoVisibleFound = false;
  57. }
  58. else
  59. {
  60. unset($aDashlets[$aKeys[$idx]]);
  61. }
  62. $idx++;
  63. }
  64. return $aDashlets;
  65. }
  66. protected function TrimCellsArray($aCells)
  67. {
  68. foreach($aCells as $key => $aDashlets)
  69. {
  70. $aCells[$key] = $this->TrimCell($aDashlets);
  71. }
  72. $aKeys = array_reverse(array_keys($aCells));
  73. $idx = 0;
  74. $bNoVisibleFound = true;
  75. while($idx < count($aKeys) && $bNoVisibleFound)
  76. {
  77. $aDashlets = $aCells[$aKeys[$idx]];
  78. if (count($aDashlets) > 0)
  79. {
  80. $bNoVisibleFound = false;
  81. }
  82. else
  83. {
  84. unset($aCells[$aKeys[$idx]]);
  85. }
  86. $idx++;
  87. }
  88. return $aCells;
  89. }
  90. public function Render($oPage, $aCells, $bEditMode = false, $aExtraParams = array())
  91. {
  92. // Trim the list of cells to remove the invisible/empty ones at the end of the array
  93. $aCells = $this->TrimCellsArray($aCells);
  94. $oPage->add('<table style="width:100%"><tbody>');
  95. $iCellIdx = 0;
  96. $fColSize = 100 / $this->iNbCols;
  97. $sStyle = $bEditMode ? 'style="border: 1px #ccc dashed; width:'.$fColSize.'%;" class="layout_cell edit_mode"' : 'style="width: '.$fColSize.'%;" class="dashboard"';
  98. $iNbRows = ceil(count($aCells) / $this->iNbCols);
  99. for($iRows = 0; $iRows < $iNbRows; $iRows++)
  100. {
  101. $oPage->add('<tr>');
  102. for($iCols = 0; $iCols < $this->iNbCols; $iCols++)
  103. {
  104. $oPage->add("<td $sStyle>");
  105. if (array_key_exists($iCellIdx, $aCells))
  106. {
  107. $aDashlets = $aCells[$iCellIdx];
  108. if (count($aDashlets) > 0)
  109. {
  110. foreach($aDashlets as $oDashlet)
  111. {
  112. if ($oDashlet->IsVisible())
  113. {
  114. $oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams);
  115. }
  116. }
  117. }
  118. else
  119. {
  120. $oPage->add('&nbsp;');
  121. }
  122. }
  123. else
  124. {
  125. $oPage->add('&nbsp;');
  126. }
  127. $oPage->add('</td>');
  128. $iCellIdx++;
  129. }
  130. $oPage->add('</tr>');
  131. }
  132. if ($bEditMode) // Add one row for extensibility
  133. {
  134. $sStyle = 'style="border: 1px #ccc dashed; width:'.$fColSize.'%;" class="layout_cell edit_mode layout_extension"';
  135. $oPage->add('<tr>');
  136. for($iCols = 0; $iCols < $this->iNbCols; $iCols++)
  137. {
  138. $oPage->add("<td $sStyle>");
  139. $oPage->add('&nbsp;');
  140. $oPage->add('</td>');
  141. }
  142. $oPage->add('</tr>');
  143. }
  144. $oPage->add('</tbody></table>');
  145. }
  146. }
  147. class DashboardLayoutOneCol extends DashboardLayoutMultiCol
  148. {
  149. public function __construct()
  150. {
  151. parent::__construct();
  152. $this->iNbCols = 1;
  153. }
  154. static public function GetInfo()
  155. {
  156. return array(
  157. 'label' => 'One Column',
  158. 'icon' => 'images/layout_1col.png',
  159. 'description' => '',
  160. );
  161. }
  162. }
  163. class DashboardLayoutTwoCols extends DashboardLayoutMultiCol
  164. {
  165. public function __construct()
  166. {
  167. parent::__construct();
  168. $this->iNbCols = 2;
  169. }
  170. static public function GetInfo()
  171. {
  172. return array(
  173. 'label' => 'Two Columns',
  174. 'icon' => 'images/layout_2col.png',
  175. 'description' => '',
  176. );
  177. }
  178. }
  179. class DashboardLayoutThreeCols extends DashboardLayoutMultiCol
  180. {
  181. public function __construct()
  182. {
  183. parent::__construct();
  184. $this->iNbCols = 3;
  185. }
  186. static public function GetInfo()
  187. {
  188. return array(
  189. 'label' => 'Two Columns',
  190. 'icon' => 'images/layout_3col.png',
  191. 'description' => '',
  192. );
  193. }
  194. }