Completed
Push — development ( 987e6d...b78414 )
by
unknown
04:31
created

Set_Field::set_value_from_input()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 15
loc 15
rs 9.2
c 1
b 1
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Set field class.
7
 * Allows to create a set of checkboxes where multiple can be selected.
8
 */
9
class Set_Field extends Predefined_Options_Field {
10
	/**
11
	 * The options limit.
12
	 *
13
	 * @var int
14
	 */
15
	protected $limit_options = 0;
16
17
	/**
18
	 * Default field value
19
	 *
20
	 * @var array
21
	 */
22
	protected $default_value = array();
23
24
	/**
25
	 * Load the field value from an input array based on it's name
26
	 *
27
	 * @param array $input (optional) Array of field names and values. Defaults to $_POST
28
	 **/
29 View Code Duplication
	public function set_value_from_input( $input = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
		if ( is_null( $input ) ) {
31
			$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...
32
		}
33
34
		if ( ! isset( $input[ $this->name ] ) ) {
35
			$this->set_value( null );
36
		} else {
37
			$value = stripslashes_deep( $input[ $this->name ] );
38
			if ( is_array( $value ) ) {
39
				$value = array_values( $value );
40
			}
41
			$this->set_value( $value );
42
		}
43
	}
44
45
	/**
46
	 * Set the number of the options to be displayed at the initial field display.
47
	 *
48
	 * @param  int $limit
49
	 */
50
	public function limit_options( $limit ) {
51
		$this->limit_options = $limit;
52
		return $this;
53
	}
54
55
	/**
56
	 * Retrieve the field value(s).
57
	 *
58
	 * @return array
59
	 */
60
	public function get_value() {
61
		if ( $this->value === false ) {
62
			return $this->set_value( $this->default_value );
63
		}
64
65
		if ( !is_array( $this->value ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
66
			$this->value = maybe_unserialize( $this->value );
67
			if ( !is_array( $this->value ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
68
				if ( is_null( $this->value ) ) {
69
					return array();
70
				}
71
				return array( $this->value );
72
			}
73
		}
74
75
		return (array) $this->value;
76
	}
77
78
	/**
79
	 * Returns an array that holds the field data, suitable for JSON representation.
80
	 * This data will be available in the Underscore template and the Backbone Model.
81
	 *
82
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
83
	 * @return array
84
	 */
85 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
		$field_data = parent::to_json( $load );
87
88
		$field_data = array_merge( $field_data, array(
89
			'limit_options' => $this->limit_options,
90
			'options' => $this->parse_options( $this->get_options() ),
91
		) );
92
93
		return $field_data;
94
	}
95
96
	/**
97
	 * The Underscore template of this field.
98
	 */
99
	public function template() {
100
		?>
101
		<# if (_.isEmpty(options)) { #>
102
			<em><?php _e( 'no options', 'carbon-fields' ); ?></em>
103
		<# } else { #>
104
			<div class="carbon-set-list">
105
				<# _.each(options, function(option, i) { #>
106
					<# 
107
						var selected = jQuery.inArray(String(option.value), value) > -1;
108
						var counter = i + 1;
109
						var exceed = limit_options > 0 && counter > limit_options;
110
						var last = options.length === counter;
111
					#>
112
113
					<p {{{ exceed ? 'style="display:none"' : '' }}}>
114
						<label>
115
							<input type="checkbox" name="{{{ name }}}[{{{ i }}}]" value="{{ option.value }}" {{{ selected ? 'checked="checked"' : '' }}} />
116
							{{{ option.name }}}
117
						</label>
118
					</p>
119
120
					<# if (!exceed && !last && counter == limit_options) { #>
121
						<p>... <a href="#" class="carbon-set-showall"><?php _e( 'Show All Options', 'carbon-fields' ); ?></a></p>
122
					<# } #>
123
				<# }) #>
124
			</div>
125
		<# } #>
126
		<?php
127
	}
128
}
129