1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30: 31:
32: class GridView extends RepeatingView
33: {
34: private $columns;
35: private $rows = array();
36: private $columnId;
37: private $callback;
38:
39: public function __construct($id, $columnId, $columns, $callback = null, $model = null)
40: {
41: parent::__construct($id, $model);
42:
43: if($callback!=null)
44: {
45: Args::callBackArgs($callback, 1, 'callback');
46: }
47: $this->columns = $columns;
48: $this->columnId = $columnId;
49: $this->callback = $callback;
50: }
51:
52: public function getColumns()
53: {
54: return $this->columns;
55: }
56:
57: protected function populate()
58: {
59: $this->removeAll();
60:
61: foreach($this->getRecords() as $index => $object)
62: {
63: $this->populateRow(new BasicModel($object), $index);
64: }
65: }
66:
67: protected function populateRow(Model $model, $index)
68: {
69: $row = new ListItem($this->getNextChildId(), $model, $index);
70: array_push($this->rows, $row);
71: $this->add($row);
72: $cells = new RepeatingView($this->columnId);
73: $row->add($cells);
74: for($i = 0; $i < $this->getColumns(); $i++)
75: {
76: $item = new GridItem($cells->getNextChildId(), $model, $index, $i);
77: $cells->add($item);
78: $this->populateItem($item);
79: }
80: }
81:
82: protected function removeAll()
83: {
84: foreach($this->rows as $row)
85: {
86: $this->remove($row);
87: }
88: $this->rows = array();
89: }
90:
91: protected function populateItem(GridItem $item)
92: {
93: if($this->callback==null)
94: {
95: throw new \IllegalStateException('When not passing a callback to GridView it is expected that the populateItem() method will be overriden');
96: }
97: $callablel = $this->callback;
98: $callablel($item);
99: }
100:
101: protected function getRecords()
102: {
103: return $this->getModelObject();
104: }
105: }
106:
107: ?>
108: