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

NutriHolder::NumberOfTableColums()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
1
<?php
2
3
class NutriHolder extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by Jack Stinchcombe
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $singular_name = 'Nutritional Information Profile';
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
6
    public function i18n_singular_name()
7
    {
8
        return self::$singular_name;
9
    }
10
11
    private static $plural_name = 'Nutritional Information Profiles';
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
12
    public function i18n_plural_name()
13
    {
14
        return self::$plural_name;
15
    }
16
17
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Jack Stinchcombe
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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(
0 ignored issues
show
Comprehensibility introduced by Nicolaas
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Nicolaas
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        'NutriRows' => 'NutriRow',
31
        'Products' => 'ProductGroup',
32
        'ProductVariations' => 'ProductVariation'
33
    );
34
35
    /**
36
     * @inherited
37
     */
38
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Jack Stinchcombe
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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);
0 ignored issues
show
Documentation introduced by Jack Stinchcombe
The property ServingSize 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...
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;
0 ignored issues
show
Documentation introduced by Sunny Side Up
The property ProfileName does not exist on object<NutriHolder>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
74
        if (!$string) {
75
            $string = 'Profile #'.$this->ID.' (please customise) ';
76
        }
77
        $string .= ': ';
78
        $string .= "serving: ".$this->ServingCount."; ";
0 ignored issues
show
Documentation introduced by Sunny Side Up
The property ServingCount 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...
79
        $string .= "size: ".$this->ServingSize."; ";
0 ignored issues
show
Documentation introduced by Sunny Side Up
The property ServingSize 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...
80
        $string .= "container: ".$this->Container."; ";
0 ignored issues
show
Documentation introduced by Sunny Side Up
The property Container 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...
81
        return $string;
82
    }
83
84
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Jack Stinchcombe
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
85
        'Title' => 'Title',
86
    );
87
88
    private static $searchable_fields = array(
0 ignored issues
show
Comprehensibility introduced by Jack Stinchcombe
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by Jack Stinchcombe
The property $searchable_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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);
0 ignored issues
show
Bug introduced by sunnysideup
The method setConfig() does not exist on FormField. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
122
        }
123
124
        $productVariationsGrid = $fields->dataFieldByName('ProductVariations');
125
        if ($productVariationsGrid) {
126
            $productVariationsConfig = GridFieldConfig_RecordViewer::create();
127
            $productVariationsGrid -> setConfig($productVariationsConfig);
0 ignored issues
show
Bug introduced by Nicolaas
The method setConfig() does not exist on FormField. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
128
        }
129
130
        $nutriRowsGridField = $fields->dataFieldByName('NutriRows');
131
132
        if ($nutriRowsGridField) {
133
            $nutriRowsFieldConfig = $nutriRowsGridField ->getConfig();
0 ignored issues
show
Bug introduced by Nicolaas
The method getConfig() does not exist on FormField. Did you maybe mean config()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by sunnysideup
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
                                return new TextField($column, "Serve");
150
                            }),
151
152
                        'Per100' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by sunnysideup
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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()
0 ignored issues
show
Bug introduced by Jack Stinchcombe
The method NutriRows() does not exist on NutriHolder. Did you maybe mean ShownNutriRows()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
170
            ->exclude(array("Hide" => 1));
171
    }
172
173
    /**
174
     * @return DataList
0 ignored issues
show
Documentation introduced by ShaneW
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
175
     */
176
    public function NumberOfTableColums()
177
    {
178
        $tablesCols = 4;
179
        if($this->HidePerServeColumn){
0 ignored issues
show
Documentation introduced by ShaneW
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
Documentation introduced by ShaneW
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
Documentation introduced by ShaneW
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