Skip to content

Commit 1d251ee

Browse files
committed
Event listener refactoring
1 parent 5791c78 commit 1d251ee

2 files changed

Lines changed: 21 additions & 30 deletions

File tree

event/listener.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -279,80 +279,75 @@ public function adjust_quickmod_tools($event)
279279
*/
280280
public function viewonline_ideas($event)
281281
{
282-
if (($event['on_page'][1] === 'app' && strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas') === 0) ||
283-
($event['on_page'][1] === 'viewtopic' && $event['row']['session_forum_id'] == $this->config['ideas_forum_id']))
282+
if (($event['on_page'][1] === 'viewtopic' && $event['row']['session_forum_id'] == $this->config['ideas_forum_id']) ||
283+
($event['on_page'][1] === 'app' && strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas') === 0))
284284
{
285285
$event['location'] = $this->language->lang('VIEWING_IDEAS');
286286
$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
287287
}
288288
}
289289

290290
/**
291-
* Modify template vars on the post a new idea page
291+
* Modify the Ideas forum's posting page
292292
*
293293
* @param \phpbb\event\data $event The event object
294294
*/
295295
public function submit_idea_template($event)
296296
{
297-
if (!$this->in_post_idea($event['mode'], $event['forum_id']))
297+
if (!$this->is_ideas_forum($event['forum_id']))
298298
{
299299
return;
300300
}
301301

302-
// Alter posting page template vars
303-
$event['page_title'] = $this->language->lang('NEW_IDEA');
304-
$event->update_subarray('page_data', 'L_POST_A', $this->language->lang('POST_IDEA'));
305-
$event->update_subarray('page_data', 'U_VIEW_FORUM', $this->helper->route('phpbb_ideas_index_controller'));
306-
307-
// Do not show the Save Draft button
308-
$event->update_subarray('page_data', 'S_SAVE_ALLOWED', false);
309-
$event->update_subarray('page_data', 'S_HAS_DRAFTS', false);
302+
// Alter some posting page template vars
303+
if ($event['mode'] === 'post')
304+
{
305+
$event['page_title'] = $this->language->lang('POST_IDEA');
306+
$event->update_subarray('page_data', 'L_POST_A', $this->language->lang('POST_IDEA'));
307+
$event->update_subarray('page_data', 'U_VIEW_FORUM', $this->helper->route('phpbb_ideas_index_controller'));
308+
}
310309

311-
// Alter posting page breadcrumbs
310+
// Alter posting page breadcrumbs to link to the ideas controller
312311
$this->template->alter_block_array('navlinks', [
313312
'U_BREADCRUMB' => $this->helper->route('phpbb_ideas_index_controller'),
314313
'BREADCRUMB_NAME' => $this->language->lang('IDEAS'),
315314
], false, 'change');
316-
317-
$this->template->alter_block_array('navlinks', [
318-
'U_VIEW_FORUM' => $this->helper->route('phpbb_ideas_post_controller'),
319-
'FORUM_NAME' => $this->language->lang('NEW_IDEA'),
320-
], true, 'insert');
321315
}
322316

323317
/**
324-
* Prepare posting parameters before posting a new idea/topic.
318+
* Prepare post data vars before posting a new idea/topic.
325319
*
326320
* @param \phpbb\event\data $event The event object
327321
*/
328322
public function submit_idea_before($event)
329323
{
330-
if (!$this->in_post_idea($event['mode'], $event['data']['forum_id'], empty($event['data']['topic_id'])))
324+
if (!$this->is_post_idea($event['mode'], $event['data']['forum_id'], empty($event['data']['topic_id'])))
331325
{
332326
return;
333327
}
334328

329+
// We need $post_time after submit_post(), but it's not available in the post $data, unless we set it now
335330
$event->update_subarray('data', 'post_time', time());
336331
}
337332

338333
/**
339-
* Submit an idea after submit_post when posting a new idea/topic.
334+
* Submit the idea data after posting a new idea/topic.
340335
*
341336
* @param \phpbb\event\data $event The event object
342337
*/
343338
public function submit_idea_after($event)
344339
{
345-
if (!$this->in_post_idea($event['mode'], $event['data']['forum_id'], !empty($event['data']['topic_id'])))
340+
if (!$this->is_post_idea($event['mode'], $event['data']['forum_id'], !empty($event['data']['topic_id'])))
346341
{
347342
return;
348343
}
349344

350345
$this->ideas->submit($event['data']);
351346

352347
// Show users who's posts need approval a special message
353-
if (!$this->auth->acl_get('f_noapprove', $this->config['ideas_forum_id']))
348+
if (!$this->auth->acl_get('f_noapprove', $event['data']['forum_id']))
354349
{
355-
// Use refresh and trigger error because we can't throw http_exceptions from posting.php
350+
// Using refresh and trigger error because we can't throw http_exceptions from posting.php
356351
$url = $this->helper->route('phpbb_ideas_index_controller');
357352
meta_refresh(10, $url);
358353
trigger_error($this->language->lang('IDEA_STORED_MOD', $url));
@@ -390,7 +385,7 @@ public function edit_idea_title($event)
390385
* @return bool True if mode is post, forum is Ideas forum, and a topic id is
391386
* expected to exist yet, false if any of these tests failed.
392387
*/
393-
protected function in_post_idea($mode, $forum_id, $topic_flag = true)
388+
protected function is_post_idea($mode, $forum_id, $topic_flag = true)
394389
{
395390
if ($mode !== 'post')
396391
{

tests/event/listener_test.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,19 +426,15 @@ public function test_submit_idea($mode, $forum_id, $topic_id, $approved, $succes
426426

427427
if ($mode === 'post' && $forum_id === 2)
428428
{
429-
$this->assertStringContainsString('NEW_IDEA', $event['page_title']);
429+
$this->assertStringContainsString('POST_IDEA', $event['page_title']);
430430
$this->assertSame('phpbb_ideas_index_controller', $event['page_data']['U_VIEW_FORUM']);
431431
$this->assertSame('POST_IDEA', $event['page_data']['L_POST_A']);
432-
$this->assertFalse($event['page_data']['S_SAVE_ALLOWED']);
433-
$this->assertFalse($event['page_data']['S_HAS_DRAFTS']);
434432
}
435433
else
436434
{
437435
$this->assertStringContainsString('NEW_POST', $event['page_title']);
438436
$this->assertEmpty($event['page_data']['U_VIEW_FORUM']);
439437
$this->assertEmpty($event['page_data']['L_POST_A']);
440-
$this->assertEmpty($event['page_data']['S_SAVE_ALLOWED']);
441-
$this->assertEmpty($event['page_data']['S_HAS_DRAFTS']);
442438
}
443439

444440
// Test submit_idea_before()

0 commit comments

Comments
 (0)