Passed
Push — release-3.2.0 ( 24bcfb...4d8cca )
by Daniel
04:25
created

code_editor::prep_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
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 implements cfg_field_interface
16
{
17
	/** @var \phpbb\language\language */
18
	protected $translator;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \phpbb\language\language	$translator		Language object
24
	 */
25
	public function __construct(\phpbb\language\language $translator)
26
	{
27
		$this->translator = $translator;
28
	}
29
30
	/**
31
	 * @inheritdoc
32
	 */
33
	public function get_name()
34
	{
35
		return 'code_editor';
36
	}
37
38
	/**
39
	 * {@inheritdoc}
40
	 */
41
	public function prep_field(array &$vars, array &$type, $field, array $db_settings)
42
	{
43
		$vars['method'] = 'build_code_editor';
44
		$vars['params'] = array_reverse((array) $vars['params']);
45
		$vars['params'][] = $vars['lang_explain'];
46
		$vars['params'][] = $db_settings[$field];
47
		$vars['params'][] = $field;
48
		$vars['params'] = array_reverse($vars['params']);
49
50
		$type[0] = 'custom';
51
	}
52
53
	/**
54
	 * Used to add a code editor to blocks config
55
	 *
56
	 * @param string $key
57
	 * @param string $value
58
	 * @param string $explain
59
	 * @param array $data_props
60
	 * @param string $label
61
	 * @return string
62
	 */
63
	public function build_code_editor($key, $value, $explain, array $data_props = array(), $label = '')
64
	{
65
		$html = '';
66
		$id = $key . '-editor';
67
		$class = $id . '-button';
68
		$attributes = $this->get_code_editor_attributes($data_props);
69
70
		$html .= ($label) ? '<label for="' . $key . '"><strong>' . $this->translator->lang($label) . '</strong></label>' : '';
71
		$html .= ($explain) ? '<span>' . $explain . '</span>' : '';
72
		$html .= '<textarea id="' . $id . '" class="code-editor" name="config[' . $key . ']"' . $attributes . '>' . $value . '</textarea>';
73
		$html .= '<div class="align-right">';
74
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="undo" title="' . $this->translator->lang('UNDO') . '"><i class="fa fa-undo" aria-hidden="true"></i></button>';
75
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="redo" title="' . $this->translator->lang('REDO') . '"><i class="fa fa-repeat" aria-hidden="true"></i></button>';
76
		$html .= '<button class="' . $class . ' CodeMirror-button" data-action="clear" title="' . $this->translator->lang('CLEAR') . '"><i class="fa fa-ban" aria-hidden="true"></i></button>';
77
		$html .= $this->fullscreen_allowed($data_props) ? '<button class="' . $class . ' CodeMirror-button" data-action="fullscreen" title="' . $this->translator->lang('FULLSCREEN') . '"><i class="fa fa-window-restore" aria-hidden="true"></i></button>' : '';
78
		$html .= '</div>';
79
80
		return $html;
81
	}
82
83
	/**
84
	 * @param array $data_props
85
	 * @return bool
86
	 */
87
	protected function fullscreen_allowed(array $data_props = array())
88
	{
89
		return (!isset($data_props['allow-full-screen']) || $data_props['allow-full-screen']);
90
	}
91
92
	/**
93
	 * @param array $data_props
94
	 * @return string
95
	 */
96
	protected function get_code_editor_attributes(array $data_props = array())
97
	{
98
		$attributes = '';
99
		foreach ($data_props as $prop => $value)
100
		{
101
			$value = (gettype($value) === 'boolean') ? (int) $value : $value;
102
			$attributes .= " data-{$prop}=\"{$value}\"";
103
		}
104
		return $attributes;
105
	}
106
}
107