Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu committed Apr 12, 2024
1 parent 755708b commit 392f370
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 59 deletions.
86 changes: 35 additions & 51 deletions core/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class helper
protected $forum_thanks = [];

/** @var int max_post_thanks */
protected $max_post_thanks = 0;
protected $max_post_thanks = 1;

/** @var int max_topic_thanks */
protected $max_topic_thanks = 0;
Expand Down Expand Up @@ -149,54 +149,40 @@ public function __construct(
public function get_thanks($post_id)
{
$view = $this->request->variable('view', '');
$further_thanks_text = $return = '';
$user_list = array();
$further_thanks = $count = 0;
$user_list = [];
$maxcount = (int) $this->config['thanks_number_post'];

foreach ($this->thankers as $thanker)
$posts_thanks = array_keys(array_column($this->thankers, 'post_id', 'id'), $post_id);
if (!$posts_thanks)
{
if ($thanker['post_id'] == $post_id)
{
if ($count >= $maxcount)
{
$further_thanks++;
}
else
{
$thanks_time_info = (($this->config['thanks_time_view'] && $thanker['thanks_time']) ? $this->user->format_date($thanker['thanks_time'], false, ($view == 'print') ? true : false) : '');
$usertname_string_tpl = ($thanks_time_info) ? '<span title="' . $thanks_time_info . '">' . 'USERNAME_STRING' . '</span>' : 'USERNAME_STRING';
$user_list[] = str_replace('USERNAME_STRING', get_username_string('full', $thanker['user_id'], $thanker['username'], $thanker['user_colour']), $usertname_string_tpl);
$count++;
}
}
return false;
}
$further_thanks = max(0, (count($posts_thanks) - $maxcount));

if (!empty($user_list))
foreach ($posts_thanks as $key => $id)
{
$return = implode(', ', $user_list);
}

if ($further_thanks > 0)
{
$further_thanks_text = $this->language->lang('FURTHER_THANKS', $further_thanks);
if ($key == $maxcount)
{
break;
}
else
{
$thanker = $this->thankers[$id];
$user_list[$key]['THANKS_TIME_INFO'] = (($this->config['thanks_time_view'] && $thanker['thanks_time']) ? $this->user->format_date($thanker['thanks_time'], false, ($view == 'print') ? true : false) : '');
$user_list[$key]['USERNAME_STRING'] = get_username_string('full', $thanker['user_id'], $thanker['username'], $thanker['user_colour']);
}
}
$return = ($return == '') ? false : ($return . $further_thanks_text);

return $return;
return [
'DATA' => $user_list,
'FURTHER_THANKS'=> $further_thanks,
];
}

public function get_thanks_number($post_id)
{
$i = 0;
foreach ($this->thankers as $thanker)
{
if ($thanker['post_id'] == $post_id)
{
$i++;
}
}
return $i;
$posts_thanks_number = count(array_keys(array_column($this->thankers, 'post_id', 'id'), $post_id));
return $posts_thanks_number;
}

public function insert_thanks($post_id, $user_id, $forum_id, $post_list)
Expand Down Expand Up @@ -350,7 +336,7 @@ public function delete_thanks($post_id, $forum_id, $post_list)

if ($this->db->sql_affectedrows())
{
$thanks_data = array(
$thanks_data = [
'user_id' => (int) $this->user->data['user_id'],
'post_id' => $post_id,
'poster_id' => $to_id,
Expand All @@ -360,7 +346,7 @@ public function delete_thanks($post_id, $forum_id, $post_list)
'username' => $this->user->data['username'],
'lang_act' => 'REMOVE',
'post_subject' => $row['post_subject'],
);
];
$this->add_notification($thanks_data, 'gfksx.thanksforposts.notification.type.thanks_remove');

$this->handle_ajax_request('delete', $row, $to_id, $from_id, $post_list);
Expand Down Expand Up @@ -560,6 +546,7 @@ public function output_thanks($poster_id, &$postrow, $row, $topic_data, $forum_i
{
if (!empty($postrow))
{
$thanks_number = $this->get_thanks_number($row['post_id']);
$thanks_text = $this->get_thanks_text($row['post_id']);
$thank_mode = $this->get_thanks_link($row['post_id']);
$already_thanked = $this->already_thanked($row['post_id'], $this->user->data['user_id']);
Expand All @@ -571,12 +558,11 @@ public function output_thanks($poster_id, &$postrow, $row, $topic_data, $forum_i
$u_give_count_url = $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $poster_id, 'give' => 'true']);

$postrow = array_merge($postrow, $thanks_text, [
'COND' => $already_thanked,
'THANKS' => $this->get_thanks($row['post_id']),
'THANK_MODE' => $thank_mode,
'THANKS_LINK' => append_sid("{$this->phpbb_root_path}viewtopic.$this->php_ext", 'f=' . $forum_id . '&amp;p=' . $row['post_id'] . '&amp;' . $thank_mode . '=' . $row['post_id'] . '&amp;to_id=' . $poster_id . '&amp;from_id=' . $this->user->data['user_id']),
'THANK_TEXT' => $this->language->lang('THANK_TEXT_1'),
'THANK_TEXT_2' => ($this->get_thanks_number($row['post_id']) != 1) ? $this->language->lang('THANK_TEXT_2PL', $this->get_thanks_number((int) $row['post_id'])) : $this->language->lang('THANK_TEXT_2'),
'THANK_TEXT_2' => ($thanks_number != 1) ? $this->language->lang('THANK_TEXT_2PL', $thanks_number) : $this->language->lang('THANK_TEXT_2'),
'THANKS_FROM' => $this->language->lang('THANK_FROM'),
'POSTER_RECEIVE_COUNT' => $l_poster_receive_count,
'POSTER_GIVE_COUNT' => $l_poster_give_count,
Expand All @@ -591,8 +577,8 @@ public function output_thanks($poster_id, &$postrow, $row, $topic_data, $forum_i
'S_ALREADY_THANKED' => $already_thanked,
'S_REMOVE_THANKS' => (bool) $this->config['remove_thanks'],
'S_FIRST_POST_ONLY' => (bool) $this->config['thanks_only_first_post'],
'POST_REPUT' => ($this->get_thanks_number($row['post_id']) != 0) ? round($this->get_thanks_number($row['post_id']) / ($this->max_post_thanks / 100), (int) $this->config['thanks_number_digits']) . '%' : '',
'POST_THANKS_COUNT' => $this->get_thanks_number($row['post_id']) ?: '',
'POST_REPUT' => $thanks_number ? round($thanks_number / ($this->max_post_thanks / 100), (int) $this->config['thanks_number_digits']) : 0,
'POST_THANKS_COUNT' => $thanks_number,
'S_THANKS_POST_REPUT_VIEW' => (bool) $this->config['thanks_post_reput_view'],
'S_THANKS_REPUT_GRAPHIC' => (bool) $this->config['thanks_reput_graphic'],
'THANKS_REPUT_HEIGHT' => $this->config['thanks_reput_height'] ?: false,
Expand Down Expand Up @@ -637,6 +623,7 @@ public function array_all_thanks($post_list, $forum_id)
while ($row = $this->db->sql_fetchrow($result))
{
$this->thankers[] = [
'id' => count($this->thankers),
'user_id' => $row['user_id'],
'poster_id' => $row['poster_id'],
'post_id' => $row['post_id'],
Expand Down Expand Up @@ -694,7 +681,7 @@ public function array_all_thanks($post_list, $forum_id)
public function get_thanks_topic_reput($topic_id, $max_topic_thanks, $topic_thanks)
{
return [
'TOPIC_REPUT' => (isset($topic_thanks[$topic_id])) ? round((int) $topic_thanks[$topic_id] / ($max_topic_thanks / 100), (int) $this->config['thanks_number_digits']) . '%' : '',
'TOPIC_REPUT' => (isset($topic_thanks[$topic_id])) ? round((int) $topic_thanks[$topic_id] / ($max_topic_thanks / 100), (int) $this->config['thanks_number_digits']) : '',
'S_THANKS_TOPIC_REPUT_VIEW' => (bool) $this->config['thanks_topic_reput_view'],
'S_THANKS_REPUT_GRAPHIC' => (bool) $this->config['thanks_reput_graphic'],
'THANKS_REPUT_HEIGHT' => $this->config['thanks_reput_height'] ?: false,
Expand Down Expand Up @@ -749,11 +736,8 @@ public function get_max_post_thanks()
$this->max_post_thanks = (int) $this->db->sql_fetchfield('max_post_thanks');
$this->db->sql_freeresult($result);
}
else
{
$this->max_post_thanks = 1;
}
return $this->max_post_thanks;

return $this->max_post_thanks ?: 1;
}

// Generate thankslist if required
Expand Down Expand Up @@ -916,7 +900,7 @@ public function handle_ajax_request($mode, $row, $to_id, $from_id, $post_list)
$poster_id = (int) $row['poster_id'];
$this->array_all_thanks($post_list, $forum_id);
$post_thanks_number = $this->get_thanks_number($post_id);
$post_reput = $post_thanks_number != 0 ? round($post_thanks_number / ($this->max_post_thanks / 100), (int) $this->config['thanks_number_digits']) . '%' : '';
$post_reput = $post_thanks_number ? round($post_thanks_number / ($this->max_post_thanks / 100), (int) $this->config['thanks_number_digits']) : 0;
$post_anonymous = $poster_id == ANONYMOUS;
$received_count = empty($this->poster_list_count[$to_id]['R']) ? 0 : (int) $this->poster_list_count[$to_id]['R'];
$given_count = empty($this->poster_list_count[$from_id]['G']) ? 0 : (int) $this->poster_list_count[$from_id]['G'];
Expand All @@ -932,7 +916,7 @@ public function handle_ajax_request($mode, $row, $to_id, $from_id, $post_list)
'POSTER_ID' => $poster_id,
'POSTER_GIVE_COUNT_LINK' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $poster_id, 'give' => 'true']),
'POSTER_RECEIVE_COUNT_LINK' => $this->controller_helper->route('gfksx_thanksforposts_thankslist_controller_user', ['mode' => 'givens', 'author_id' => $poster_id, 'give' => 'false']),
'POST_REPUT' => ($post_thanks_number != 0) ? round($post_thanks_number / ($this->max_post_thanks / 100), (int) $this->config['thanks_number_digits']) . '%' : '',
'POST_REPUT' => $post_reput,
'THANKS' => $this->get_thanks($post_id),
'THANKS_POSTLIST_VIEW' => $this->config['thanks_postlist_view'],
'THANK_TEXT' => $this->language->lang('THANK_TEXT_1'),
Expand Down
4 changes: 2 additions & 2 deletions styles/prosilver/template/ajax_post_reput_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{% if S_THANKS_POST_REPUT_VIEW and POST_REPUT and not S_POST_ANONYMOUS and not S_IS_BOT %}
<div class="notice">
<dl>
<dt class="small"><strong>{{ lang('REPUT') }}{{ lang('COLON') }}</strong>&nbsp;{{ POST_REPUT }}</dt>
<dt class="small"><strong>{{ lang('REPUT') }}{{ lang('COLON') }}</strong>&nbsp;{{ POST_REPUT ~ '%' }}</dt>
<dd>
{% if S_THANKS_REPUT_GRAPHIC %}
<span style="display: block; float: left; width: {{ THANKS_REPUT_GRAPHIC_WIDTH }}px; height: {{ THANKS_REPUT_HEIGHT }}px; background: url({{ THANKS_REPUT_IMAGE_BACK }}); background-repeat: repeat-x;">
<span style="display: block; height: {{ THANKS_REPUT_HEIGHT }}px; width: {{ POST_REPUT }}; background: url({{ THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span>
<span style="display: block; height: {{ THANKS_REPUT_HEIGHT }}px; width: {{ POST_REPUT ~ '%' }}; background: url({{ THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span>
</span>&nbsp;
{% endif %}
</dd>
Expand Down
7 changes: 6 additions & 1 deletion styles/prosilver/template/ajax_post_thanks_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
{% endif %}

<dt>{{ THANK_TEXT }}{{ POST_AUTHOR_FULL }}{{ THANK_TEXT_2 }}</dt>
<dd>{{ THANKS }}</dd>
<dd>
{% for userdata in THANKS.DATA %}
<span title="{{ userdata.THANKS_TIME_INFO }}">{{ userdata.USERNAME_STRING}}</span>{% if not loop.last %}{{ lang('COMMA_SEPARATOR') }}{% endif %}
{% endfor %}
{% if THANKS.FURTHER_THANKS %}{{ lang('FURTHER_THANKS', THANKS.FURTHER_THANKS)}}{% endif %}
</dd>
</dl>
</div>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
{% endif %}

<dt>{{ postrow.THANK_TEXT }}{{ postrow.POST_AUTHOR_FULL }}{{ postrow.THANK_TEXT_2 }}</dt>
<dd>{{ postrow.THANKS }}</dd>
<dd>
{% for userdata in postrow.THANKS.DATA %}
<span title="{{ userdata.THANKS_TIME_INFO }}">{{ userdata.USERNAME_STRING}}</span>{% if not loop.last %}{{ lang('COMMA_SEPARATOR') }}{% endif %}
{% endfor %}
{% if postrow.THANKS.FURTHER_THANKS %}{{ lang('FURTHER_THANKS', postrow.THANKS.FURTHER_THANKS)}}{% endif %}
</dd>
</dl>
</div>
{% endif %}
Expand All @@ -22,11 +27,11 @@
{% if postrow.S_THANKS_POST_REPUT_VIEW and postrow.POST_REPUT and not postrow.S_POST_ANONYMOUS and not S_IS_BOT %}
<div class="notice">
<dl>
<dt class="small"><strong>{{ lang('REPUT') }}{{ lang('COLON') }}</strong>&nbsp;{{ postrow.POST_REPUT }}</dt>
<dt class="small"><strong>{{ lang('REPUT') }}{{ lang('COLON') }}</strong>&nbsp;{{ postrow.POST_REPUT ~ '%' }}</dt>
<dd>
{% if postrow.S_THANKS_REPUT_GRAPHIC %}
<span style="display: block; float: left; width: {{ postrow.THANKS_REPUT_GRAPHIC_WIDTH }}px; height: {{ postrow.THANKS_REPUT_HEIGHT }}px; background: url({{ postrow.THANKS_REPUT_IMAGE_BACK }}); background-repeat: repeat-x;">
<span style="display: block; height: {{ postrow.THANKS_REPUT_HEIGHT }}px; width: {{ postrow.POST_REPUT }}; background: url({{ postrow.THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span>
<span style="display: block; height: {{ postrow.THANKS_REPUT_HEIGHT }}px; width: {{ postrow.POST_REPUT ~ '%' }}; background: url({{ postrow.THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span>
</span>&nbsp;
{% endif %}
</dd>
Expand Down
4 changes: 2 additions & 2 deletions styles/prosilver/template/topicrow_rating_body.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% if rating.S_THANKS_TOPIC_REPUT_VIEW and rating.TOPIC_REPUT and not S_IS_BOT %}
<br><span style="display: block; clear: left;">
{% if rating.S_THANKS_REPUT_GRAPHIC %}
<span style="display: block; float: left; width: {{ rating.THANKS_REPUT_GRAPHIC_WIDTH }}px; height: {{ rating.THANKS_REPUT_HEIGHT }}px; background: url({{ rating.THANKS_REPUT_IMAGE_BACK }}); background-repeat: repeat-x;"><span style="display: block; height: {{ rating.THANKS_REPUT_HEIGHT }}px; width: {{ rating.TOPIC_REPUT }}; background: url({{ rating.THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span></span>&nbsp;
<span style="display: block; float: left; width: {{ rating.THANKS_REPUT_GRAPHIC_WIDTH }}px; height: {{ rating.THANKS_REPUT_HEIGHT }}px; background: url({{ rating.THANKS_REPUT_IMAGE_BACK }}); background-repeat: repeat-x;"><span style="display: block; height: {{ rating.THANKS_REPUT_HEIGHT }}px; width: {{ rating.TOPIC_REPUT ~ '%' }}; background: url({{ rating.THANKS_REPUT_IMAGE }}); background-repeat: repeat-x;"></span></span>&nbsp;
{% endif %}
{{ lang('REPUT') }}{{ lang('COLON') }}&nbsp;{{ rating.TOPIC_REPUT }}
{{ lang('REPUT') }}{{ lang('COLON') }}&nbsp;{{ rating.TOPIC_REPUT ~ '%' }}
</span>
{% endif %}

0 comments on commit 392f370

Please sign in to comment.