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 nystudio107\recipe\assetbundles\recipefield\RecipeFieldAsset; |
15
|
|
|
use nystudio107\recipe\models\Recipe as RecipeModel; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\base\ElementInterface; |
19
|
|
|
use craft\base\Field; |
20
|
|
|
use craft\elements\Asset; |
21
|
|
|
use craft\helpers\Html; |
22
|
|
|
use craft\helpers\Json; |
23
|
|
|
|
24
|
|
|
use Twig\Error\LoaderError; |
25
|
|
|
use Twig\Error\RuntimeError; |
26
|
|
|
use Twig\Error\SyntaxError; |
27
|
|
|
use yii\base\Exception; |
28
|
|
|
use yii\base\InvalidConfigException; |
29
|
|
|
use yii\db\Schema; |
30
|
|
|
|
31
|
|
|
/** |
|
|
|
|
32
|
|
|
* @author nystudio107 |
|
|
|
|
33
|
|
|
* @package Recipe |
|
|
|
|
34
|
|
|
* @since 1.0.0 |
|
|
|
|
35
|
|
|
*/ |
|
|
|
|
36
|
|
|
class Recipe extends Field |
37
|
|
|
{ |
38
|
|
|
// Public Properties |
39
|
|
|
// ========================================================================= |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $assetSources = []; |
45
|
|
|
|
46
|
|
|
// Static Methods |
47
|
|
|
// ========================================================================= |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
public static function displayName(): string |
53
|
|
|
{ |
54
|
|
|
return Craft::t('recipe', 'Recipe'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Public Methods |
58
|
|
|
// ========================================================================= |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
|
|
|
|
63
|
|
|
public function rules() |
64
|
|
|
{ |
65
|
|
|
$rules = parent::rules(); |
66
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
67
|
|
|
]); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $rules; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
|
|
|
|
75
|
|
|
public function getContentColumnType(): string |
76
|
|
|
{ |
77
|
|
|
return Schema::TYPE_TEXT; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
|
|
|
|
81
|
|
|
* @inheritdoc |
82
|
|
|
* @since 1.2.1 |
|
|
|
|
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function useFieldset(): bool |
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
|
|
|
|
92
|
|
|
public function normalizeValue($value, ElementInterface $element = null) |
93
|
|
|
{ |
94
|
|
|
if (is_string($value) && !empty($value)) { |
95
|
|
|
$value = Json::decode($value); |
96
|
|
|
} |
97
|
|
|
$model = new RecipeModel($value); |
98
|
|
|
|
99
|
|
|
return $model; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
public function getSettingsHtml() |
106
|
|
|
{ |
107
|
|
|
// Render the settings template |
108
|
|
|
return Craft::$app->getView()->renderTemplate( |
109
|
|
|
'recipe/_components/fields/Recipe_settings', |
110
|
|
|
[ |
111
|
|
|
'field' => $this, |
112
|
|
|
'assetSources' => $this->getSourceOptions(), |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
|
|
|
|
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
|
|
|
|
120
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
121
|
|
|
{ |
122
|
|
|
// Register our asset bundle |
123
|
|
|
try { |
124
|
|
|
Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
125
|
|
|
} catch (InvalidConfigException $e) { |
126
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Get our id and namespace |
130
|
|
|
$id = Craft::$app->getView()->formatInputId($this->handle); |
131
|
|
|
$nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
132
|
|
|
|
133
|
|
|
// Variables to pass down to our field JavaScript to let it namespace properly |
134
|
|
|
$jsonVars = [ |
135
|
|
|
'id' => $id, |
136
|
|
|
'name' => $this->handle, |
137
|
|
|
'namespace' => $nameSpacedId, |
138
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
139
|
|
|
]; |
140
|
|
|
$jsonVars = Json::encode($jsonVars); |
141
|
|
|
Craft::$app->getView()->registerJs("$('#{$nameSpacedId}-field').RecipeRecipe(".$jsonVars.");"); |
142
|
|
|
|
143
|
|
|
// Set asset elements |
144
|
|
|
$elements = []; |
145
|
|
|
if ($value->imageId) { |
146
|
|
|
if (is_array($value->imageId)) { |
147
|
|
|
$value->imageId = $value->imageId[0]; |
148
|
|
|
} |
149
|
|
|
$elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Render the input template |
153
|
|
|
try { |
154
|
|
|
return Craft::$app->getView()->renderTemplate( |
155
|
|
|
'recipe/_components/fields/Recipe_input', |
156
|
|
|
[ |
157
|
|
|
'name' => $this->handle, |
158
|
|
|
'value' => $value, |
159
|
|
|
'field' => $this, |
160
|
|
|
'id' => $id, |
161
|
|
|
'nameSpacedId' => $nameSpacedId, |
162
|
|
|
'prefix' => Craft::$app->getView()->namespaceInputId(''), |
163
|
|
|
'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
164
|
|
|
'elements' => $elements, |
165
|
|
|
'elementType' => Asset::class, |
166
|
|
|
'assetSources' => $this->assetSources, |
167
|
|
|
] |
168
|
|
|
); |
169
|
|
|
} catch (LoaderError $e) { |
170
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
|
|
|
171
|
|
|
} catch (RuntimeError $e) { |
172
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
173
|
|
|
} catch (SyntaxError $e) { |
174
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
175
|
|
|
} catch (Exception $e) { |
176
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
|
|
|
|
183
|
|
|
public function getSourceOptions(): array |
184
|
|
|
{ |
185
|
|
|
$sourceOptions = []; |
186
|
|
|
|
187
|
|
|
foreach (Asset::sources('settings') as $key => $volume) { |
188
|
|
|
if (!isset($volume['heading'])) { |
189
|
|
|
$sourceOptions[] = [ |
190
|
|
|
'label' => Html::encode($volume['label']), |
191
|
|
|
'value' => $volume['key'] |
192
|
|
|
]; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $sourceOptions; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|