PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/zurmoinc/framework/views/GridView.php

https://bitbucket.org/zurmo/zurmo/
PHP | 146 lines | 93 code | 6 blank | 47 comment | 11 complexity | d08326e828b3536a3045023828b19aeb MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. /**
  27. * A view that renders contained views in a grid.
  28. */
  29. class GridView extends View
  30. {
  31. private $rows;
  32. private $columns;
  33. private $columnWidths;
  34. private $containedViews;
  35. /**
  36. * Constructs a GridView specifying a its number
  37. * of rows and columns.
  38. */
  39. public function __construct($rows, $columns)
  40. {
  41. assert('is_int($rows) && $rows > 0');
  42. assert('is_int($columns) && $columns > 0');
  43. $this->rows = $rows;
  44. $this->columns = $columns;
  45. $this->columnWidths = array_pad(array(), $columns, 0);
  46. $containedViews = array_pad(array(), $rows, null);
  47. for ($row = 0; $row < $rows; $row++)
  48. {
  49. $this->containedViews[] = array_pad(array(), $columns, null);
  50. }
  51. }
  52. /**
  53. * Overridden from View, specifies that GridView is not unique
  54. * to a page.
  55. * @see View::isUniqueToAPage()
  56. */
  57. public function isUniqueToAPage()
  58. {
  59. return false;
  60. }
  61. /**
  62. * Sets the contained view that will be rendered in a
  63. * specify cell of the grid.
  64. */
  65. public function setView(View $view, $row, $column)
  66. {
  67. assert('is_int($row) && $row >= 0 && $row < $this->rows');
  68. assert('is_int($column) && $column >= 0 && $column < $this->columns');
  69. $this->containedViews[$row][$column] = $view;
  70. }
  71. /**
  72. * Sets the width of the column in pixels.
  73. */
  74. public function setColumnWidth($column, $width)
  75. {
  76. assert('is_int($column) && $column >= 0 && $column < $this->columns');
  77. assert('is_int($width) && $width > 0');
  78. $this->columnWidths[$column] = $width;
  79. }
  80. protected function renderContent()
  81. {
  82. // The if ($this->rows > 1) and if ($this->columns > 1)
  83. // checks make it only generate as many divs as it needs
  84. // to. If there is only one row or one columns it wont
  85. // wrap it in another div.
  86. $content = null;
  87. for ($row = 0; $row < $this->rows; $row++)
  88. {
  89. $rowContent = null;
  90. if ($this->rows > 1)
  91. {
  92. $rowContent .= "<div>\n";
  93. }
  94. $totalColumnsWidth = null;
  95. for ($column = 0; $column < $this->columns; $column++)
  96. {
  97. $columnContent = null;
  98. if ($this->columns > 1)
  99. {
  100. $styles = array();
  101. if ($column < $this->columns - 1)
  102. {
  103. $styles[] = 'float: left;';
  104. }
  105. if (isset($totalColumnsWidth))
  106. {
  107. $styles[] = "margin-left: {$totalColumnsWidth}px;";
  108. }
  109. if ($this->columnWidths[$column] > 0)
  110. {
  111. $styles[] = 'width: ' . $this->columnWidths[$column] . 'px;';
  112. $totalColumnsWidth += $this->columnWidths[$column];
  113. }
  114. if (count($styles) > 0)
  115. {
  116. $style = ' style="' . join($styles, ' ') . '"';
  117. }
  118. else
  119. {
  120. $style = '';
  121. }
  122. $columnContent .= "<div$style>\n";
  123. }
  124. $columnContent .= $this->containedViews[$row][$column]->render();
  125. if ($this->columns > 1)
  126. {
  127. $columnContent .= "</div>\n";
  128. }
  129. $rowContent .= $columnContent;
  130. }
  131. if ($this->rows > 1)
  132. {
  133. $rowContent .= "</div>\n";
  134. }
  135. $content .= $rowContent;
  136. }
  137. return $content;
  138. }
  139. }
  140. ?>