diff --git a/phpbb/config/default/container/services_mention.yml b/phpbb/config/default/container/services_mention.yml index e1e8fa9a35..bae6b45bfe 100644 --- a/phpbb/config/default/container/services_mention.yml +++ b/phpbb/config/default/container/services_mention.yml @@ -30,3 +30,12 @@ services: - '@dbal.conn' tags: - { name: mention.source } + + phpbb.mention.source.usergroup: + class: phpbb\mention\source\usergroup + arguments: + - '@dbal.conn' + - '@group_helper' + - '@user' + tags: + - { name: mention.source } diff --git a/phpbb/phpbb/mention/controller/mention.php b/phpbb/phpbb/mention/controller/mention.php index 14ec892269..8d731ae210 100644 --- a/phpbb/phpbb/mention/controller/mention.php +++ b/phpbb/phpbb/mention/controller/mention.php @@ -43,10 +43,10 @@ class mention public function handle() { - if (!$this->request->is_ajax()) - { - redirect(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); - } +// if (!$this->request->is_ajax()) +// { +// redirect(append_sid($this->phpbb_root_path . 'index.' . $this->php_ext)); +// } $keyword = $this->request->variable('keyword', '', true); $topic_id = $this->request->variable('topic_id', 0); diff --git a/phpbb/phpbb/mention/source/friend.php b/phpbb/phpbb/mention/source/friend.php index 8f78159b6c..bb3ba9ecb7 100644 --- a/phpbb/phpbb/mention/source/friend.php +++ b/phpbb/phpbb/mention/source/friend.php @@ -15,9 +15,6 @@ namespace phpbb\mention\source; class friend extends user { - /** @var \phpbb\db\driver\driver_interface */ - protected $db; - /** @var \phpbb\user */ protected $user; @@ -26,7 +23,6 @@ class friend extends user */ public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user) { - $this->db = $db; $this->user = $user; parent::__construct($db); diff --git a/phpbb/phpbb/mention/source/group.php b/phpbb/phpbb/mention/source/group.php new file mode 100644 index 0000000000..b503ac714c --- /dev/null +++ b/phpbb/phpbb/mention/source/group.php @@ -0,0 +1,103 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\mention\source; + +abstract class group implements source_interface +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\group\helper */ + protected $helper; + + /** + * Constructor + */ + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\group\helper $helper) + { + $this->db = $db; + $this->helper = $helper; + } + + /** + * Returns data for all board groups + * + * @return array Array of groups' data + */ + protected function get_groups() + { + static $groups = null; + + if (is_null($groups)) + { + $query = $this->db->sql_build_query('SELECT', [ + 'SELECT' => 'g.*', + 'FROM' => [ + GROUPS_TABLE => 'g', + ], + ]); + $res = $this->db->sql_query($query); + + $groups = []; + while ($row = $this->db->sql_fetchrow($res)) + { + $group_name = $this->helper->get_name($row['group_name']); + $groups['names'][$row['group_id']] = $group_name; + $groups[$row['group_id']] = $row; + $groups[$row['group_id']]['group_name'] = $group_name; + } + } + return $groups; + } + + /** + * Builds a query for getting group IDs based on user input + * + * @param string $keyword Search string + * @param int $topic_id Current topic ID + * @return string Query ready for execution + */ + abstract protected function query($keyword, $topic_id); + + /** + * {@inheritdoc} + */ + public function get($keyword, $topic_id) + { + // Grab all group IDs + $res = $this->db->sql_query($this->query($keyword, $topic_id)); + + $group_ids = []; + while ($row = $this->db->sql_fetchrow($res)) + { + $group_ids[] = $row['group_id']; + } + + // Grab group data + $groups = $this->get_groups(); + + $matches = preg_grep('/^' . $keyword . '.*/i', $groups['names']); + $group_ids = array_intersect($group_ids, array_flip($matches)); + + $names = []; + foreach ($group_ids as $group_id) + { + $names['g' . $group_id] = [ + 'name' => $groups[$group_id]['group_name'], + ]; + } + + return $names; + } +} diff --git a/phpbb/phpbb/mention/source/user.php b/phpbb/phpbb/mention/source/user.php index be0d39f3af..55f94e4866 100644 --- a/phpbb/phpbb/mention/source/user.php +++ b/phpbb/phpbb/mention/source/user.php @@ -46,7 +46,9 @@ abstract class user implements source_interface $names = []; while ($row = $this->db->sql_fetchrow($res)) { - $names['u' . $row['user_id']] = $row['username']; + $names['u' . $row['user_id']] = [ + 'name' => $row['username'], + ]; } return $names; diff --git a/phpbb/phpbb/mention/source/usergroup.php b/phpbb/phpbb/mention/source/usergroup.php new file mode 100644 index 0000000000..1a0e20eff8 --- /dev/null +++ b/phpbb/phpbb/mention/source/usergroup.php @@ -0,0 +1,51 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\mention\source; + +class usergroup extends group +{ + /** @var \phpbb\user */ + protected $user; + + /** + * Constructor + */ + public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\group\helper $helper, \phpbb\user $user) + { + $this->user = $user; + + parent::__construct($db, $helper); + } + + /** + * {@inheritdoc} + */ + protected function query($keyword, $topic_id) + { + $query = $this->db->sql_build_query('SELECT', [ + 'SELECT' => 'g.group_id', + 'FROM' => [ + GROUPS_TABLE => 'g', + ], + 'LEFT_JOIN' => [ + [ + 'FROM' => [USER_GROUP_TABLE => 'ug'], + 'ON' => 'g.group_id = ug.group_id' + ] + ], + 'WHERE' => 'ug.user_id = ' . (int) $this->user->data['user_id'], + ]); + return $query; + } +}