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

custom::get_scripts_ui()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 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\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
14
/**
15
 * Custom Block
16
 */
17
class custom extends block
18
{
19
	/** @var \phpbb\cache\driver\driver_interface */
20
	protected $cache;
21
22
	/** @var \phpbb\db\driver\driver_interface */
23
	protected $db;
24
25
	/** @var \phpbb\request\request_interface */
26
	protected $request;
27
28
	/** @var \blitze\sitemaker\services\util */
29
	protected $util;
30
31
	/** @var string */
32
	protected $cblocks_table;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\cache\driver\driver_interface	$cache					Cache driver interface
38
	 * @param \phpbb\db\driver\driver_interface		$db						Database object
39
	 * @param \phpbb\request\request_interface		$request				Request object
40
	 * @param \blitze\sitemaker\services\util		$util					Sitemaker util object
41
	 * @param string								$cblocks_table			Name of custom blocks database table
42
	 */
43
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \blitze\sitemaker\services\util $util, $cblocks_table)
44
	{
45
		$this->cache = $cache;
46
		$this->db = $db;
47
		$this->request = $request;
48
		$this->util = $util;
49
		$this->cblocks_table = $cblocks_table;
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55
	public function get_config(array $settings)
56
	{
57
		return array(
58
			'legend1'		=> 'HTML',
59
			'source'			=> array('type' => 'code_editor', 'params' => [['height' => 200]], 'default' => '', 'explain' => true, 'lang_explain' => 'HTML_EXPLAIN'),
60
61
			'legend2'		=> 'SCRIPTS',
62
			'js_scripts'		=> array('lang' => 'JS_SCRIPTS', 'type' => 'multi_input:1', 'default' => []),
63
			'css_scripts'		=> array('lang' => 'CSS_SCRIPTS', 'type' => 'multi_input:1', 'default' => []),
64
		);
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70
	public function display(array $bdata, $edit_mode = false)
71
	{
72
		$status = $bdata['status'];
73
		$cblock = $this->get_block_data($bdata['bid']);
74
		$content = $this->get_content_for_display($cblock, $bdata['settings']['source']);
75
76
		$this->add_scripts((array) $bdata['settings']['js_scripts'], 'js');
77
		$this->add_scripts((array) $bdata['settings']['css_scripts'], 'css');
78
79
		if (!$bdata['settings']['source'])
80
		{
81
			$this->show_editor($cblock, $content, $status, $edit_mode);
82
		}
83
84
		return array(
85
			'title'		=> 'BLOCK_TITLE',
86
			'content'	=> $content,
87
			'status'	=> $status,
88
		);
89
	}
90
91
	/**
92
	 * @param int $block_id
93
	 * @return array
94
	 */
95
	public function edit($block_id)
96
	{
97
		$content = $this->request->variable('content', '', true);
98
		$cblocks = $this->get_custom_blocks();
99
100
		$sql_data =	$this->get_default_fields($block_id);
101
		$sql_data['block_content'] = $content;
102
103
		generate_text_for_storage($sql_data['block_content'], $sql_data['bbcode_uid'], $sql_data['bbcode_bitfield'], $sql_data['bbcode_options'], true, false, true);
104
105
		$this->save($sql_data, isset($cblocks[$block_id]));
106
107
		return array(
108
			'id'		=> $block_id,
109
			'content'	=> $this->get_content_for_display($sql_data),
110
		);
111
	}
112
113
	/**
114
	 * @param int $from_bid
115
	 * @param int $to_bid
116
	 * @see \blitze\sitemaker\services\blocks\action\copy_route::copy_custom_block
117
	 */
118
	public function copy($from_bid, $to_bid)
119
	{
120
		$cblocks = $this->get_custom_blocks();
121
122
		if (isset($cblocks[$from_bid]))
123
		{
124
			$sql_data = $cblocks[$from_bid];
125
			$sql_data['block_id'] = $to_bid;
126
127
			$this->save($sql_data, isset($cblocks[$to_bid]));
128
		}
129
	}
130
131
	/**
132
	 * @param array $scripts
133
	 * @param string $type
134
	 * @return void
135
	 */
136
	private function add_scripts($scripts, $type)
137
	{
138
		$this->util->add_assets(array_filter(array($type => array_filter($scripts))));
139
	}
140
141
	/**
142
	 * @param int $bid
143
	 * @return array
144
	 */
145
	private function get_block_data($bid)
146
	{
147
		$cblock = $this->get_custom_blocks();
148
149
		return (isset($cblock[$bid])) ? $cblock[$bid] : $this->get_default_fields($bid);
150
	}
151
152
	/**
153
	 * @param array $data
154
	 * @param string $content
155
	 * @return string
156
	 */
157
	private function get_content_for_display(array $data, $content = '')
158
	{
159
		if (!$content)
160
		{
161
			$content = generate_text_for_display($data['block_content'], $data['bbcode_uid'], $data['bbcode_bitfield'], $data['bbcode_options']);
162
		}
163
		return html_entity_decode($content);
164
	}
165
166
	/**
167
	 * @param array $sql_data
168
	 * @param bool $block_exists
169
	 */
170
	private function save(array $sql_data, $block_exists)
171
	{
172
		if (!$block_exists)
173
		{
174
			$sql = 'INSERT INTO ' . $this->cblocks_table . ' ' . $this->db->sql_build_array('INSERT', $sql_data);
175
		}
176
		else
177
		{
178
			$sql = 'UPDATE ' . $this->cblocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE block_id = ' . (int) $sql_data['block_id'];
179
		}
180
		$this->db->sql_query($sql);
181
		$this->cache->destroy('sm_cblocks');
182
	}
183
184
	/**
185
	 * @param array $cblock
186
	 * @param string $content
187
	 * @param int $status
188
	 * @param bool $edit_mode
189
	 */
190
	private function show_editor(array $cblock, &$content, &$status, $edit_mode)
191
	{
192
		if ($edit_mode !== false)
193
		{
194
			decode_message($cblock['block_content'], $cblock['bbcode_uid']);
195
196
			$block_is_active = $status;
197
			$status = ($content && $status) ? true : false;
198
			$content = '<div id="block-editor-' . $cblock['block_id'] . '" class="editable editable-block" data-service="blitze.sitemaker.block.custom" data-method="edit" data-raw="' . $cblock['block_content'] . '" data-active="' . $block_is_active . '">' . $content . '</div>';
199
		}
200
	}
201
202
	/**
203
	 * @return array
204
	 */
205
	private function get_custom_blocks()
206
	{
207
		if (($cblocks = $this->cache->get('sm_cblocks')) === false)
208
		{
209
			$sql = 'SELECT *
210
				FROM ' . $this->cblocks_table;
211
			$result = $this->db->sql_query($sql);
212
213
			$cblocks = array();
214
			while ($row = $this->db->sql_fetchrow($result))
215
			{
216
				$cblocks[$row['block_id']] = $row;
217
			}
218
			$this->db->sql_freeresult($result);
219
220
			$this->cache->put('sm_cblocks', $cblocks);
221
		}
222
223
		return $cblocks;
224
	}
225
226
	/**
227
	 * @param int $bid
228
	 * @return array
229
	 */
230
	private function get_default_fields($bid)
231
	{
232
		return array(
233
			'block_id'			=> $bid,
234
			'block_content'		=> '',
235
			'bbcode_bitfield'	=> '',
236
			'bbcode_options'	=> 7,
237
			'bbcode_uid'		=> '',
238
		);
239
	}
240
}
241