From fdcdf03bd353148fe9a47c0be22b645d8360d736 Mon Sep 17 00:00:00 2001 From: John Rayes Date: Tue, 24 Dec 2024 02:46:25 -0700 Subject: [PATCH 1/2] Add ability to manually override templates in PageIndex --- Sources/PageIndex.php | 70 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/Sources/PageIndex.php b/Sources/PageIndex.php index 3863f0cd07..8888b4ecec 100644 --- a/Sources/PageIndex.php +++ b/Sources/PageIndex.php @@ -202,9 +202,19 @@ class PageIndex implements \Stringable * "url;start=offset". Default: false. * @param bool $show_prevnext Whether the Previous and Next links should be * shown. Default: true. + * @param array|null $templateOverrides Array of template strings to override defaults. + * Supported keys: extra_before, previous_page, current_page, page, + * expand_pages, next_page, extra_after. */ - public function __construct(string $base_url, int &$start, int $max_value, int $num_per_page, bool $short_format = false, bool $show_prevnext = true) - { + public function __construct( + string $base_url, + int &$start, + int $max_value, + int $num_per_page, + bool $short_format = false, + bool $show_prevnext = true, + ?array $templateOverrides = null + ) { $this->base_url = $base_url; $this->max_value = $max_value; $this->num_per_page = $num_per_page; @@ -212,8 +222,6 @@ public function __construct(string $base_url, int &$start, int $max_value, int $ $this->show_prevnext = $show_prevnext; $this->start = $start = $this->fixStart($start); - $this->extra_before = str_replace('{txt_pages}', Lang::$txt['pages'], $this->extra_before); - if (isset(Theme::$current->settings['page_index'])) { foreach (Theme::$current->settings['page_index'] as $key => $value) { if (property_exists($this, $key)) { @@ -225,6 +233,33 @@ public function __construct(string $base_url, int &$start, int $max_value, int $ if (!isset(Utils::$context['current_page'])) { Utils::$context['current_page'] = $this->start / $this->num_per_page; } + + // Override templates if provided + if ($templateOverrides) { + foreach ($templateOverrides as $key => $value) { + if (property_exists($this, $key)) { + $this->{$key} = $value; + } + } + } + + $this->extra_before = str_replace('{txt_pages}', Lang::$txt['pages'], $this->extra_before); + } + + /** + * Sets template overrides. + * + * @param array $templateOverrides Array of template strings to override defaults. + * Supported keys: extra_before, previous_page, current_page, page, + * expand_pages, next_page, extra_after. + */ + public function setTemplateOverrides(array $templateOverrides): void + { + foreach ($templateOverrides as $key => $value) { + if (property_exists($this, $key)) { + $this->{$key} = $value; + } + } } /** @@ -276,11 +311,32 @@ public function __toString(): string /** * Static wrapper for constructor. * + * @param string $base_url The basic URL to be used for each link. + * @param int &$start The start position, by reference. If this is not a + * multiple of the number of items per page, it is sanitized to be so and + * the value will persist upon the function's return. + * @param int $max_value The total number of items you are paginating for. + * @param int $num_per_page The number of items to be displayed on a given + * page. $start will be forced to be a multiple of this value. + * @param bool $short_format Whether to use "url.offset" instead of + * "url;start=offset". Default: false. + * @param bool $show_prevnext Whether the Previous and Next links should be + * shown. Default: true. + * @param array|null $templateOverrides Array of template strings to override defaults. + * Supported keys: extra_before, previous_page, current_page, page, + * expand_pages, next_page, extra_after. * @return self An instance of this class. */ - public static function load(string $base_url, int &$start, int $max_value, int $num_per_page, bool $short_format = false, bool $show_prevnext = true): self - { - return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext); + public static function load( + string $base_url, + int &$start, + int $max_value, + int $num_per_page, + bool $short_format = false, + bool $show_prevnext = true, + ?array $templateOverrides = null + ): self { + return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext, $templateOverrides); } /****************** From b7fe484083327932d8ae8197b5fb8b4cfec04996 Mon Sep 17 00:00:00 2001 From: John Rayes Date: Thu, 26 Dec 2024 20:15:47 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Jon Stovell --- Sources/PageIndex.php | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/Sources/PageIndex.php b/Sources/PageIndex.php index 8888b4ecec..3f326005e8 100644 --- a/Sources/PageIndex.php +++ b/Sources/PageIndex.php @@ -202,7 +202,7 @@ class PageIndex implements \Stringable * "url;start=offset". Default: false. * @param bool $show_prevnext Whether the Previous and Next links should be * shown. Default: true. - * @param array|null $templateOverrides Array of template strings to override defaults. + * @param array $template_overrides Array of template strings to override defaults. * Supported keys: extra_before, previous_page, current_page, page, * expand_pages, next_page, extra_after. */ @@ -213,7 +213,7 @@ public function __construct( int $num_per_page, bool $short_format = false, bool $show_prevnext = true, - ?array $templateOverrides = null + array $template_overrides = [] ) { $this->base_url = $base_url; $this->max_value = $max_value; @@ -234,14 +234,7 @@ public function __construct( Utils::$context['current_page'] = $this->start / $this->num_per_page; } - // Override templates if provided - if ($templateOverrides) { - foreach ($templateOverrides as $key => $value) { - if (property_exists($this, $key)) { - $this->{$key} = $value; - } - } - } + $this->setTemplateOverrides($template_overrides); $this->extra_before = str_replace('{txt_pages}', Lang::$txt['pages'], $this->extra_before); } @@ -249,13 +242,13 @@ public function __construct( /** * Sets template overrides. * - * @param array $templateOverrides Array of template strings to override defaults. + * @param array $template_overrides Array of template strings to override defaults. * Supported keys: extra_before, previous_page, current_page, page, * expand_pages, next_page, extra_after. */ - public function setTemplateOverrides(array $templateOverrides): void + public function setTemplateOverrides(array $template_overrides = []): void { - foreach ($templateOverrides as $key => $value) { + foreach ($template_overrides as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; } @@ -322,7 +315,7 @@ public function __toString(): string * "url;start=offset". Default: false. * @param bool $show_prevnext Whether the Previous and Next links should be * shown. Default: true. - * @param array|null $templateOverrides Array of template strings to override defaults. + * @param array $template_overrides Array of template strings to override defaults. * Supported keys: extra_before, previous_page, current_page, page, * expand_pages, next_page, extra_after. * @return self An instance of this class. @@ -334,9 +327,9 @@ public static function load( int $num_per_page, bool $short_format = false, bool $show_prevnext = true, - ?array $templateOverrides = null + array $template_overrides = [] ): self { - return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext, $templateOverrides); + return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext, $template_overrides); } /******************