Completed
Push — master ( f8f77e...d2a4ab )
by Jason
05:01
created

FoxyStripeDropdownField   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
dl 0
loc 39
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setDisabledItems() 0 13 4
A setSource() 0 13 4
1
<?php
2
3
namespace Dynamic\FoxyStripe\Form;
4
5
use SilverStripe\Forms\DropdownField;
6
7
/**
8
 * Dropdown field, created from a <select> tag. This field handles cart encryption based on store settings.
9
 **
10
 * <b>Populate with Array</b>.
11
 *
12
 * Example instantiation:
13
 * <code>
14
 * FoxyStripeDropdownField::create('Country')
15
 *      ->setSource(array(
16
 *     'NZ' => 'New Zealand',
17
 *     'US' => 'United States',
18
 *     'GEM'=> 'Germany'
19
 *   ));
20
 * </code>
21
 *
22
 * <b>Populate with Enum-Values</b>
23
 *
24
 * You can automatically create a map of possible values from an {@link Enum} database column.
25
 *
26
 * Example model definition:
27
 * <code>
28
 * class MyObject extends DataObject {
29
 *   static $db = array(
30
 *     'Country' => "Enum('New Zealand,United States,Germany','New Zealand')"
31
 *   );
32
 * }
33
 * </code>
34
 *
35
 * Field construction:
36
 * <code>
37
 * FoxyStripeDropdownField::create('Country')
38
 *   ->setSource(singleton('MyObject')->dbObject('Country')->enumValues());
39
 * </code>
40
 *
41
 * <b>Disabling individual items</b>
42
 *
43
 * Individual items can be disabled by feeding their array keys to setDisabledItems.
44
 *
45
 * <code>
46
 * $DrDownField->setDisabledItems( array( 'US', 'GEM' ) );
47
 * </code>
48
 *
49
 * @see CheckboxSetField for multiple selections through checkboxes instead.
50
 * @see ListboxField for a single <select> box (with single or multiple selections).
51
 * @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
52
 */
53
class FoxyStripeDropdownField extends DropdownField
54
{
55
    /**
56
     * Mark certain elements as disabled,
57
     * regardless of the {@link setDisabled()} settings.
58
     *
59
     * @param array $items Collection of array keys, as defined in the $source array
60
     */
61
    public function setDisabledItems($items)
62
    {
63
        $controller = Controller::curr();
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
        $code = $controller->data()->Code;
65
        $updated = [];
66
        if (is_array($items) && !empty($items)) {
67
            foreach ($items as $item) {
68
                array_push($updated, ProductPage::getGeneratedValue($code, $this->getName(), $item, 'value'));
0 ignored issues
show
Bug introduced by
The type Dynamic\FoxyStripe\Form\ProductPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
            }
70
        }
71
        $this->disabledItems = $updated;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param array $source
78
     */
79
    public function setSource($source)
80
    {
81
        $controller = Controller::curr();
82
        $code = $controller->data()->Code;
83
        $updated = [];
84
        if (is_array($source) && !empty($source)) {
85
            foreach ($source as $key => $val) {
86
                $updated[ProductPage::getGeneratedValue($code, $this->getName(), $key, 'value')] = $val;
87
            }
88
        }
89
        $this->source = $updated;
90
91
        return $this;
92
    }
93
}
94