nystudio107 /
craft-recipe
| 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 |
||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||
| 9 | * @copyright Copyright (c) 2017 nystudio107 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 10 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 24 | * @author nystudio107 |
||||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||||
| 25 | * @package Recipe |
||||||
|
0 ignored issues
–
show
|
|||||||
| 26 | * @since 1.3.0 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 27 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 28 | class NutritionApiController extends Controller |
||||||
| 29 | { |
||||||
| 30 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 31 | * @var ?string The handle of the section. |
||||||
| 32 | */ |
||||||
| 33 | public ?string $section = null; |
||||||
| 34 | |||||||
| 35 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 36 | * @var ?string The handle of the recipe field. |
||||||
| 37 | */ |
||||||
| 38 | public ?string $field = null; |
||||||
| 39 | |||||||
| 40 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 41 | * @inheritdoc |
||||||
| 42 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 55 | public function actionGenerate(): int |
||||||
| 56 | { |
||||||
| 57 | /** @var Settings $settings */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 58 | $settings = Recipe::$plugin->getSettings(); |
||||||
|
0 ignored issues
–
show
The method
getSettings() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 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 |