Completed
Push — release-3.2.0 ( 5118c1...fcf3db )
by Daniel
15:42
created

code_editor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fullscreen_allowed() 0 3 2
A build_code_editor() 0 12 1
A get_code_editor_attributes() 0 9 3
A prep_field() 0 10 1
A get_name() 0 3 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2019 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\blocks\config\fields;
11
12
/**
13
 * @package sitemaker
14
 */
15
class code_editor extends cfg_field_base
16
{
17
	/**
18
	 * @inheritdoc
19
	 */
20
	public function get_name()
21
	{
22
		return 'code_editor';
23
	}
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28
	public function prep_field(array &$vars, array &$type, $field, array $db_settings)
29
	{
30
		$vars['method'] = 'build_code_editor';
31
		$vars['params'] = array_reverse((array) $vars['params']);
32
		$vars['params'][] = $vars['lang_explain'];
33
		$vars['params'][] = $db_settings[$field];
34
		$vars['params'][] = $field;
35
		$vars['params'] = array_reverse($vars['params']);
36
37
		$type[0] = 'custom';
38
	}
39
40
	/**
41
	 * Used to add a code editor to blocks config
42
	 *
43
	 * @param string $key
44
	 * @param string $value
45
	 * @param string $explain
46
	 * @param array $data_props
47
	 * @param string $label
48
	 * @return string
49
	 */
50
	public function build_code_editor($key, $value, $explain, array $data_props = array(), $label = '')
51
	{
52
		$this->ptemplate->assign_vars(array(
53
			'key'			=> $key,
54
			'value'			=> $value,
55
			'label'			=> $label,
56
			'explain'		=> $explain,
57
			'attributes'	=> $this->get_code_editor_attributes($data_props),
58
			'fullscreen'	=> $this->fullscreen_allowed($data_props),
59
		));
60
61
		return $this->ptemplate->render_view('blitze/sitemaker', 'cfg_fields/code_editor.html', $key);
62
	}
63
64
	/**
65
	 * @param array $data_props
66
	 * @return bool
67
	 */
68
	protected function fullscreen_allowed(array $data_props = array())
69
	{
70
		return (!isset($data_props['allow-full-screen']) || $data_props['allow-full-screen']);
71
	}
72
73
	/**
74
	 * @param array $data_props
75
	 * @return string
76
	 */
77
	protected function get_code_editor_attributes(array $data_props = array())
78
	{
79
		$attributes = '';
80
		foreach ($data_props as $prop => $value)
81
		{
82
			$value = (gettype($value) === 'boolean') ? (int) $value : $value;
83
			$attributes .= " data-{$prop}=\"{$value}\"";
84
		}
85
		return $attributes;
86
	}
87
}
88