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\console\controllers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\console\Controller; |
16
|
|
|
use craft\elements\Entry; |
17
|
|
|
use craft\helpers\Console; |
18
|
|
|
use nystudio107\recipe\Recipe; |
19
|
|
|
use yii\console\ExitCode; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package Recipe |
|
|
|
|
24
|
|
|
* @since 1.3.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class NutritionApiController extends Controller |
27
|
|
|
{ |
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var string The handle of the section. |
30
|
|
|
*/ |
31
|
|
|
public $section; |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @var string The handle of the recipe field. |
35
|
|
|
*/ |
36
|
|
|
public $field; |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
public function options($actionID) |
42
|
|
|
{ |
43
|
|
|
$options = parent::options($actionID); |
44
|
|
|
$options[] = 'section'; |
45
|
|
|
$options[] = 'field'; |
46
|
|
|
|
47
|
|
|
return $options; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generates nutritional information for all entries in a section provided using --section. |
52
|
|
|
*/ |
|
|
|
|
53
|
|
|
public function actionGenerate() |
54
|
|
|
{ |
55
|
|
|
if (Recipe::$plugin->settings->hasApiCredentials() === false) { |
56
|
|
|
$this->stderr(Craft::t('recipe', 'API credentials do not exist in plugin settings.').PHP_EOL, Console::FG_RED); |
57
|
|
|
|
58
|
|
|
return ExitCode::OK; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->section === null) { |
62
|
|
|
$this->stderr(Craft::t('recipe', 'A section handle must be provided using --section.').PHP_EOL, Console::FG_RED); |
63
|
|
|
|
64
|
|
|
return ExitCode::OK; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->field === null) { |
68
|
|
|
$this->stderr(Craft::t('recipe', 'A field handle must be provided using --field.').PHP_EOL, Console::FG_RED); |
69
|
|
|
|
70
|
|
|
return ExitCode::OK; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->field === null) { |
74
|
|
|
$this->stderr(Craft::t('recipe', 'A field handle must be provided using --field.').PHP_EOL, Console::FG_RED); |
75
|
|
|
|
76
|
|
|
return ExitCode::OK; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$entries = Entry::find()->section($this->section)->all(); |
80
|
|
|
|
81
|
|
|
if (empty($entries)) { |
82
|
|
|
$this->stderr(Craft::t('recipe', 'No entries found in the section with handle `{handle}`.', ['handle' => $this->section]).PHP_EOL, Console::FG_RED); |
83
|
|
|
|
84
|
|
|
return ExitCode::OK; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$total = count($entries); |
88
|
|
|
$count = 0; |
89
|
|
|
$failed = 0; |
90
|
|
|
|
91
|
|
|
$this->stdout(Craft::t('recipe', 'Generating nutritional information for {count} entries...', ['count' => $total]).PHP_EOL, Console::FG_YELLOW); |
92
|
|
|
|
93
|
|
|
Console::startProgress($count, $total, '', 0.8); |
|
|
|
|
94
|
|
|
|
95
|
|
|
foreach ($entries as $entry) { |
96
|
|
|
$field = $entry->{$this->field}; |
97
|
|
|
$ingredients = $field->ingredients; |
98
|
|
|
|
99
|
|
|
foreach ($ingredients as $key => $value) { |
100
|
|
|
$ingredients[$key] = implode(' ', $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$nutritionalInfo = Recipe::$plugin->nutritionApi->getNutritionalInfo($ingredients, $field->serves); |
104
|
|
|
|
105
|
|
|
if (empty($nutritionalInfo['error'])) { |
106
|
|
|
$recipe = $entry->{$this->field}; |
107
|
|
|
|
108
|
|
|
foreach ($nutritionalInfo as $fieldHandle => $value) { |
109
|
|
|
$recipe[$fieldHandle] = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$entry->setFieldValue($this->field, $recipe); |
113
|
|
|
|
114
|
|
|
if (!Craft::$app->getElements()->saveElement($entry)) { |
115
|
|
|
$failed++; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
else { |
|
|
|
|
119
|
|
|
$failed++; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$count++; |
123
|
|
|
|
124
|
|
|
Console::updateProgress($count, $total); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
Console::endProgress(); |
128
|
|
|
|
129
|
|
|
$succeeded = $count - $failed; |
130
|
|
|
|
131
|
|
|
$this->stdout(Craft::t('recipe', 'Successfully generated nutritional information for {count} entries.', ['count' => $succeeded]).PHP_EOL, Console::FG_GREEN); |
132
|
|
|
|
133
|
|
|
if ($failed > 0) { |
134
|
|
|
$this->stderr(Craft::t('recipe', 'Failed to generate nutritional information for {count} entries.', ['count' => $failed]).PHP_EOL, Console::FG_RED); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return ExitCode::OK; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|