|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Helper methods to lay out DataObjects in a grid of rows and colums to a |
|
4
|
|
|
* required size. These methods are intended to be called from a template |
|
5
|
|
|
*/ |
|
6
|
|
|
class GridRowsExtension extends DataExtension |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
/* |
|
9
|
|
|
If you are laying out using some form of grid, e.g. HTML table (ugh) or |
|
10
|
|
|
bootstraps span classes it is useful to have the DataList split by row. |
|
11
|
|
|
Here the DataList is generated from a method accessible to the current |
|
12
|
|
|
controller |
|
13
|
|
|
|
|
14
|
|
|
See README.md for a worked example |
|
15
|
|
|
|
|
16
|
|
|
*/ |
|
17
|
|
|
public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$methodFound = false; |
|
20
|
|
|
|
|
21
|
|
|
$itemsInGrid = null; |
|
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
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (!$methodFound && $this->owner->model->hasMethod($itemsInGridMethod)) { |
|
30
|
|
|
$itemsInGrid = $this->owner->model->$itemsInGridMethod(); |
|
31
|
|
|
$methodFound = true; |
|
|
|
|
|
|
32
|
|
|
} |
|
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
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->createGrid($itemsInGrid, $numberOfCols); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/* |
|
44
|
|
|
If you are laying out using some form of grid, e.g. HTML table (ugh) or |
|
45
|
|
|
bootstraps span classes it is useful to have the DataList split by row. |
|
46
|
|
|
This is what this method does. |
|
47
|
|
|
|
|
48
|
|
|
See README.md for a worked example |
|
49
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public function SplitClassNameDataListIntoGridRows( |
|
|
|
|
|
|
52
|
|
|
$className, $numberOfCols, $limit, $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
|
|
|
|
|
59
|
|
|
/* |
|
60
|
|
|
The actual method that splits the DataList into an ArrayList of rows that |
|
61
|
|
|
contain an ArrayList of Columns |
|
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
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.