Skip to content

Commit

Permalink
Optimize thankslist code, fix MSSQL errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu committed Oct 8, 2023
1 parent 1807fd1 commit 09e87df
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
- '@profilefields.manager'
- '@request'
- '@controller.helper'
- '@user_loader'
- '%tables.posts%'
- '%tables.thanks%'
- '%tables.sessions%'
Expand Down
58 changes: 30 additions & 28 deletions controller/thankslist.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class thankslist
* @param \phpbb\profilefields\manager $profilefields_manager Profile fields manager object
* @param \phpbb\request\request_interface $request Request object
* @param \phpbb\controller\helper $controller_helper Controller helper object
* @param \phpbb\user_loader $user_loader User loader object
* @param string $posts_table POSTS_TABLE
* @param string $sessions_table SESSIONS_TABLE
* @param string $thanks_table THANKS_TABLE
Expand All @@ -99,6 +100,7 @@ public function __construct(
\phpbb\profilefields\manager $profilefields_manager,
\phpbb\request\request_interface $request,
\phpbb\controller\helper $controller_helper,
\phpbb\user_loader $user_loader,
$posts_table, $thanks_table, $sessions_table, $users_table, $phpbb_root_path, $php_ext
)
{
Expand All @@ -113,6 +115,7 @@ public function __construct(
$this->profilefields_manager = $profilefields_manager;
$this->request = $request;
$this->controller_helper = $controller_helper;
$this->user_loader = $user_loader;
$this->sessions_table = $sessions_table;
$this->thanks_table = $thanks_table;
$this->users_table = $users_table;
Expand Down Expand Up @@ -156,12 +159,12 @@ public function main($mode, $author_id, $give)
case 'givens':
$per_page = $this->config['posts_per_page'];
$page_title = $this->language->lang('SEARCH');
$template_html = 'thanks_results.html';
$template_html = '@gfksx_thanksforposts/thanks_results.html';

switch ($give)
{
case 'true':
$u_search = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $author_id, 'give' => 'true', 'tslash' => '']);
$u_search = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $author_id, 'give' => 'true']);

$sql = 'SELECT COUNT(user_id) AS total_match_count
FROM ' . $this->thanks_table . '
Expand All @@ -170,7 +173,7 @@ public function main($mode, $author_id, $give)
break;

case 'false':
$u_search = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $author_id, 'give' => 'false', 'tslash' => '']);
$u_search = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $author_id, 'give' => 'false']);

$sql = 'SELECT COUNT(DISTINCT post_id) as total_match_count
FROM ' . $this->thanks_table . '
Expand Down Expand Up @@ -292,7 +295,7 @@ public function main($mode, $author_id, $give)
'PAGE_NUMBER' => $this->pagination->on_page($total_match_count, $per_page, $start),
'TOTAL_MATCHES' => $total_match_count,
'SEARCH_MATCHES' => $l_search_matches,
'U_THANKS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['tslash' => '']),
'U_THANKS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller'),
]);

break;
Expand Down Expand Up @@ -382,7 +385,7 @@ public function main($mode, $author_id, $give)
$sort_params[] = $param;
}
}
$pagination_url = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', array_merge($params, ['tslash' => '']));
$pagination_url = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', $params);

// Grab relevant data
$sql = 'SELECT DISTINCT poster_id
Expand Down Expand Up @@ -429,7 +432,7 @@ public function main($mode, $author_id, $give)
}

$sql_array = [
'SELECT' => 'u.*',
'SELECT' => 'u.user_id',
'FROM' => [$this->users_table => 'u'],
'ORDER_BY' => $order_by,
];
Expand Down Expand Up @@ -490,13 +493,10 @@ public function main($mode, $author_id, $give)
}
$this->db->sql_freeresult($result_sessions);

$user_list = $id_cache = [];
$user_list = [];
do
{
$row['session_time'] = (!empty($session_times[$row['user_id']])) ? (int) $session_times[$row['user_id']] : 0;
$row['last_visit'] = (!empty($row['session_time'])) ? (int) $row['session_time'] : (int) $row['user_lastvisit'];
$user_list[] = (int) $row['user_id'];
$id_cache[$row['user_id']] = $row;
}
while ($row = $this->db->sql_fetchrow($result));
$this->db->sql_freeresult($result);
Expand Down Expand Up @@ -526,18 +526,15 @@ public function main($mode, $author_id, $give)
}
}

$this->user_loader->load_users($user_list);
for ($i = 0, $end = count($user_list); $i < $end; ++$i)
{
$user_id = $user_list[$i];
$row = $id_cache[$user_id];
$rank_title = $rank_img = $rank_img_src = '';
$sthanks = true;
$row = $this->user_loader->get_user($user_id);
$row['session_time'] = (!empty($session_times[$row['user_id']])) ? (int) $session_times[$row['user_id']] : 0;
$row['last_visit'] = (!empty($row['session_time'])) ? (int) $row['session_time'] : (int) $row['user_lastvisit'];

if (!function_exists('get_user_rank'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
}
get_user_rank($row['user_rank'], (($user_id == ANONYMOUS) ? false : $row['user_posts']), $rank_title, $rank_img, $rank_img_src);
$sthanks = true;

// Custom Profile Fields
$cp_row = [];
Expand All @@ -546,12 +543,17 @@ public function main($mode, $author_id, $give)
$cp_row = $this->profilefields_manager->generate_profile_fields_template_data($profile_fields_cache[$user_id], false);
}

if (!function_exists('phpbb_show_profile'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
}

$memberrow = array_merge(phpbb_show_profile($row, false, false, false), [
'ROW_NUMBER' => $row_number + ($start + 1),
'GIVENS' => (!isset($givens[$user_id])) ? 0 : $givens[$user_id],
'RECEIVED' => (!isset($reseved[$user_id])) ? 0 : $reseved[$user_id],
'U_SEARCH_USER_GIVENS' => ($this->auth->acl_get('u_search')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $user_id, 'give' => 'true', 'tslash' => '']) : '',
'U_SEARCH_USER_RECEIVED'=> ($this->auth->acl_get('u_search')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $user_id, 'give' => 'false', 'tslash' => '']) : ''
'U_SEARCH_USER_GIVENS' => ($this->auth->acl_get('u_search')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $user_id, 'give' => 'true']) : '',
'U_SEARCH_USER_RECEIVED'=> ($this->auth->acl_get('u_search')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $user_id, 'give' => 'false']) : ''
]);

if (isset($cp_row['row']) && count($cp_row['row']))
Expand All @@ -573,13 +575,13 @@ public function main($mode, $author_id, $give)
$this->pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_users, $this->config['topics_per_page'], $start);
$this->template->assign_vars([
'PAGE_NUMBER' => $this->pagination->on_page($total_users, $this->config['topics_per_page'], $start),
'U_SORT_POSTS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'd', 'sd' => (($sort_key == 'd' && $sort_dir == 'a') ? 'd' : 'a'), 'tslash' => '']),
'U_SORT_USERNAME' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'a', 'sd' => (($sort_key == 'a' && $sort_dir == 'a') ? 'd' : 'a'), 'tslash' => '']),
'U_SORT_FROM' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'b', 'sd' => (($sort_key == 'b' && $sort_dir == 'a') ? 'd' : 'a'), 'tslash' => '']),
'U_SORT_JOINED' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'c', 'sd' => (($sort_key == 'c' && $sort_dir == 'a') ? 'd' : 'a'), 'tslash' => '']),
'U_SORT_THANKS_R' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'e', 'sd' => (($sort_key == 'e' && $sort_dir == 'd') ? 'a' : 'd'), 'tslash' => '']),
'U_SORT_THANKS_G' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'f', 'sd' => (($sort_key == 'f' && $sort_dir == 'd') ? 'a' : 'd'), 'tslash' => '']),
'U_SORT_ACTIVE' => ($this->auth->acl_get('u_viewonline')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'l', 'sd' => (($sort_key == 'l' && $sort_dir == 'a') ? 'd' : 'a'), 'tslash' => '']) : '',
'U_SORT_POSTS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'd', 'sd' => (($sort_key == 'd' && $sort_dir == 'a') ? 'd' : 'a')]),
'U_SORT_USERNAME' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'a', 'sd' => (($sort_key == 'a' && $sort_dir == 'a') ? 'd' : 'a')]),
'U_SORT_FROM' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'b', 'sd' => (($sort_key == 'b' && $sort_dir == 'a') ? 'd' : 'a')]),
'U_SORT_JOINED' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'c', 'sd' => (($sort_key == 'c' && $sort_dir == 'a') ? 'd' : 'a')]),
'U_SORT_THANKS_R' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'e', 'sd' => (($sort_key == 'e' && $sort_dir == 'd') ? 'a' : 'd')]),
'U_SORT_THANKS_G' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'f', 'sd' => (($sort_key == 'f' && $sort_dir == 'd') ? 'a' : 'd')]),
'U_SORT_ACTIVE' => ($this->auth->acl_get('u_viewonline')) ? $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['mode' => $mode, 'sk' => 'l', 'sd' => (($sort_key == 'l' && $sort_dir == 'a') ? 'd' : 'a')]) : '',
'S_VIEWONLINE' => $this->auth->acl_get('u_viewonline'),
]);
}
Expand All @@ -589,7 +591,7 @@ public function main($mode, $author_id, $give)
// Output the page
$this->template->assign_vars([
'TOTAL_USERS' => $this->language->lang('LIST_USERS', $total_users),
'U_THANKS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['tslash' => '']),
'U_THANKS' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller'),
'S_THANKS' => $sthanks,
]);

Expand Down
6 changes: 2 additions & 4 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ public function viewtopic_handle_thanks($event)
{
$post_list = $event['post_list'];
$forum_id = (int) $event['forum_id'];
$topic_data = $event['topic_data'];
$this->helper->array_all_thanks($post_list, $forum_id);
$this->helper->topic_data = $topic_data;

if ($this->request->is_set('thanks') && !$this->request->is_set('rthanks'))
{
Expand Down Expand Up @@ -265,8 +263,8 @@ public function load_language_on_setup($event)

public function add_header_quicklinks($event)
{
$u_thankslist = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller', ['tslash' => '']);
$u_toplist = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller', ['tslash' => '']);
$u_thankslist = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller');
$u_toplist = $this->controller_helper->route('gfksx_thanksforposts_toplist_controller');
$this->template->assign_vars([
'U_THANKS_LIST' => $u_thankslist,
'U_REPUT_TOPLIST' => $u_toplist,
Expand Down

0 comments on commit 09e87df

Please sign in to comment.