Skip to content

Commit

Permalink
Merge pull request #182 from iMattPro/pagination
Browse files Browse the repository at this point in the history
Paginate list of pages in ACP
  • Loading branch information
iMattPro authored Jul 23, 2024
2 parents 5797775 + 88773df commit c02be0a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ jobs:
db: "mysql:5.7"
- php: '8.3'
db: "mysql:5.7"
- php: '8.4'
db: "mysql:5.7"

name: PHP ${{ matrix.php }} - ${{ matrix.db_alias != '' && matrix.db_alias || matrix.db }}

Expand Down Expand Up @@ -270,6 +272,8 @@ jobs:
db: "postgres:14"
- php: '8.3'
db: "postgres:14"
- php: '8.4'
db: "postgres:14"

name: PHP ${{ matrix.php }} - ${{ matrix.db }}

Expand Down
12 changes: 12 additions & 0 deletions adm/style/manage_pages.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ <h1>{{ lang('ACP_PAGES_MANAGE') }}</h1>
</fieldset>
</form>

{% if pagination %}
<div class="pagination top-pagination">
{% include 'pagination.html' %}
</div>
{% endif %}

<table class="table1 zebra-table fixed-width-table">
<thead>
<tr>
Expand Down Expand Up @@ -193,6 +199,12 @@ <h1>{{ lang('ACP_PAGES_MANAGE') }}</h1>
</tbody>
</table>

{% if pagination %}
<div class="pagination">
{% include 'pagination.html' %}
</div>
{% endif %}

<form id="pages_add_page" method="post" action="{{ U_ACTION }}">
<fieldset class="quick">
<input class="button2" type="submit" name="addpage" value="{{ lang('ACP_PAGES_CREATE_PAGE') }}" />
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"composer/installers": "~1.0"
},
"require-dev": {
"phing/phing": "2.4.*"
"phing/phing": "~2.4"
},
"extra": {
"display-name": "Pages",
Expand Down
10 changes: 9 additions & 1 deletion controller/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\
*/
public function display_pages()
{
/* @var $pagination \phpbb\pagination */
$pagination = $this->container->get('pagination');
$start = $this->request->variable('start', 0);
$total = $this->page_operator->get_total_pages();
$limit = 25;

// Grab all the pages from the db
$entities = $this->page_operator->get_pages();
$entities = $this->page_operator->get_pages($limit, $start);

// Process each page entity for display
/* @var $entity \phpbb\pages\entity\page */
Expand All @@ -121,6 +127,8 @@ public function display_pages()
));
}

$pagination->generate_template_pagination($this->u_action, 'pagination', 'start', $total, $limit, $start);

// Set output vars for display in the template
$this->template->assign_vars(array(
'U_ACTION' => $this->u_action,
Expand Down
33 changes: 26 additions & 7 deletions operators/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@ public function __construct(\phpbb\cache\driver\driver_interface $cache, Contain
}

/**
* Get all pages
*
* @return array Array of page data entities
* @access public
*/
public function get_pages()
* Get all pages
*
* @param int $limit
* @param int $start
* @return array Array of page data entities
* @access public
*/
public function get_pages($limit = 0, $start = 0)
{
$entities = array();

// Load all page data from the database
$sql = 'SELECT *
FROM ' . $this->pages_table . '
ORDER BY page_order ASC, page_id ASC';
$result = $this->db->sql_query($sql);
$result = $this->db->sql_query_limit($sql, $limit, $start);

while ($row = $this->db->sql_fetchrow($result))
{
Expand Down Expand Up @@ -367,6 +369,23 @@ public function get_link_locations()
return $rows;
}

/**
* Get the total number of pages
*
* @return int
* @access public
*/
public function get_total_pages()
{
$sql = 'SELECT COUNT(*) AS pages
FROM ' . $this->pages_table;
$result = $this->db->sql_query($sql);
$pages = $this->db->sql_fetchfield('pages');
$this->db->sql_freeresult($result);

return (int) $pages;
}

/**
* Check if a page identifier exists in the database
*
Expand Down
24 changes: 17 additions & 7 deletions operators/page_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
/**
* Interface for our pages operator
*
* This describes all of the methods we'll have for working with a set of pages
* This describes all the methods we'll have for working with a set of pages
*/
interface page_interface
{
/**
* Get all pages
*
* @return array Array of page data entities
* @access public
*/
public function get_pages();
* Get all pages
*
* @param int $limit
* @param int $start
* @return array Array of page data entities
* @access public
*/
public function get_pages($limit = 0, $start = 0);

/**
* Add a page
Expand Down Expand Up @@ -108,4 +110,12 @@ public function insert_page_links($page_id, $link_ids);
* @access public
*/
public function get_link_locations();

/**
* Get the total number of pages
*
* @return int
* @access public
*/
public function get_total_pages();
}
59 changes: 40 additions & 19 deletions tests/operators/page_operator_get_pages_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class page_operator_get_pages_test extends page_operator_base
*/
public function get_pages_test_data()
{
return array(
array(
array(
array(
return [
[
0,
0,
[
[
'page_id' => 1,
'page_order' => 1,
'page_description' => 'description_1',
Expand All @@ -34,8 +36,8 @@ public function get_pages_test_data()
'page_display_to_guests' => 1,
'page_title_switch' => 0,
'page_icon_font' => 'foo-1',
),
array(
],
[
'page_id' => 2,
'page_order' => 2,
'page_description' => 'description_2',
Expand All @@ -47,8 +49,8 @@ public function get_pages_test_data()
'page_display_to_guests' => 1,
'page_title_switch' => 0,
'page_icon_font' => '',
),
array(
],
[
'page_id' => 3,
'page_order' => 3,
'page_description' => 'description_3',
Expand All @@ -60,8 +62,8 @@ public function get_pages_test_data()
'page_display_to_guests' => 0,
'page_title_switch' => 0,
'page_icon_font' => '',
),
array(
],
[
'page_id' => 4,
'page_order' => 4,
'page_description' => 'description_4',
Expand All @@ -73,27 +75,46 @@ public function get_pages_test_data()
'page_display_to_guests' => 0,
'page_title_switch' => 0,
'page_icon_font' => '',
),
),
),
);
],
],
],
[
2,
1,
[
[
'page_id' => 3,
'page_order' => 3,
'page_description' => 'description_3',
'page_description_display' => 0,
'page_route' => 'page_3',
'page_title' => 'title_3',
'page_content' => 'message_3',
'page_display' => 1,
'page_display_to_guests' => 0,
'page_title_switch' => 0,
'page_icon_font' => '',
],
],
],
];
}

/**
* Test getting pages from the database
*
* @dataProvider get_pages_test_data
*/
public function test_get_pages($expected)
public function test_get_pages($start, $limit, $expected)
{
// Setup the operator class
// Set up the operator class
$operator = $this->get_page_operator();

// Grab the page data as an array of entities
$entities = $operator->get_pages();
$entities = $operator->get_pages($limit, $start);

// Map the fields to the getters
$map = array(
$map = [
'page_id' => 'get_id',
'page_order' => 'get_order',
'page_description' => 'get_description',
Expand All @@ -105,7 +126,7 @@ public function test_get_pages($expected)
'page_display_to_guests' => 'get_page_display_to_guests',
'page_title_switch' => 'get_page_title_switch',
'page_icon_font' => 'get_icon_font',
);
];

// Test through each entity in the array of entities
$i = 0;
Expand Down
22 changes: 22 additions & 0 deletions tests/operators/page_operator_get_total_pages_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
*
* Pages extension for the phpBB Forum Software package.
*
* @copyright (c) 2024 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\pages\tests\operators;

class page_operator_get_total_pages_test extends page_operator_base
{
public function test_get_total_pages()
{
// Set up the operator class
$operator = $this->get_page_operator();

self::assertEquals(4, $operator->get_total_pages());
}
}

0 comments on commit c02be0a

Please sign in to comment.