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
|
45 |
|
* @param \phpbb\request\request_interface $request Request object |
40
|
|
|
* @param \blitze\sitemaker\services\util $util Sitemaker util object |
41
|
45 |
|
* @param string $cblocks_table Name of custom blocks database table |
42
|
45 |
|
*/ |
43
|
45 |
|
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 |
|
{ |
45
|
45 |
|
$this->cache = $cache; |
46
|
|
|
$this->db = $db; |
47
|
|
|
$this->request = $request; |
48
|
|
|
$this->util = $util; |
49
|
|
|
$this->cblocks_table = $cblocks_table; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
1 |
|
* {@inheritdoc} |
54
|
1 |
|
*/ |
55
|
1 |
|
public function get_config(array $settings) |
56
|
|
|
{ |
57
|
|
|
return array( |
58
|
|
|
'legend1' => 'HTML', |
59
|
|
|
'source' => array('lang' => '', 'type' => 'textarea:20:40', 'default' => '', 'explain' => false, 'append' => 'HTML_EXPLAIN'), |
60
|
|
|
'legend2' => 'SCRIPTS', |
61
|
6 |
|
'js_scripts' => array('type' => 'custom', 'default' => [], 'object' => $this, 'method' => 'get_scripts_ui'), |
62
|
|
|
'css_scripts' => array('type' => 'custom', 'default' => [], 'object' => $this, 'method' => 'get_scripts_ui'), |
63
|
6 |
|
); |
64
|
6 |
|
} |
65
|
6 |
|
|
66
|
|
|
/** |
67
|
6 |
|
* {@inheritdoc} |
68
|
6 |
|
*/ |
69
|
4 |
|
public function display(array $bdata, $edit_mode = false) |
70
|
4 |
|
{ |
71
|
|
|
$status = $bdata['status']; |
72
|
|
|
$cblock = $this->get_block_data($bdata['bid']); |
73
|
6 |
|
$content = $this->get_content_for_display($cblock, $bdata['settings']['source']); |
74
|
6 |
|
|
75
|
6 |
|
$this->add_scripts((array) $bdata['settings']['js_scripts'], 'js'); |
76
|
6 |
|
$this->add_scripts((array) $bdata['settings']['css_scripts'], 'css'); |
77
|
|
|
|
78
|
|
|
if (!$bdata['settings']['source']) |
79
|
|
|
{ |
80
|
|
|
$this->show_editor($cblock, $content, $status, $edit_mode); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
return array( |
84
|
|
|
'title' => 'BLOCK_TITLE', |
85
|
3 |
|
'content' => $content, |
86
|
3 |
|
'status' => $status, |
87
|
|
|
); |
88
|
3 |
|
} |
89
|
3 |
|
|
90
|
|
|
/** |
91
|
3 |
|
* @param int $block_id |
92
|
|
|
* @return array |
93
|
3 |
|
*/ |
94
|
|
|
public function edit($block_id) |
95
|
|
|
{ |
96
|
3 |
|
$content = $this->request->variable('content', '', true); |
97
|
3 |
|
$cblocks = $this->get_custom_blocks(); |
98
|
3 |
|
|
99
|
|
|
$sql_data = $this->get_default_fields($block_id); |
100
|
|
|
$sql_data['block_content'] = $content; |
101
|
|
|
|
102
|
|
|
generate_text_for_storage($sql_data['block_content'], $sql_data['bbcode_uid'], $sql_data['bbcode_bitfield'], $sql_data['bbcode_options'], true, false, true); |
103
|
|
|
|
104
|
|
|
$this->save($sql_data, isset($cblocks[$block_id])); |
105
|
|
|
|
106
|
1 |
|
return array( |
107
|
|
|
'id' => $block_id, |
108
|
1 |
|
'content' => $this->get_content_for_display($sql_data), |
109
|
|
|
); |
110
|
1 |
|
} |
111
|
1 |
|
|
112
|
1 |
|
/** |
113
|
1 |
|
* @param int $from_bid |
114
|
|
|
* @param int $to_bid |
115
|
1 |
|
* @see \blitze\sitemaker\services\blocks\action\copy_route::copy_custom_block |
116
|
1 |
|
*/ |
117
|
1 |
|
public function copy($from_bid, $to_bid) |
118
|
|
|
{ |
119
|
|
|
$cblocks = $this->get_custom_blocks(); |
120
|
|
|
|
121
|
|
|
if (isset($cblocks[$from_bid])) |
122
|
|
|
{ |
123
|
6 |
|
$sql_data = $cblocks[$from_bid]; |
124
|
|
|
$sql_data['block_id'] = $to_bid; |
125
|
6 |
|
|
126
|
|
|
$this->save($sql_data, isset($cblocks[$to_bid])); |
127
|
6 |
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param mixed $scripts |
132
|
|
|
* @param string $key |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
9 |
|
public function get_scripts_ui($scripts, $key) |
136
|
|
|
{ |
137
|
9 |
|
$this->ptemplate->assign_vars(array( |
138
|
9 |
|
'field' => $key, |
139
|
7 |
|
'scripts' => array_filter((array) $scripts), |
140
|
7 |
|
)); |
141
|
9 |
|
|
142
|
|
|
return $this->ptemplate->render_view('blitze/sitemaker', 'blocks/custom_block_scripts.html', 'custom_block_scripts'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $scripts |
147
|
|
|
* @param string $type |
148
|
4 |
|
* @return void |
149
|
|
|
*/ |
150
|
4 |
|
private function add_scripts($scripts, $type) |
151
|
4 |
|
{ |
152
|
2 |
|
$this->util->add_assets(array_filter(array($type => array_filter($scripts)))); |
153
|
2 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
2 |
|
* @param int $bid |
157
|
|
|
* @return array |
158
|
4 |
|
*/ |
159
|
4 |
|
private function get_block_data($bid) |
160
|
4 |
|
{ |
161
|
|
|
$cblock = $this->get_custom_blocks(); |
162
|
|
|
|
163
|
|
|
return (isset($cblock[$bid])) ? $cblock[$bid] : $this->get_default_fields($bid); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param array $data |
168
|
4 |
|
* @param string $content |
169
|
|
|
* @return string |
170
|
4 |
|
*/ |
171
|
4 |
|
private function get_content_for_display(array $data, $content = '') |
172
|
2 |
|
{ |
173
|
|
|
if (!$content) |
174
|
2 |
|
{ |
175
|
2 |
|
$content = generate_text_for_display($data['block_content'], $data['bbcode_uid'], $data['bbcode_bitfield'], $data['bbcode_options']); |
176
|
2 |
|
} |
177
|
2 |
|
return html_entity_decode($content); |
178
|
4 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $sql_data |
182
|
|
|
* @param bool $block_exists |
183
|
10 |
|
*/ |
184
|
|
|
private function save(array $sql_data, $block_exists) |
185
|
10 |
|
{ |
186
|
10 |
|
if (!$block_exists) |
187
|
|
|
{ |
188
|
10 |
|
$sql = 'INSERT INTO ' . $this->cblocks_table . ' ' . $this->db->sql_build_array('INSERT', $sql_data); |
189
|
10 |
|
} |
190
|
|
|
else |
191
|
10 |
|
{ |
192
|
10 |
|
$sql = 'UPDATE ' . $this->cblocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE block_id = ' . (int) $sql_data['block_id']; |
193
|
|
|
} |
194
|
10 |
|
$this->db->sql_query($sql); |
195
|
10 |
|
$this->cache->destroy('sm_cblocks'); |
196
|
10 |
|
} |
197
|
|
|
|
198
|
10 |
|
/** |
199
|
10 |
|
* @param array $cblock |
200
|
|
|
* @param string $content |
201
|
10 |
|
* @param int $status |
202
|
|
|
* @param bool $edit_mode |
203
|
|
|
*/ |
204
|
|
|
private function show_editor(array $cblock, &$content, &$status, $edit_mode) |
205
|
|
|
{ |
206
|
|
|
if ($edit_mode !== false) |
207
|
|
|
{ |
208
|
5 |
|
decode_message($cblock['block_content'], $cblock['bbcode_uid']); |
209
|
|
|
|
210
|
|
|
$block_is_active = $status; |
211
|
5 |
|
$status = ($content && $status) ? true : false; |
212
|
5 |
|
$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>'; |
213
|
5 |
|
} |
214
|
5 |
|
} |
215
|
5 |
|
|
216
|
5 |
|
/** |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
private function get_custom_blocks() |
220
|
|
|
{ |
221
|
|
|
if (($cblocks = $this->cache->get('sm_cblocks')) === false) |
222
|
|
|
{ |
223
|
|
|
$sql = 'SELECT * |
224
|
|
|
FROM ' . $this->cblocks_table; |
225
|
|
|
$result = $this->db->sql_query($sql); |
226
|
|
|
|
227
|
|
|
$cblocks = array(); |
228
|
|
|
while ($row = $this->db->sql_fetchrow($result)) |
229
|
|
|
{ |
230
|
|
|
$cblocks[$row['block_id']] = $row; |
231
|
|
|
} |
232
|
|
|
$this->db->sql_freeresult($result); |
233
|
|
|
|
234
|
|
|
$this->cache->put('sm_cblocks', $cblocks); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $cblocks; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param int $bid |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
private function get_default_fields($bid) |
245
|
|
|
{ |
246
|
|
|
return array( |
247
|
|
|
'block_id' => $bid, |
248
|
|
|
'block_content' => '', |
249
|
|
|
'bbcode_bitfield' => '', |
250
|
|
|
'bbcode_options' => 7, |
251
|
|
|
'bbcode_uid' => '', |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|