@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | */ |
| 6 | 6 | class GridRowsExtension extends DataExtension |
| 7 | 7 | { |
| 8 | - /* |
|
| 8 | + /* |
|
| 9 | 9 | If you are laying out using some form of grid, e.g. HTML table (ugh) or |
| 10 | 10 | bootstraps span classes it is useful to have the DataList split by row. |
| 11 | 11 | Here the DataList is generated from a method accessible to the current |
@@ -14,33 +14,33 @@ discard block |
||
| 14 | 14 | See README.md for a worked example |
| 15 | 15 | |
| 16 | 16 | */ |
| 17 | - public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols) |
|
| 18 | - { |
|
| 19 | - $methodFound = false; |
|
| 17 | + public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols) |
|
| 18 | + { |
|
| 19 | + $methodFound = false; |
|
| 20 | 20 | |
| 21 | - $itemsInGrid = null; |
|
| 21 | + $itemsInGrid = null; |
|
| 22 | 22 | |
| 23 | - // Check first the controller and then the model for the method to call |
|
| 24 | - if ($this->owner->hasMethod($itemsInGridMethod)) { |
|
| 25 | - $itemsInGrid = $this->owner->$itemsInGridMethod(); |
|
| 26 | - $methodFound = true; |
|
| 27 | - } |
|
| 23 | + // Check first the controller and then the model for the method to call |
|
| 24 | + if ($this->owner->hasMethod($itemsInGridMethod)) { |
|
| 25 | + $itemsInGrid = $this->owner->$itemsInGridMethod(); |
|
| 26 | + $methodFound = true; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - if (!$methodFound && $this->owner->model->hasMethod($itemsInGridMethod)) { |
|
| 30 | - $itemsInGrid = $this->owner->model->$itemsInGridMethod(); |
|
| 31 | - $methodFound = true; |
|
| 32 | - } |
|
| 29 | + if (!$methodFound && $this->owner->model->hasMethod($itemsInGridMethod)) { |
|
| 30 | + $itemsInGrid = $this->owner->model->$itemsInGridMethod(); |
|
| 31 | + $methodFound = true; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - if ($itemsInGrid == null) { |
|
| 35 | - $message = 'Method not found. A grid cannot be formed from the ' |
|
| 36 | - . 'method ' . $itemsInGridMethod; |
|
| 37 | - throw new InvalidArgumentException($message); |
|
| 38 | - } |
|
| 34 | + if ($itemsInGrid == null) { |
|
| 35 | + $message = 'Method not found. A grid cannot be formed from the ' |
|
| 36 | + . 'method ' . $itemsInGridMethod; |
|
| 37 | + throw new InvalidArgumentException($message); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $this->createGrid($itemsInGrid, $numberOfCols); |
|
| 41 | - } |
|
| 40 | + return $this->createGrid($itemsInGrid, $numberOfCols); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /* |
|
| 43 | + /* |
|
| 44 | 44 | If you are laying out using some form of grid, e.g. HTML table (ugh) or |
| 45 | 45 | bootstraps span classes it is useful to have the DataList split by row. |
| 46 | 46 | This is what this method does. |
@@ -48,40 +48,40 @@ discard block |
||
| 48 | 48 | See USAGE.md for a worked example |
| 49 | 49 | |
| 50 | 50 | */ |
| 51 | - public function SplitClassNameDataListIntoGridRows( |
|
| 52 | - $className, $numberOfCols, $limit = 10, $sort = 'LastEdited DESC') |
|
| 53 | - { |
|
| 54 | - $clazz = Injector::inst()->create($className); |
|
| 55 | - $itemsInGrid = $clazz->get()->limit($limit)->sort($sort); |
|
| 56 | - return $this->createGrid($itemsInGrid, $numberOfCols); |
|
| 57 | - } |
|
| 51 | + public function SplitClassNameDataListIntoGridRows( |
|
| 52 | + $className, $numberOfCols, $limit = 10, $sort = 'LastEdited DESC') |
|
| 53 | + { |
|
| 54 | + $clazz = Injector::inst()->create($className); |
|
| 55 | + $itemsInGrid = $clazz->get()->limit($limit)->sort($sort); |
|
| 56 | + return $this->createGrid($itemsInGrid, $numberOfCols); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /* |
|
| 59 | + /* |
|
| 60 | 60 | The actual method that splits the DataList into an ArrayList of rows that |
| 61 | 61 | contain an ArrayList of Columns |
| 62 | 62 | */ |
| 63 | - private function createGrid($itemsInGrid, $numberOfCols) |
|
| 64 | - { |
|
| 65 | - $position = 1; |
|
| 66 | - $columns = new ArrayList(); |
|
| 67 | - $result = new ArrayList(); |
|
| 68 | - foreach ($itemsInGrid as $key => $item) { |
|
| 69 | - $columns->push($item); |
|
| 70 | - if (($position) >= $numberOfCols) { |
|
| 71 | - $position = 1; |
|
| 72 | - $row = new ArrayList(); |
|
| 73 | - $row->Columns = $columns; |
|
| 74 | - $result->push($row); |
|
| 75 | - $columns = new ArrayList(); |
|
| 76 | - } else { |
|
| 77 | - $position = $position + 1; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - if ($columns->Count() > 0) { |
|
| 81 | - $row = new ArrayList(); |
|
| 82 | - $row->Columns = $columns; |
|
| 83 | - $result->push($row); |
|
| 84 | - } |
|
| 85 | - return $result; |
|
| 86 | - } |
|
| 63 | + private function createGrid($itemsInGrid, $numberOfCols) |
|
| 64 | + { |
|
| 65 | + $position = 1; |
|
| 66 | + $columns = new ArrayList(); |
|
| 67 | + $result = new ArrayList(); |
|
| 68 | + foreach ($itemsInGrid as $key => $item) { |
|
| 69 | + $columns->push($item); |
|
| 70 | + if (($position) >= $numberOfCols) { |
|
| 71 | + $position = 1; |
|
| 72 | + $row = new ArrayList(); |
|
| 73 | + $row->Columns = $columns; |
|
| 74 | + $result->push($row); |
|
| 75 | + $columns = new ArrayList(); |
|
| 76 | + } else { |
|
| 77 | + $position = $position + 1; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + if ($columns->Count() > 0) { |
|
| 81 | + $row = new ArrayList(); |
|
| 82 | + $row->Columns = $columns; |
|
| 83 | + $result->push($row); |
|
| 84 | + } |
|
| 85 | + return $result; |
|
| 86 | + } |
|
| 87 | 87 | } |