[ticket/13713] Abstract class for usernames

PHPBB3-13713
This commit is contained in:
lavigor 2018-05-18 16:15:06 +03:00 committed by Marc Alexander
parent f757e65559
commit b6e2d1f48c
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
5 changed files with 145 additions and 43 deletions

View file

@ -3,6 +3,7 @@ services:
phpbb.mention.controller:
class: phpbb\mention\controller\mention
arguments:
- '@phpbb.mention.source_collection'
- '@request'
- '%core.root_path%'
- '%core.php_ext%'
@ -15,11 +16,17 @@ services:
tags:
- { name: service_collection, tag: mention.source }
phpbb.mention.source.friend:
class: phpbb\mention\source\friend
arguments:
- '@dbal.conn'
- '@user'
tags:
- { name: mention.source }
phpbb.mention.source.topic:
class: phpbb\mention\source\topic
arguments:
- '@dbal.conn'
- '@request'
tags:
- { name: mention.source }

View file

@ -17,6 +17,9 @@ use Symfony\Component\HttpFoundation\JsonResponse;
class mention
{
/** @var \phpbb\di\service_collection */
protected $mention_sources;
/** @var \phpbb\request\request_interface */
protected $request;
@ -30,8 +33,9 @@ class mention
* Constructor
*
*/
public function __construct(\phpbb\request\request_interface $request, $phpbb_root_path, $phpEx)
public function __construct($mention_sources, \phpbb\request\request_interface $request, $phpbb_root_path, $phpEx)
{
$this->mention_sources = $mention_sources;
$this->request = $request;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
@ -44,9 +48,15 @@ class mention
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);
// TODO
$names = [];
return new JsonResponse();
foreach ($this->mention_sources as $source)
{
$names = array_merge($names, $source->get($keyword, $topic_id));
}
return new JsonResponse($names);
}
}

View file

@ -0,0 +1,56 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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 friend extends user
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\user */
protected $user;
/**
* Constructor
*/
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user)
{
$this->db = $db;
$this->user = $user;
parent::__construct($db);
}
protected function query($keyword, $topic_id)
{
$query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id',
'FROM' => [
USERS_TABLE => 'u',
],
'LEFT_JOIN' => [
[
'FROM' => [ZEBRA_TABLE => 'z'],
'ON' => 'u.user_id = z.zebra_id'
]
],
'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . '
AND u.user_id <> ' . ANONYMOUS . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'u.user_lastvisit DESC'
]);
return $query;
}
}

View file

@ -1,36 +1,21 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\method;
class topic
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\request\request_interface */
protected $request;
/**
* Constructor
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request)
{
$this->db = $db;
$this->request = $request;
}
public function get($keyword, $topic_id)
namespace phpbb\mention\source;
class topic extends user
{
protected function query($keyword, $topic_id)
{
$query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.username, u.user_id',
@ -38,16 +23,16 @@ class topic
USERS_TABLE => 'u',
],
'LEFT_JOIN' => [
[
'FROM' => [POSTS_TABLE => 'p'],
'ON' => 'u.user_id = p.poster_id'
]
],
'WHERE' => 'p.topic_id = ' . $topic_id . ' AND u.user_id <> ' . ANONYMOUS . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'p.post_time DESC'
]);
$res = $this->db->sql_query_limit($query, 5);
return $this->db->sql_fetchrowset($res);
return $query;
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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 user
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/**
* Constructor
*/
public function __construct(\phpbb\db\driver\driver_interface $db)
{
$this->db = $db;
}
abstract protected function query($keyword, $topic_id);
public function get($keyword, $topic_id)
{
$keyword = utf8_clean_string($keyword);
$res = $this->db->sql_query_limit($this->query($keyword, $topic_id), 5);
$names = [];
while ($row = $this->db->sql_fetchrow($res))
{
$names['u' . $row['user_id']] = $row['username'];
}
return $names;
}
}