1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\RecipeBook\Page; |
4
|
|
|
|
5
|
|
|
use Dynamic\RecipeBook\Model\RecipeDirection; |
6
|
|
|
use Dynamic\RecipeBook\Model\RecipeIngredient; |
7
|
|
|
use Sheadawson\Linkable\Forms\EmbeddedObjectField; |
8
|
|
|
use Sheadawson\Linkable\Models\EmbeddedObject; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FieldGroup; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\GridField\GridField; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
17
|
|
|
use SilverStripe\Forms\NumericField; |
18
|
|
|
use SilverStripe\Forms\ReadonlyField; |
19
|
|
|
use SilverStripe\Forms\TextField; |
20
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
21
|
|
|
use SilverStripe\ORM\DataObject; |
22
|
|
|
use SilverStripe\ORM\DB; |
23
|
|
|
use SilverStripe\ORM\HasManyList; |
24
|
|
|
use SilverStripe\ORM\ManyManyList; |
25
|
|
|
use SilverStripe\Versioned\GridFieldArchiveAction; |
26
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
27
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class RecipePage |
31
|
|
|
* @package Dynamic\RecipeBook\Page |
32
|
|
|
* |
33
|
|
|
* @property int $Servings |
34
|
|
|
* @property string $PrepTime |
35
|
|
|
* @property string $CookTime |
36
|
|
|
* @property string $Difficulty |
37
|
|
|
* @property int $VideoID |
38
|
|
|
* @method EmbeddedObject Video() |
39
|
|
|
* @method HasManyList Ingredients() |
40
|
|
|
* @method HasManyList Directions() |
41
|
|
|
* @method ManyManyList Categories() |
42
|
|
|
*/ |
43
|
|
|
class RecipePage extends \Page |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private static $singular_name = 'Recipe'; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private static $plural_name = 'Recipes'; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private static $table_name = 'RecipePage'; |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private static $db = [ |
|
|
|
|
64
|
|
|
'Servings' => 'Varchar(20)', |
65
|
|
|
'PrepTime' => 'Varchar(255)', |
66
|
|
|
'CookTime' => 'Varchar(255)', |
67
|
|
|
'Difficulty' => 'Varchar(255)', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
private static $has_many = [ |
|
|
|
|
74
|
|
|
'Ingredients' => RecipeIngredient::class, |
75
|
|
|
'Directions' => RecipeDirection::class, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
private static $many_many = [ |
|
|
|
|
82
|
|
|
'Categories' => RecipeCategoryPage::class, |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
89
|
|
|
'Categories' => [ |
90
|
|
|
'SortOrder' => 'Int', |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
private static $defaults = [ |
|
|
|
|
98
|
|
|
'ShowInMenu' => false, |
99
|
|
|
'RelatedLimit' => 2, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var bool |
104
|
|
|
*/ |
105
|
|
|
private static $can_be_root = false; |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var bool |
109
|
|
|
*/ |
110
|
|
|
private static $show_in_sitetree = false; |
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var array |
114
|
|
|
*/ |
115
|
|
|
private static $allowed_children = []; |
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return FieldList |
119
|
|
|
*/ |
120
|
|
|
public function getCMSFields() |
121
|
|
|
{ |
122
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
123
|
|
|
|
124
|
|
|
$fields->addFieldsToTab( |
125
|
|
|
'Root.Main', |
126
|
|
|
[ |
127
|
|
|
$toggle = FieldGroup::create( |
|
|
|
|
128
|
|
|
'RecipeStatistics', |
129
|
|
|
[ |
130
|
|
|
TextField::create('Servings') |
131
|
|
|
->setTitle('Servings'), |
132
|
|
|
TextField::create('PrepTime') |
133
|
|
|
->setTitle('Prep Time'), |
134
|
|
|
TextField::create('CookTime') |
135
|
|
|
->setTitle('Cook Time'), |
136
|
|
|
TextField::create('Difficulty') |
137
|
|
|
->setTitle('Difficulty'), |
138
|
|
|
] |
139
|
|
|
)->setTitle('Info'), |
140
|
|
|
], |
141
|
|
|
'Content' |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
if ($this->exists()) { |
145
|
|
|
$fields->addFieldToTab( |
146
|
|
|
'Root.Ingredients', |
147
|
|
|
$ingredients = GridField::create( |
148
|
|
|
'Ingredients', |
149
|
|
|
'Ingredients', |
150
|
|
|
$this->Ingredients(), |
151
|
|
|
$ingredientsConfig = GridFieldConfig_RelationEditor::create() |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$fields->addFieldToTab( |
156
|
|
|
'Root.Directions', |
157
|
|
|
$directions = GridField::create( |
158
|
|
|
'Directions', |
159
|
|
|
'Directions', |
160
|
|
|
$this->Directions(), |
161
|
|
|
$directionsConfig = GridFieldConfig_RelationEditor::create() |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$fields->addFieldsToTab( |
166
|
|
|
'Root.Categories', |
167
|
|
|
[ |
168
|
|
|
ReadonlyField::create('PrimaryCategoryDisplay') |
169
|
|
|
->setTitle('Primary Category') |
170
|
|
|
->setValue($this->getPrimaryCategory()->Title), |
171
|
|
|
$categories = GridField::create( |
|
|
|
|
172
|
|
|
'Categories', |
173
|
|
|
'Additional Categories', |
174
|
|
|
$this->Categories()->exclude('ID', $this->ParentID)->sort('SortOrder'), |
175
|
|
|
$catConfig = GridFieldConfig_RelationEditor::create() |
176
|
|
|
), |
177
|
|
|
] |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$ingredientsConfig |
181
|
|
|
->addComponent(new GridFieldOrderableRows('Sort')) |
182
|
|
|
->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
183
|
|
|
|
184
|
|
|
$directionsConfig |
185
|
|
|
->addComponent(new GridFieldOrderableRows('Sort')) |
186
|
|
|
->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
187
|
|
|
|
188
|
|
|
$catConfig |
189
|
|
|
->removeComponentsByType([ |
190
|
|
|
GridFieldAddExistingAutocompleter::class, |
191
|
|
|
GridFieldArchiveAction::class, |
192
|
|
|
GridFieldEditButton::class, |
193
|
|
|
]) |
194
|
|
|
->addComponents( |
195
|
|
|
new GridFieldOrderableRows('SortOrder'), |
196
|
|
|
$list = new GridFieldAddExistingSearchButton() |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$list->setSearchList(RecipeCategoryPage::get()->exclude('ID', $this->ParentID)); |
200
|
|
|
} |
201
|
|
|
}); |
202
|
|
|
|
203
|
|
|
return parent::getCMSFields(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return RecipeCategory|DataObject|null |
208
|
|
|
*/ |
209
|
|
|
public function getPrimaryCategory() |
210
|
|
|
{ |
211
|
|
|
return $this->Parent(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return \SilverStripe\ORM\DataList |
216
|
|
|
*/ |
217
|
|
|
public function getCategoryList() |
218
|
|
|
{ |
219
|
|
|
$categories[] = $this->ParentID; |
|
|
|
|
220
|
|
|
|
221
|
|
|
foreach ($this->Categories() as $cat) { |
222
|
|
|
$categories[] = $cat->ID; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$records = RecipeCategoryPage::get()->byIDs($categories); |
226
|
|
|
|
227
|
|
|
return $records; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return \SilverStripe\ORM\DataList |
232
|
|
|
*/ |
233
|
|
|
public function getRelatedRecipes() |
234
|
|
|
{ |
235
|
|
|
$categories = $this->getCategoryList()->column('ID'); |
236
|
|
|
|
237
|
|
|
$recipes = RecipePage::get() |
238
|
|
|
->exclude('ID', $this->ID) |
239
|
|
|
->filterAny([ |
240
|
|
|
'ParentID' => $categories, |
241
|
|
|
'Categories.ID' => $categories, |
242
|
|
|
]) |
243
|
|
|
->limit(15); |
244
|
|
|
|
245
|
|
|
$random = DB::get_conn()->random(); |
246
|
|
|
$records = $recipes->sort($random)->limit($this->RelatedLimit); |
|
|
|
|
247
|
|
|
|
248
|
|
|
return $records; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|