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