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