Move itemCount parameter from constructor to renderFull()

- Remove Pager->itemCount property and Pager->setItemCount() method
- Update usage
This commit is contained in:
Hypolite Petovan 2018-10-24 11:42:59 -04:00
parent 14237a9599
commit 7c0b591043
12 changed files with 70 additions and 80 deletions

View file

@ -483,7 +483,7 @@ function admin_page_contactblock(App $a)
$total = DBA::count('contact', $condition); $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()]]); $statement = DBA::select('contact', [], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
@ -513,7 +513,7 @@ function admin_page_contactblock(App $a)
'$contacts' => $contacts, '$contacts' => $contacts,
'$total_contacts' => L10n::tt('%s total blocked contact', '%s total blocked contacts', $total), '$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.")], '$contacturl' => ['contact_url', L10n::t("Profile URL"), '', L10n::t("URL of the remote contact to block.")],
]); ]);
return $o; return $o;
@ -1812,7 +1812,7 @@ function admin_page_users(App $a)
/* get pending */ /* get pending */
$pending = Register::getPending(); $pending = Register::getPending();
$pager = new Pager($a->query_string, DBA::count('user'), 100); $pager = new Pager($a->query_string, 100);
/* ordering */ /* ordering */
$valid_orders = [ $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.")], '$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'], '$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; return $o;
} }

View file

@ -46,7 +46,7 @@ function allfriends_content(App $a)
$total = Model\GContact::countAllFriends(local_user(), $cid); $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()); $r = Model\GContact::allFriends(local_user(), $cid, $pager->getStart(), $pager->getItemsPerPage());
if (!DBA::isResult($r)) { if (!DBA::isResult($r)) {
@ -104,7 +104,7 @@ function allfriends_content(App $a)
//'$title' => L10n::t('Friends of %s', htmlentities($c[0]['name'])), //'$title' => L10n::t('Friends of %s', htmlentities($c[0]['name'])),
'$tab_str' => $tab_str, '$tab_str' => $tab_str,
'$contacts' => $entries, '$contacts' => $entries,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;

View file

@ -82,44 +82,44 @@ function common_content(App $a)
} }
if ($cid) { if ($cid) {
$t = Model\GContact::countCommonFriends($uid, $cid); $total = Model\GContact::countCommonFriends($uid, $cid);
} else { } else {
$t = Model\GContact::countCommonFriendsZcid($uid, $zcid); $total = Model\GContact::countCommonFriendsZcid($uid, $zcid);
} }
if ($t > 0) { if ($total < 1) {
$pager = new Pager($a->query_string, $t);
} else {
notice(L10n::t('No contacts in common.') . EOL); notice(L10n::t('No contacts in common.') . EOL);
return $o; return $o;
} }
$pager = new Pager($a->query_string);
if ($cid) { if ($cid) {
$r = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage()); $common_friends = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage());
} else { } 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; return $o;
} }
$id = 0; $id = 0;
$entries = []; $entries = [];
foreach ($r as $rr) { foreach ($common_friends as $common_friend) {
//get further details of the contact //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() // $rr['id'] is needed to use contact_photo_menu()
/// @TODO Adding '/" here avoids E_NOTICE on missing constants /// @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 = [ $entry = [
'url' => $rr['url'], 'url' => $common_friend['url'],
'itemurl' => defaults($contact_details, 'addr', $rr['url']), 'itemurl' => defaults($contact_details, 'addr', $common_friend['url']),
'name' => $contact_details['name'], 'name' => $contact_details['name'],
'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB), 'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
'img_hover' => htmlentities($contact_details['name']), 'img_hover' => htmlentities($contact_details['name']),
@ -148,7 +148,7 @@ function common_content(App $a)
'$title' => $title, '$title' => $title,
'$tab_str' => $tab_str, '$tab_str' => $tab_str,
'$contacts' => $entries, '$contacts' => $entries,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;

View file

@ -89,7 +89,7 @@ function directory_content(App $a)
if (DBA::isResult($cnt)) { if (DBA::isResult($cnt)) {
$total = $cnt['total']; $total = $cnt['total'];
} }
$pager = new Pager($a->query_string, $total, 60); $pager = new Pager($a->query_string, 60);
$order = " ORDER BY `name` ASC "; $order = " ORDER BY `name` ASC ";
@ -213,7 +213,7 @@ function directory_content(App $a)
'$findterm' => (strlen($search) ? $search : ""), '$findterm' => (strlen($search) ? $search : ""),
'$title' => L10n::t('Site Directory'), '$title' => L10n::t('Site Directory'),
'$submit' => L10n::t('Find'), '$submit' => L10n::t('Find'),
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
} else { } else {
info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL); info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);

View file

@ -188,7 +188,7 @@ function dirfind_content(App $a, $prefix = "") {
} }
if (!empty($j->results)) { 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; $id = 0;
@ -254,7 +254,7 @@ function dirfind_content(App $a, $prefix = "") {
$o .= replace_macros($tpl,[ $o .= replace_macros($tpl,[
'title' => $header, 'title' => $header,
'$contacts' => $entries, '$contacts' => $entries,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($j->total),
]); ]);
} else { } else {

View file

@ -54,6 +54,8 @@ function match_content(App $a)
$tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']); $tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']);
if ($tags) { if ($tags) {
$pager = new Pager($a->query_string, $j->items_page);
$params['s'] = $tags; $params['s'] = $tags;
if ($pager->getPage() != 1) { if ($pager->getPage() != 1) {
$params['p'] = $pager->getPage(); $params['p'] = $pager->getPage();
@ -68,8 +70,6 @@ function match_content(App $a)
$j = json_decode($x); $j = json_decode($x);
if (count($j->results)) { if (count($j->results)) {
$pager = new Pager($a->query_string, $j->total, $j->items_page);
$id = 0; $id = 0;
foreach ($j->results as $jj) { foreach ($j->results as $jj) {
@ -112,13 +112,11 @@ function match_content(App $a)
$tpl = get_markup_template('viewcontact_template.tpl'); $tpl = get_markup_template('viewcontact_template.tpl');
$o .= replace_macros( $o .= replace_macros($tpl, [
$tpl,
[
'$title' => L10n::t('Profile Match'), '$title' => L10n::t('Profile Match'),
'$contacts' => $entries, '$contacts' => $entries,
'$paginate' => $pager->renderFull()] '$paginate' => $pager->renderFull($j->total)
); ]);
} else { } else {
info(L10n::t('No matches') . EOL); info(L10n::t('No matches') . EOL);
} }

View file

@ -283,7 +283,7 @@ function message_content(App $a)
$total = $r[0]['total']; $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()); $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 .= render_messages($r, 'mail_list.tpl');
$o .= $pager->renderFull(); $o .= $pager->renderFull($total);
return $o; return $o;
} }

View file

@ -1146,7 +1146,7 @@ function photos_content(App $a)
$total = count($r); $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? /// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it?
$order_field = defaults($_GET, 'order', ''); $order_field = defaults($_GET, 'order', '');
@ -1233,7 +1233,7 @@ function photos_content(App $a)
'$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)], '$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)],
'$order' => $order, '$order' => $order,
'$edit' => $edit, '$edit' => $edit,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;
@ -1388,13 +1388,16 @@ function photos_content(App $a)
$map = null; $map = null;
$link_item = []; $link_item = [];
$total = 0;
if (DBA::isResult($linked_items)) { if (DBA::isResult($linked_items)) {
// This is a workaround to not being forced to rewrite the while $sql_extra handling // This is a workaround to not being forced to rewrite the while $sql_extra handling
$link_item = Item::selectFirst([], ['id' => $linked_items[0]['id']]); $link_item = Item::selectFirst([], ['id' => $linked_items[0]['id']]);
$condition = ["`parent` = ? AND `parent` != `id`", $link_item['parent']]; $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()]]; $params = ['order' => ['id'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$result = Item::selectForUser($link_item['uid'], Item::ITEM_FIELDLIST, $condition, $params); $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); $responses = get_responses($conv_responses, $response_verbs, '', $link_item);
$paginate = $pager->renderFull(); $paginate = $pager->renderFull($total);
} }
$photo_tpl = get_markup_template('photo_view.tpl'); $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) // Default - show recent photos with upload link (if applicable)
//$o = ''; //$o = '';
$total = 0;
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $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`", $sql_extra GROUP BY `resource-id`",
intval($a->data['user']['uid']), intval($a->data['user']['uid']),
DBA::escape('Contact Photos'), DBA::escape('Contact Photos'),
DBA::escape(L10n::t('Contact Photos')) DBA::escape(L10n::t('Contact Photos'))
); );
if (DBA::isResult($r)) { 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`, $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(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
ANY_VALUE(`created`) AS `created` FROM `photo` ANY_VALUE(`created`) AS `created` FROM `photo`
@ -1711,7 +1715,7 @@ function photos_content(App $a)
'$can_post' => $can_post, '$can_post' => $can_post,
'$upload' => [L10n::t('Upload New Photos'), 'photos/'.$a->data['user']['nickname'].'/upload'], '$upload' => [L10n::t('Upload New Photos'), 'photos/'.$a->data['user']['nickname'].'/upload'],
'$photos' => $photos, '$photos' => $photos,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;

View file

@ -343,7 +343,8 @@ function videos_content(App $a)
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$total = count($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`, $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype` 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) '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
]); ]);
$o .= $pager->renderFull(); $o .= $pager->renderFull($total);
return $o; return $o;
} }

View file

@ -76,7 +76,7 @@ function viewcontacts_content(App $a)
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$total = $r[0]['total']; $total = $r[0]['total'];
} }
$pager = new Pager($a->query_string, $total); $pager = new Pager($a->query_string);
$r = q("SELECT * FROM `contact` $r = q("SELECT * FROM `contact`
WHERE `uid` = %d AND NOT `blocked` AND NOT `pending` WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
@ -128,7 +128,7 @@ function viewcontacts_content(App $a)
$o .= replace_macros($tpl, [ $o .= replace_macros($tpl, [
'$title' => L10n::t('Contacts'), '$title' => L10n::t('Contacts'),
'$contacts' => $contacts, '$contacts' => $contacts,
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;

View file

@ -19,10 +19,6 @@ class Pager
* @var integer * @var integer
*/ */
private $itemsPerPage = 50; private $itemsPerPage = 50;
/**
* @var integer
*/
private $itemCount = 0;
/** /**
* @var string * @var string
@ -35,14 +31,12 @@ class Pager
* Guesses the page number from the GET parameter 'page'. * Guesses the page number from the GET parameter 'page'.
* *
* @param string $queryString The query string of the current 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 * @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->setQueryString($queryString);
$this->setItemsPerPage($itemsPerPage); $this->setItemsPerPage($itemsPerPage);
$this->setItemCount(defaults($itemCount, $this->getItemsPerPage()));
$this->setPage(defaults($_GET, 'page', 1)); $this->setPage(defaults($_GET, 'page', 1));
} }
@ -80,7 +74,7 @@ class Pager
* Returns the base query string. * Returns the base query string.
* *
* Warning: this isn't the same value as passed to the constructor. * 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() * @see setBaseQuery()
* @return string * @return string
@ -110,16 +104,6 @@ class Pager
$this->page = max(1, intval($page)); $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. * Sets the base query string from a full query string.
* *
@ -172,7 +156,7 @@ class Pager
*/ */
public function renderMinimal($itemCount) public function renderMinimal($itemCount)
{ {
$this->setItemCount($itemCount); $displayedItemCount = max(0, intval($itemCount));
$data = [ $data = [
'class' => 'pager', 'class' => 'pager',
@ -184,7 +168,7 @@ class Pager
'next' => [ 'next' => [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)), 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
'text' => L10n::t('older'), 'text' => L10n::t('older'),
'class' => 'next' . ($this->itemCount <= 0 ? ' disabled' : '') 'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '')
] ]
]; ];
@ -209,14 +193,17 @@ class Pager
* *
* $html = $pager->renderFull(); * $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 * @return string HTML string of the pager
*/ */
public function renderFull() public function renderFull($itemCount)
{ {
$totalItemCount = max(0, intval($itemCount));
$data = []; $data = [];
$data['class'] = 'pagination'; $data['class'] = 'pagination';
if ($this->itemCount > $this->getItemsPerPage()) { if ($totalItemCount > $this->getItemsPerPage()) {
$data['first'] = [ $data['first'] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'), 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'),
'text' => L10n::t('first'), 'text' => L10n::t('first'),
@ -228,7 +215,7 @@ class Pager
'class' => $this->getPage() == 1 ? 'disabled' : '' 'class' => $this->getPage() == 1 ? 'disabled' : ''
]; ];
$numpages = $this->itemCount / $this->getItemsPerPage(); $numpages = $totalItemCount / $this->getItemsPerPage();
$numstart = 1; $numstart = 1;
$numstop = $numpages; $numstop = $numpages;
@ -257,7 +244,7 @@ class Pager
} }
} }
if (($this->itemCount % $this->getItemsPerPage()) != 0) { if (($totalItemCount % $this->getItemsPerPage()) != 0) {
if ($i == $this->getPage()) { if ($i == $this->getPage()) {
$pages[$i] = [ $pages[$i] = [
'url' => '#', 'url' => '#',

View file

@ -781,7 +781,7 @@ class Contact extends BaseModule
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
$total = $r[0]['total']; $total = $r[0]['total'];
} }
$pager = new Pager($a->query_string, $total); $pager = new Pager($a->query_string);
$sql_extra3 = Widget::unavailableNetworks(); $sql_extra3 = Widget::unavailableNetworks();
@ -822,7 +822,7 @@ class Contact extends BaseModule
'contacts_batch_drop' => L10n::t('Delete'), 'contacts_batch_drop' => L10n::t('Delete'),
], ],
'$h_batch_actions' => L10n::t('Batch Actions'), '$h_batch_actions' => L10n::t('Batch Actions'),
'$paginate' => $pager->renderFull(), '$paginate' => $pager->renderFull($total),
]); ]);
return $o; return $o;