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