Source of file GridRowsExtension.php
Size: 3,135 Bytes - Last Modified: 2021-12-23T10:58:54+00:00
/var/www/docs.ssmods.com/process/src/code/GridRowsExtension.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | <?php namespace WebOfTalent\GridRows; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataExtension; /** * Helper methods to lay out DataObjects in a grid of rows and colums to a * required size. These methods are intended to be called from a template */ class GridRowsExtension extends DataExtension { /* If you are laying out using some form of grid, e.g. HTML table (ugh) or bootstraps span classes it is useful to have the DataList split by row. Here the DataList is generated from a method accessible to the current controller See README.md for a worked example Note, this method should have been called SplitDataListMethodIntoGridRows */ public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols) { $methodFound = false; $itemsInGrid = null; // Check first the controller and then the model for the method to call if ($this->owner->hasMethod($itemsInGridMethod)) { $itemsInGrid = $this->owner->$itemsInGridMethod(); $methodFound = true; } if (!$methodFound && method_exists($this->owner->model, $itemsInGridMethod)) { $itemsInGrid = $this->owner->model->$itemsInGridMethod(); $methodFound = true; } if ($itemsInGrid == null) { $message = 'Method not found. A grid cannot be formed from the ' . 'method ' . $itemsInGridMethod; throw new \InvalidArgumentException($message); } return $this->createGridLayout($itemsInGrid, $numberOfCols); } /* If you are laying out using some form of grid, e.g. HTML table (ugh) or bootstraps span classes it is useful to have the DataList split by row. This is what this method does. See USAGE.md for a worked example */ public function SplitClassNameDataListIntoGridRows( $className, $numberOfCols, $limit = 10, $sort = 'LastEdited DESC' ) { $clazz = Injector::inst()->create($className); $itemsInGrid = $clazz->get()->limit($limit)->sort($sort); return $this->createGridLayout($itemsInGrid, $numberOfCols); } /* The actual method that splits the DataList into an ArrayList of rows that contain an ArrayList of Columns */ public function createGridLayout($itemsInGrid, $numberOfCols) { $position = 1; $columns = new ArrayList(); $result = new ArrayList(); foreach ($itemsInGrid as $key => $item) { $columns->push($item); if (($position) >= $numberOfCols) { $position = 1; $row = new ArrayList(); $row->Columns = $columns; $result->push($row); $columns = new ArrayList(); } else { $position = $position + 1; } } if ($columns->Count() > 0) { $row = new ArrayList(); $row->Columns = $columns; $result->push($row); } return $result; } } |