Completed
Push — milestone/2.0 ( 2fc31a...c63841 )
by
unknown
02:32
created

Set_Field::set_value_from_input()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 4
eloc 10
c 4
b 2
f 0
nc 6
nop 1
dl 0
loc 15
rs 9.2
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Value_Set\Value_Set;
6
7
/**
8
 * Set field class.
9
 * Allows to create a set of checkboxes where multiple can be selected.
10
 */
11
class Set_Field extends Predefined_Options_Field {
12
	/**
13
	 * The options limit.
14
	 *
15
	 * @var int
16
	 */
17
	protected $limit_options = 0;
18
19
	/**
20
	 * Default field value
21
	 *
22
	 * @var array
23
	 */
24
	protected $default_value = array();
25
26
	/**
27
	 * Create a field from a certain type with the specified label.
28
	 * @param string $name  Field name
29
	 * @param string $label Field label
30
	 */
31
	protected function __construct( $name, $label ) {
32
		$this->value = new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES );
33
		parent::__construct( $name, $label );
34
	}
35
36
	/**
37
	 * Set the number of the options to be displayed at the initial field display.
38
	 *
39
	 * @param  int $limit
40
	 */
41
	public function limit_options( $limit ) {
42
		$this->limit_options = $limit;
43
		return $this;
44
	}
45
46
	/**
47
	 * Load the field value from an input array based on it's name
48
	 *
49
	 * @param array $input (optional) Array of field names and values. Defaults to $_POST
50
	 **/
51
	public function set_value_from_input( $input = null ) {
52
		if ( is_null( $input ) ) {
53
			$input = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
54
		}
55
56
		if ( ! isset( $input[ $this->name ] ) ) {
57
			$this->set_value( null );
58
		} else {
59
			$value = stripslashes_deep( $input[ $this->name ] );
60
			if ( is_array( $value ) ) {
61
				$value = array_values( $value );
62
			}
63
			$this->set_value( $value );
64
		}
65
	}
66
67
	/**
68
	 * Returns an array that holds the field data, suitable for JSON representation.
69
	 * This data will be available in the Underscore template and the Backbone Model.
70
	 *
71
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
72
	 * @return array
73
	 */
74 View Code Duplication
	public function to_json( $load ) {
75
		$field_data = parent::to_json( $load );
76
77
		$field_data = array_merge( $field_data, array(
78
			'limit_options' => $this->limit_options,
79
			'options' => $this->parse_options( $this->get_options() ),
80
		) );
81
82
		return $field_data;
83
	}
84
85
	/**
86
	 * The Underscore template of this field.
87
	 */
88
	public function template() {
89
		?>
90
		<# if (_.isEmpty(options)) { #>
91
			<em><?php _e( 'no options', \Carbon_Fields\TEXT_DOMAIN ); ?></em>
92
		<# } else { #>
93
			<div class="carbon-set-list">
94
				<# _.each(options, function(option, i) { #>
95
					<# 
96
						var selected = jQuery.inArray(String(option.value), value) > -1;
97
						var counter = i + 1;
98
						var exceed = limit_options > 0 && counter > limit_options;
99
						var last = options.length === counter;
100
					#>
101
102
					<p {{{ exceed ? 'style="display:none"' : '' }}}>
103
						<label>
104
							<input type="checkbox" name="{{{ name }}}[{{{ i }}}]" value="{{ option.value }}" {{{ selected ? 'checked="checked"' : '' }}} />
105
							{{{ option.name }}}
106
						</label>
107
					</p>
108
109
					<# if (!exceed && !last && counter == limit_options) { #>
110
						<p>... <a href="#" class="carbon-set-showall"><?php _e( 'Show All Options', \Carbon_Fields\TEXT_DOMAIN ); ?></a></p>
111
					<# } #>
112
				<# }) #>
113
			</div>
114
		<# } #>
115
		<?php
116
	}
117
}
118