Add pagination for like details
authorMatthias Schmidt <gravatronics@live.com>
Sat, 19 Apr 2014 18:34:11 +0000 (20:34 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 19 Apr 2014 18:34:11 +0000 (20:34 +0200)
wcfsetup/install/files/js/WCF.Like.js
wcfsetup/install/files/lib/data/like/LikeAction.class.php

index a87501dee868ea51018deebf4bc4a156ccf0a72d..9c3cacec31f88cc087b6e70d49f37470160ef166 100644 (file)
@@ -42,17 +42,11 @@ WCF.Like = Class.extend({
        _isBusy: false,
        
        /**
-        * cached template for like details
+        * cached grouped user lists for like details
         * @var object
         */
        _likeDetails: { },
        
-       /**
-        * dialog overlay for like details
-        * @var jQuery
-        */
-       _likeDetailsDialog: null,
-       
        /**
         * proxy object
         * @var WCF.Action.Proxy
@@ -61,6 +55,7 @@ WCF.Like = Class.extend({
        
        /**
         * shows the detailed summary of users who liked the object
+        * @var boolean
         */
        _showSummary: true,
        
@@ -77,7 +72,6 @@ WCF.Like = Class.extend({
                this._enableDislikes = enableDislikes;
                this._isBusy = false;
                this._likeDetails = { };
-               this._likeDetailsDialog = null;
                this._showSummary = showSummary;
                this._allowForOwnContent = allowForOwnContent;
                
@@ -248,30 +242,16 @@ WCF.Like = Class.extend({
                var $containerID = (event === null) ? containerID : $(event.currentTarget).data('containerID');
                
                if (this._likeDetails[$containerID] === undefined) {
-                       this._proxy.setOption('data', {
-                               actionName: 'getLikeDetails',
-                               className: 'wcf\\data\\like\\LikeAction',
-                               parameters: {
-                                       data: {
-                                               containerID: $containerID,
-                                               objectID: this._containerData[$containerID].objectID,
-                                               objectType: this._containerData[$containerID].objectType
-                                       }
+                       this._likeDetails[$containerID] = new WCF.User.List('wcf\\data\\like\\LikeAction', WCF.Language.get('wcf.like.details'), {
+                               data: {
+                                       containerID: $containerID,
+                                       objectID: this._containerData[$containerID].objectID,
+                                       objectType: this._containerData[$containerID].objectType
                                }
                        });
-                       this._proxy.sendRequest();
-               }
-               else {
-                       if (this._likeDetailsDialog === null) {
-                               this._likeDetailsDialog = $('<div>' + this._likeDetails[$containerID] + '</div>').hide().appendTo(document.body);
-                               this._likeDetailsDialog.wcfDialog({
-                                       title: WCF.Language.get('wcf.like.details')
-                               });
-                       }
-                       else {
-                               this._likeDetailsDialog.html(this._likeDetails[$containerID]).wcfDialog('open');
-                       }
                }
+               
+               this._likeDetails[$containerID].open();
        },
        
        /**
@@ -360,11 +340,6 @@ WCF.Like = Class.extend({
                                
                                this._isBusy = false;
                        break;
-                       
-                       case 'getLikeDetails':
-                               this._likeDetails[$containerID] = data.returnValues.template;
-                               this._showLikeDetails(null, $containerID);
-                       break;
                }
        },
        
index 59c929e8f686a9af8fa99d064260f8a4709f3d93..0db4cc971ce09fecf78ea4f8e73b577af262f506 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\like;
 use wcf\data\AbstractDatabaseObjectAction;
+use wcf\data\IGroupedUserListAction;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\UserInputException;
 use wcf\system\like\LikeHandler;
@@ -18,11 +19,11 @@ use wcf\system\WCF;
  * @subpackage data.like
  * @category   Community Framework
  */
-class LikeAction extends AbstractDatabaseObjectAction {
+class LikeAction extends AbstractDatabaseObjectAction implements IGroupedUserListAction {
        /**
         * @see \wcf\data\AbstractDatabaseObjectAction::$allowGuestAccess
         */
-       protected $allowGuestAccess = array('getLikeDetails');
+       protected $allowGuestAccess = array('getGroupedUserList', 'getLikeDetails');
        
        /**
         * @see \wcf\data\AbstractDatabaseObjectAction::$className
@@ -202,4 +203,71 @@ class LikeAction extends AbstractDatabaseObjectAction {
                        throw new PermissionDeniedException();
                }
        }
+       
+       /**
+        * @see \wcf\data\IGroupedUserListAction::validateGetGroupedUserList()
+        */
+       public function validateGetGroupedUserList() {
+               $this->validateObjectParameters();
+               
+               $this->readInteger('pageNo');
+       }
+       
+       /**
+        * @see \wcf\data\IGroupedUserListAction::getGroupedUserList()
+        */
+       public function getGroupedUserList() {
+               // fetch number of pages
+               $sql = "SELECT  COUNT(*)
+                       FROM    wcf".WCF_N."_like
+                       WHERE   objectID = ?
+                               AND objectTypeID = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array(
+                       $this->parameters['data']['objectID'],
+                       $this->objectType->objectTypeID
+               ));
+               $pageCount = ceil($statement->fetchColumn() / 20);
+               
+               $sql = "SELECT          userID, likeValue
+                       FROM            wcf".WCF_N."_like
+                       WHERE           objectID = ?
+                                       AND objectTypeID = ?
+                       ORDER BY        likeValue DESC, time DESC";
+               $statement = WCF::getDB()->prepareStatement($sql, 20, ($this->parameters['pageNo'] - 1) * 20);
+               $statement->execute(array(
+                       $this->parameters['data']['objectID'],
+                       $this->objectType->objectTypeID
+               ));
+               $data = array(
+                       Like::LIKE => array(),
+                       Like::DISLIKE => array()
+               );
+               while ($row = $statement->fetchArray()) {
+                       $data[$row['likeValue']][] = $row['userID'];
+               }
+               
+               $values = array();
+               if (!empty($data[Like::LIKE])) {
+                       $values[Like::LIKE] = new GroupedUserList(WCF::getLanguage()->get('wcf.like.details.like'));
+                       $values[Like::LIKE]->addUserIDs($data[Like::LIKE]);
+               }
+               if (!empty($data[Like::DISLIKE])) {
+                       $values[Like::DISLIKE] = new GroupedUserList(WCF::getLanguage()->get('wcf.like.details.dislike'));
+                       $values[Like::DISLIKE]->addUserIDs($data[Like::DISLIKE]);
+               }
+               
+               // load user profiles
+               GroupedUserList::loadUsers();
+               
+               WCF::getTPL()->assign(array(
+                       'groupedUsers' => $values
+               ));
+               
+               return array(
+                       'containerID' => $this->parameters['data']['containerID'],
+                       'pageCount' => $pageCount,
+                       'template' => WCF::getTPL()->fetch('groupedUserList')
+               );
+       }
 }