Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Model/Variation/AttributeType.php (1 issue)

1
<?php
2
3
namespace SilverShop\Model\Variation;
4
5
use SilverShop\Page\Product;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
10
use SilverStripe\Forms\LiteralField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\HasManyList;
15
use SilverStripe\ORM\ManyManyList;
16
17
/**
18
 * Producte Attribute Type
19
 * Types of product attributes.
20
 * eg: color, size, length
21
 *
22
 * @property string $Name
23
 * @property string $Label
24
 * @method   AttributeValue[]|HasManyList Values()
25
 * @method   Product[]|ManyManyList Product()
26
 */
27
class AttributeType extends DataObject
28
{
29
    private static $db = [
30
        'Name' => 'Varchar', //for back-end use
31
        'Label' => 'Varchar' //for front-end use
32
    ];
33
34
    private static $has_many = [
35
        'Values' => AttributeValue::class,
36
    ];
37
38
    private static $belongs_many_many = [
39
        'Product' => Product::class,
40
    ];
41
42
    private static $summary_fields = [
43
        'Name' => 'Name',
44
        'Label' => 'Label',
45
    ];
46
47
    private static $indexes = [
48
        'LastEdited' => true,
49
    ];
50
51
    private static $default_sort = 'ID ASC';
52
53
    private static $singular_name = 'Attribute';
54
55
    private static $plural_name = 'Attributes';
56
57
    private static $table_name = 'SilverShop_AttributeType';
58
59
    public static function find_or_make($name)
60
    {
61
        if ($type = AttributeType::get()->filter('Name:nocase', $name)->first()
62
        ) {
63
            return $type;
64
        }
65
        $type = AttributeType::create();
66
        $type->Name = $name;
67
        $type->Label = $name;
68
        $type->write();
69
70
        return $type;
71
    }
72
73
    public function getCMSFields()
74
    {
75
        $fields = FieldList::create(
76
            TextField::create('Name', $this->fieldLabel('Name')),
77
            TextField::create('Label', $this->fieldLabel('Label'))
78
        );
79
        if ($this->isInDB()) {
80
            $fields->push(
81
                GridField::create(
82
                    'Values',
83
                    $this->fieldLabel('Values'),
84
                    $this->Values(),
85
                    GridFieldConfig_RecordEditor::create()
86
                )
87
            );
88
        } else {
89
            $fields->push(
90
                LiteralField::create(
91
                    'Values',
92
                    '<p class="message warning">' .
93
                    _t(__CLASS__ . '.SaveFirstInfo', 'Save first, then you can add values.') .
94
                    '</p>'
95
                )
96
            );
97
        }
98
99
        $this->extend('updateCMSFields', $fields);
100
101
        return $fields;
102
    }
103
104
    public function addValues(array $values)
105
    {
106
        $avalues = $this->convertArrayToValues($values);
107
        $this->Values()->addMany($avalues);
0 ignored issues
show
$avalues of type SilverStripe\ORM\ArrayList is incompatible with the type array expected by parameter $items of SilverStripe\ORM\DataList::addMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $this->Values()->addMany(/** @scrutinizer ignore-type */ $avalues);
Loading history...
108
    }
109
110
    /**
111
     * Finds or creates values for this type.
112
     *
113
     * @param array $values
114
     *
115
     * @return ArrayList
116
     */
117
    public function convertArrayToValues(array $values)
118
    {
119
        $set = ArrayList::create();
120
        foreach ($values as $value) {
121
            $val = $this->Values()->find('Value', $value);
122
            if (!$val) {  //TODO: ignore case, if possible
123
                $val = AttributeValue::create();
124
                $val->Value = $value;
125
                $val->TypeID = $this->ID;
126
                $val->write();
127
            }
128
            $set->push($val);
129
        }
130
131
        return $set;
132
    }
133
134
    /**
135
     * Returns a dropdown field for the user to select a variant.
136
     *
137
     * @param string    $emptyString
138
     * @param ArrayList $values
139
     *
140
     * @return DropdownField
141
     */
142
    public function getDropDownField($emptystring = null, $values = null)
143
    {
144
        $values = ($values) ? $values : $this->Values()->sort(['Sort' => 'ASC', 'Value' => 'ASC']);
145
146
        if ($values->exists()) {
147
            $field = DropdownField::create(
148
                'ProductAttributes[' . $this->ID . ']',
149
                $this->Label,
150
                $values->map('ID', 'Value')
151
            );
152
153
            if ($emptystring) {
154
                $field->setEmptyString($emptystring);
155
            }
156
157
            return $field;
158
        }
159
160
        return null;
161
    }
162
163
    public function onBeforeWrite()
164
    {
165
        parent::onBeforeWrite();
166
        if ($this->Name && !$this->Label) {
167
            $this->Label = $this->Name;
168
        } elseif ($this->Label && !$this->Name) {
169
            $this->Name = $this->Label;
170
        }
171
    }
172
173
    public function canDelete($member = null)
174
    {
175
        //TODO: prevent deleting if has been used
176
        return true;
177
    }
178
}
179