Completed
Push — master ( 4bbf9b...f9af1a )
by
unknown
03:00
created

Complex_Field::template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
cc 1
eloc 5
c 6
b 2
f 1
nc 1
nop 0
dl 0
loc 53
rs 9.5797

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Datastore\Datastore_Interface;
6
use Carbon_Fields\Helper\Helper;
7
use Carbon_Fields\Field\Field;
8
use Carbon_Fields\Field\Group_Field;
9
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
10
11
/**
12
 * Complex field class.
13
 * Allows nested repeaters with multiple field groups to be created.
14
 */
15
class Complex_Field extends Field {
16
	const LAYOUT_GRID = 'grid';
17
	const LAYOUT_LIST = 'list'; // deprecated
18
	const LAYOUT_TABBED = 'tabbed';
19
20
	protected $fields = array();
21
	protected $values = array();
22
	protected $groups = array();
23
24
	protected $layout = self::LAYOUT_GRID;
25
	protected $values_min = -1;
26
	protected $values_max = -1;
27
28
	public $labels = array(
29
		'singular_name' => 'Entry',
30
		'plural_name' => 'Entries',
31
	);
32
33
	/**
34
	 * Initialization tasks.
35
	 */
36
	public function init() {
37
		$this->labels = array(
38
			'singular_name' => __( 'Entry', 'carbon-fields' ),
39
			'plural_name' => __( 'Entries', 'carbon-fields' ),
40
		);
41
42
		// Include the complex group Underscore templates
43
		$this->add_template( 'Complex-Group', array( $this, 'template_group' ) );
44
		$this->add_template( 'Complex-Group-Tab-Item', array( $this, 'template_group_tab_item' ) );
45
46
		parent::init();
47
	}
48
49
	/**
50
	 * Add a set/group of fields.
51
	 *
52
	 * @return $this
53
	 */
54
	public function add_fields() {
55
		$argv = func_get_args();
56
		$argc = count( $argv );
57
58
		if ( $argc == 1 ) {
59
			$fields = $argv[0];
60
			$name = '';
61
			$label = null;
62
		} else if ( $argc == 2 ) {
63 View Code Duplication
			if ( is_array( $argv[0] ) ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
64
				list( $fields, $name ) = $argv;
65
			} else {
66
				list( $name, $fields ) = $argv;
67
			}
68
			$label = null;
69
		} else if ( $argc == 3 ) {
70 View Code Duplication
			if ( is_array( $argv[0] ) ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
71
				list( $fields, $name, $label ) = $argv;
72
			} else {
73
				list( $name, $label, $fields ) = $argv;
74
			}
75
		}
76
77
		if ( array_key_exists( '_' . $name, $this->groups ) ) {
78
			Incorrect_Syntax_Exception::raise( 'Group with name "' . $name . '" in Complex Field "' . $this->get_label() . '" already exists.' );
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
		}
80
81
		$group = new Group_Field($name, $label, $fields);
0 ignored issues
show
Bug introduced by
The variable $label does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $fields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
82
83
		$this->groups[ $group->get_name() ] = $group;
84
85
		return $this;
86
	}
87
88
	/**
89
	 * Set the group label Underscore template.
90
	 *
91
	 * @param  string|callable $template
92
	 * @return $this
93
	 */
94
	public function set_header_template( $template ) {
95
		if ( count($this->groups) === 0 ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
96
			Incorrect_Syntax_Exception::raise( "Can't set group label template. There are no present groups for Complex Field " . $this->get_label() . "." );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal . does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
97
		}
98
99
		$template = is_callable( $template ) ? call_user_func( $template ) : $template;
100
101
		// Assign the template to the group that was added last
102
		$values = array_values( $this->groups );
103
		$group = end( $values );
104
		$group->set_label_template( $template );
105
106
		// Include the group label Underscore template
107
		$this->add_template( $group->get_group_id(), array( $group, 'template_label' ) );
108
109
		$this->groups[ $group->get_name() ] = $group;
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Retrieve all groups of fields.
116
	 *
117
	 * @return array $fields
118
	 */
119
	public function get_fields() {
120
		$fields = array();
121
122
		foreach ( $this->groups as $group ) {
123
			$group_fields = $group->get_fields();
124
125
			$fields = array_merge( $fields, $group_fields );
126
		}
127
128
		return $fields;
129
	}
130
131
	/**
132
	 * Set the field labels.
133
	 * Currently supported values:
134
	 *  - singular_name - the singular entry label
135
	 *  - plural_name - the plural entries label
136
	 *
137
	 * @param  array $labels Labels
138
	 */
139
	public function setup_labels( $labels ) {
140
		$this->labels = array_merge( $this->labels, $labels );
141
		return $this;
142
	}
143
144
	/**
145
	 * Set the datastore of this field.
146
	 *
147
	 * @param Datastore_Interface $store
148
	 */
149
	public function set_datastore( Datastore_Interface $store ) {
150
		$this->store = $store;
151
152
		foreach ( $this->groups as $group ) {
153
			$group->set_datastore( $this->store );
154
		}
155
	}
156
157
	/**
158
	 * Load the field value from an input array based on it's name.
159
	 *
160
	 * @param array $input (optional) Array of field names and values. Defaults to $_POST
161
	 **/
162
	public function set_value_from_input( $input = null ) {
163
		$this->values = array();
164
165
		if ( is_null( $input ) ) {
166
			$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...
167
		}
168
169
		if ( ! isset( $input[ $this->get_name() ] ) ) {
170
			return;
171
		}
172
173
		$input_groups = $input[ $this->get_name() ];
174
		$index = 0;
175
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
176
177
		foreach ( $input_groups as $values ) {
178
			$value_group = array();
179
			if ( ! isset( $values['group'] ) || ! isset( $this->groups[ $values['group'] ] ) ) {
180
				continue;
181
			}
182
183
			$group = $this->groups[ $values['group'] ];
184
			unset( $values['group'] );
185
186
			$group_fields = $group->get_fields();
187
188
			// trim input values to those used by the field
189
			$group_field_names = array_flip( $group->get_field_names() );
190
			$values = array_intersect_key( $values, $group_field_names );
191
192
			foreach ( $group_fields as $field ) {
193
				// set value from the group
194
				$tmp_field = clone $field;
195
				if ( is_a( $tmp_field, __NAMESPACE__ . '\\Complex_Field' ) ) {
196
					if ( ! isset( $values[ $tmp_field->get_name() ] ) ) {
197
						continue; // bail if the complex field is empty
198
					}
199
200
					$new_name = $this->get_name() . $group->get_name() . '-' . $field->get_name() . '_' . $index;
201
					$new_values = array( $new_name => $values[ $tmp_field->get_name() ] );
202
203
					$tmp_field->set_name( $new_name );
204
					$tmp_field->set_value_from_input( $new_values );
205
				} else {
206
					$tmp_field->set_value_from_input( $values );
207
				}
208
209
				// update name to group name
210
				$tmp_field->set_name( $this->get_name() . $group->get_name() . '-' . $field->get_name() . '_' . $index );
211
				$value_group[] = $tmp_field;
212
			}
213
214
			$this->values[] = $value_group;
215
			$index++;
216
		}
217
	}
218
219
	/**
220
	 * Load all groups of fields and their data.
221
	 */
222
	public function load() {
223
		// load existing groups
224
		$this->load_values();
225
	}
226
227
	/**
228
	 * Save all contained groups of fields.
229
	 */
230
	public function save() {
231
		$this->delete();
232
233
		foreach ( $this->values as $value ) {
234
			foreach ( $value as $field ) {
235
				$field->save();
236
			}
237
		}
238
	}
239
240
	/**
241
	 * Delete the values of all contained fields.
242
	 */
243
	public function delete() {
244
		return $this->store->delete_values( $this );
245
	}
246
247
	/**
248
	 * Load and parse the field data.
249
	 */
250
	public function load_values() {
251
		return $this->load_values_from_db();
252
	}
253
254
	/**
255
	 * Load and parse the field data from the database.
256
	 */
257
	public function load_values_from_db() {
258
		$this->values = array();
259
260
		$group_rows = $this->store->load_values( $this );
261
262
		return $this->process_loaded_values( $group_rows );
263
	}
264
265
	/**
266
	 * Load and parse a raw set of field data.
267
	 *
268
	 * @param  array $values Raw data entries
269
	 * @return array 		 Processed data entries
270
	 */
271
	public function load_values_from_array( $values ) {
272
		$this->values = array();
273
274
		$group_rows = array();
275
276
		$meta_key = $this->get_name();
277
278
		foreach ( $values as $key => $value ) {
279
			if ( strpos( $key, $meta_key ) !== 0 ) {
280
				continue;
281
			}
282
283
			$group_rows[] = array(
284
				'field_key' => preg_replace( '~^(' . preg_quote( $this->name, '~' ) . ')_\d+_~', '$1_', $key ),
285
				'field_value' => $value,
286
			);
287
		}
288
289
		return $this->process_loaded_values( $group_rows );
290
	}
291
292
	/**
293
	 * Parse groups of raw field data into the actual field hierarchy.
294
	 *
295
	 * @param  array $group_rows Group rows
296
	 */
297
	public function process_loaded_values( $group_rows ) {
298
		$input_groups = array();
299
300
		// Set default values
301
		$field_names = array();
302
		foreach ( $this->groups as $group ) {
303
			$group_fields = $group->get_fields();
304
			foreach ( $group_fields as $field ) {
305
				$field_names[] = $field->get_name();
306
				$field->set_value( $field->get_default_value() );
307
			}
308
		}
309
310
		if ( empty( $group_rows ) ) {
311
			return;
312
		}
313
314
		// load and parse values and group type
315
		foreach ( $group_rows as $row ) {
316
			if ( ! preg_match( Helper::get_complex_field_regex( $this->name, array_keys( $this->groups ), $field_names ), $row['field_key'], $field_name ) ) {
317
				continue;
318
			}
319
320
			$row['field_value'] = maybe_unserialize( $row['field_value'] );
321
			$input_groups[ $field_name['index'] ]['type'] = $field_name['group'];
322
323
			if ( ! empty( $field_name['trailing'] ) ) {
324
				$input_groups[ $field_name['index'] ][ $field_name['key'] . '_' . $field_name['sub'] . '-' . $field_name['trailing'] ] = $row['field_value'];
325 View Code Duplication
			} else if ( ! empty( $field_name['sub'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
326
				$input_groups[ $field_name['index'] ][ $field_name['key'] ][ $field_name['sub'] ] = $row['field_value'];
327
			} else {
328
				$input_groups[ $field_name['index'] ][ $field_name['key'] ] = $row['field_value'];
329
			}
330
		}
331
332
		// create groups list with loaded fields
333
		ksort( $input_groups );
334
335
		foreach ( $input_groups as $index => $values ) {
336
			$value_group = array( 'type' => $values['type'] );
337
			$group_fields = $this->groups[ $values['type'] ]->get_fields();
338
			unset( $values['type'] );
339
340
			foreach ( $group_fields as $field ) {
341
				// set value from the group
342
				$tmp_field = clone $field;
343
344
				if ( is_a( $field, __NAMESPACE__ . '\\Complex_Field' ) ) {
345
					$tmp_field->load_values_from_array( $values );
346
				} else {
347
					$tmp_field->set_value_from_input( $values );
348
				}
349
350
				$value_group[] = $tmp_field;
351
			}
352
353
			$this->values[] = $value_group;
354
		}
355
	}
356
357
	/**
358
	 * Retrieve the field values.
359
	 * @return array
360
	 */
361
	public function get_values() {
362
		return $this->values;
363
	}
364
365
	/**
366
	 * Generate and set the field prefix.
367
	 * @param string $prefix
368
	 */
369
	public function set_prefix( $prefix ) {
370
		parent::set_prefix( $prefix );
371
372
		foreach ( $this->groups as $group ) {
373
			$group->set_prefix( $prefix );
374
		}
375
	}
376
377
	/**
378
	 * Returns an array that holds the field data, suitable for JSON representation.
379
	 * This data will be available in the Underscore template and the Backbone Model.
380
	 *
381
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
382
	 * @return array
383
	 */
384
	public function to_json( $load ) {
385
		$complex_data = parent::to_json( $load );
386
387
		$groups_data = array();
388
		$values_data = array();
389
390
		foreach ( $this->groups as $group ) {
391
			$groups_data[] = $group->to_json( false );
392
		}
393
394
		foreach ( $this->values as $fields ) {
395
			$group = $this->get_group_by_name( $fields['type'] );
396
			unset( $fields['type'] );
397
398
			$data = array(
399
				'name' => $group->get_name(),
400
				'label' => $group->get_label(),
401
				'group_id' => $group->get_group_id(),
402
				'fields' => array(),
403
			);
404
405
			foreach ( $fields as $index => $field ) {
406
				$data['fields'][] = $field->to_json( false );
407
			}
408
409
			$values_data[] = $data;
410
		}
411
412
		$complex_data = array_merge( $complex_data, array(
413
			'layout' => $this->layout,
414
			'labels' => $this->labels,
415
			'min' => $this->get_min(),
416
			'max' => $this->get_max(),
417
			'multiple_groups' => count( $groups_data ) > 1,
418
			'groups' => $groups_data,
419
			'value' => $values_data,
420
		) );
421
422
		return $complex_data;
423
	}
424
425
	/**
426
	 * The main Underscore template.
427
	 */
428
	public function template() {
429
		?>
430
		<div class="carbon-subcontainer carbon-grid {{ multiple_groups ? 'multiple-groups' : '' }}">
431
			<div class="carbon-empty-row">
432
				{{{ crbl10n.complex_no_rows.replace('%s', labels.plural_name) }}}
433
			</div>
434
435
			<div class="groups-wrapper layout-{{ layout }}">
436
				<# if (layout === '<?php echo self::LAYOUT_TABBED ?>') { #>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
437
					<div class="group-tabs-nav-holder">
438
						<ul class="group-tabs-nav"></ul>
439
440
						<div class="carbon-actions">
441
							<div class="carbon-button">
442
								<a href="#" class="button" data-group="{{{ multiple_groups ? '' : groups[0].name }}}">
443
									+
444
								</a>
445
446
								<# if (multiple_groups) { #>
447
									<ul>
448
										<# _.each(groups, function(group) { #>
449
											<li><a href="#" data-group="{{{ group.name }}}">{{{ group.label }}}</a></li>
450
										<# }); #>
451
									</ul>
452
								<# } #>
453
							</div>
454
						</div>
455
					</div><!-- /.group-tabs-nav-holder -->
456
				<# } #>
457
458
				<div class="carbon-groups-holder"></div>
459
				<div class="clear"></div>
460
			</div>
461
462
			<div class="carbon-actions">
463
				<div class="carbon-button">
464
					<a href="#" class="button" data-group="{{{ multiple_groups ? '' : groups[0].name }}}">
465
						{{{ crbl10n.complex_add_button.replace('%s', labels.singular_name) }}}
466
						{{{ multiple_groups ? '&#8681;' : '' }}}
467
					</a>
468
469
					<# if (multiple_groups) { #>
470
						<ul>
471
							<# _.each(groups, function(group) { #>
472
								<li><a href="#" data-group="{{{ group.name }}}">{{{ group.label }}}</a></li>
473
							<# }); #>
474
						</ul>
475
					<# } #>
476
				</div>
477
			</div>
478
		</div>
479
		<?php
480
	}
481
482
	/**
483
	 * The Underscore template for the complex field group.
484
	 */
485
	public function template_group() {
486
		?>
487
		<div id="carbon-{{{ complex_name }}}-complex-container" class="carbon-row carbon-group-row" data-group-id="{{ id }}">
488
			<input type="hidden" name="{{{ complex_name + '[' + index + ']' }}}[group]" value="{{ name }}" />
489
490
			<div class="carbon-drag-handle">
491
				<span class="group-number">{{{ order + 1 }}}</span><span class="group-name">{{{ label_template || label }}}</span>
492
			</div>
493
494
			<div class="carbon-group-actions">
495
				<a class="carbon-btn-collapse" href="#" title="<?php esc_attr_e( 'Collapse/Expand', 'carbon_fields' ); ?>">
496
					<?php _e( 'Collapse/Expand', 'carbon_fields' ); ?>
497
				</a>
498
499
				<a class="carbon-btn-duplicate" href="#" title="<?php esc_attr_e( 'Clone', 'carbon_fields' ); ?>">
500
					<?php _e( 'Clone', 'carbon_fields' ); ?>
501
				</a>
502
503
				<a class="carbon-btn-remove" href="#" title="<?php esc_attr_e( 'Remove', 'carbon_fields' ); ?>">
504
					<?php _e( 'Remove', 'carbon_fields' ); ?>
505
				</a>
506
			</div>
507
508
			<div class="fields-container">
509
				<# _.each(fields, function(field) { #>
510
					<div class="carbon-row carbon-subrow subrow-{{{ field.type }}} {{{ field.classes.join(' ') }}}">
511
						<label for="{{{ complex_id + '-' + field.id + '-' + index }}}">
512
							{{ field.label }}
513
514
							<# if (field.required) { #>
515
								 <span class="carbon-required">*</span>
516
							<# } #>
517
						</label>
518
519
						<div class="field-holder {{{ complex_id + '-' + field.id + '-' + index }}}"></div>
520
521
						<# if (field.help_text) { #>
522
							<em class="help-text">
523
								{{{ field.help_text }}}
524
							</em>
525
						<# } #>
526
527
						<em class="carbon-error"></em>
528
					</div>
529
				<# }) #>
530
			</div>
531
		</div>
532
		<?php
533
	}
534
535
	/**
536
	 * The Underscore template for the group item tab.
537
	 */
538
	public function template_group_tab_item() {
539
		?>
540
		<li class="group-tab-item" data-group-id="{{ id }}">
541
			<a href="#">
542
				<# if (label_template || label) { #>
543
					<span class="group-name">{{{ label_template || label }}}</span>
544
				<# } #>
545
				<span class="group-number">{{{ order + 1 }}}</span>
546
			</a>
547
		</li>
548
		<?php
549
	}
550
551
	/**
552
	 * Modify the layout of this field.
553
	 * Deprecated in favor of set_width().
554
	 *
555
	 * @deprecated
556
	 *
557
	 * @param string $layout
558
	 */
559
	public function set_layout( $layout ) {
560
		if ( ! in_array( $layout, array( self::LAYOUT_GRID, self::LAYOUT_TABBED, self::LAYOUT_LIST ) ) ) {
561
			Incorrect_Syntax_Exception::raise( 'Incorrect layout specified. Available values are "<code>' . self::LAYOUT_GRID . '</code>" and "<code>' . self::LAYOUT_TABBED . '</code>"' );
562
		}
563
564
		if ( $layout === self::LAYOUT_LIST ) {
565
			_doing_it_wrong( __METHOD__, __( 'Complex field <code>' . self::LAYOUT_LIST . '</code> layout is deprecated, please use <code>set_width()</code> instead.', 'carbon_fields' ), null );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
566
		}
567
568
		$this->layout = $layout;
569
570
		return $this;
571
	}
572
573
	/**
574
	 * Set the minimum number of entries.
575
	 *
576
	 * @param int $min
577
	 */
578
	public function set_min( $min ) {
579
		$this->values_min = intval( $min );
580
		return $this;
581
	}
582
583
	/**
584
	 * Get the minimum number of entries.
585
	 *
586
	 * @return int $min
587
	 */
588
	public function get_min() {
589
		return $this->values_min;
590
	}
591
592
	/**
593
	 * Set the maximum number of entries.
594
	 *
595
	 * @param int $max
596
	 */
597
	public function set_max( $max ) {
598
		$this->values_max = intval( $max );
599
		return $this;
600
	}
601
602
	/**
603
	 * Get the maximum number of entries.
604
	 *
605
	 * @return int $max
606
	 */
607
	public function get_max() {
608
		return $this->values_max;
609
	}
610
611
	/**
612
	 * Retrieve the groups of this field.
613
	 *
614
	 * @return array
615
	 */
616
	public function get_group_names() {
617
		return array_keys( $this->groups );
618
	}
619
620
	/**
621
	 * Retrieve a group by its name.
622
	 *
623
	 * @param  string $group_name        Group name
624
	 * @return Group_Field $group_object Group object
625
	 */
626
	public function get_group_by_name( $group_name ) {
627
		$group_object = null;
628
629
		foreach ( $this->groups as $group ) {
630
			if ( $group->get_name() == $group_name ) {
631
				$group_object = $group;
632
			}
633
		}
634
635
		return $group_object;
636
	}
637
}
638