From 7c0b591043b8c5183b1b697be9de772667f4778b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 24 Oct 2018 11:42:59 -0400 Subject: [PATCH] Move itemCount parameter from constructor to renderFull() - Remove Pager->itemCount property and Pager->setItemCount() method - Update usage --- mod/admin.php | 8 ++++---- mod/allfriends.php | 4 ++-- mod/common.php | 30 +++++++++++++++--------------- mod/directory.php | 4 ++-- mod/dirfind.php | 4 ++-- mod/match.php | 14 ++++++-------- mod/message.php | 4 ++-- mod/photos.php | 34 +++++++++++++++++++--------------- mod/videos.php | 5 +++-- mod/viewcontacts.php | 4 ++-- src/Content/Pager.php | 35 +++++++++++------------------------ src/Module/Contact.php | 4 ++-- 12 files changed, 70 insertions(+), 80 deletions(-) diff --git a/mod/admin.php b/mod/admin.php index d90cfb7ab9..5de292916e 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -483,7 +483,7 @@ function admin_page_contactblock(App $a) $total = DBA::count('contact', $condition); - $pager = new Pager($a->query_string, $total, 30); + $pager = new Pager($a->query_string, 30); $statement = DBA::select('contact', [], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); @@ -513,7 +513,7 @@ function admin_page_contactblock(App $a) '$contacts' => $contacts, '$total_contacts' => L10n::tt('%s total blocked contact', '%s total blocked contacts', $total), - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), '$contacturl' => ['contact_url', L10n::t("Profile URL"), '', L10n::t("URL of the remote contact to block.")], ]); return $o; @@ -1812,7 +1812,7 @@ function admin_page_users(App $a) /* get pending */ $pending = Register::getPending(); - $pager = new Pager($a->query_string, DBA::count('user'), 100); + $pager = new Pager($a->query_string, 100); /* ordering */ $valid_orders = [ @@ -1951,7 +1951,7 @@ function admin_page_users(App $a) '$newusernickname' => ['new_user_nickname', L10n::t("Nickname"), '', L10n::t("Nickname of the new user.")], '$newuseremail' => ['new_user_email', L10n::t("Email"), '', L10n::t("Email address of the new user."), '', '', 'email'], ]); - $o .= $pager->renderFull(); + $o .= $pager->renderFull(DBA::count('user')); return $o; } diff --git a/mod/allfriends.php b/mod/allfriends.php index d2501e9fe4..83ea73af6b 100644 --- a/mod/allfriends.php +++ b/mod/allfriends.php @@ -46,7 +46,7 @@ function allfriends_content(App $a) $total = Model\GContact::countAllFriends(local_user(), $cid); - $pager = new Pager($a->query_string, $total); + $pager = new Pager($a->query_string); $r = Model\GContact::allFriends(local_user(), $cid, $pager->getStart(), $pager->getItemsPerPage()); if (!DBA::isResult($r)) { @@ -104,7 +104,7 @@ function allfriends_content(App $a) //'$title' => L10n::t('Friends of %s', htmlentities($c[0]['name'])), '$tab_str' => $tab_str, '$contacts' => $entries, - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); return $o; diff --git a/mod/common.php b/mod/common.php index 993bc58777..8fb19b57cd 100644 --- a/mod/common.php +++ b/mod/common.php @@ -82,44 +82,44 @@ function common_content(App $a) } if ($cid) { - $t = Model\GContact::countCommonFriends($uid, $cid); + $total = Model\GContact::countCommonFriends($uid, $cid); } else { - $t = Model\GContact::countCommonFriendsZcid($uid, $zcid); + $total = Model\GContact::countCommonFriendsZcid($uid, $zcid); } - if ($t > 0) { - $pager = new Pager($a->query_string, $t); - } else { + if ($total < 1) { notice(L10n::t('No contacts in common.') . EOL); return $o; } + $pager = new Pager($a->query_string); + if ($cid) { - $r = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage()); + $common_friends = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage()); } else { - $r = Model\GContact::commonFriendsZcid($uid, $zcid, $pager->getStart(), $pager->getItemsPerPage()); + $common_friends = Model\GContact::commonFriendsZcid($uid, $zcid, $pager->getStart(), $pager->getItemsPerPage()); } - if (!DBA::isResult($r)) { + if (!DBA::isResult($common_friends)) { return $o; } $id = 0; $entries = []; - foreach ($r as $rr) { + foreach ($common_friends as $common_friend) { //get further details of the contact - $contact_details = Model\Contact::getDetailsByURL($rr['url'], $uid); + $contact_details = Model\Contact::getDetailsByURL($common_friend['url'], $uid); // $rr['id'] is needed to use contact_photo_menu() /// @TODO Adding '/" here avoids E_NOTICE on missing constants - $rr['id'] = $rr['cid']; + $common_friend['id'] = $common_friend['cid']; - $photo_menu = Model\Contact::photoMenu($rr); + $photo_menu = Model\Contact::photoMenu($common_friend); $entry = [ - 'url' => $rr['url'], - 'itemurl' => defaults($contact_details, 'addr', $rr['url']), + 'url' => $common_friend['url'], + 'itemurl' => defaults($contact_details, 'addr', $common_friend['url']), 'name' => $contact_details['name'], 'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB), 'img_hover' => htmlentities($contact_details['name']), @@ -148,7 +148,7 @@ function common_content(App $a) '$title' => $title, '$tab_str' => $tab_str, '$contacts' => $entries, - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); return $o; diff --git a/mod/directory.php b/mod/directory.php index f9104b9a53..6c8be7c3c5 100644 --- a/mod/directory.php +++ b/mod/directory.php @@ -89,7 +89,7 @@ function directory_content(App $a) if (DBA::isResult($cnt)) { $total = $cnt['total']; } - $pager = new Pager($a->query_string, $total, 60); + $pager = new Pager($a->query_string, 60); $order = " ORDER BY `name` ASC "; @@ -213,7 +213,7 @@ function directory_content(App $a) '$findterm' => (strlen($search) ? $search : ""), '$title' => L10n::t('Site Directory'), '$submit' => L10n::t('Find'), - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); } else { info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL); diff --git a/mod/dirfind.php b/mod/dirfind.php index 7cff5f28a3..9d3c852f49 100644 --- a/mod/dirfind.php +++ b/mod/dirfind.php @@ -188,7 +188,7 @@ function dirfind_content(App $a, $prefix = "") { } if (!empty($j->results)) { - $pager = new Pager($a->query_string, $j->total, $j->items_page); + $pager = new Pager($a->query_string, $j->items_page); $id = 0; @@ -254,7 +254,7 @@ function dirfind_content(App $a, $prefix = "") { $o .= replace_macros($tpl,[ 'title' => $header, '$contacts' => $entries, - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($j->total), ]); } else { diff --git a/mod/match.php b/mod/match.php index 4254e4c45a..a514439db6 100644 --- a/mod/match.php +++ b/mod/match.php @@ -54,6 +54,8 @@ function match_content(App $a) $tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']); if ($tags) { + $pager = new Pager($a->query_string, $j->items_page); + $params['s'] = $tags; if ($pager->getPage() != 1) { $params['p'] = $pager->getPage(); @@ -68,8 +70,6 @@ function match_content(App $a) $j = json_decode($x); if (count($j->results)) { - $pager = new Pager($a->query_string, $j->total, $j->items_page); - $id = 0; foreach ($j->results as $jj) { @@ -112,13 +112,11 @@ function match_content(App $a) $tpl = get_markup_template('viewcontact_template.tpl'); - $o .= replace_macros( - $tpl, - [ - '$title' => L10n::t('Profile Match'), + $o .= replace_macros($tpl, [ + '$title' => L10n::t('Profile Match'), '$contacts' => $entries, - '$paginate' => $pager->renderFull()] - ); + '$paginate' => $pager->renderFull($j->total) + ]); } else { info(L10n::t('No matches') . EOL); } diff --git a/mod/message.php b/mod/message.php index de2ee3cf01..5f7ffb2b70 100644 --- a/mod/message.php +++ b/mod/message.php @@ -283,7 +283,7 @@ function message_content(App $a) $total = $r[0]['total']; } - $pager = new Pager($a->query_string, $total); + $pager = new Pager($a->query_string); $r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage()); @@ -294,7 +294,7 @@ function message_content(App $a) $o .= render_messages($r, 'mail_list.tpl'); - $o .= $pager->renderFull(); + $o .= $pager->renderFull($total); return $o; } diff --git a/mod/photos.php b/mod/photos.php index b8f00c358a..008d59cd9c 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -1146,7 +1146,7 @@ function photos_content(App $a) $total = count($r); } - $pager = new Pager($a->query_string, $total, 20); + $pager = new Pager($a->query_string, 20); /// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it? $order_field = defaults($_GET, 'order', ''); @@ -1227,14 +1227,14 @@ function photos_content(App $a) $tpl = get_markup_template('photo_album.tpl'); $o .= replace_macros($tpl, [ - '$photos' => $photos, - '$album' => $album, - '$can_post' => $can_post, - '$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)], - '$order' => $order, - '$edit' => $edit, - '$paginate' => $pager->renderFull(), - ]); + '$photos' => $photos, + '$album' => $album, + '$can_post' => $can_post, + '$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)], + '$order' => $order, + '$edit' => $edit, + '$paginate' => $pager->renderFull($total), + ]); return $o; @@ -1388,13 +1388,16 @@ function photos_content(App $a) $map = null; $link_item = []; + $total = 0; if (DBA::isResult($linked_items)) { // This is a workaround to not being forced to rewrite the while $sql_extra handling $link_item = Item::selectFirst([], ['id' => $linked_items[0]['id']]); $condition = ["`parent` = ? AND `parent` != `id`", $link_item['parent']]; - $pager = new Pager($a->query_string, DBA::count('item', $condition)); + $total = DBA::count('item', $condition); + + $pager = new Pager($a->query_string); $params = ['order' => ['id'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; $result = Item::selectForUser($link_item['uid'], Item::ITEM_FIELDLIST, $condition, $params); @@ -1611,7 +1614,7 @@ function photos_content(App $a) } $responses = get_responses($conv_responses, $response_verbs, '', $link_item); - $paginate = $pager->renderFull(); + $paginate = $pager->renderFull($total); } $photo_tpl = get_markup_template('photo_view.tpl'); @@ -1647,18 +1650,19 @@ function photos_content(App $a) // Default - show recent photos with upload link (if applicable) //$o = ''; - + $total = 0; $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra GROUP BY `resource-id`", intval($a->data['user']['uid']), DBA::escape('Contact Photos'), DBA::escape(L10n::t('Contact Photos')) ); - if (DBA::isResult($r)) { - $pager = new Pager($a->query_string, count($r), 20); + $total = count($r); } + $pager = new Pager($a->query_string, 20); + $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`, ANY_VALUE(`created`) AS `created` FROM `photo` @@ -1711,7 +1715,7 @@ function photos_content(App $a) '$can_post' => $can_post, '$upload' => [L10n::t('Upload New Photos'), 'photos/'.$a->data['user']['nickname'].'/upload'], '$photos' => $photos, - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); return $o; diff --git a/mod/videos.php b/mod/videos.php index ee50d26d77..51f15a47ae 100644 --- a/mod/videos.php +++ b/mod/videos.php @@ -343,7 +343,8 @@ function videos_content(App $a) if (DBA::isResult($r)) { $total = count($r); } - $pager = new Pager($a->query_string, $total, 20); + + $pager = new Pager($a->query_string, 20); $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype` @@ -389,7 +390,7 @@ function videos_content(App $a) '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false) ]); - $o .= $pager->renderFull(); + $o .= $pager->renderFull($total); return $o; } diff --git a/mod/viewcontacts.php b/mod/viewcontacts.php index ea5bcc6527..9a2513a6c4 100644 --- a/mod/viewcontacts.php +++ b/mod/viewcontacts.php @@ -76,7 +76,7 @@ function viewcontacts_content(App $a) if (DBA::isResult($r)) { $total = $r[0]['total']; } - $pager = new Pager($a->query_string, $total); + $pager = new Pager($a->query_string); $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` @@ -128,7 +128,7 @@ function viewcontacts_content(App $a) $o .= replace_macros($tpl, [ '$title' => L10n::t('Contacts'), '$contacts' => $contacts, - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); return $o; diff --git a/src/Content/Pager.php b/src/Content/Pager.php index 185c5506d9..a95fc7cf65 100644 --- a/src/Content/Pager.php +++ b/src/Content/Pager.php @@ -19,10 +19,6 @@ class Pager * @var integer */ private $itemsPerPage = 50; - /** - * @var integer - */ - private $itemCount = 0; /** * @var string @@ -35,14 +31,12 @@ class Pager * Guesses the page number from the GET parameter 'page'. * * @param string $queryString The query string of the current page - * @param integer $itemCount The total item count (for the full mode) or null (for the minimal mode) * @param integer $itemsPerPage An optional number of items per page to override the default value */ - public function __construct($queryString, $itemCount = null, $itemsPerPage = 50) + public function __construct($queryString, $itemsPerPage = 50) { $this->setQueryString($queryString); $this->setItemsPerPage($itemsPerPage); - $this->setItemCount(defaults($itemCount, $this->getItemsPerPage())); $this->setPage(defaults($_GET, 'page', 1)); } @@ -80,7 +74,7 @@ class Pager * Returns the base query string. * * Warning: this isn't the same value as passed to the constructor. - * See setQueryString for the inventory of transformations + * See setQueryString() for the inventory of transformations * * @see setBaseQuery() * @return string @@ -110,16 +104,6 @@ class Pager $this->page = max(1, intval($page)); } - /** - * Sets the item count, 0 minimum. - * - * @param integer $itemCount - */ - public function setItemCount($itemCount) - { - $this->itemCount = max(0, intval($itemCount)); - } - /** * Sets the base query string from a full query string. * @@ -172,7 +156,7 @@ class Pager */ public function renderMinimal($itemCount) { - $this->setItemCount($itemCount); + $displayedItemCount = max(0, intval($itemCount)); $data = [ 'class' => 'pager', @@ -184,7 +168,7 @@ class Pager 'next' => [ 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), 'text' => L10n::t('older'), - 'class' => 'next' . ($this->itemCount <= 0 ? ' disabled' : '') + 'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '') ] ]; @@ -209,14 +193,17 @@ class Pager * * $html = $pager->renderFull(); * + * @param integer $itemCount The total number of items including those note displayed on the page * @return string HTML string of the pager */ - public function renderFull() + public function renderFull($itemCount) { + $totalItemCount = max(0, intval($itemCount)); + $data = []; $data['class'] = 'pagination'; - if ($this->itemCount > $this->getItemsPerPage()) { + if ($totalItemCount > $this->getItemsPerPage()) { $data['first'] = [ 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'), 'text' => L10n::t('first'), @@ -228,7 +215,7 @@ class Pager 'class' => $this->getPage() == 1 ? 'disabled' : '' ]; - $numpages = $this->itemCount / $this->getItemsPerPage(); + $numpages = $totalItemCount / $this->getItemsPerPage(); $numstart = 1; $numstop = $numpages; @@ -257,7 +244,7 @@ class Pager } } - if (($this->itemCount % $this->getItemsPerPage()) != 0) { + if (($totalItemCount % $this->getItemsPerPage()) != 0) { if ($i == $this->getPage()) { $pages[$i] = [ 'url' => '#', diff --git a/src/Module/Contact.php b/src/Module/Contact.php index b6ead69f0f..817c70e852 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -781,7 +781,7 @@ class Contact extends BaseModule if (DBA::isResult($r)) { $total = $r[0]['total']; } - $pager = new Pager($a->query_string, $total); + $pager = new Pager($a->query_string); $sql_extra3 = Widget::unavailableNetworks(); @@ -822,7 +822,7 @@ class Contact extends BaseModule 'contacts_batch_drop' => L10n::t('Delete'), ], '$h_batch_actions' => L10n::t('Batch Actions'), - '$paginate' => $pager->renderFull(), + '$paginate' => $pager->renderFull($total), ]); return $o;