1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Model;
11:
12: use Illuminate\Database\Eloquent\Model;
13: use Illuminate\Database\Eloquent\Builder;
14: use rsanchez\Deep\Model\AbstractEntity;
15: use rsanchez\Deep\Collection\GridRowCollection;
16: use rsanchez\Deep\Collection\GridColCollection;
17:
18: /**
19: * Model for the channel_grid_field_X table(s)
20: */
21: class GridRow extends AbstractEntity
22: {
23: /**
24: * {@inheritdoc}
25: *
26: * @var string
27: */
28: protected $primaryKey = 'row_id';
29:
30: /**
31: * {@inheritdoc}
32: */
33: protected = array('site_id', 'entry_id', 'field_id', 'row_order');
34:
35: /**
36: * Cols associated with this row
37: * @var \rsanchez\Deep\Collection\GridColCollection
38: */
39: protected $cols;
40:
41: /**
42: * {@inheritdoc}
43: *
44: * @param array $models
45: * @return \rsanchez\Deep\Collection\GridRowCollection
46: */
47: public function newCollection(array $models = array())
48: {
49: return new GridRowCollection($models);
50: }
51:
52: /**
53: * {@inheritdoc}
54: */
55: public function getId()
56: {
57: return $this->row_id;
58: }
59:
60: /**
61: * {@inheritdoc}
62: */
63: public function getType()
64: {
65: return 'grid';
66: }
67:
68: /**
69: * Filter by Entry ID
70: *
71: * @param \Illuminate\Database\Eloquent\Builder $query
72: * @param int|array $entryId
73: * @return \Illuminate\Database\Eloquent\Builder
74: */
75: public function scopeEntryId(Builder $query, $entryId)
76: {
77: $entryId = is_array($entryId) ? $entryId : array($entryId);
78:
79: return $query->whereIn('entry_id', $entryId);
80: }
81:
82: /**
83: * Filter by Field ID
84: *
85: * @param \Illuminate\Database\Eloquent\Builder $query
86: * @param int $fieldId
87: * @return \Illuminate\Database\Eloquent\Builder
88: */
89: public function scopeFieldId(Builder $query, $fieldId)
90: {
91: return $query->from('channel_grid_field_'.$fieldId);
92: }
93:
94: /**
95: * Set the Grid columns for this row
96: *
97: * @param \rsanchez\Deep\Collection\GridColCollection $cols
98: * @return void
99: */
100: public function setCols(GridColCollection $cols)
101: {
102: $row = $this;
103:
104: $this->cols = $cols;
105:
106: $cols->each(function ($col) use ($row) {
107: $hidden = $row->getHidden();
108:
109: $hidden[] = 'col_id_'.$col->col_id;
110:
111: $row->setHidden($hidden);
112:
113: $row->setAttribute($col->col_name, $row->getAttribute('col_id_'.$col->col_id));
114: });
115: }
116:
117: /**
118: * {@inheritdoc}
119: */
120: public function toArray()
121: {
122: $array = parent::toArray();
123:
124: foreach ($array as &$row) {
125: if (method_exists($row, 'toArray')) {
126: $row = $row->toArray();
127: }
128: }
129:
130: return $array;
131: }
132: }
133: