1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebOfTalent\GridRows; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
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) |
|
|
|
|
24
|
1 |
|
{ |
25
|
1 |
|
$methodFound = false; |
26
|
1 |
|
$itemsInGrid = null; |
27
|
|
|
|
28
|
3 |
|
// Check first the controller and then the model for the method to call |
29
|
|
|
if ($this->owner->hasMethod($itemsInGridMethod)) { |
30
|
|
|
$itemsInGrid = $this->owner->$itemsInGridMethod(); |
31
|
|
|
$methodFound = true; |
32
|
|
|
} |
33
|
3 |
|
|
34
|
|
|
if (!$methodFound && method_exists($this->owner->model, $itemsInGridMethod)) { |
35
|
2 |
|
$itemsInGrid = $this->owner->model->$itemsInGridMethod(); |
36
|
2 |
|
$methodFound = true; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
if ($itemsInGrid == null) { |
40
|
|
|
$message = 'Method not found. A grid cannot be formed from the ' |
41
|
|
|
. 'method ' . $itemsInGridMethod; |
42
|
|
|
throw new \InvalidArgumentException($message); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->createGrid($itemsInGrid, $numberOfCols); |
46
|
|
|
} |
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( |
|
|
|
|
57
|
|
|
$className, |
58
|
|
|
$numberOfCols, |
59
|
|
|
$limit = 10, |
60
|
|
|
$sort = 'LastEdited DESC' |
61
|
|
|
) { |
62
|
2 |
|
$clazz = Injector::inst()->create($className); |
63
|
|
|
$itemsInGrid = $clazz->get()->limit($limit)->sort($sort); |
64
|
2 |
|
return $this->createGrid($itemsInGrid, $numberOfCols); |
65
|
2 |
|
} |
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; |
91
|
|
|
$result->push($row); |
92
|
|
|
} |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|