1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Recipe plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
6
|
|
|
* conversion, portion calculation, and JSON-LD microdata support |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\recipe\fields; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
16
|
|
|
use craft\base\Field; |
17
|
|
|
use craft\base\PreviewableFieldInterface; |
18
|
|
|
use craft\elements\Asset; |
|
|
|
|
19
|
|
|
use craft\helpers\Html; |
20
|
|
|
use craft\helpers\Json; |
21
|
|
|
use nystudio107\recipe\assetbundles\recipefield\RecipeFieldAsset; |
22
|
|
|
use nystudio107\recipe\models\Recipe as RecipeModel; |
23
|
|
|
use nystudio107\recipe\models\Settings; |
24
|
|
|
use nystudio107\recipe\Recipe as RecipePlugin; |
25
|
|
|
use Throwable; |
26
|
|
|
use yii\base\InvalidConfigException; |
27
|
|
|
use yii\db\Schema; |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @author nystudio107 |
|
|
|
|
31
|
|
|
* @package Recipe |
|
|
|
|
32
|
|
|
* @since 1.0.0 |
|
|
|
|
33
|
|
|
*/ |
|
|
|
|
34
|
|
|
class Recipe extends Field implements PreviewableFieldInterface |
35
|
|
|
{ |
36
|
|
|
// Public Properties |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public array $assetSources = []; |
43
|
|
|
|
44
|
|
|
// Static Methods |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
|
|
|
|
50
|
|
|
public static function dbType(): array|string|null |
51
|
|
|
{ |
52
|
|
|
return Schema::TYPE_TEXT; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
|
|
|
|
58
|
|
|
public static function displayName(): string |
59
|
|
|
{ |
60
|
|
|
return Craft::t('recipe', 'Recipe'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public static function icon(): string |
67
|
|
|
{ |
68
|
|
|
return 'hat-chef'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public static function phpType(): string |
75
|
|
|
{ |
76
|
|
|
return sprintf('\\%s', RecipeModel::class); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Public Methods |
80
|
|
|
// ========================================================================= |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
public function rules(): array |
86
|
|
|
{ |
87
|
|
|
$rules = parent::rules(); |
88
|
|
|
|
89
|
|
|
return array_merge($rules, [ |
|
|
|
|
90
|
|
|
]); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* @inheritdoc |
95
|
|
|
* @since 1.2.1 |
|
|
|
|
96
|
|
|
*/ |
|
|
|
|
97
|
|
|
public function useFieldset(): bool |
98
|
|
|
{ |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
public function normalizeValue(mixed $value, ?ElementInterface $element = null): RecipeModel |
106
|
|
|
{ |
107
|
|
|
$config = []; |
108
|
|
|
// If we already have a model, just return it |
109
|
|
|
if ($value instanceof RecipeModel) { |
110
|
|
|
return $value; |
111
|
|
|
} |
112
|
|
|
// If we have a non-empty string, try to JSON-decode it |
113
|
|
|
if (is_string($value)) { |
114
|
|
|
$value = Json::decodeIfJson($value); |
115
|
|
|
} |
116
|
|
|
// If we have an array, use that for our config data |
117
|
|
|
if (is_array($value)) { |
118
|
|
|
$config = $value; |
119
|
|
|
} |
120
|
|
|
// Ensure we save our asset ids as integers, not arrays |
121
|
|
|
if (isset($config['imageId'])) { |
122
|
|
|
if (is_array($config['imageId'])) { |
123
|
|
|
$config['imageId'] = $config['imageId'][0]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
if (isset($config['videoId'])) { |
127
|
|
|
if (is_array($config['videoId'])) { |
128
|
|
|
$config['videoId'] = $config['videoId'][0]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new RecipeModel($config); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
|
|
|
|
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
|
|
|
|
138
|
|
|
public function getSettingsHtml(): ?string |
139
|
|
|
{ |
140
|
|
|
// Render the settings template |
141
|
|
|
return Craft::$app->getView()->renderTemplate( |
142
|
|
|
'recipe/_components/fields/Recipe_settings', |
143
|
|
|
[ |
144
|
|
|
'field' => $this, |
145
|
|
|
'assetSources' => $this->getSourceOptions(), |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
|
|
|
|
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
|
|
|
|
153
|
|
|
public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
154
|
|
|
{ |
155
|
|
|
// Register our asset bundle |
156
|
|
|
try { |
157
|
|
|
Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
158
|
|
|
} catch (InvalidConfigException $invalidConfigException) { |
159
|
|
|
Craft::error($invalidConfigException->getMessage(), __METHOD__); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Get our id and namespace |
163
|
|
|
$id = Html::id($this->handle); |
164
|
|
|
$nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
165
|
|
|
|
166
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
167
|
|
|
$jsonVars = [ |
168
|
|
|
'id' => $id, |
169
|
|
|
'name' => $this->handle, |
170
|
|
|
'namespace' => $nameSpacedId, |
171
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
172
|
|
|
]; |
173
|
|
|
$jsonVars = Json::encode($jsonVars); |
174
|
|
|
Craft::$app->getView()->registerJs(sprintf('$(\'#%s-field\').RecipeRecipe(', $nameSpacedId) . $jsonVars . ");"); |
175
|
|
|
|
176
|
|
|
// Set asset elements |
177
|
|
|
$elements = []; |
178
|
|
|
if ($value->imageId) { |
179
|
|
|
if (is_array($value->imageId)) { |
180
|
|
|
$value->imageId = $value->imageId[0]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$videoElements = []; |
187
|
|
|
if ($value->videoId) { |
188
|
|
|
if (is_array($value->videoId)) { |
189
|
|
|
$value->videoId = $value->videoId[0]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$videoElements = [Craft::$app->getAssets()->getAssetById($value->videoId)]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** @var Settings $settings */ |
|
|
|
|
196
|
|
|
$settings = RecipePlugin::$plugin->getSettings(); |
|
|
|
|
197
|
|
|
// Render the input template |
198
|
|
|
try { |
199
|
|
|
return Craft::$app->getView()->renderTemplate( |
200
|
|
|
'recipe/_components/fields/Recipe_input', |
201
|
|
|
[ |
202
|
|
|
'name' => $this->handle, |
203
|
|
|
'value' => $value, |
204
|
|
|
'field' => $this, |
205
|
|
|
'id' => $id, |
206
|
|
|
'nameSpacedId' => $nameSpacedId, |
207
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
208
|
|
|
'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
209
|
|
|
'elements' => $elements, |
210
|
|
|
'videoElements' => $videoElements, |
211
|
|
|
'elementType' => Asset::class, |
212
|
|
|
'assetSources' => $this->assetSources, |
213
|
|
|
'hasApiCredentials' => $settings->hasApiCredentials(), |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
} catch (Throwable $throwable) { |
217
|
|
|
Craft::error($throwable->getMessage(), __METHOD__); |
218
|
|
|
return ''; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the asset sources |
224
|
|
|
*/ |
|
|
|
|
225
|
|
|
public function getSourceOptions(): array |
226
|
|
|
{ |
227
|
|
|
$sourceOptions = []; |
228
|
|
|
|
229
|
|
|
foreach (Asset::sources('settings') as $volume) { |
230
|
|
|
if (!isset($volume['heading'])) { |
231
|
|
|
$sourceOptions[] = [ |
232
|
|
|
'label' => Html::encode($volume['label']), |
233
|
|
|
'value' => $volume['key'], |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $sourceOptions; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|