Completed
Push — develop ( cacd87...03ec06 )
by David
01:29
created

layout::base_component()   A

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
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 1
	 */
47
	public function setup() {
48
49 1
		// create components.
50
		if ( ! empty( $this->struct['component'] ) ) {
51
			$this->register_components();
52
		}
53 1
54
		foreach ( $this->modals as $modal ) {
55
56
			if ( $modal->is_submitted() ) {
57
				wp_send_json_success( $modal->get_value() );
58
			}
59
		}
60 1
61 1
		parent::setup();
62
	}
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();
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
	public function set_assets() {
98
99
		// set style.
100
		$this->assets['style']['layout']      = $this->url . 'assets/controls/layout/css/layout' . UIX_ASSET_DEBUG . '.css';
101
		$this->assets['style']['layout-grid'] = $this->url . 'assets/controls/layout/css/layout-grid' . UIX_ASSET_DEBUG . '.css';
102
103
		// push to register script.
104
		$this->assets['script']['layout']              = array(
105
			'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
			'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 1
121 1
		// modals.
122
		$this->assets['script']['modals'] = array(
123
			'src'  => $this->url . 'assets/js/modals' . UIX_ASSET_DEBUG . '.js',
124
			'deps' => array( 'baldrick' ),
125
		);
126
		$this->assets['style']['modals']  = $this->url . 'assets/css/modals' . UIX_ASSET_DEBUG . '.css';
127
128
129 1
		parent::set_assets();
130 1
	}
131
132 1
	/**
133 1
	 * Gets the attributes for the control.
134
	 *
135
	 * @since  1.0.0
136
	 * @access public
137
	 */
138 1
	public function set_attributes() {
139 1
140
		parent::set_attributes();
141
		$this->attributes['class'] = 'hidden';
142 1
143
	}
144
145 1
	/**
146 1
	 * 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
	public function render() {
154 1
155
		$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
		$output .= $this->modal_template();
162
163
		return $output;
164
	}
165
166
	/**
167
	 * Returns the label for the control
168
	 *
169 1
	 * @since  1.0.0
170
	 * @access public
171 1
	 * @return string label of control
172 1
	 */
173 1
	public function label() {
174
		$output = null;
175 1
		if ( isset( $this->struct['label'] ) ) {
176
			$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 1
		}
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 1
	 */
190 1
	public function input() {
191 1
		return '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
192 1
	}
193
194
	/**
195 1
	 * 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
	public function component_templates() {
203
		$output = null;
204
		foreach ( $this->modals as $modal ) {
205
206 1
			$width  = 'data-width="950"';
207 1
			$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 1
				$output .= '<div>' . $modal->struct['label'] . '</div>';
219 1
			}
220 1
			$output .= '</script>';
221
222
		}
223
224
		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
	public function modal_template() {
236
237
		$output = '<script type="text/html" id="' . esc_attr( $this->id() ) . '-components">';
238
		foreach ( $this->modals as $modal ) {
239
240 1
			$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 1
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 1
254 1
		}
255
		$output .= '</script>';
256
257
		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 1
		$back = array();
272
		foreach ( $array as $key => $value ) {
273 1
			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
	private function base_component() {
305
		return array(
306
			'attributes' => array(
307
				'data-master' => true,
308
				'style'       => 'margin:6px 0 0 6px;',
309
			),
310
			'footer'     => array(
311
				'id'      => $component_id . '_foot',
0 ignored issues
show
Bug introduced by
The variable $component_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
312
				'control' => array(
313
					'set_component' => array(
314
						'label'      => 'Send to Layout',
315
						'type'       => 'button',
316
						'attributes' => array(
317
							'type' => 'submit',
318
						),
319
					),
320
				),
321
			),
322
		);
323
	}
324
}
325