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