1 | <?php |
||
6 | use SilverStripe\ORM\ArrayList; |
||
7 | use SilverStripe\ORM\DataExtension; |
||
8 | /** |
||
9 | * Helper methods to lay out DataObjects in a grid of rows and colums to a |
||
10 | * required size. These methods are intended to be called from a template |
||
11 | */ |
||
12 | class GridRowsExtension extends DataExtension |
||
13 | { |
||
14 | /* |
||
15 | If you are laying out using some form of grid, e.g. HTML table (ugh) or |
||
16 | bootstraps span classes it is useful to have the DataList split by row. |
||
17 | 3 | Here the DataList is generated from a method accessible to the current |
|
18 | controller |
||
19 | 3 | ||
20 | 3 | See README.md for a worked example |
|
21 | |||
22 | */ |
||
23 | 3 | public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols) |
|
47 | |||
48 | /* |
||
49 | If you are laying out using some form of grid, e.g. HTML table (ugh) or |
||
50 | 1 | bootstraps span classes it is useful to have the DataList split by row. |
|
51 | This is what this method does. |
||
52 | |||
53 | 1 | See USAGE.md for a worked example |
|
54 | 1 | ||
55 | 1 | */ |
|
56 | public function SplitClassNameDataListIntoGridRows( |
||
66 | 2 | ||
67 | 2 | /* |
|
68 | 2 | The actual method that splits the DataList into an ArrayList of rows that |
|
69 | 2 | contain an ArrayList of Columns |
|
70 | 2 | */ |
|
71 | 2 | private function createGrid($itemsInGrid, $numberOfCols) |
|
72 | 2 | { |
|
73 | 2 | $position = 1; |
|
74 | 2 | $columns = new ArrayList(); |
|
75 | 2 | $result = new ArrayList(); |
|
76 | 2 | foreach ($itemsInGrid as $key => $item) { |
|
77 | $columns->push($item); |
||
78 | 2 | if (($position) >= $numberOfCols) { |
|
79 | 2 | $position = 1; |
|
80 | 2 | $row = new ArrayList(); |
|
81 | 2 | $row->Columns = $columns; |
|
82 | 2 | $result->push($row); |
|
83 | 2 | $columns = new ArrayList(); |
|
84 | 2 | } else { |
|
85 | $position = $position + 1; |
||
86 | } |
||
87 | 1 | } |
|
88 | if ($columns->Count() > 0) { |
||
89 | $row = new ArrayList(); |
||
90 | $row->Columns = $columns; |
||
96 |