|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Forms\DropdownField; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Dropdown field, created from a <select> tag. This field handles cart encryption based on store settings. |
|
11
|
|
|
** |
|
12
|
|
|
* <b>Populate with Array</b>. |
|
13
|
|
|
* |
|
14
|
|
|
* Example instantiation: |
|
15
|
|
|
* <code> |
|
16
|
|
|
* FoxyStripeDropdownField::create('Country') |
|
17
|
|
|
* ->setSource(array( |
|
18
|
|
|
* 'NZ' => 'New Zealand', |
|
19
|
|
|
* 'US' => 'United States', |
|
20
|
|
|
* 'GEM'=> 'Germany' |
|
21
|
|
|
* )); |
|
22
|
|
|
* </code> |
|
23
|
|
|
* |
|
24
|
|
|
* <b>Populate with Enum-Values</b> |
|
25
|
|
|
* |
|
26
|
|
|
* You can automatically create a map of possible values from an {@link Enum} database column. |
|
27
|
|
|
* |
|
28
|
|
|
* Example model definition: |
|
29
|
|
|
* <code> |
|
30
|
|
|
* class MyObject extends DataObject { |
|
31
|
|
|
* static $db = array( |
|
32
|
|
|
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')" |
|
33
|
|
|
* ); |
|
34
|
|
|
* } |
|
35
|
|
|
* </code> |
|
36
|
|
|
* |
|
37
|
|
|
* Field construction: |
|
38
|
|
|
* <code> |
|
39
|
|
|
* FoxyStripeDropdownField::create('Country') |
|
40
|
|
|
* ->setSource(singleton('MyObject')->dbObject('Country')->enumValues()); |
|
41
|
|
|
* </code> |
|
42
|
|
|
* |
|
43
|
|
|
* <b>Disabling individual items</b> |
|
44
|
|
|
* |
|
45
|
|
|
* Individual items can be disabled by feeding their array keys to setDisabledItems. |
|
46
|
|
|
* |
|
47
|
|
|
* <code> |
|
48
|
|
|
* $DrDownField->setDisabledItems( array( 'US', 'GEM' ) ); |
|
49
|
|
|
* </code> |
|
50
|
|
|
* |
|
51
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes instead. |
|
52
|
|
|
* @see ListboxField for a single <select> box (with single or multiple selections). |
|
53
|
|
|
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements |
|
54
|
|
|
*/ |
|
55
|
|
|
class FoxyStripeDropdownField extends DropdownField |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* Mark certain elements as disabled, |
|
59
|
|
|
* regardless of the {@link setDisabled()} settings. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $items Collection of array keys, as defined in the $source array |
|
62
|
|
|
* |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setDisabledItems($items) |
|
66
|
|
|
{ |
|
67
|
|
|
$controller = Controller::curr(); |
|
68
|
|
|
$code = $controller->data()->Code; |
|
69
|
|
|
$updated = []; |
|
70
|
|
|
if (is_array($items) && !empty($items)) { |
|
71
|
|
|
foreach ($items as $item) { |
|
72
|
|
|
array_push($updated, ProductPage::getGeneratedValue($code, $this->getName(), $item, 'value')); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
$this->disabledItems = $updated; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $source |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setSource($source) |
|
86
|
|
|
{ |
|
87
|
|
|
$controller = Controller::curr(); |
|
88
|
|
|
$code = $controller->data()->Code; |
|
89
|
|
|
$updated = []; |
|
90
|
|
|
if (is_array($source) && !empty($source)) { |
|
91
|
|
|
foreach ($source as $key => $val) { |
|
92
|
|
|
$updated[ProductPage::getGeneratedValue($code, $this->getName(), $key, 'value')] = $val; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
$this->source = $updated; |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|