diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index cca655263f..d32b665039 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -288,6 +288,7 @@ services: - @template - @user - @controller.helper + - @dispatcher path_helper: class: phpbb\path_helper diff --git a/phpBB/phpbb/pagination.php b/phpBB/phpbb/pagination.php index 8aba41d651..7a81c25ad2 100644 --- a/phpBB/phpbb/pagination.php +++ b/phpBB/phpbb/pagination.php @@ -21,18 +21,26 @@ class pagination /** @var \phpbb\user */ protected $user; + /** @var \phpbb\controller\helper */ + protected $helper; + + /** @var \phpbb\event\dispatcher_interface */ + protected $phpbb_dispatcher; + /** * Constructor * - * @param \phpbb\template\template $template - * @param \phpbb\user $user - * @param \phpbb\controller\helper $helper + * @param \phpbb\template\template $template + * @param \phpbb\user $user + * @param \phpbb\controller\helper $helper + * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher */ - public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper) + public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\event\dispatcher_interface $phpbb_dispatcher) { $this->template = $template; $this->user = $user; $this->helper = $helper; + $this->phpbb_dispatcher = $phpbb_dispatcher; } /** @@ -50,6 +58,36 @@ class pagination */ protected function generate_page_link($base_url, $on_page, $start_name, $per_page) { + // A listener can set this variable to the new pagination URL + // to override the generate_page_link() function generated value + $generate_page_link_override = false; + + /** + * Execute code and/or override generate_page_link() + * + * To override the generate_page_link() function generated value + * set $generate_page_link_override to the new URL value + * + * @event core.pagination_generate_page_link + * @var string base_url is url prepended to all links generated within the function + * If you use page numbers inside your controller route, base_url should contains a placeholder (%d) + * for the page. Also be sure to specify the pagination path information into the start_name argument + * @var string on_page is the page for which we want to generate the link + * @var string start_name is the name of the parameter containing the first item of the given page (example: start=20) + * If you use page numbers inside your controller route, start name should be the string + * that should be removed for the first page (example: /page/%d) + * @var int per_page the number of items, posts, etc. to display per page, used to determine the number of pages to produce + * @var bool|string generate_page_link_override Shall we return custom pagination link (string URL) or not (false) + * @since 3.1.0-RC5 + */ + $vars = array('base_url', 'on_page', 'start_name', 'per_page', 'generate_page_link_override'); + extract($this->phpbb_dispatcher->trigger_event('core.pagination_generate_page_link', compact($vars))); + + if ($generate_page_link_override) + { + return $generate_page_link_override; + } + if (!is_string($base_url)) { if (is_array($base_url['routes'])) diff --git a/tests/mock/event_dispatcher.php b/tests/mock/event_dispatcher.php index 613551bffd..fa8b4a1036 100644 --- a/tests/mock/event_dispatcher.php +++ b/tests/mock/event_dispatcher.php @@ -11,9 +11,18 @@ * */ -class phpbb_mock_event_dispatcher +class phpbb_mock_event_dispatcher extends \phpbb\event\dispatcher { - public function trigger_event($eventName, $data) + /** + * Constructor. + * + * Overwrite the constructor to get rid of ContainerInterface param instance + */ + public function __construct() + { + } + + public function trigger_event($eventName, $data = array()) { return array(); } diff --git a/tests/pagination/pagination_test.php b/tests/pagination/pagination_test.php index 95856dd07d..d36aa11a8a 100644 --- a/tests/pagination/pagination_test.php +++ b/tests/pagination/pagination_test.php @@ -28,7 +28,7 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case global $phpbb_dispatcher; - $phpbb_dispatcher = new phpbb_mock_event_dispatcher; + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); $this->user->expects($this->any()) ->method('lang') @@ -58,7 +58,7 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case ); $this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $provider, $manager, $symfony_request, $filesystem, '', 'php', dirname(__FILE__) . '/'); - $this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper); + $this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper, $phpbb_dispatcher); } public function generate_template_pagination_data()