Passed
Push — develop ( 6a6de9...5f38a6 )
by Andrew
03:51
created

Recipe::getSettingsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
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
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
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 yii\db\Schema;
25
26
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
28
 * @package   Recipe
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
29
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
30
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
31
class Recipe extends Field
32
{
33
    // Public Properties
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var array
38
     */
39
    public $assetSources = [];
40
41
    // Static Methods
42
    // =========================================================================
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @inheritdoc
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    public static function displayName(): string
48
    {
49
        return Craft::t('recipe', 'Recipe');
50
    }
51
52
    // Public Methods
53
    // =========================================================================
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @inheritdoc
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public function rules()
59
    {
60
        $rules = parent::rules();
61
        $rules = array_merge($rules, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
62
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
63
64
        return $rules;
65
    }
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @inheritdoc
69
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
70
    public function getContentColumnType(): string
71
    {
72
        return Schema::TYPE_TEXT;
73
    }
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
76
     * @inheritdoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function normalizeValue($value, ElementInterface $element = null)
79
    {
80
        if (is_string($value) && !empty($value)) {
81
            $value = Json::decode($value);
82
        }
83
        $model = new RecipeModel($value);
84
85
        return $model;
86
    }
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
89
     * @inheritdoc
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91
    public function getSettingsHtml()
92
    {
93
        // Render the settings template
94
        return Craft::$app->getView()->renderTemplate(
95
            'recipe'
96
            .DIRECTORY_SEPARATOR
97
            .'_components'
98
            .DIRECTORY_SEPARATOR
99
            .'fields'
100
            .DIRECTORY_SEPARATOR
101
            .'Recipe_settings',
102
            [
103
                'field' => $this,
104
                'assetSources' => $this->getSourceOptions(),
105
            ]
106
        );
107
    }
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $element should have a doc-comment as per coding-style.
Loading history...
110
     * @inheritdoc
111
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
112
    public function getInputHtml($value, ElementInterface $element = null): string
113
    {
114
        // Register our asset bundle
115
        Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class);
116
117
        // Get our id and namespace
118
        $id = Craft::$app->getView()->formatInputId($this->handle);
119
        $nameSpacedId = Craft::$app->getView()->namespaceInputId($id);
120
121
        // Variables to pass down to our field JavaScript to let it namespace properly
122
        $jsonVars = [
123
            'id' => $id,
124
            'name' => $this->handle,
125
            'namespace' => $nameSpacedId,
126
            'prefix' => Craft::$app->getView()->namespaceInputId(''),
127
        ];
128
        $jsonVars = Json::encode($jsonVars);
129
        Craft::$app->getView()->registerJs("$('#{$nameSpacedId}-field').RecipeRecipe(".$jsonVars.");");
130
131
        // Set asset elements
132
        $elements = [];
133
        if ($value->imageId) {
134
            if (is_array($value->imageId)) {
135
                $value->imageId = $value->imageId[0];
136
            }
137
            $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)];
138
        }
139
140
        // Render the input template
141
        return Craft::$app->getView()->renderTemplate(
142
            'recipe'
143
            .DIRECTORY_SEPARATOR
144
            .'_components'
145
            .DIRECTORY_SEPARATOR
146
            .'fields'
147
            .DIRECTORY_SEPARATOR
148
            .'Recipe_input',
149
            [
150
                'name' => $this->handle,
151
                'value' => $value,
152
                'field' => $this,
153
                'id' => $id,
154
                'nameSpacedId' => $nameSpacedId,
155
                'prefix' => Craft::$app->getView()->namespaceInputId(''),
156
                'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()),
157
                'elements' => $elements,
158
                'elementType' => Asset::class,
159
                'assetSources' => $this->assetSources,
160
            ]
161
        );
162
    }
163
164
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
165
     * @inheritdoc
166
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
167
    public function getSourceOptions(): array
168
    {
169
        $sourceOptions = [];
170
171
        foreach (Asset::sources('settings') as $key => $volume) {
172
            if (!isset($volume['heading'])) {
173
                $sourceOptions[] = [
174
                    'label' => Html::encode($volume['label']),
175
                    'value' => $volume['key']
176
                ];
177
            }
178
        }
179
180
        return $sourceOptions;
181
    }
182
}
183