phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlGrid.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
18 class HtmlGrid extends HtmlSemCollection {
20  private $_createCols;
21  private $_colSizing=true;
22  private $_implicitRows=false;
23 
24  public function __construct($identifier, $numRows=1, $numCols=NULL, $createCols=true, $implicitRows=false) {
25  parent::__construct($identifier, "div", "ui grid");
26  $this->_implicitRows=$implicitRows;
27  $this->_createCols=$createCols;
28  if (isset($numCols)) {
29  $this->_colSizing=false;
30  $this->setWide($numCols);
31  }
32  if($createCols)
33  $this->setRowsCount($numRows, $numCols);
34  }
35 
36  public function asSegment() {
37  return $this->addToPropertyCtrl("class", "segment", array ("segment" ));
38  }
39 
40  public function asContainer() {
41  return $this->addToPropertyCtrl("class", "container", array ("container" ));
42  }
43 
48  public function setWide($wide) {
49  if(isset(Wide::getConstants()["W" . $wide])){
50  $wide=Wide::getConstants()["W" . $wide];
51  $this->addToPropertyCtrl("class", $wide, Wide::getConstants());
52  return $this->addToPropertyCtrl("class", "column", array ("column" ));
53  }
54  return $this;
55  }
56 
62  public function setWidth($width) {
63  return $this->setWide($width);
64  }
65 
71  public function addRow($colsCount=NULL) {
72  $rowCount=$this->rowCount() + 1;
73  $this->setRowsCount($rowCount, $colsCount, true);
74  return $this->content[$rowCount - 1];
75  }
76 
82  public function addCol($width=NULL) {
83  $colCount=$this->colCount() + 1;
84  $this->setColsCount($colCount, true, $width);
85  if ($this->hasOnlyCols($this->count()))
86  return $this->content[$colCount - 1];
87  return $this;
88  }
89 
95  public function addCols($sizes=array()) {
96  foreach ( $sizes as $size ) {
97  $this->addCol($size);
98  }
99  return $this;
100  }
101 
108  public function setRowsCount($rowsCount, $colsCount=NULL, $force=false) {
109  $count=$this->count();
110  if ($rowsCount < 2 && $force === false) {
111  for($i=$count; $i < $colsCount; $i++) {
112  $this->addItem(new HtmlGridCol("col-" . $this->identifier . "-" . $i));
113  }
114  } else {
115  if ($this->hasOnlyCols($count)) {
116  $tmpContent=$this->content;
117  $item=$this->addItem($colsCount);
118  $item->setContent($tmpContent);
119  $this->content=array ();
120  $count=1;
121  }
122  for($i=$count; $i < $rowsCount; $i++) {
123  $this->addItem($colsCount);
124  }
125  }
126  return $this;
127  }
128 
129  protected function hasOnlyCols($count) {
130  return $count > 0 && $this->content[0] instanceof HtmlGridCol;
131  }
132 
140  public function setColsCount($numCols, $toCreate=true, $width=NULL) {
141  if (isset($width)===false) {
142  $this->setWide($numCols);
143  }
144  if ($toCreate === true) {
145  $count=$this->count();
146  if ($count == 0 || $this->hasOnlyCols($count)) {
147  for($i=$count; $i < $numCols; $i++) {
148  $this->addItem(new HtmlGridCol("col-" . $this->identifier . "-" . $i, $width));
149  }
150  } else {
151  for($i=0; $i < $count; $i++) {
152  $this->getItem($i)->setColsCount($numCols);
153  }
154  }
155  }
156  return $this;
157  }
158 
164  public function getRow($index) {
165  return $this->getItem($index);
166  }
167 
171  public function getItem($index){
172  return parent::getItem($index);
173  }
174 
179  public function rowCount() {
180  $count=$this->count();
181  if ($this->hasOnlyCols($count))
182  return 0;
183  return $count;
184  }
185 
190  public function colCount() {
191  $count=$this->count();
192  if ($this->hasOnlyCols($count))
193  return $count;
194  if ($count > 0)
195  return $this->getItem(0)->count();
196  return 0;
197  }
198 
205  public function getCell($row, $col) {
206  if ($row < 2 && $this->hasOnlyCols($this->count()))
207  return $this->getItem($col);
208  $rowO=$this->getItem($row);
209  if (isset($rowO)) {
210  $colO=$rowO->getItem($col);
211  }
212  return $colO;
213  }
214 
220  public function setDivided($vertically=false) {
221  $value=($vertically === true) ? "vertically divided" : "divided";
222  return $this->addToPropertyCtrl("class", $value, array ("divided" ));
223  }
224 
230  public function setCelled($internally=false) {
231  $value=($internally === true) ? "internally celled" : "celled";
232  return $this->addToPropertyCtrl("class", $value, array ("celled","internally celled" ));
233  }
234 
238  public function setCentered() {
239  return $this->addToPropertyCtrl("class", "centered", array ("centered" ));
240  }
241 
246  public function setEqualWidth() {
247  return $this->addToProperty("class", "equal width");
248  }
249 
255  public function setPadded($value=NULL) {
256  if (isset($value))
257  $this->addToPropertyCtrl("class", $value, array ("vertically","horizontally" ));
258  return $this->addToProperty("class", "padded");
259  }
260 
266  public function setRelaxed($very=false) {
267  $value=($very === true) ? "very relaxed" : "relaxed";
268  return $this->addToPropertyCtrl("class", $value, array ("relaxed","very relaxed" ));
269  }
270 
272  return $this->addToPropertyCtrl("class", $value . " aligned", VerticalAlignment::getConstantValues("aligned"));
273  }
274 
281  protected function createItem($value) {
282  if ($this->_createCols === false)
283  $value=null;
284  $item=new HtmlGridRow($this->identifier . "-row-" . ($this->count() + 1), $value, $this->_colSizing, $this->_implicitRows);
285  return $item;
286  }
287 
292  public function setValues($values, $force=true) {
293  $count=$this->count();
294  $valuesSize=\sizeof($values);
295  if ($this->_createCols === false || $force === true) {
296  for($i=$count; $i < $valuesSize; $i++) {
297  $colSize=\sizeof($values[$i]);
298  $this->addItem(new HtmlGridRow($this->identifier . "-row-" . ($this->count() + 1), $colSize, $this->_colSizing, $this->_implicitRows));
299  }
300  }
301  $count=\min(array ($this->count(),$valuesSize ));
302  for($i=0; $i < $count; $i++) {
303  $this->content[$i]->setValues($values[$i], $this->_createCols === false);
304  }
305  }
306 
307  public function setColWidth($numCol,$width){
308  foreach ($this->content as $row){
309  $row->getCol($numCol)->setWidth($width);
310  }
311  return $this;
312  }
313 
318  public function setStretched() {
319  return $this->addToProperty("class", "stretched");
320  }
321 
329  public function addDivider($afterColIndex, $vertical=true, $content=NULL) {
330  $col=$this->getCell(0, $afterColIndex);
331  if($col instanceof HtmlGridCol)
332  $col->addDivider($vertical, $content);
333  return $this;
334  }
335 }
addDivider($afterColIndex, $vertical=true, $content=NULL)
Adds a divider after the specified col.
Definition: HtmlGrid.php:329
addCol($width=NULL)
Adds a col.
Definition: HtmlGrid.php:82
addToProperty($name, $value, $separator=" ")
getRow($index)
return the row at $index
Definition: HtmlGrid.php:164
static getConstants()
Definition: BaseEnum.php:17
getCell($row, $col)
Returns the cell (HtmlGridCol) at position $row,$col.
Definition: HtmlGrid.php:205
setCentered()
A grid can have its columns centered.
Definition: HtmlGrid.php:238
setValues($values, $force=true)
Sets $values to the grid.
Definition: HtmlGrid.php:292
A row for the Semantic Grid component.
Definition: HtmlGridRow.php:19
Semantic Grid component.
Definition: HtmlGrid.php:18
addItem($item)
adds and returns an item
rowCount()
Returns the row count.
Definition: HtmlGrid.php:179
colCount()
Returns the column count.
Definition: HtmlGrid.php:190
addRow($colsCount=NULL)
Adds a row with $colsCount columns.
Definition: HtmlGrid.php:71
setDivided($vertically=false)
Adds dividers between columns ($vertically=false) or between rows ($vertically=true) ...
Definition: HtmlGrid.php:220
setWidth($width)
Defines the grid width.
Definition: HtmlGrid.php:62
__construct($identifier, $numRows=1, $numCols=NULL, $createCols=true, $implicitRows=false)
Definition: HtmlGrid.php:24
static getConstantValues($postFix="", $prefixBefore=false)
Definition: BaseEnum.php:29
setStretched()
stretch the row contents to take up the entire column height
Definition: HtmlGrid.php:318
setEqualWidth()
automatically resize all elements to split the available width evenly
Definition: HtmlGrid.php:246
setPadded($value=NULL)
Adds vertical or/and horizontal gutters.
Definition: HtmlGrid.php:255
A col in the Semantic Grid component.
Definition: HtmlGridCol.php:16
setRowsCount($rowsCount, $colsCount=NULL, $force=false)
Create $rowsCount rows.
Definition: HtmlGrid.php:108
Base class for Semantic Html collections.
setCelled($internally=false)
Divides rows into cells.
Definition: HtmlGrid.php:230
setVerticalAlignment($value=VerticalAlignment::MIDDLE)
Definition: HtmlGrid.php:271
setColsCount($numCols, $toCreate=true, $width=NULL)
Defines the number of columns in the grid.
Definition: HtmlGrid.php:140
addToPropertyCtrl($name, $value, $typeCtrl)
setWide($wide)
Defines the grid width (alias for setWidth)
Definition: HtmlGrid.php:48