1 | <?php |
||
2 | |||
3 | namespace SilverStripe\UserForms\Model\EditableFormField; |
||
4 | |||
5 | use SilverStripe\CMS\Controllers\CMSMain; |
||
6 | use SilverStripe\Control\Controller; |
||
7 | use SilverStripe\Core\Convert; |
||
8 | use SilverStripe\ORM\DataObject; |
||
9 | use SilverStripe\UserForms\Model\EditableFormField\EditableMultipleOptionField; |
||
10 | use SilverStripe\Versioned\Versioned; |
||
11 | |||
12 | /** |
||
13 | * Base Class for EditableOption Fields such as the ones used in |
||
14 | * dropdown fields and in radio check box groups |
||
15 | * |
||
16 | * @method EditableMultipleOptionField Parent() |
||
17 | * @package userforms |
||
18 | */ |
||
19 | class EditableOption extends DataObject |
||
20 | { |
||
21 | private static $default_sort = 'Sort'; |
||
22 | |||
23 | private static $db = [ |
||
24 | 'Name' => 'Varchar(255)', |
||
25 | 'Title' => 'Varchar(255)', |
||
26 | 'Default' => 'Boolean', |
||
27 | 'Sort' => 'Int', |
||
28 | 'Value' => 'Varchar(255)', |
||
29 | ]; |
||
30 | |||
31 | private static $has_one = [ |
||
32 | 'Parent' => EditableMultipleOptionField::class, |
||
33 | ]; |
||
34 | |||
35 | private static $extensions = [ |
||
36 | Versioned::class . "('Stage', 'Live')" |
||
37 | ]; |
||
38 | |||
39 | private static $summary_fields = [ |
||
40 | 'Title', |
||
41 | 'Default' |
||
42 | ]; |
||
43 | |||
44 | protected static $allow_empty_values = false; |
||
45 | |||
46 | private static $table_name = 'EditableOption'; |
||
47 | |||
48 | /** |
||
49 | * Returns whether to allow empty values or not. |
||
50 | * |
||
51 | * @return boolean |
||
52 | */ |
||
53 | public static function allow_empty_values() |
||
54 | { |
||
55 | return (bool) self::$allow_empty_values; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set whether to allow empty values. |
||
60 | * |
||
61 | * @param boolean $allow |
||
62 | */ |
||
63 | public static function set_allow_empty_values($allow) |
||
64 | { |
||
65 | self::$allow_empty_values = (bool) $allow; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @deprecated 5.0 Use "$Title.XML" in templates instead |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getEscapedTitle() |
||
73 | { |
||
74 | return Convert::raw2att($this->Title); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Fetches a value for $this->Value. If empty values are not allowed, |
||
79 | * then this will return the title in the case of an empty value. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getValue() |
||
84 | { |
||
85 | $value = $this->getField('Value'); |
||
86 | if (empty($value) && !self::allow_empty_values()) { |
||
87 | return $this->Title; |
||
88 | } |
||
89 | return $value; |
||
90 | } |
||
91 | |||
92 | protected function onBeforeWrite() |
||
93 | { |
||
94 | if (!$this->Sort) { |
||
95 | $this->Sort = EditableOption::get()->max('Sort') + 1; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
96 | } |
||
97 | |||
98 | parent::onBeforeWrite(); |
||
99 | } |
||
100 | } |
||
101 |