Skip to content

Commit 7f0f9f5

Browse files
authored
Merge pull request #127 from VSEphpbb/issue/126
Use core events to post new ideas
2 parents 03c5d47 + 1d251ee commit 7f0f9f5

16 files changed

Lines changed: 618 additions & 540 deletions

File tree

controller/admin_controller.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -166,31 +166,10 @@ public function set_ideas_forum_options()
166166
trigger_error($this->language->lang('ACP_IDEAS_NO_FORUM') . adm_back_link($this->u_action), E_USER_WARNING);
167167
}
168168

169-
if (!class_exists('auth_admin'))
170-
{
171-
include $this->phpbb_root_path . 'includes/acp/auth.' . $this->php_ext;
172-
}
173-
$auth_admin = new \auth_admin();
174-
175169
$forum_id = (int) $this->config['ideas_forum_id'];
176170

177-
// Get the REGISTERED usergroup ID
178-
$sql = 'SELECT group_id
179-
FROM ' . GROUPS_TABLE . "
180-
WHERE group_name = '" . $this->db->sql_escape('REGISTERED') . "'";
181-
$this->db->sql_query($sql);
182-
$group_id = (int) $this->db->sql_fetchfield('group_id');
183-
184-
// Get 'f_' local REGISTERED users group permissions array for the ideas forum
185-
// Default undefined permissions to ACL_NO
186-
$hold_ary = $auth_admin->get_mask('set', false, $group_id, $forum_id, 'f_', 'local', ACL_NO);
187-
$auth_settings = $hold_ary[$group_id][$forum_id];
188-
189-
// Set 'Can start new topics' permissions to 'Never' for the ideas forum
190-
$auth_settings['f_post'] = ACL_NEVER;
191-
192-
// Update the registered usergroup permissions for selected Ideas forum...
193-
$auth_admin->acl_set('group', $forum_id, $group_id, $auth_settings);
171+
$permission_helper = new \phpbb\ideas\factory\permission_helper($this->config, $this->db, $this->phpbb_root_path, $this->php_ext);
172+
$permission_helper->set_ideas_forum_permissions($forum_id);
194173

195174
// Disable auto-pruning for ideas forum
196175
$sql = 'UPDATE ' . FORUMS_TABLE . '

controller/idea_controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public function implemented()
225225
*
226226
* @return bool True if set, false if not
227227
* @access public
228+
*
229+
* @deprecated 2.1.0 (No longer used, may be removed one day)
228230
*/
229231
public function title()
230232
{

controller/post_controller.php

Lines changed: 8 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ class post_controller extends base
1717
{
1818
/**
1919
* Controller for /post
20+
* Redirects to the idea forum's posting page.
2021
*
2122
* @throws http_exception
22-
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
23+
* @return \Symfony\Component\HttpFoundation\RedirectResponse A Symfony Response object
2324
*/
2425
public function post()
2526
{
@@ -33,95 +34,13 @@ public function post()
3334
throw new http_exception(404, 'LOGGED_OUT');
3435
}
3536

36-
$this->language->add_lang('posting');
37+
$params = [
38+
'mode' => 'post',
39+
'f' => $this->config['ideas_forum_id'],
40+
];
3741

38-
if (!function_exists('submit_post'))
39-
{
40-
include $this->root_path . 'includes/functions_posting.' . $this->php_ext;
41-
}
42-
43-
if (!function_exists('display_custom_bbcodes'))
44-
{
45-
include $this->root_path . 'includes/functions_display.' . $this->php_ext;
46-
}
47-
48-
$title = $this->request->variable('title', '', true);
49-
$message = $this->request->variable('message', '', true);
50-
51-
if ($this->request->is_set_post('post'))
52-
{
53-
$submit = $this->ideas->submit($title, $message, $this->user->data['user_id']);
54-
55-
if (is_array($submit))
56-
{
57-
$this->template->assign_vars(array(
58-
'ERROR' => implode('<br />', $submit),
59-
'MESSAGE' => $message,
60-
));
61-
}
62-
else if (!$this->auth->acl_get('f_noapprove', $this->config['ideas_forum_id']))
63-
{
64-
// Show users who's posts need approval a special message
65-
throw new http_exception(200, 'IDEA_STORED_MOD', array($this->helper->route('phpbb_ideas_index_controller')));
66-
}
67-
else
68-
{
69-
return new RedirectResponse($this->helper->route('phpbb_ideas_idea_controller', array('idea_id' => $submit)));
70-
}
71-
}
72-
73-
if ($this->request->is_set_post('preview'))
74-
{
75-
$preview_message = $this->ideas->preview($message);
76-
77-
$this->template->assign_vars(array(
78-
'S_DISPLAY_PREVIEW' => !empty($preview_message),
79-
'PREVIEW_SUBJECT' => $title,
80-
'PREVIEW_MESSAGE' => $preview_message,
81-
));
82-
}
83-
84-
display_custom_bbcodes();
85-
generate_smilies('inline', 0);
86-
87-
// BBCode, Smilies, Images URL, and Flash statuses
88-
$bbcode_status = (bool) $this->config['allow_bbcode'] && $this->auth->acl_get('f_bbcode', $this->config['ideas_forum_id']);
89-
$smilies_status = (bool) $this->config['allow_smilies'] && $this->auth->acl_get('f_smilies', $this->config['ideas_forum_id']);
90-
$img_status = (bool) $bbcode_status && $this->auth->acl_get('f_img', $this->config['ideas_forum_id']);
91-
$url_status = (bool) $this->config['allow_post_links'];
92-
$flash_status = (bool) $bbcode_status && $this->auth->acl_get('f_flash', $this->config['ideas_forum_id']) && $this->config['allow_post_flash'];
93-
94-
$this->template->assign_vars(array(
95-
'TITLE' => $title,
96-
'MESSAGE' => $message,
97-
98-
'S_POST_ACTION' => $this->helper->route('phpbb_ideas_post_controller'),
99-
'S_BBCODE_ALLOWED' => $bbcode_status,
100-
'S_SMILIES_ALLOWED' => $smilies_status,
101-
'S_LINKS_ALLOWED' => $url_status,
102-
'S_BBCODE_IMG' => $img_status,
103-
'S_BBCODE_FLASH' => $flash_status,
104-
'S_BBCODE_QUOTE' => true,
105-
106-
'BBCODE_STATUS' => $this->language->lang(($bbcode_status ? 'BBCODE_IS_ON' : 'BBCODE_IS_OFF'), '<a href="' . $this->helper->route('phpbb_help_bbcode_controller') . '">', '</a>'),
107-
'IMG_STATUS' => $img_status ? $this->language->lang('IMAGES_ARE_ON') : $this->language->lang('IMAGES_ARE_OFF'),
108-
'FLASH_STATUS' => $flash_status ? $this->language->lang('FLASH_IS_ON') : $this->language->lang('FLASH_IS_OFF'),
109-
'URL_STATUS' => ($bbcode_status && $url_status) ? $this->language->lang('URL_IS_ON') : $this->language->lang('URL_IS_OFF'),
110-
'SMILIES_STATUS' => $smilies_status ? $this->language->lang('SMILIES_ARE_ON') : $this->language->lang('SMILIES_ARE_OFF'),
111-
));
112-
113-
// Assign breadcrumb template vars
114-
$this->template->assign_block_vars_array('navlinks', array(
115-
array(
116-
'U_VIEW_FORUM' => $this->helper->route('phpbb_ideas_index_controller'),
117-
'FORUM_NAME' => $this->language->lang('IDEAS'),
118-
),
119-
array(
120-
'U_VIEW_FORUM' => $this->helper->route('phpbb_ideas_post_controller'),
121-
'FORUM_NAME' => $this->language->lang('NEW_IDEA'),
122-
),
123-
));
42+
$url = append_sid(generate_board_url() . "/posting.{$this->php_ext}", $params, false);
12443

125-
return $this->helper->render('idea_new.html', $this->language->lang('NEW_IDEA'));
44+
return new RedirectResponse($url);
12645
}
12746
}

event/listener.php

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public static function getSubscribedEvents()
8686
'core.viewtopic_modify_page_title' => 'show_idea',
8787
'core.viewtopic_add_quickmod_option_before' => 'adjust_quickmod_tools',
8888
'core.viewonline_overwrite_location' => 'viewonline_ideas',
89-
'core.posting_modify_submit_post_after' => 'edit_idea_title',
89+
'core.posting_modify_template_vars' => 'submit_idea_template',
90+
'core.posting_modify_submit_post_before' => 'submit_idea_before',
91+
'core.posting_modify_submit_post_after' => ['submit_idea_after', 'edit_idea_title'],
9092
);
9193
}
9294

@@ -275,26 +277,81 @@ public function adjust_quickmod_tools($event)
275277
*/
276278
public function viewonline_ideas($event)
277279
{
278-
if ($event['on_page'][1] === 'app')
279-
{
280-
if (strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas/post') === 0)
281-
{
282-
$event['location'] = $this->language->lang('POSTING_NEW_IDEA');
283-
$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
284-
}
285-
else if (strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas') === 0)
286-
{
287-
$event['location'] = $this->language->lang('VIEWING_IDEAS');
288-
$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
289-
}
290-
}
291-
else if ($event['on_page'][1] === 'viewtopic' && $event['row']['session_forum_id'] == $this->config['ideas_forum_id'])
280+
if (($event['on_page'][1] === 'viewtopic' && $event['row']['session_forum_id'] == $this->config['ideas_forum_id']) ||
281+
($event['on_page'][1] === 'app' && strrpos($event['row']['session_page'], 'app.' . $this->php_ext . '/ideas') === 0))
292282
{
293283
$event['location'] = $this->language->lang('VIEWING_IDEAS');
294284
$event['location_url'] = $this->helper->route('phpbb_ideas_index_controller');
295285
}
296286
}
297287

288+
/**
289+
* Modify the Ideas forum's posting page
290+
*
291+
* @param \phpbb\event\data $event The event object
292+
*/
293+
public function submit_idea_template($event)
294+
{
295+
if (!$this->is_ideas_forum($event['forum_id']))
296+
{
297+
return;
298+
}
299+
300+
// Alter some posting page template vars
301+
if ($event['mode'] === 'post')
302+
{
303+
$event['page_title'] = $this->language->lang('POST_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+
308+
// Alter posting page breadcrumbs to link to the ideas controller
309+
$this->template->alter_block_array('navlinks', [
310+
'U_BREADCRUMB' => $this->helper->route('phpbb_ideas_index_controller'),
311+
'BREADCRUMB_NAME' => $this->language->lang('IDEAS'),
312+
], false, 'change');
313+
}
314+
315+
/**
316+
* Prepare post data vars before posting a new idea/topic.
317+
*
318+
* @param \phpbb\event\data $event The event object
319+
*/
320+
public function submit_idea_before($event)
321+
{
322+
if (!$this->is_post_idea($event['mode'], $event['data']['forum_id'], empty($event['data']['topic_id'])))
323+
{
324+
return;
325+
}
326+
327+
// We need $post_time after submit_post(), but it's not available in the post $data, unless we set it now
328+
$event->update_subarray('data', 'post_time', time());
329+
}
330+
331+
/**
332+
* Submit the idea data after posting a new idea/topic.
333+
*
334+
* @param \phpbb\event\data $event The event object
335+
*/
336+
public function submit_idea_after($event)
337+
{
338+
if (!$this->is_post_idea($event['mode'], $event['data']['forum_id'], !empty($event['data']['topic_id'])))
339+
{
340+
return;
341+
}
342+
343+
$this->ideas->submit($event['data']);
344+
345+
// Show users who's posts need approval a special message
346+
if (!$this->auth->acl_get('f_noapprove', $event['data']['forum_id']))
347+
{
348+
// Using refresh and trigger error because we can't throw http_exceptions from posting.php
349+
$url = $this->helper->route('phpbb_ideas_index_controller');
350+
meta_refresh(10, $url);
351+
trigger_error($this->language->lang('IDEA_STORED_MOD', $url));
352+
}
353+
}
354+
298355
/**
299356
* Update the idea's title when post title is edited.
300357
*
@@ -316,6 +373,31 @@ public function edit_idea_title($event)
316373
$this->ideas->set_title($idea['idea_id'], $event['post_data']['post_subject']);
317374
}
318375

376+
/**
377+
* Test if we are on the posting page for a new idea
378+
*
379+
* @param string $mode Mode should be post
380+
* @param int $forum_id The forum posting is being made in
381+
* @param bool $topic_flag Flag for the state of the topic_id
382+
*
383+
* @return bool True if mode is post, forum is Ideas forum, and a topic id is
384+
* expected to exist yet, false if any of these tests failed.
385+
*/
386+
protected function is_post_idea($mode, $forum_id, $topic_flag = true)
387+
{
388+
if ($mode !== 'post')
389+
{
390+
return false;
391+
}
392+
393+
if (!$this->is_ideas_forum($forum_id))
394+
{
395+
return false;
396+
}
397+
398+
return $topic_flag;
399+
}
400+
319401
/**
320402
* Check if forum id is for the ideas the forum
321403
*

0 commit comments

Comments
 (0)