1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2016 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\content\services\form; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\Container; |
13
|
|
|
use Cocur\Slugify\Slugify; |
14
|
|
|
|
15
|
|
|
class builder |
16
|
|
|
{ |
17
|
|
|
/** @var \phpbb\auth\auth */ |
18
|
|
|
protected $auth; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\event\dispatcher_interface */ |
21
|
|
|
protected $phpbb_dispatcher; |
22
|
|
|
|
23
|
|
|
/** @var \phpbb\language\language */ |
24
|
|
|
protected $language; |
25
|
|
|
|
26
|
|
|
/** @var \phpbb\request\request_interface */ |
27
|
|
|
protected $request; |
28
|
|
|
|
29
|
|
|
/** @var \phpbb\template\context */ |
30
|
|
|
protected $template_context; |
31
|
|
|
|
32
|
|
|
/** @var \phpbb\user */ |
33
|
|
|
protected $user; |
34
|
|
|
|
35
|
|
|
/* @var \blitze\content\services\fields */ |
36
|
|
|
protected $fields; |
37
|
|
|
|
38
|
|
|
/* @var \blitze\content\services\types */ |
39
|
|
|
protected $types; |
40
|
|
|
|
41
|
|
|
/** @var \blitze\content\services\form\form */ |
42
|
|
|
protected $form; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
protected $phpbb_root_path; |
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
protected $php_ext; |
49
|
|
|
|
50
|
|
|
/** @var bool */ |
51
|
|
|
protected $req_approval = false; |
52
|
|
|
|
53
|
|
|
/** @var bool */ |
54
|
|
|
protected $req_mod_input = false; |
55
|
|
|
|
56
|
|
|
/** @var bool */ |
57
|
|
|
protected $user_is_mod = false; |
58
|
|
|
|
59
|
|
|
/** @var array */ |
60
|
|
|
protected $content_fields = array(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* |
65
|
|
|
* @param \phpbb\auth\auth $auth Auth object |
66
|
|
|
* @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object |
67
|
|
|
* @param \phpbb\language\language $language Language object |
68
|
|
|
* @param \phpbb\request\request_interface $request Request object |
69
|
|
|
* @param \phpbb\template\context $template_context Template context object |
70
|
|
|
* @param \phpbb\user $user User object |
71
|
|
|
* @param \blitze\content\services\fields $fields Content fields object |
72
|
|
|
* @param \blitze\content\services\types $types Content types object |
73
|
|
|
* @param \blitze\content\services\form\form $form Form object |
74
|
|
|
* @param string $phpbb_root_path Path to the phpbb includes directory. |
75
|
|
|
* @param string $php_ext php file extension |
76
|
|
|
*/ |
77
|
|
|
public function __construct(\phpbb\auth\auth $auth, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\request\request_interface $request, \phpbb\template\context $template_context, \phpbb\user $user, \blitze\content\services\fields $fields, \blitze\content\services\types $types, \blitze\content\services\form\form $form, $phpbb_root_path, $php_ext) |
78
|
|
|
{ |
79
|
|
|
$this->auth = $auth; |
80
|
|
|
$this->phpbb_dispatcher = $phpbb_dispatcher; |
81
|
|
|
$this->language = $language; |
82
|
|
|
$this->request = $request; |
83
|
|
|
$this->template_context = $template_context; |
84
|
|
|
$this->user = $user; |
85
|
|
|
$this->fields = $fields; |
86
|
|
|
$this->types = $types; |
87
|
|
|
$this->form = $form; |
88
|
|
|
$this->phpbb_root_path = $phpbb_root_path; |
89
|
|
|
$this->php_ext = $php_ext; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $forum_id |
94
|
|
|
* @param int $topic_id |
95
|
|
|
* @param string $mode |
96
|
|
|
* @param bool $save_draft |
97
|
|
|
* @return string|false |
98
|
|
|
*/ |
99
|
|
|
public function init($forum_id, $topic_id, $mode, $save_draft) |
100
|
|
|
{ |
101
|
|
|
if ($type = $this->types->get_forum_type($forum_id)) |
102
|
|
|
{ |
103
|
|
|
$this->language->add_lang('manager', 'blitze/content'); |
104
|
|
|
|
105
|
|
|
$entity = $this->types->get_type($type); |
|
|
|
|
106
|
|
|
$fields_data = $entity->get_content_fields(); |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Event to set the values for fields that are stored in the database, for purposes of displaying the form field |
110
|
|
|
* |
111
|
|
|
* @event blitze.content.builder.set_values |
112
|
|
|
* @var int topic_id Current topic id |
113
|
|
|
* @var array fields_data Array containing fields data, minus 'field_value' prop, which is what we are setting here |
114
|
|
|
* @var \blitze\content\model\entity\type entity Content type entity |
115
|
|
|
*/ |
116
|
|
|
$vars = array('topic_id', 'fields_data', 'entity'); |
117
|
|
|
extract($this->phpbb_dispatcher->trigger_event('blitze.content.builder.set_values', compact($vars))); |
118
|
|
|
|
119
|
|
|
$this->content_fields = $fields_data; |
120
|
|
|
$this->user_is_mod = $this->auth->acl_get('m_', $entity->get_forum_id()); |
121
|
|
|
$this->req_approval = $entity->get_req_approval(); |
122
|
|
|
$this->mode = $this->request->variable('cp', ($this->user_is_mod && $mode !== 'post') ? 'mcp' : 'ucp'); |
|
|
|
|
123
|
|
|
|
124
|
|
|
if ($save_draft && !$this->request->is_set('message')) |
125
|
|
|
{ |
126
|
|
|
$this->request->overwrite('message', $this->generate_message()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $type; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function generate_message() |
137
|
|
|
{ |
138
|
|
|
$fields_data = $this->form->get_submitted_data($this->content_fields, $this->req_mod_input, $this->mode); |
139
|
|
|
|
140
|
|
|
$message = ''; |
141
|
|
|
foreach ($fields_data as $field => $value) |
142
|
|
|
{ |
143
|
|
|
$value = is_array($value) ? join("\n", $value) : $value; |
144
|
|
|
$message .= '[tag=' . $field . ']' . $value . '[/tag]'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $message; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $topic_id |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function save_db_fields($topic_id) |
155
|
|
|
{ |
156
|
|
|
$this->form->save_db_fields($topic_id, $this->content_fields); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function get_errors() |
163
|
|
|
{ |
164
|
|
|
return $this->form->get_errors(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function get_cp_url() |
171
|
|
|
{ |
172
|
|
|
return append_sid("{$this->phpbb_root_path}{$this->mode}.$this->php_ext", "i=-blitze-content-{$this->mode}-content_module&mode=content"); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $content_type |
177
|
|
|
* @param array $topic_data |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function get_post_url($content_type, array $topic_data) |
181
|
|
|
{ |
182
|
|
|
return $this->fields->get_topic_url($content_type, $topic_data); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $content_type |
187
|
|
|
* @param array $post_data |
188
|
|
|
* @param string $view |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function get_content_view($content_type, array $post_data, $view) |
192
|
|
|
{ |
193
|
|
|
$entity = $this->types->get_type($content_type); |
194
|
|
|
$get_tags = 'get_' . $view . '_tags'; |
195
|
|
|
$get_template = 'get_' . $view . '_tpl'; |
196
|
|
|
|
197
|
|
|
$this->fields->prepare_to_show($entity, array($post_data['topic_id']), $entity->$get_tags(), $entity->$get_template(), $view); |
|
|
|
|
198
|
|
|
$content = $this->fields->build_content(array_change_key_case($post_data, CASE_UPPER)); |
199
|
|
|
|
200
|
|
|
return isset($content['SEQ_DISPLAY']) ? $content['SEQ_DISPLAY'] : $content['CUSTOM_DISPLAY']; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $content_type |
205
|
|
|
* @param array $post_data |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function generate_preview($content_type, array $post_data) |
209
|
|
|
{ |
210
|
|
|
$dataref = $this->template_context->get_data_ref(); |
211
|
|
|
$post_data['MESSAGE'] = $dataref['.'][0]['PREVIEW_MESSAGE']; |
212
|
|
|
|
213
|
|
|
return $this->get_content_view($content_type, $post_data, 'detail'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $sql_data |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function modify_posting_data(array &$sql_data) |
221
|
|
|
{ |
222
|
|
|
$slugify = new Slugify(); |
223
|
|
|
$sql_data['topic_slug'] = $slugify->slugify($sql_data['topic_title']); |
224
|
|
|
$sql_data['req_mod_input'] = $this->req_mod_input; |
225
|
|
|
|
226
|
|
|
if ($this->mode === 'mcp') |
227
|
|
|
{ |
228
|
|
|
$topic_time = $this->request->variable('topic_time', 0); |
229
|
|
|
$publish_on = $this->request->variable('publish_on', ''); |
230
|
|
|
|
231
|
|
|
$posted_on = $this->user->format_date($topic_time, 'm/d/Y H:i'); |
232
|
|
|
|
233
|
|
|
if ($publish_on !== $posted_on) |
234
|
|
|
{ |
235
|
|
|
$sql_data['topic_time'] = strtotime($publish_on); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param int $topic_id |
242
|
|
|
* @param array $post_data |
243
|
|
|
* @param array $page_data |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function generate_form($topic_id, array &$post_data, array $page_data = array()) |
247
|
|
|
{ |
248
|
|
|
$this->set_field_values($topic_id, $post_data['post_text']); |
249
|
|
|
|
250
|
|
|
$this->form->create('postform', 'posting') |
251
|
|
|
->add('cp', 'hidden', array('field_value' => $this->mode)); |
252
|
|
|
|
253
|
|
|
foreach ($this->content_fields as $field => $field_data) |
254
|
|
|
{ |
255
|
|
|
if ($field_data['field_type'] === 'textarea') |
256
|
|
|
{ |
257
|
|
|
$field_data += $page_data; |
258
|
|
|
} |
259
|
|
|
$this->add_field($field, $field_data, $topic_id); |
260
|
|
|
} |
261
|
|
|
$this->add_moderator_fields($post_data); |
262
|
|
|
|
263
|
|
|
return $this->form->get_form(false); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $field |
268
|
|
|
* @param array $field_data |
269
|
|
|
* @param int $topic_id |
270
|
|
|
* @return void |
271
|
|
|
*/ |
272
|
|
|
protected function add_field($field, array $field_data, $topic_id) |
273
|
|
|
{ |
274
|
|
|
if (!$field_data['field_mod_only'] || $this->mode === 'mcp') |
275
|
|
|
{ |
276
|
|
|
$this->form->add($field, $field_data['field_type'], $field_data, $topic_id); |
277
|
|
|
} |
278
|
|
|
else if (!empty($field_data['field_value'])) |
279
|
|
|
{ |
280
|
|
|
$this->form->add($field, 'hidden', $field_data, $topic_id); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param array $post_data |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
|
|
protected function add_moderator_fields(array $post_data) |
289
|
|
|
{ |
290
|
|
|
if ($this->mode === 'mcp') |
291
|
|
|
{ |
292
|
|
|
$this->form->add('topic_time', 'hidden', array('field_value' => $post_data['topic_time'])) |
293
|
|
|
->add('publish_on', 'datetime', array( |
294
|
|
|
'field_label' => $this->language->lang('CONTENT_POST_DATE'), |
295
|
|
|
'field_value' => $this->user->format_date($post_data['topic_time'], 'm/d/Y H:i'), |
296
|
|
|
'field_props' => array( |
297
|
|
|
'min_date' => 0, |
298
|
|
|
), |
299
|
|
|
)) |
300
|
|
|
->add('force_status', 'radio', array( |
301
|
|
|
'field_label' => 'FORCE_STATUS', |
302
|
|
|
'field_value' => 'NO', |
303
|
|
|
'field_props' => array( |
304
|
|
|
'vertical' => true, |
305
|
|
|
'options' => array( |
306
|
|
|
'' => 'NO', |
307
|
|
|
ITEM_UNAPPROVED => 'STATUS_DISAPPROVE', |
308
|
|
|
ITEM_APPROVED => 'STATUS_APPROVE', |
309
|
|
|
ITEM_REAPPROVE => 'STATUS_REAPPROVE', |
310
|
|
|
) |
311
|
|
|
) |
312
|
|
|
) |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param array $data |
319
|
|
|
* @return void |
320
|
|
|
*/ |
321
|
|
|
public function force_visibility(array &$data) |
322
|
|
|
{ |
323
|
|
|
if ($this->mode === 'mcp') |
324
|
|
|
{ |
325
|
|
|
if ('' !== $force_status = $this->request->variable('force_status', '')) |
326
|
|
|
{ |
327
|
|
|
$data['force_approved_state'] = $force_status; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
else |
331
|
|
|
{ |
332
|
|
|
if ($this->force_state()) |
333
|
|
|
{ |
334
|
|
|
$data['force_approved_state'] = (empty($data['topic_id'])) ? ITEM_UNAPPROVED : ITEM_REAPPROVE; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return bool |
341
|
|
|
*/ |
342
|
|
|
protected function force_state() |
343
|
|
|
{ |
344
|
|
|
return ($this->req_approval || $this->req_mod_input); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param int $topic_id |
349
|
|
|
* @param string $post_text |
350
|
|
|
* @return void |
351
|
|
|
*/ |
352
|
|
|
protected function set_field_values($topic_id, $post_text) |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$fields_data = $this->get_fields_data_from_post($post_text, array_keys($this->content_fields)); |
355
|
|
|
|
356
|
|
|
foreach ($fields_data as $field => $value) |
357
|
|
|
{ |
358
|
|
|
if (isset($this->content_fields[$field])) |
359
|
|
|
{ |
360
|
|
|
$this->content_fields[$field]['field_value'] = $value; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get fields data from post |
367
|
|
|
* |
368
|
|
|
* @param string $post_text |
369
|
|
|
* @param array $fields |
370
|
|
|
* @return array |
371
|
|
|
*/ |
372
|
|
|
protected function get_fields_data_from_post($post_text, array $fields) |
373
|
|
|
{ |
374
|
|
|
$fields_data = array(); |
375
|
|
|
$find_tags = join('|', $fields); |
376
|
|
|
|
377
|
|
|
if (preg_match_all("/\[tag=($find_tags)\](.*?)\[\/tag]/s", $post_text, $matches)) |
378
|
|
|
{ |
379
|
|
|
$fields_data = array_combine($matches[1], $matches[2]); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $fields_data; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.