dashboardlayout.class.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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%;table-layout:fixed;"><tbody>');
  95. $iCellIdx = 0;
  96. $fColSize = 100 / $this->iNbCols;
  97. $sStyle = $bEditMode ? 'border: 1px #ccc dashed; width:'.$fColSize.'%;' : 'width: '.$fColSize.'%;';
  98. $sClass = $bEditMode ? 'layout_cell edit_mode' : 'dashboard';
  99. $iNbRows = ceil(count($aCells) / $this->iNbCols);
  100. for($iRows = 0; $iRows < $iNbRows; $iRows++)
  101. {
  102. $oPage->add('<tr>');
  103. for($iCols = 0; $iCols < $this->iNbCols; $iCols++)
  104. {
  105. $sCellClass = ($iRows == $iNbRows-1) ? $sClass.' layout_last_used_rank' : $sClass;
  106. $oPage->add("<td style=\"$sStyle\" class=\"$sCellClass\" data-dashboard-cell-index=\"$iCellIdx\">");
  107. if (array_key_exists($iCellIdx, $aCells))
  108. {
  109. $aDashlets = $aCells[$iCellIdx];
  110. if (count($aDashlets) > 0)
  111. {
  112. foreach($aDashlets as $oDashlet)
  113. {
  114. if ($oDashlet->IsVisible())
  115. {
  116. $oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams);
  117. }
  118. }
  119. }
  120. else
  121. {
  122. $oPage->add('&nbsp;');
  123. }
  124. }
  125. else
  126. {
  127. $oPage->add('&nbsp;');
  128. }
  129. $oPage->add('</td>');
  130. $iCellIdx++;
  131. }
  132. $oPage->add('</tr>');
  133. }
  134. if ($bEditMode) // Add one row for extensibility
  135. {
  136. $sStyle = 'style="border: 1px #ccc dashed; width:'.$fColSize.'%;" class="layout_cell edit_mode layout_extension" data-dashboard-cell-index="'.$iCellIdx.'"';
  137. $oPage->add('<tr>');
  138. for($iCols = 0; $iCols < $this->iNbCols; $iCols++)
  139. {
  140. $oPage->add("<td $sStyle>");
  141. $oPage->add('&nbsp;');
  142. $oPage->add('</td>');
  143. }
  144. $oPage->add('</tr>');
  145. }
  146. $oPage->add('</tbody></table>');
  147. }
  148. }
  149. class DashboardLayoutOneCol extends DashboardLayoutMultiCol
  150. {
  151. public function __construct()
  152. {
  153. parent::__construct();
  154. $this->iNbCols = 1;
  155. }
  156. static public function GetInfo()
  157. {
  158. return array(
  159. 'label' => 'One Column',
  160. 'icon' => 'images/layout_1col.png',
  161. 'description' => '',
  162. );
  163. }
  164. }
  165. class DashboardLayoutTwoCols extends DashboardLayoutMultiCol
  166. {
  167. public function __construct()
  168. {
  169. parent::__construct();
  170. $this->iNbCols = 2;
  171. }
  172. static public function GetInfo()
  173. {
  174. return array(
  175. 'label' => 'Two Columns',
  176. 'icon' => 'images/layout_2col.png',
  177. 'description' => '',
  178. );
  179. }
  180. }
  181. class DashboardLayoutThreeCols extends DashboardLayoutMultiCol
  182. {
  183. public function __construct()
  184. {
  185. parent::__construct();
  186. $this->iNbCols = 3;
  187. }
  188. static public function GetInfo()
  189. {
  190. return array(
  191. 'label' => 'Two Columns',
  192. 'icon' => 'images/layout_3col.png',
  193. 'description' => '',
  194. );
  195. }
  196. }