From: Meitar Moscovitz <meitarm@gmail.com>
Date: Tue, 10 Feb 2009 08:42:01 +0000 (+1100)
Subject: Beginning to refactor document relationship links to reduce common code.
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b240a1719485b15c4a6497b6053c24e8a40a4e3c;p=quix0rs-gnu-social.git

Beginning to refactor document relationship links to reduce common code.

My attempts here are to mimic the `pagination()` method shared by
actions. I'm tentatively adding the `$count` property to actions so that
we can query the number of notices ''being displayed'' per page prior to
calling the actual `pagination()` method itself, since document
relationship `<link>` elements need to be output inside of `showHead()`,
before `showContent()`, which is where `pagination()` is, gets called.
---

diff --git a/actions/all.php b/actions/all.php
index 76b1bbcdf8..4a625207a4 100644
--- a/actions/all.php
+++ b/actions/all.php
@@ -85,19 +85,8 @@ class AllAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('all',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('all',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'all', array('nickname' => $this->user->nickname));
     }
 
     function showLocalNav()
diff --git a/actions/favorited.php b/actions/favorited.php
index 367fb6dd69..74920ca7e4 100644
--- a/actions/favorited.php
+++ b/actions/favorited.php
@@ -203,16 +203,7 @@ class FavoritedAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('favorited',
-                                                                    array('page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('favorited',
-                                                                array('page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'favorited');
     }
 }
diff --git a/actions/groupmembers.php b/actions/groupmembers.php
index 2b2bdba93d..b80f3d90d1 100644
--- a/actions/groupmembers.php
+++ b/actions/groupmembers.php
@@ -145,18 +145,7 @@ class GroupmembersAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('groupmembers',
-                                                                    array('nickname' => $this->group->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Group Members')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('groupmembers',
-                                                                array('nickname' => $this->group->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Group Members')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'groupmembers', array('nickname' => $this->group->nickname));
     }
 }
diff --git a/actions/groups.php b/actions/groups.php
index b0bf7cfc2a..e158698de2 100644
--- a/actions/groups.php
+++ b/actions/groups.php
@@ -137,16 +137,7 @@ class GroupsAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('groups',
-                                                                    array('page' => $this->page - 1)),
-                                         'title' => _('Next Groups')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('groups',
-                                                                array('page' => $this->page + 1)),
-                                     'title' => _('Previous Groups')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'groups', array('nickname' => $this->group->nickname));
     }
 }
diff --git a/actions/inbox.php b/actions/inbox.php
index d12f3f20ae..7b5cf2d203 100644
--- a/actions/inbox.php
+++ b/actions/inbox.php
@@ -70,19 +70,8 @@ class InboxAction extends MailboxAction
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('inbox',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Messages')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('inbox',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Messages')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'inbox', array('nickname' => $this->user->nickname));
     }
 
     /**
diff --git a/actions/outbox.php b/actions/outbox.php
index 043b74edc4..deef1cc870 100644
--- a/actions/outbox.php
+++ b/actions/outbox.php
@@ -69,19 +69,8 @@ class OutboxAction extends MailboxAction
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('outbox',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Messages')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('outbox',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Messages')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'outbox', array('nickname' => $this->user->nickname));
     }
 
     /**
diff --git a/actions/public.php b/actions/public.php
index 25889eee5a..eb2a4b1b00 100644
--- a/actions/public.php
+++ b/actions/public.php
@@ -56,6 +56,18 @@ class PublicAction extends Action
 
     var $page = null;
 
+    /**
+     * Number of notices being shown on this page.
+     */
+    //    Does this need to be here? Should it be?
+    //    If it does, this property needs to be
+    //    added to other actions as well, like $page.
+    //    I'm trying to find a way to capture the
+    //    output of the $cnt variable from this
+    //    action's showContent() method but need
+    //    to do so earlier, I think...?
+    var $count = null;
+
     function isReadOnly()
     {
         return true;
@@ -134,17 +146,8 @@ class PublicAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('public',
-                                                                    array('page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('public',
-                                                                array('page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'public');
     }
 
     /**
diff --git a/actions/replies.php b/actions/replies.php
index 5ae99e1652..7c24b554e0 100644
--- a/actions/replies.php
+++ b/actions/replies.php
@@ -148,19 +148,8 @@ class RepliesAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('replies',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('replies',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'replies', array('nickname' => $this->user->nickname));
     }
 
     /**
diff --git a/actions/showfavorites.php b/actions/showfavorites.php
index f0297172a6..585b3b75ab 100644
--- a/actions/showfavorites.php
+++ b/actions/showfavorites.php
@@ -160,21 +160,11 @@ class ShowfavoritesAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('showfavorites',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Favorite Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('showfavorites',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Favorite Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'showfavorites', array('nickname' => $this->user->nickname));
     }
 
+
     /**
      * show the personal group nav
      *
diff --git a/actions/showgroup.php b/actions/showgroup.php
index 7599a8de6e..a2b40f9948 100644
--- a/actions/showgroup.php
+++ b/actions/showgroup.php
@@ -333,19 +333,8 @@ class ShowgroupAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('showgroup',
-                                                                    array('nickname' => $this->group->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('showgroup',
-                                                                array('nickname' => $this->group->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'showgroup', array('nickname' => $this->group->nickname));
     }
 
     /**
diff --git a/actions/showstream.php b/actions/showstream.php
index 0c8fea1821..1779c70f2c 100644
--- a/actions/showstream.php
+++ b/actions/showstream.php
@@ -202,19 +202,8 @@ class ShowstreamAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('showstream',
-                                                                    array('nickname' => $this->user->nickname,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('showstream',
-                                                                array('nickname' => $this->user->nickname,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'showstream', array('nickname' => $this->user->nickname));
     }
 
     function extraHead()
diff --git a/actions/tag.php b/actions/tag.php
index f71f6d342f..2a564a25d4 100644
--- a/actions/tag.php
+++ b/actions/tag.php
@@ -76,19 +76,8 @@ class TagAction extends Action
      */
     function showRelationshipLinks()
     {
-        // Machine-readable pagination
-        if ($this->page > 1) {
-            $this->element('link', array('rel' => 'next',
-                                         'href' => common_local_url('tag',
-                                                                    array('tag' => $this->tag,
-                                                                          'page' => $this->page - 1)),
-                                         'title' => _('Next Notices')));
-        }
-        $this->element('link', array('rel' => 'prev',
-                                     'href' => common_local_url('tag',
-                                                                array('tag' => $this->tag,
-                                                                      'page' => $this->page + 1)),
-                                     'title' => _('Previous Notices')));
+        $this->sequenceRelationships($this->page > 1, $this->count > NOTICES_PER_PAGE, // FIXME
+                                     $this->page, 'tag', array('tag' => $this->tag));
     }
 
     function showPageNotice()
diff --git a/lib/action.php b/lib/action.php
index 8d0fea7afd..80a8969fa2 100644
--- a/lib/action.php
+++ b/lib/action.php
@@ -938,4 +938,38 @@ class Action extends HTMLOutputter // lawsuit
             $this->elementEnd('div');
         }
     }
+
+    /**
+     * Generate document metadata for sequential navigation
+     *
+     * @param boolean $have_before is there something before?
+     * @param boolean $have_after  is there something after?
+     * @param integer $page        current page
+     * @param string  $action      current action
+     * @param array   $args        rest of query arguments
+     *
+     * @return nothing
+     */
+    function sequenceRelationships($have_next, $have_previous, $page, $action, $args=null)
+    {
+        // Outputs machine-readable pagination in <link> elements.
+        // Pattern taken from $this->pagination() method.
+
+        // "next" is equivalent to "after"
+        if ($have_next) {
+            $pargs   = array('page' => $page-1);
+            $newargs = $args ? array_merge($args, $pargs) : $pargs;
+            $this->element('link', array('rel' => 'next',
+                                         'href' => common_local_url($action, $newargs),
+                                         'title' => _('Next')));
+        }
+        // "previous" is equivalent to "before"
+        if ($have_previous=true) { // FIXME
+            $pargs   = array('page' => $page+1);
+            $newargs = $args ? array_merge($args, $pargs) : $pargs;
+            $this->element('link', array('rel' => 'prev',
+                                         'href' => common_local_url($action, $newargs),
+                                         'title' => _('Previous')));
+        }
+    }
 }