Completed
Push — master ( d3c311...e03f0a )
by
unknown
04:12
created

code/model/NutriHolder.php (3 issues)

Check for undocumented magic property with read access

Documentation Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class NutriHolder extends DataObject
4
{
5
    private static $singular_name = 'Nutritional Information Profile';
6
    public function i18n_singular_name()
7
    {
8
        return self::$singular_name;
9
    }
10
11
    private static $plural_name = 'Nutritional Information Profiles';
12
    public function i18n_plural_name()
13
    {
14
        return self::$plural_name;
15
    }
16
17
    private static $db = array(
18
        'ProfileName' => 'Varchar(50)',
19
        'ServingCount' => 'Int',
20
        'Container' => 'Varchar(10)',
21
        'ServingSize' => 'Varchar(15)',
22
        'AdditionalInfo' => 'Varchar(100)',
23
        'HidePerServeColumn' => 'Boolean',
24
        'HidePer100gColumn' => 'Boolean',
25
        'HidePerDVColumn' => 'Boolean'
26
    );
27
28
29
    private static $has_many = array(
30
        'NutriRows' => 'NutriRow',
31
        'Products' => 'ProductGroup',
32
        'ProductVariations' => 'ProductVariation'
33
    );
34
35
    /**
36
     * @inherited
37
     */
38
    private static $casting = array(
39
        'Title' => "Varchar",
40
        'Per100Unit' => "Varchar"
41
    );
42
43
    /**
44
     * Returns the unit for the 'Per 100' column of nutritional information. It tries
45
     * to get the unit from the ServingSize field, and if no match returns 'g'
46
     * @return String
47
     */
48
    public function Per100Unit()
49
    {
50
        return $this->getPer100Unit();
51
    }
52
53
    public function getPer100Unit()
54
    {
55
        $string = trim($this->ServingSize);
56
        $matches = array();
57
        $matchResult = preg_match('/ ?([A-Z]|[a-z]){1,7}/', $string, $matches);
58
        if (!$matchResult) {
59
            return "g";
60
        }
61
        return trim($matches[0]);
62
    }
63
64
    /**
65
     * @return String
66
     */
67
    public function Title()
68
    {
69
        return $this->getTitle();
70
    }
71
    public function getTitle()
72
    {
73
        $string = $this->ProfileName;
74
        if (!$string) {
75
            $string = 'Profile #'.$this->ID.' (please customise) ';
76
        }
77
        $string .= ': ';
78
        $string .= "serving: ".$this->ServingCount."; ";
79
        $string .= "size: ".$this->ServingSize."; ";
80
        $string .= "container: ".$this->Container."; ";
81
        return $string;
82
    }
83
84
    private static $summary_fields = array(
85
        'Title' => 'Title',
86
    );
87
88
    private static $searchable_fields = array(
89
        'ProfileName' => 'PartialMatchFilter',
90
        'ServingCount' => 'ExactMatchFilter',
91
        'Container' => 'PartialMatchFilter',
92
        'ServingSize' => 'ExactMatchFilter',
93
        'AdditionalInfo' => 'PartialMatchFilter',
94
        'AdditionalInfo' => 'PartialMatchFilter'
95
    );
96
97
    public function getCMSFields()
98
    {
99
        $fields = parent::getCMSFields();
100
101
        $config = GridFieldConfig_RelationEditor::create();
102
        $config->addComponent(new GridFieldSortableRows('SortOrder'));
103
104
        $fields->addFieldsToTab(
105
            'Root.Main',
106
            array(
107
                NumericField::create('ServingCount', 'Servings per package')
108
                    ->setRightTitle('The number of servings in the jar/bucket/bottle/conatiner'),
109
                TextField::create('Container', 'The product container')
110
                    ->setRightTitle('The conatiner for the product e.g. Jar, Bottle, Bucket'),
111
                TextField::create('ServingSize', 'The size of each serving')
112
                    ->setRightTitle('The size of each serving e.g., 3g, 30ml'),
113
                TextField::create('AdditionalInfo', 'Additional information')
114
                    ->setRightTitle('For example "Remove label with care."'),
115
            )
116
        );
117
118
        $productsGrid = $fields->dataFieldByName('Products');
119
        if ($productsGrid) {
120
            $productsFieldConfig = GridFieldConfig_RecordViewer::create();
121
            $productsGrid -> setConfig($productsFieldConfig);
122
        }
123
124
        $productVariationsGrid = $fields->dataFieldByName('ProductVariations');
125
        if ($productVariationsGrid) {
126
            $productVariationsConfig = GridFieldConfig_RecordViewer::create();
127
            $productVariationsGrid -> setConfig($productVariationsConfig);
128
        }
129
130
        $nutriRowsGridField = $fields->dataFieldByName('NutriRows');
131
132
        if ($nutriRowsGridField) {
133
            $nutriRowsFieldConfig = $nutriRowsGridField ->getConfig();
134
            $nutriRowsFieldConfig
135
                ->addComponent(new GridFieldEditableColumns())
136
                ->addComponent(new GridFieldDeleteAction())
137
                ->addComponent(new GridFieldSortableRows('SortOrder'))
138
                ->getComponentByType('GridFieldEditableColumns')
139
140
                ->setDisplayFields(
141
                    array(
142
                        'Title' => array(
143
                            'title' => 'Item',
144
                            'field' => 'ReadonlyField'
145
                        ),
146
                        'PerServe'  => array(
147
                            "title" => "Per Serve",
148
                            "callback" => function ($record, $column, $grid) {
149
                                return new TextField($column, "Serve");
150
                            }),
151
152
                        'Per100' => function ($record, $column, $grid) {
153
                            return new TextField($column, "Per 100");
154
                        }
155
156
                    )
157
                );
158
        }
159
        $fields->removeFieldFromTab('Root.Main', 'SortOrder');
160
161
        return $fields;
162
    }
163
164
    /**
165
     * @return DataList
166
     */
167
    public function ShownNutriRows()
168
    {
169
        return $this->NutriRows()
170
            ->exclude(array("Hide" => 1));
171
    }
172
173
    /**
174
     * @return DataList
175
     */
176
    public function NumberOfTableColums()
177
    {
178
        $tablesCols = 4;
179
        if($this->HidePerServeColumn){
0 ignored issues
show
The property HidePerServeColumn does not exist on object<NutriHolder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
            $tablesCols--;
181
        }
182
        if($this->HidePer100gColumn){
0 ignored issues
show
The property HidePer100gColumn does not exist on object<NutriHolder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
183
            $tablesCols--;
184
        }
185
        if($this->HidePerDVColumn){
0 ignored issues
show
The property HidePerDVColumn does not exist on object<NutriHolder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
186
            $tablesCols--;
187
        }
188
        return $tablesCols;
189
    }
190
}
191