Beispiel #1
0
 /**
  * Get a count or list of following
  *
  * @param   array   $filters  Filters to apply to the query that retrieves records
  * @param   string  $what     Following what? A collection or a member, etc.
  * @return  mixed   Integer or object
  */
 public function following($filters = array(), $what = 'all')
 {
     $filters['follower_id'] = $this->_object_id;
     $filters['follower_type'] = $this->_object_type;
     if (isset($filters['count']) && $filters['count']) {
         $tbl = new Tables\Following($this->_db);
         return $tbl->count($filters);
     }
     if ($what == 'first') {
         $filters['limit'] = 1;
     }
     if (!$this->_following instanceof ItemList) {
         $tbl = new Tables\Following($this->_db);
         if ($results = $tbl->find($filters)) {
             // Loop through all the items and push assets and tags
             foreach ($results as $key => $result) {
                 $results[$key] = new Following($result);
             }
         }
         $this->_following = new ItemList($results);
     }
     if ($what == 'collections') {
         $ids = array();
         $members = array();
         $groups = array();
         foreach ($this->_following as $following) {
             if ($following->get('following_type') == 'collection') {
                 $ids[] = $following->get('following_id');
             } else {
                 if ($following->get('following_type') == 'member') {
                     $members[] = $following->get('following_id');
                 } else {
                     if ($following->get('following_type') == 'group') {
                         $groups[] = $following->get('following_id');
                     }
                 }
             }
         }
         if (count($members) > 0 || count($groups) > 0) {
             if (count($members) > 0) {
                 $query1 = "SELECT id FROM `#__collections` WHERE object_id IN (" . implode(',', $members) . ") AND object_type='member'";
             }
             if (count($groups) > 0) {
                 $query2 = "SELECT id FROM `#__collections` WHERE object_id IN (" . implode(',', $groups) . ") AND object_type='group'";
             }
             if (count($members) > 0 && count($groups) > 0) {
                 $query = "( {$query1} ) UNION ( {$query2} );";
             } else {
                 if (count($members) > 0) {
                     $query = $query1;
                 } else {
                     if (count($groups) > 0) {
                         $query = $query2;
                     }
                 }
             }
             $this->_db->setQuery($query);
             $ids = array_merge($ids, $this->_db->loadColumn());
         }
         return $ids;
     }
     return $this->_following;
 }
 /**
  * Get a count of data associated with this collection
  *
  * @param   string $what What to count
  * @return  integer
  */
 public function count($what = '')
 {
     if (!isset($this->_counts) || !is_array($this->_counts)) {
         $this->_counts = array();
     }
     $what = strtolower(trim($what));
     switch ($what) {
         case 'collection':
         case 'image':
         case 'text':
         case 'file':
         case 'link':
             if (isset($this->_counts[$what])) {
                 return (int) $this->_counts[$what];
             } else {
                 return 0;
             }
             break;
         case 'followers':
             if (!isset($this->_counts[$what])) {
                 $tbl = new Tables\Following($this->_db);
                 $this->_counts[$what] = $tbl->count(array('following_type' => 'collection', 'following_id' => $this->get('id')));
             }
             return $this->_counts[$what];
             break;
         case 'likes':
         case 'like':
         case 'votes':
         case 'vote':
             if ($this->get('likes', null) == null) {
                 $tbl = new Tables\Item($this->_db);
                 $this->set('likes', $tbl->getLikes(array('object_type' => 'collection', 'object_id' => $this->get('id'))));
             }
             return (int) $this->get('likes', 0);
             break;
         case 'reposts':
         case 'repost':
             if ($this->get('reposts', null) == null) {
                 $tbl = new Tables\Item($this->_db);
                 $this->set('reposts', $tbl->getReposts(array('object_type' => 'collection', 'object_id' => $this->get('id'))));
             }
             return (int) $this->get('reposts', 0);
             break;
         case 'posts':
         case 'post':
             if ($this->get('posts', null) == null) {
                 $this->set('posts', $this->posts(array('count' => true)));
             }
             return (int) $this->get('posts', 0);
             break;
         default:
             return 0;
             break;
     }
 }