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\MatrixRowCollection;
16: use rsanchez\Deep\Collection\MatrixColCollection;
17:
18: /**
19: * Model for the matrix_data table
20: */
21: class MatrixRow extends AbstractEntity
22: {
23: /**
24: * {@inheritdoc}
25: *
26: * @var string
27: */
28: protected $table = 'matrix_data';
29:
30: /**
31: * {@inheritdoc}
32: *
33: * @var string
34: */
35: protected $primaryKey = 'row_id';
36:
37: /**
38: * {@inheritdoc}
39: */
40: protected = array('site_id', 'entry_id', 'field_id', 'var_id', 'is_draft', 'row_order');
41:
42: /**
43: * Cols associated with this row
44: * @var \rsanchez\Deep\Collection\MatrixColCollection
45: */
46: protected $cols;
47:
48: /**
49: * {@inheritdoc}
50: *
51: * @param array $models
52: * @return \rsanchez\Deep\Collection\MatrixRowCollection
53: */
54: public function newCollection(array $models = array())
55: {
56: return new MatrixRowCollection($models);
57: }
58:
59: /**
60: * {@inheritdoc}
61: */
62: public function getId()
63: {
64: return $this->row_id;
65: }
66:
67: /**
68: * {@inheritdoc}
69: */
70: public function getType()
71: {
72: return 'matrix';
73: }
74:
75: /**
76: * Filter by Entry ID
77: *
78: * @param \Illuminate\Database\Eloquent\Builder $query
79: * @param int|array $entryId
80: * @return \Illuminate\Database\Eloquent\Builder
81: */
82: public function scopeEntryId(Builder $query, $entryId)
83: {
84: $entryId = is_array($entryId) ? $entryId : array($entryId);
85:
86: return $query->whereIn('matrix_data.entry_id', $entryId);
87: }
88:
89: /**
90: * Set the Matrix columns for this row
91: *
92: * @param \rsanchez\Deep\Collection\MatrixColCollection $cols
93: * @return void
94: */
95: public function setCols(MatrixColCollection $cols)
96: {
97: $row = $this;
98:
99: $this->cols = $cols;
100:
101: $cols->each(function ($col) use ($row) {
102: $hidden = $row->getHidden();
103:
104: $hidden[] = 'col_id_'.$col->col_id;
105:
106: $row->setHidden($hidden);
107:
108: $row->setAttribute($col->col_name, $row->getAttribute('col_id_'.$col->col_id));
109: });
110: }
111:
112: public function getCols()
113: {
114: return $this->cols;
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: