layout::base_component()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 20
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 2
b 0
f 0
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
 * Layout Grid Editor
18
 *
19
 * @since 1.0.0
20
 */
21
class layout 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 = 'layout';
31
32
	/**
33
	 * The component modals
34
	 *
35
	 * @since       1.0.0
36
	 * @access      public
37
	 * @var         array
38
	 */
39
	public $modals = array();
40
41
	/**
42
	 * Autoload Children - Checks structure for nested structures
43
	 *
44
	 * @since  1.0.0
45
	 * @access public
46
	 */
47 1
	public function setup() {
48
49
		// create components.
50 1
		if ( ! empty( $this->struct['component'] ) ) {
51
			$this->register_components();
52
		}
53
54 1
		foreach ( $this->modals as $modal ) {
55
56
			if ( $modal->is_submitted() ) {
57
				wp_send_json_success( $modal->get_value() );
58
			}
59
		}
60
61 1
		parent::setup();
62 1
	}
63
64
	/**
65
	 * Register components for layout.
66
	 *
67
	 * @since  1.0.0
68
	 * @access public
69
	 */
70
	public function register_components() {
71
72
		foreach ( $this->struct['component'] as $component_id => $component_struct ) {
73
74
			if ( ! empty( $component_struct['id'] ) ) {
75
				$component_id = $component_struct['id'];
76
			}
77
78
			$component_struct += $this->base_component( $component_id );
79
80
			if ( ! empty( $component_struct['setup'] ) ) {
81
				$component_struct['section'] = $component_struct['setup'];
82
				unset( $component_struct['setup'] );
83
			}
84
85
			$this->modals[] = $this->modal( $component_id, $component_struct );
0 ignored issues
show
Documentation Bug introduced by
The method modal does not exist on object<uix\ui\control\layout>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
		}
87
88
	}
89
90
	/**
91
	 * Define core UIX scripts - override to register core ( common scripts for
92
	 * uix type )
93
	 *
94
	 * @since  1.0.0
95
	 * @access public
96
	 */
97 1
	public function set_assets() {
98
99
		// set style.
100 1
		$this->assets['style']['layout']      = $this->url . 'assets/controls/layout/css/layout' . UIX_ASSET_DEBUG . '.css';
101 1
		$this->assets['style']['layout-grid'] = $this->url . 'assets/controls/layout/css/layout-grid' . UIX_ASSET_DEBUG . '.css';
102
103
		// push to register script.
104 1
		$this->assets['script']['layout']              = array(
105 1
			'src'       => $this->url . 'assets/controls/layout/js/layout' . UIX_ASSET_DEBUG . '.js',
106
			'deps'      => array(
107
				'jquery-ui-draggable',
108
				'jquery-ui-droppable',
109
				'jquery-ui-sortable',
110
			),
111
			'in_footer' => true,
112
		);
113 1
		$this->assets['script']['handlebars']          = array(
114 1
			'src' => $this->url . 'assets/js/handlebars-latest' . UIX_ASSET_DEBUG . '.js',
115
		);
116 1
		$this->assets['script']['baldrick-handlebars'] = array(
117 1
			'src'  => $this->url . 'assets/js/handlebars.baldrick' . UIX_ASSET_DEBUG . '.js',
118
			'deps' => array( 'baldrick' ),
119
		);
120
121
		// modals.
122 1
		$this->assets['script']['modals'] = array(
123 1
			'src'  => $this->url . 'assets/js/modals' . UIX_ASSET_DEBUG . '.js',
124
			'deps' => array( 'baldrick' ),
125
		);
126 1
		$this->assets['style']['modals']  = $this->url . 'assets/css/modals' . UIX_ASSET_DEBUG . '.css';
127
128
129 1
		parent::set_assets();
130 1
	}
131
132
	/**
133
	 * Gets the attributes for the control.
134
	 *
135
	 * @since  1.0.0
136
	 * @access public
137
	 */
138 1
	public function set_attributes() {
139
140 1
		parent::set_attributes();
141 1
		$this->attributes['class'][] = 'hidden';
142
143 1
	}
144
145
	/**
146
	 * Render the Control
147
	 *
148
	 * @since  1.0.0
149
	 * @see    \uix\ui\uix
150
	 * @access public
151
	 * @return string HTML of rendered control
152
	 */
153 1
	public function render() {
154
155 1
		$output = '<div id="' . esc_attr( $this->id() ) . '" data-color="' . esc_attr( $this->base_color() ) . '" data-for="' . esc_attr( $this->id() ) . '-control" class="uix-control uix-control-' . esc_attr( $this->type ) . ' ' . esc_attr( $this->id() ) . '"></div>';
156 1
		$output .= $this->label();
157 1
		$output .= $this->input();
158
159 1
		$output .= $this->component_templates();
160
161 1
		$output .= $this->modal_template();
162
163 1
		return $output;
164
	}
165
166
	/**
167
	 * Returns the label for the control
168
	 *
169
	 * @since  1.0.0
170
	 * @access public
171
	 * @return string label of control
172
	 */
173 1
	public function label() {
174 1
		$output = null;
175 1
		if ( isset( $this->struct['label'] ) ) {
176 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>';
177
		}
178
179 1
		return $output;
180
	}
181
182
	/**
183
	 * Returns the main input field for rendering
184
	 *
185
	 * @since  1.0.0
186
	 * @see    \uix\ui\uix
187
	 * @access public
188
	 * @return string Input field HTML striung
189
	 */
190 1
	public function input() {
191 1
		return '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
192
	}
193
194
	/**
195
	 * Render component templates
196
	 *
197
	 * @since  1.0.0
198
	 * @see    \uix\ui\uix
199
	 * @access public
200
	 * @return string HTML of templates for components
201
	 */
202 1
	public function component_templates() {
203 1
		$output = null;
204 1
		foreach ( $this->modals as $modal ) {
205
206
			$width  = 'data-width="950"';
207
			$height = 'data-height="550"';
208
209
			$output .= '<script type="text/html" ' . $width . ' ' . $height . ' id="' . esc_attr( $this->id() ) . '-' . esc_attr( $modal->slug ) . '">';
210
			if ( isset( $modal->struct['preview'] ) ) {
211
				$template = uix()->add( 'control', $modal->slug, array(
212
					'type'     => 'template',
213
					'template' => $modal->struct['preview'],
214
				) );
215
216
				$output .= $template->render();
217
			} else {
218
				$output .= '<div>' . $modal->struct['label'] . '</div>';
219
			}
220
			$output .= '</script>';
221
222
		}
223
224 1
		return $output;
225
	}
226
227
	/**
228
	 * Render the modal template
229
	 *
230
	 * @since  1.0.0
231
	 * @see    \uix\ui\uix
232
	 * @access public
233
	 * @return string HTML of modals templates
234
	 */
235 1
	public function modal_template() {
236
237 1
		$output = '<script type="text/html" id="' . esc_attr( $this->id() ) . '-components">';
238 1
		foreach ( $this->modals as $modal ) {
239
240
			$label         = $modal->struct['label'];
241
			$data          = $modal->get_data();
242
			$data_template = $this->drill_in( $data[ $modal->slug ], '{{@root' );
243
			$modal->set_data( array( $modal->slug => $data_template ) );
244
245
			$modal->render();
246
247
			$setup = null;
248
			if ( count( $modal->child ) > 1 ) {
249
				$setup = ' data-setup="true" ';
250
			}
251
252
			$output .= '<button type="button" class="button uix-component-trigger" style="margin:12px 0 0 12px;" data-label="' . esc_attr( $modal->attributes['data-title'] ) . '" data-type="' . $modal->slug . '" ' . $setup . ' data-id="' . esc_attr( $modal->id() ) . '">' . $label . '</button> ';
253
254
		}
255 1
		$output .= '</script>';
256
257 1
		return $output;
258
	}
259
260
	/**
261
	 * builds the handlebars based structure for template render
262
	 *
263
	 * @param array  $array the dat astructure to drill into
264
	 * @param string $tag   the final tag to replace the data with.
265
	 *
266
	 * @since  1.0.0
267
	 * @access public
268
	 * @return array array of the data structure
269
	 */
270
	public function drill_in( $array, $tag = null ) {
271
		$back = array();
272
		foreach ( $array as $key => $value ) {
273
			if ( is_array( $value ) && ! empty( $value ) ) {
274
				$back[ $key ] = $this->drill_in( $value, $tag . '.' . $key );
275
			} else {
276
				$back[ $key ] = $tag . '.' . $key . '}}';
277
			}
278
		}
279
280
		return $back;
281
	}
282
283
	/**
284
	 * Sets styling colors
285
	 *
286
	 * @since  1.0.0
287
	 * @access protected
288
	 */
289
	protected function set_active_styles() {
290
291
		$style = '.' . $this->id() . ' .dashicons.dashicons-plus-alt{ color: ' . $this->base_color() . ' !important;}';
292
		$style .= '.' . $this->id() . ' .column-handle{background-color: ' . $this->base_color() . ' !important;}';
293
		$style .= '.' . $this->id() . ' .uix-component-toolbar{background-color: ' . $this->base_color() . ' !important;}';
294
295
		uix_share()->set_active_styles( $style );
296
	}
297
298
	/**
299
	 * Base component structures.
300
	 *
301
	 * @since  3.0.0
302
	 * @access private
303
	 *
304
	 * @param string $id The id for this component.
305
	 *
306
	 * @return array
307
	 */
308
	private function base_component( $id ) {
309
		return array(
310
			'attributes' => array(
311
				'data-master' => true,
312
				'style'       => 'margin:6px 0 0 6px;',
313
			),
314
			'footer'     => array(
315
				'id'      => $id . '_foot',
316
				'control' => array(
317
					'set_component' => array(
318
						'label'      => 'Send to Layout',
319
						'type'       => 'button',
320
						'attributes' => array(
321
							'type' => 'submit',
322
						),
323
					),
324
				),
325
			),
326
		);
327
	}
328
}
329