@@ -92,6 +92,10 @@ |
||
92 | 92 | } |
93 | 93 | |
94 | 94 | |
95 | + /** |
|
96 | + * @param integer $maxWidth |
|
97 | + * @param integer $amount |
|
98 | + */ |
|
95 | 99 | private function checkGrid($grid, $maxWidth, $amount) { |
96 | 100 | $items = 0; |
97 | 101 | $rows = 0; |
@@ -2,69 +2,69 @@ discard block |
||
2 | 2 | |
3 | 3 | class GridRowsExtensionTest extends FunctionalTest |
4 | 4 | { |
5 | - protected static $fixture_file = 'GridRowsExtensionTest.yml'; |
|
5 | + protected static $fixture_file = 'GridRowsExtensionTest.yml'; |
|
6 | 6 | |
7 | - protected $extraDataObjects = array( |
|
7 | + protected $extraDataObjects = array( |
|
8 | 8 | 'GridRowItemTO' |
9 | 9 | ); |
10 | 10 | |
11 | 11 | public function setUp() { |
12 | - $this->requiredExtensions = array( |
|
13 | - 'GridRowItemTO' => array('GridRowsExtension') |
|
14 | - ); |
|
12 | + $this->requiredExtensions = array( |
|
13 | + 'GridRowItemTO' => array('GridRowsExtension') |
|
14 | + ); |
|
15 | 15 | |
16 | - parent::setUp(); |
|
16 | + parent::setUp(); |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | |
20 | 20 | public function testSplitDataListMethodDoesNotExist() |
21 | 21 | { |
22 | - $message = 'Method not found. A grid cannot be formed from the method' |
|
23 | - . ' GridRowItemsTHISMETHODDOESNOTEXIST'; |
|
24 | - $this->setExpectedException(\InvalidArgumentException::class, $message); |
|
22 | + $message = 'Method not found. A grid cannot be formed from the method' |
|
23 | + . ' GridRowItemsTHISMETHODDOESNOTEXIST'; |
|
24 | + $this->setExpectedException(\InvalidArgumentException::class, $message); |
|
25 | 25 | |
26 | - $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
27 | - $page->doPublish(); |
|
26 | + $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
27 | + $page->doPublish(); |
|
28 | 28 | $controller = new GridRowItemPageTO_Controller(); |
29 | 29 | $controller->setDataModel($page); |
30 | 30 | |
31 | 31 | for ($columns=1; $columns < 15; $columns++) { |
32 | - $grid = $controller->SplitDataListIntoGridRows( |
|
33 | - 'GridRowItemsTHISMETHODDOESNOTEXIST', // method from model |
|
34 | - $columns |
|
35 | - ); |
|
36 | - $this->checkGrid($grid, $columns, 10); |
|
32 | + $grid = $controller->SplitDataListIntoGridRows( |
|
33 | + 'GridRowItemsTHISMETHODDOESNOTEXIST', // method from model |
|
34 | + $columns |
|
35 | + ); |
|
36 | + $this->checkGrid($grid, $columns, 10); |
|
37 | 37 | } |
38 | 38 | } |
39 | 39 | |
40 | - public function testSplitDataListFromModelIntoGridRows() |
|
40 | + public function testSplitDataListFromModelIntoGridRows() |
|
41 | 41 | { |
42 | - $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
43 | - $page->doPublish(); |
|
42 | + $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
43 | + $page->doPublish(); |
|
44 | 44 | $controller = new GridRowItemPageTO_Controller(); |
45 | 45 | $controller->setDataModel($page); |
46 | 46 | |
47 | 47 | for ($columns=1; $columns < 15; $columns++) { |
48 | - $grid = $controller->SplitDataListIntoGridRows( |
|
49 | - 'GridRowItems', // method from model |
|
50 | - $columns |
|
51 | - ); |
|
52 | - $this->checkGrid($grid, $columns, 10); |
|
48 | + $grid = $controller->SplitDataListIntoGridRows( |
|
49 | + 'GridRowItems', // method from model |
|
50 | + $columns |
|
51 | + ); |
|
52 | + $this->checkGrid($grid, $columns, 10); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
56 | 56 | public function testSplitDataListFromControllerIntoGridRows() |
57 | 57 | { |
58 | - $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
59 | - $page->doPublish(); |
|
58 | + $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
59 | + $page->doPublish(); |
|
60 | 60 | $controller = new GridRowItemPageTO_Controller(); |
61 | 61 | $controller->setDataModel($page); |
62 | 62 | for ($columns=1; $columns < 15; $columns++) { |
63 | - $grid = $controller->SplitDataListIntoGridRows( |
|
64 | - 'GridItems', // method from controller |
|
65 | - $columns |
|
66 | - ); |
|
67 | - $this->checkGrid($grid, $columns, 10); |
|
63 | + $grid = $controller->SplitDataListIntoGridRows( |
|
64 | + 'GridItems', // method from controller |
|
65 | + $columns |
|
66 | + ); |
|
67 | + $this->checkGrid($grid, $columns, 10); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
@@ -74,67 +74,67 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function testSplitClassNameDataListIntoGridRows() |
76 | 76 | { |
77 | - $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
78 | - $page->doPublish(); |
|
77 | + $page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
|
78 | + $page->doPublish(); |
|
79 | 79 | $controller = new Page_Controller(); |
80 | 80 | $controller->setDataModel($page); |
81 | 81 | for ($columns=1; $columns < 15; $columns++) { |
82 | - for ($i=1; $i < 15; $i++) { |
|
83 | - $grid = $controller->SplitClassNameDataListIntoGridRows( |
|
84 | - 'GridRowItemTO', |
|
85 | - $columns, |
|
86 | - $i, |
|
87 | - $sort = 'LastEdited DESC'); |
|
88 | - $amount = $i > 12 ? 12: $i; |
|
89 | - $this->checkGrid($grid, $columns, $amount); |
|
90 | - } |
|
82 | + for ($i=1; $i < 15; $i++) { |
|
83 | + $grid = $controller->SplitClassNameDataListIntoGridRows( |
|
84 | + 'GridRowItemTO', |
|
85 | + $columns, |
|
86 | + $i, |
|
87 | + $sort = 'LastEdited DESC'); |
|
88 | + $amount = $i > 12 ? 12: $i; |
|
89 | + $this->checkGrid($grid, $columns, $amount); |
|
90 | + } |
|
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | 94 | |
95 | 95 | private function checkGrid($grid, $maxWidth, $amount) { |
96 | - $items = 0; |
|
97 | - $rows = 0; |
|
98 | - $widths = array(); |
|
99 | - foreach ($grid->getIterator() as $row) { |
|
100 | - $rows++; |
|
101 | - $width = 0; |
|
102 | - foreach ($row->Columns->getIterator() as $column) { |
|
103 | - $items++; |
|
104 | - $width++; |
|
105 | - } |
|
106 | - array_push($widths, $width); |
|
107 | - } |
|
108 | - |
|
109 | - // last value will be <= max width |
|
110 | - $lastVal = array_pop($widths); |
|
111 | - $this->assertLessThanOrEqual($maxWidth, $lastVal); |
|
112 | - |
|
113 | - // All but the last row should equal the expected width, $maxWidth |
|
114 | - foreach ($widths as $width) { |
|
115 | - $this->assertEquals($maxWidth, $width); |
|
116 | - } |
|
117 | - $this->assertEquals($amount, $items); |
|
96 | + $items = 0; |
|
97 | + $rows = 0; |
|
98 | + $widths = array(); |
|
99 | + foreach ($grid->getIterator() as $row) { |
|
100 | + $rows++; |
|
101 | + $width = 0; |
|
102 | + foreach ($row->Columns->getIterator() as $column) { |
|
103 | + $items++; |
|
104 | + $width++; |
|
105 | + } |
|
106 | + array_push($widths, $width); |
|
107 | + } |
|
108 | + |
|
109 | + // last value will be <= max width |
|
110 | + $lastVal = array_pop($widths); |
|
111 | + $this->assertLessThanOrEqual($maxWidth, $lastVal); |
|
112 | + |
|
113 | + // All but the last row should equal the expected width, $maxWidth |
|
114 | + foreach ($widths as $width) { |
|
115 | + $this->assertEquals($maxWidth, $width); |
|
116 | + } |
|
117 | + $this->assertEquals($amount, $items); |
|
118 | 118 | } |
119 | 119 | } |
120 | 120 | |
121 | 121 | |
122 | 122 | class GridRowItemTO extends DataObject implements TestOnly { |
123 | - private static $db = array('Name' => 'Varchar'); |
|
123 | + private static $db = array('Name' => 'Varchar'); |
|
124 | 124 | |
125 | - private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO'); |
|
125 | + private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO'); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | class GridRowItemPageTO extends Page implements TestOnly { |
129 | - private static $has_many = array('GridRowItems' => 'GridRowItemTO'); |
|
129 | + private static $has_many = array('GridRowItems' => 'GridRowItemTO'); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | class GridRowItemPageTO_Controller extends Page_Controller implements TestOnly { |
133 | 133 | |
134 | - /* |
|
134 | + /* |
|
135 | 135 | This is a test method on the *controller* |
136 | 136 | */ |
137 | - public function GridItems() { |
|
138 | - return $this->model->GridRowItems(); |
|
139 | - } |
|
137 | + public function GridItems() { |
|
138 | + return $this->model->GridRowItems(); |
|
139 | + } |
|
140 | 140 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | $controller = new GridRowItemPageTO_Controller(); |
29 | 29 | $controller->setDataModel($page); |
30 | 30 | |
31 | - for ($columns=1; $columns < 15; $columns++) { |
|
31 | + for ($columns = 1; $columns < 15; $columns++) { |
|
32 | 32 | $grid = $controller->SplitDataListIntoGridRows( |
33 | 33 | 'GridRowItemsTHISMETHODDOESNOTEXIST', // method from model |
34 | 34 | $columns |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | $controller = new GridRowItemPageTO_Controller(); |
45 | 45 | $controller->setDataModel($page); |
46 | 46 | |
47 | - for ($columns=1; $columns < 15; $columns++) { |
|
47 | + for ($columns = 1; $columns < 15; $columns++) { |
|
48 | 48 | $grid = $controller->SplitDataListIntoGridRows( |
49 | 49 | 'GridRowItems', // method from model |
50 | 50 | $columns |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | $page->doPublish(); |
60 | 60 | $controller = new GridRowItemPageTO_Controller(); |
61 | 61 | $controller->setDataModel($page); |
62 | - for ($columns=1; $columns < 15; $columns++) { |
|
62 | + for ($columns = 1; $columns < 15; $columns++) { |
|
63 | 63 | $grid = $controller->SplitDataListIntoGridRows( |
64 | 64 | 'GridItems', // method from controller |
65 | 65 | $columns |
@@ -78,14 +78,14 @@ discard block |
||
78 | 78 | $page->doPublish(); |
79 | 79 | $controller = new Page_Controller(); |
80 | 80 | $controller->setDataModel($page); |
81 | - for ($columns=1; $columns < 15; $columns++) { |
|
82 | - for ($i=1; $i < 15; $i++) { |
|
81 | + for ($columns = 1; $columns < 15; $columns++) { |
|
82 | + for ($i = 1; $i < 15; $i++) { |
|
83 | 83 | $grid = $controller->SplitClassNameDataListIntoGridRows( |
84 | 84 | 'GridRowItemTO', |
85 | 85 | $columns, |
86 | 86 | $i, |
87 | 87 | $sort = 'LastEdited DESC'); |
88 | - $amount = $i > 12 ? 12: $i; |
|
88 | + $amount = $i > 12 ? 12 : $i; |
|
89 | 89 | $this->checkGrid($grid, $columns, $amount); |
90 | 90 | } |
91 | 91 | } |
@@ -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 README.md for a worked example |
49 | 49 | |
50 | 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 | - } |
|
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 | 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 | } |