1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Collection;
11:
12: use Illuminate\Database\Eloquent\Collection;
13: use rsanchez\Deep\Collection\MatrixColCollection;
14: use rsanchez\Deep\Collection\GridColCollection;
15: use rsanchez\Deep\Collection\TitleCollection;
16: use rsanchez\Deep\Model\Field;
17: use rsanchez\Deep\Collection\FieldCollection;
18: use rsanchez\Deep\Repository\ChannelRepository;
19: use rsanchez\Deep\Repository\FieldRepository;
20:
21: /**
22: * Collection of \rsanchez\Deep\Model\Entry
23: */
24: class EntryCollection extends TitleCollection
25: {
26: /**
27: * Matrix columns used by this collection
28: * @var \rsanchez\Deep\Collection\MatrixColCollection
29: */
30: protected $matrixCols;
31:
32: /**
33: * Grid columns used by this collection
34: * @var \rsanchez\Deep\Collection\GridColCollection
35: */
36: protected $gridCols;
37:
38: /**
39: * Fields used by this collection
40: * @var \rsanchez\Deep\Collection\FieldCollection
41: */
42: protected $fields;
43:
44: /**
45: * Instantiate a collection of models
46: * @param array $models
47: * @param \rsanchez\Deep\Repository\ChannelRepository $channelRepository
48: * @param \rsanchez\Deep\Repository\FieldRepository $fieldRepository
49: * @return \rsanchez\Deep\Collection\EntryCollection
50: */
51: public static function createWithFields(array $models, ChannelRepository $channelRepository, FieldRepository $fieldRepository)
52: {
53: $collection = self::create($models, $channelRepository);
54:
55: $collection->setFieldRepository($fieldRepository);
56:
57: $channels = $collection->getChannels();
58:
59: $fields = $fieldRepository->getFieldsByChannelCollection($channels);
60:
61: $collection->setFields($fields);
62:
63: return $collection;
64: }
65:
66: /**
67: * {@inheritdoc}
68: */
69: public function createChildCollection(array $models)
70: {
71: return self::createWithFields($models, $this->channelRepository, $this->fieldRepository);
72: }
73:
74: /**
75: * Set the Field Repository
76: * @param \rsanchez\Deep\Repository\FieldRepository $fieldRepository
77: * @return void
78: */
79: public function setFieldRepository(FieldRepository $fieldRepository)
80: {
81: $this->fieldRepository = $fieldRepository;
82: }
83:
84: /**
85: * Get a list of names of fieldtypes used by this collection
86: * @return array
87: */
88: public function getFieldtypes()
89: {
90: return $this->fields->getFieldtypes();
91: }
92:
93: /**
94: * Get the fields used by this collection
95: * @return \rsanchez\Deep\Collection\FieldCollection
96: */
97: public function getFields()
98: {
99: return $this->fields;
100: }
101:
102: /**
103: * Get the fields used by this collection
104: * @param \rsanchez\Deep\Collection\FieldCollection
105: * @return void
106: */
107: public function setFields(FieldCollection $fields)
108: {
109: $this->fields = $fields;
110: }
111:
112: /**
113: * Check if this collection uses the specified fieldtype
114: *
115: * @param string $fieldtype
116: * @return boolean
117: */
118: public function hasFieldtype($fieldtype)
119: {
120: return $this->fields->hasFieldtype($fieldtype);
121: }
122:
123: /**
124: * {@inheritdoc}
125: */
126: public function hasCustomFields()
127: {
128: return true;
129: }
130:
131: /**
132: * Get the field IDs for the specified fieldtype
133: *
134: * @param string $fieldtype
135: * @return array
136: */
137: public function getFieldIdsByFieldtype($fieldtype)
138: {
139: return $this->fields->getFieldIdsByFieldtype($fieldtype);
140: }
141:
142: /**
143: * Set the Matrix columns for this collection
144: *
145: * @param \rsanchez\Deep\Collection\MatrixColCollection $matrixCols
146: * @return void
147: */
148: public function setMatrixCols(MatrixColCollection $matrixCols)
149: {
150: $fields = $this->fields;
151:
152: $matrixCols->each(function ($col) use ($fields) {
153: $fields->addFieldtype($col->col_type);
154: });
155:
156: $this->matrixCols = $matrixCols;
157: }
158:
159: /**
160: * Get the Matrix columns for this collection
161: *
162: * @return \rsanchez\Deep\Collection\MatrixColCollection|null
163: */
164: public function getMatrixCols()
165: {
166: return $this->matrixCols;
167: }
168:
169: /**
170: * Set the Grid columns for this collection
171: *
172: * @param \rsanchez\Deep\Collection\GridColCollection $gridCols
173: * @return void
174: */
175: public function setGridCols(GridColCollection $gridCols)
176: {
177: $fields = $this->fields;
178:
179: $gridCols->each(function ($col) use (&$fields) {
180: $fields->addFieldtype($col->col_type);
181: });
182:
183: $this->gridCols = $gridCols;
184: }
185:
186: /**
187: * Get the Grid columns for this collection
188: *
189: * @return \rsanchez\Deep\Collection\GridColCollection|null
190: */
191: public function getGridCols()
192: {
193: return $this->gridCols;
194: }
195: }
196: