|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Single checkbox field class. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Checkbox_Field extends Field { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @{inheritDoc} |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $default_value = false; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The value that is saved in the database when |
|
17
|
|
|
* this checkbox field is enabled. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $option_value = 'yes'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Get the option value. |
|
25
|
|
|
* |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function get_option_value() { |
|
29
|
|
|
return $this->option_value; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the option value. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $value New value |
|
36
|
|
|
* @return Field $this |
|
37
|
|
|
*/ |
|
38
|
|
|
public function set_option_value( $value ) { |
|
39
|
|
|
$this->option_value = $value; |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Load the field value from an input array based on it's name. |
|
45
|
|
|
* If not enabled, set to empty string for easier data querying. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $input Array of field names and values. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function set_value_from_input( $input ) { |
|
50
|
|
|
parent::set_value_from_input( $input ); |
|
51
|
|
|
if ( $this->get_value() !== $this->get_option_value() ) { |
|
52
|
|
|
$this->set_value( '' ); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return a differently formatted value for end-users |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function get_formatted_value() { |
|
62
|
|
|
return ( $this->get_value() === $this->get_option_value() ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
|
67
|
|
|
* In addition to default data, option value and label are added. |
|
68
|
|
|
* |
|
69
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function to_json( $load ) { |
|
73
|
|
|
$field_data = parent::to_json( $load ); |
|
74
|
|
|
|
|
75
|
|
|
$field_data = array_merge( $field_data, array( |
|
76
|
|
|
'option_value' => $this->get_option_value(), |
|
77
|
|
|
'option_label' => parent::get_label(), |
|
78
|
|
|
) ); |
|
79
|
|
|
|
|
80
|
|
|
return $field_data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the field label. |
|
85
|
|
|
* Label here is empty because it is displayed in the front-end. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string Label of the field. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function get_label() { |
|
90
|
|
|
return ''; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|