Completed
Push — develop ( 03ec06...1b5c10 )
by David
01:58
created

item::get_base()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 41
ccs 0
cts 13
cp 0
crap 2
rs 8.8571
1
<?php
2
/**
3
 * UIX Control
4
 *
5
 * @package   controls
6
 * @author    David Cramer
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 David Cramer
10
 */
11
12
namespace uix\ui\control;
13
14
use uix\ui\modal;
15
16
/**
17
 * Modal based config items
18
 *
19
 * @since 1.0.0
20
 */
21
class item extends \uix\ui\control {
22
23
	/**
24
	 * The type of object
25
	 *
26
	 * @since       1.0.0
27
	 * @access      public
28
	 * @var         string
29
	 */
30
	public $type = 'item';
31
32
	/**
33
	 * The templates to render in the footer
34
	 *
35
	 * @since  1.0.0
36
	 * @access public
37
	 * @var      string
38
	 */
39
	public $templates = null;
40
41
	/**
42
	 * Autoload Children - Checks structure for nested structures
43
	 *
44
	 * @since  1.0.0
45
	 * @access public
46 1
	 */
47
	public function setup() {
48 1
49 1
		$this->struct['modal'] = $this->get_base();
50 1
51 1
		if ( ! empty( $this->struct['config'] ) ) {
52 1
			$this->struct['modal']['config'] = array_merge( $this->struct['modal']['config'], $this->struct['config'] );
53
			if ( ! empty( $this->struct['config']['add_text'] ) ) {
54
				$this->struct['modal']['config']['footer']['control']['add_item']['label'] = $this->struct['config']['add_text'];
55
			}
56
			if ( ! empty( $this->struct['config']['update_text'] ) ) {
57 1
				$this->struct['modal']['config']['footer']['control']['update_item']['label'] = $this->struct['config']['update_text'];
58 1
			}
59
			unset( $this->struct['config'] ); // remove so no clashing.
60 1
		}
61 1
62
		$this->struct['modal']['config']['view'] = $this->struct['modal']['config']['template'];
63 1
		unset( $this->struct['modal']['config']['template'] );
64
65 1
		add_action( 'uix_control_item_submit_' . $this->slug, $this->struct['modal']['config']['callback'], 100 );
66
67
		parent::setup();
68 1
69 1
		// set data for templates.
70
		if ( ! $this->child['config']->is_submitted() ) {
71
			$data                                                        = $this->child['config']->get_data();
72
			$this->child['config']->struct['attributes']['data-default'] = json_encode( $data );
73
			$data_template                                               = $this->drill_in( $data );
74
75
			$this->child['config']->set_data( $data_template );
76 1
		}
77 1
78
	}
79
80
	/**
81
	 * Builds the handlebars based structure for template render
82
	 *
83
	 * @param array  $array the data structure to drill into.
84
	 * @param string $tag   , the final tag to replace the data with.
85
	 *
86
	 * @since  1.0.0
87
	 * @access public
88 1
	 * @return array array of the data structure
89
	 */
90
	public function drill_in( $array, $tag = '{{json @root' ) {
91
92
		$back = array();
93
		foreach ( $array as $key => $value ) {
94
			if ( is_array( $value ) && ! empty( $value ) ) {
95
				$back[ $key ] = $this->drill_in( $value, $tag . '.' . $key );
96
			} else {
97
				$back[ $key ] = $tag . '.' . $key . '}}';
98
			}
99 1
		}
100 1
101
		return $back;
102 1
	}
103
104 1
	/**
105
	 * Sends the items json code back to the browser
106
	 *
107 1
	 * @since  1.0.0
108 1
	 * @access public
109 1
	 */
110 1
	public function send_item_json() {
111
		// send to browser.
112 1
		wp_send_json_success( $this->child['config']->get_value() );
113
	}
114
115 1
	/**
116
	 * All objects loaded - application method for finishing off loading objects
117
	 *
118
	 * @since  1.0.0
119
	 * @access public
120
	 */
121
	public function init() {
122
		$this->handle_submit();
123
		parent::init();
124
	}
125
126
	/**
127 1
	 * Handles the new item create submission
128
	 *
129 1
	 * @since  1.0.0
130 1
	 * @access public
131 1
	 */
132 1
	public function handle_submit() {
133
		if ( isset( $this->child['config'] ) && $this->child['config']->is_submitted() ) {
134 1
			$data = $this->child['config']->get_value();
135
			if ( isset( $data['config_foot'] ) ) {
136
				unset( $data['config_foot'] ); // default footer has no values. override to include values in it.
137
			}
138 1
			do_action( 'uix_control_item_submit_' . $this->slug, $data, $this );
139
		}
140
	}
141
142
	/**
143
	 * Define core UIX scripts - override to register core ( common scripts for
144
	 * uix type )
145
	 *
146
	 * @since  1.0.0
147
	 * @access public
148
	 */
149
	public function set_assets() {
150
151
		$this->assets['style']['item']                 = $this->url . 'assets/css/item' . UIX_ASSET_DEBUG . '.css';
152
		$this->assets['script']['handlebars']          = array(
153
			'src' => $this->url . 'assets/js/handlebars-latest' . UIX_ASSET_DEBUG . '.js',
154
		);
155
		$this->assets['script']['baldrick-handlebars'] = array(
156
			'src'  => $this->url . 'assets/js/handlebars.baldrick' . UIX_ASSET_DEBUG . '.js',
157
			'deps' => array( 'baldrick' ),
158
		);
159
		$this->assets['script']['item']                = array(
160
			'src' => $this->url . 'assets/js/item' . UIX_ASSET_DEBUG . '.js',
161
		);
162
		parent::set_assets();
163
	}
164
165
	/**
166
	 * Gets the attributes for the control.
167
	 *
168
	 * @since  1.0.0
169
	 * @access public
170
	 */
171
	public function set_attributes() {
172
173
		parent::set_attributes();
174
		$this->attributes['class'] = 'hidden';
175
		$this->attributes['value'] = '{{json this}}';
176
177
	}
178
179
	/**
180
	 * Render the Control
181
	 *
182
	 * @since  1.0.0
183
	 * @see    \uix\ui\uix
184
	 * @access public
185 1
	 * @return string HTML of rendered control
186
	 */
187 1
	public function render() {
188 1
		add_action( 'admin_footer', array( $this, 'render_footer_script' ) );
189 1
		add_action( 'wp_footer', array( $this, 'render_footer_script' ) );
190
		$output          = null;
191 1
		$this->templates .= $this->item_template();
192 1
		$output          .= '<div id="' . esc_attr( $this->id() ) . '" data-color="' . esc_attr( $this->base_color() ) . '" data-for="' . esc_attr( $this->id() ) . '-control" class="processing uix-control uix-control-' . esc_attr( $this->type ) . ' ' . esc_attr( $this->id() ) . '">';
193
		$output          .= '</div>';
194
		$output          .= $this->child['config']->render();
195 1
		$output          .= $this->input();
196 1
197
198 1
		return $output;
199 1
	}
200
201
	/**
202
	 * Render the modal template
203
	 *
204
	 * @since  1.0.0
205
	 * @see    \uix\ui\uix
206
	 * @access public
207 1
	 * @return string HTML of modals templates
208
	 */
209 1
	public function item_template() {
210 1
		$output = '<script type="text/html" id="' . esc_attr( $this->id() ) . '-tmpl">';
211 1
		$output .= '<div class="uix-item">';
212
		if ( false !== strpos( $this->child['config']->struct['view'], ABSPATH ) ) {
213 1
			ob_start();
214
			include $this->child['config']->struct['view'];
215
			$output .= ob_get_clean();
216
		} else {
217
			$output .= $this->child['config']->struct['view'];
218
		}
219
		$output .= '</div>';
220
		$output .= '</script>';
221
222
		return $output;
223 1
	}
224 1
225 1
	/**
226 1
	 * Returns the main input field for rendering
227 1
	 *
228 1
	 * @since  1.0.0
229 1
	 * @see    \uix\ui\uix
230 1
	 * @access public
231 1
	 * @return string Input field HTML striung
232
	 */
233
	public function input() {
234 1
		return '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
235
	}
236
237
	/**
238
	 * Returns the label for the control
239
	 *
240
	 * @since  1.0.0
241
	 * @access public
242
	 * @return string label of control
243
	 */
244
	public function label() {
245 1
		$output = null;
246 1
		if ( isset( $this->struct['label'] ) ) {
247 1
			$output .= '<label for="' . esc_attr( $this->id() ) . '-control" class="uix-add-row"><span class="uix-control-label">' . esc_html( $this->struct['label'] ) . '</span></label>';
248 1
		}
249
250
		return $output;
251
	}
252
253 1
	/**
254
	 * Render the script footer template
255 1
	 *
256 1
	 * @since  1.0.0
257
	 * @see    \uix\ui\uix
258 1
	 * @access public
259
	 */
260
	public function render_footer_script() {
261
		$output = null;
262
		if ( ! empty( $this->templates ) ) {
263
			$output .= $this->templates;
264
		}
265
266
		echo $output;
267
	}
268
269 1
	/**
270 1
	 * Sets styling colors
271
	 *
272
	 * @since  1.0.0
273
	 * @access protected
274
	 */
275
	protected function set_active_styles() {
276
277
		$style = '.' . $this->id() . ' .dashicons.dashicons-plus-alt{ color: ' . $this->base_color() . ' !important;}';
278
		$style .= '.' . $this->id() . ' .column-handle{background-color: ' . $this->base_color() . ' !important;}';
279
		$style .= '.' . $this->id() . ' .uix-component-toolbar{background-color: ' . $this->base_color() . ' !important;}';
280
		$style .= '.' . $this->id() . '.processing:after {background: url(' . $this->url . 'assets/svg/loading.php?base_color=' . urlencode( str_replace( '#', '', $this->base_color() ) ) . ') no-repeat center center;}';
281
282
		uix_share()->set_active_styles( $style );
283
284
	}
285
286
	/**
287
	 * Gets the base struct for an item.
288
	 *
289
	 * @since  1.0.0
290
	 * @access protected
291
	 * @return array
292
	 */
293
	private function get_base() {
294
		return array(
295
			'config' => array(
296
				'label'       => __( 'Add Item', 'uix' ),
297
				'description' => __( 'Setup Item', 'uix' ),
298
				'callback'    => array( $this, 'send_item_json' ),
299
				'attributes'  => array(
300
					'class'        => 'page-title-action',
301
					'data-content' => 'uix_item_control_modal',
302
				),
303
				'height'      => 540,
304
				'width'       => 380,
305
				'config'      => array(
306
					'target'  => 'uix_item_control_modal_handler',
307
					'control' => $this->id(),
308
				),
309
				'template'    => '{{json this}} <button type="button" class="uix-item-edit button button-small">' . esc_html__( 'Edit', 'uix' ) . '</button> | <button type="button" class="uix-item-remove button button-small">' . esc_html__( 'Remove', 'uix' ) . '</button>',
310
				'footer'      => array(
311
					'id'      => $this->slug . '_foot',
312
					'control' => array(
313
						'add_item'    => array(
314
							'label'      => __( 'Add', 'uix' ),
315
							'type'       => 'button',
316
							'attributes' => array(
317
								'type'       => 'submit',
318
								'data-state' => 'add',
319
							),
320
						),
321
						'update_item' => array(
322
							'label'      => __( 'Update', 'uix' ),
323
							'type'       => 'button',
324
							'attributes' => array(
325
								'type'       => 'submit',
326
								'data-state' => 'update',
327
							),
328
						),
329
					),
330
				),
331
			),
332
		);
333
	}
334
}
335