Skip to content

Commit

Permalink
Fixed compatibility with First Post On Every Page extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kasimi committed Aug 3, 2017
1 parent 1212169 commit 31839a1
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions kasimi/postnumbers/event/listener.php
Expand Up @@ -14,6 +14,10 @@

class listener implements EventSubscriberInterface
{
const PAGE_VIEWTOPIC = 'viewtopic';
const PAGE_REVIEW_REPLY = 'review_reply';
const PAGE_REVIEW_MCP = 'review_mcp';

/* @var \phpbb\user */
protected $user;

Expand All @@ -35,6 +39,9 @@ class listener implements EventSubscriberInterface
/** @var boolean */
protected $is_active = false;

/** @var string */
protected $page;

/** @var string */
protected $location;

Expand Down Expand Up @@ -89,11 +96,13 @@ static public function getSubscribedEvents()
/**
* Prepare template data
*
* @param bool $is_active
* @param string $mode
*/
protected function init($is_active)
protected function init($mode)
{
if (!($this->is_active = $is_active))
$this->is_active = $this->cfg('enabled.' . $mode) && !$this->user->data['is_bot'];

if (!$this->is_active)
{
return;
}
Expand Down Expand Up @@ -134,7 +143,7 @@ protected function init($is_active)
*/
public function init_viewtopic($event)
{
$this->init($this->cfg('enabled.viewtopic') && !$this->user->data['is_bot']);
$this->init(self::PAGE_VIEWTOPIC);
}

/**
Expand All @@ -154,7 +163,7 @@ public function postnum_in_viewtopic($event)
$this->firstPostOnEveryPage = null;
}

if ($post_num = $this->get_post_number($this->user->data['user_post_sortby_type'], $this->user->data['user_post_sortby_dir'], $this->user->data['user_post_show_days'], $event['row'], $event['topic_data']['topic_posts_approved'], $event['total_posts'], $event['start'], false))
if ($post_num = $this->get_post_number($this->user->data['user_post_sortby_type'], $this->user->data['user_post_sortby_dir'], $this->user->data['user_post_show_days'], $event['row'], $event['topic_data']['topic_posts_approved'], $event['total_posts'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
Expand All @@ -168,7 +177,7 @@ public function postnum_in_viewtopic($event)
*/
public function init_topic_review($event)
{
$this->init($this->cfg('enabled.review_reply'));
$this->init(self::PAGE_REVIEW_REPLY);
}

/**
Expand All @@ -180,7 +189,7 @@ public function postnum_in_topic_review($event)
{
if ($this->is_active && $event['mode'] == 'topic_review')
{
if ($post_num = $this->get_post_number('t', 'd', 0, $event['row'], $event['total_posts'], $event['total_posts'], $event['start'], true))
if ($post_num = $this->get_post_number('t', 'd', 0, $event['row'], $event['total_posts'], $event['total_posts'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
Expand All @@ -194,7 +203,10 @@ public function postnum_in_topic_review($event)
*/
public function init_mcp_review($event)
{
$this->init($this->cfg('enabled.review_mcp') && $event['mode'] == 'topic_view');
if ($event['mode'] == 'topic_view')
{
$this->init(self::PAGE_REVIEW_MCP);
}
}

/**
Expand All @@ -206,7 +218,7 @@ public function postnum_in_mcp_review($event)
{
if ($this->is_active)
{
if ($post_num = $this->get_post_number('t', 'a', 0, $event['row'], $event['topic_info']['topic_posts_approved'], $event['total'], $event['start'], false))
if ($post_num = $this->get_post_number('t', 'a', 0, $event['row'], $event['topic_info']['topic_posts_approved'], $event['total'], $event['start']))
{
$event['post_row'] = $this->inject_post_num($event['post_row'], $post_num);
}
Expand All @@ -223,10 +235,9 @@ public function postnum_in_mcp_review($event)
* @param int $approved_posts
* @param int $total_posts
* @param int $start
* @param bool $is_reply_review
* @return bool|int
*/
protected function get_post_number($default_sort_by, $default_sort_dir, $default_days, $row, $approved_posts, $total_posts, $start, $is_reply_review)
protected function get_post_number($default_sort_by, $default_sort_dir, $default_days, $row, $approved_posts, $total_posts, $start)
{
// If we display IDs, skip all checks and calculations and return immediately
if ($this->cfg('display_ids'))
Expand Down Expand Up @@ -254,11 +265,15 @@ protected function get_post_number($default_sort_by, $default_sort_dir, $default
{
$this->firstPostOnEveryPage = null;

// If posts are sorted ascending, we display #1 on all except the first page.
// If posts are sorted descending, we always display #1.
if (!$is_ascending || $start != 0)
// First Post On Every Page extension is only active on viewtopic
if ($this->page === self::PAGE_VIEWTOPIC)
{
return 1;
// If posts are sorted ascending, we display #1 on all except the first page.
// If posts are sorted descending, we always display #1.
if (!$is_ascending || $start != 0)
{
return 1;
}
}
}

Expand All @@ -268,7 +283,7 @@ protected function get_post_number($default_sort_by, $default_sort_dir, $default
$need_new_start = false;

// We only need to query the number of previous/non-approved posts in certain situations
if ($is_reply_review || $this->request->variable('st', $default_days) != 0)
if ($this->page === self::PAGE_REVIEW_REPLY || $this->request->variable('st', $default_days) != 0)
{
$need_new_start = true;
}
Expand Down

0 comments on commit 31839a1

Please sign in to comment.