Exemplo n.º 1
0
 public function getActivityAbout(Core_Model_Item_Abstract $about, User_Model_User $user, array $params = array())
 {
     // Proc args
     extract($this->_getInfo($params));
     // action_id, limit, min_id, max_id, actionFilter, filterValue
     // Prepare main query
     $streamTable = Engine_Api::_()->getDbtable('stream', 'activity');
     $streamName = $streamTable->info('name');
     $actionTableName = $this->info('name');
     $db = $streamTable->getAdapter();
     $union = new Zend_Db_Select($db);
     // Prepare action types
     $masterActionTypes = Engine_Api::_()->getDbtable('actionTypes', 'activity')->getActionTypes();
     $subjectActionTypes = array();
     $objectActionTypes = array();
     // Filter types based on displayable
     foreach ($masterActionTypes as $type) {
         if ($about->getType() == 'event' && Engine_Api::_()->hasItemType('event') || $about->getType() == 'group' && Engine_Api::_()->hasItemType('group') || $about->getType() == 'ynbusinesspages_business' && Engine_Api::_()->hasItemType('ynbusinesspages_business')) {
             if ($actionFilter == 'owner' && isset($type->is_object_thumb) && !$type->is_object_thumb) {
                 continue;
             }
             if ($actionFilter == 'membership' && isset($type->is_object_thumb) && $type->is_object_thumb) {
                 continue;
             }
         }
         if ($type->displayable & 1) {
             $subjectActionTypes[] = $type->type;
         }
         if ($type->displayable & 2) {
             $objectActionTypes[] = $type->type;
         }
     }
     // Filter types based on user request
     if (isset($showTypes) && is_array($showTypes) && !empty($showTypes)) {
         $subjectActionTypes = array_intersect($subjectActionTypes, $showTypes);
         $objectActionTypes = array_intersect($objectActionTypes, $showTypes);
     } else {
         if (isset($hideTypes) && is_array($hideTypes) && !empty($hideTypes)) {
             $subjectActionTypes = array_diff($subjectActionTypes, $hideTypes);
             $objectActionTypes = array_diff($objectActionTypes, $hideTypes);
         }
     }
     // Nothing to show
     if (empty($subjectActionTypes) && empty($objectActionTypes)) {
         return null;
     }
     if (empty($subjectActionTypes)) {
         $subjectActionTypes = null;
     } else {
         if (count($subjectActionTypes) == count($masterActionTypes)) {
             $subjectActionTypes = true;
         } else {
             $subjectActionTypes = "'" . join("', '", $subjectActionTypes) . "'";
         }
     }
     if (empty($objectActionTypes)) {
         $objectActionTypes = null;
     } else {
         if (count($objectActionTypes) == count($masterActionTypes)) {
             $objectActionTypes = true;
         } else {
             $objectActionTypes = "'" . join("', '", $objectActionTypes) . "'";
         }
     }
     // Prepare sub queries
     $event = Engine_Hooks_Dispatcher::getInstance()->callEvent('getActivity', array('for' => $user, 'about' => $about));
     $responses = (array) $event->getResponses();
     if (empty($responses)) {
         return null;
     }
     $friendsFlage = false;
     $action_ids = array();
     // Filter by hashtag
     if ($actionFilter == 'hashtag' && !empty($filterValue)) {
         $action_ids = Engine_Api::_()->getDbtable('hashtags', 'ynfeed')->getHashtagFeeds($filterValue, array(), array('limit' => $limit, 'max_id' => $max_id));
         if (empty($action_ids)) {
             return null;
         }
     }
     // Filter by login as business post
     if ($actionFilter == 'business') {
         $select = $this->select()->where('`subject_type` = ?', 'ynbusinesspages_business')->where('subject_id', $about->getIdentity())->limit($limit);
         if (null !== $max_id) {
             $select->where('action_id <= ?', $max_id);
         }
         $data = $select->query()->fetchAll();
         foreach ($data as $row) {
             $action_ids[] = $row['action_id'];
         }
         if (empty($action_ids)) {
             return null;
         }
     }
     $member_ids = array();
     if ($actionFilter == 'owner') {
         if ($about instanceof User_Model_User) {
             $member_ids[] = $about->getIdentity();
         } elseif ($about instanceof Group_Model_Group || $about instanceof Advgroup_Model_Group) {
             $objectParent = $about->getParent('user');
             if ($objectParent instanceof User_Model_User) {
                 $member_ids[] = $objectParent->getIdentity();
             }
         } else {
             $objectParent = $about->getParent('user');
             if ($objectParent instanceof User_Model_User) {
                 $member_ids[] = $objectParent->getIdentity();
             }
         }
     } elseif ($actionFilter == 'officers') {
         if ($about instanceof Group_Model_Group || $about instanceof Advgroup_Model_Group) {
             $objectParent = $about->getParent('user');
             if ($objectParent instanceof User_Model_User) {
                 $member_ids[] = $objectParent->getIdentity();
             }
             foreach ($about->getOfficerList()->getAll() as $value) {
                 $member_ids[] = $value->child_id;
             }
         }
     } elseif ($actionFilter == 'admins') {
         if ($about instanceof Ynbusinesspages_Model_Business) {
             $objectParent = $about->getParent('user');
             if ($objectParent instanceof User_Model_User) {
                 $member_ids[] = $objectParent->getIdentity();
             }
             foreach ($about->getAdminList()->getAll() as $value) {
                 $member_ids[] = $value->child_id;
             }
         }
     } elseif ($actionFilter == 'membership') {
         if (in_array($about->getType(), array('event', 'group', 'ynbusinesspages_business'))) {
             $members = $about->membership()->getMembers(true);
             foreach ($members as $member) {
                 $member_ids[] = $member->getIdentity();
             }
         } else {
             $member_ids = $user->membership()->getMembershipsOfIds();
         }
         if (empty($member_ids)) {
             return array();
         }
     }
     foreach ($responses as $response) {
         if (empty($response)) {
             continue;
         }
         // Target info
         $select = $streamTable->select()->from($streamTable->info('name'), 'action_id')->where('target_type = ?', $response['type']);
         if (empty($response['data'])) {
             // Simple
             $select->where('target_id = ?', 0);
         } else {
             if (is_scalar($response['data']) || count($response['data']) === 1) {
                 // Single
                 if (is_array($response['data'])) {
                     list($response['data']) = $response['data'];
                 }
                 $select->where('target_id = ?', $response['data']);
             } else {
                 if (is_array($response['data'])) {
                     // Array
                     $select->where('target_id IN(?)', (array) $response['data']);
                 } else {
                     // Unknown
                     continue;
                 }
             }
         }
         // Add action_id/max_id/min_id
         if (null !== $action_id) {
             $select->where('action_id = ?', $action_id);
         } else {
             if (null !== $min_id) {
                 $select->where('action_id >= ?', $min_id);
             } else {
                 if (null !== $max_id) {
                     $select->where('action_id <= ?', $max_id);
                 }
             }
         }
         // Add order/limit
         $select->order('action_id DESC')->limit($limit);
         if (!empty($action_ids)) {
             $select->where($streamName . '.action_id IN(?)', (array) $action_ids);
         }
         // Add subject to main query
         $selectSubject = clone $select;
         if ($subjectActionTypes !== null) {
             if ($subjectActionTypes !== true) {
                 $selectSubject->where('type IN(' . $subjectActionTypes . ')');
             }
             $selectSubject->where('subject_type = ?', $about->getType())->where('subject_id = ?', $about->getIdentity());
             if (!empty($member_ids)) {
                 $selectSubject->where('object_type = ?', 'user')->where('object_id  IN (?)', (array) $member_ids);
             }
             $union->union(array('(' . $selectSubject->__toString() . ')'));
         }
         // Add object to main query
         $selectObject = clone $select;
         if ($objectActionTypes !== null) {
             if ($objectActionTypes !== true) {
                 $selectObject->where('type IN(' . $objectActionTypes . ')');
             }
             $selectObject->where('object_type = ?', $about->getType())->where('object_id = ?', $about->getIdentity());
             if (!empty($member_ids)) {
                 $selectObject->where('subject_type = ?', 'user')->where('subject_id IN (?)', (array) $member_ids);
             }
             $union->union(array('(' . $selectObject->__toString() . ')'));
             // (string) not work before PHP 5.2.0
         }
     }
     // Finish main query
     $union->order('action_id DESC')->limit($limit);
     // Get actions
     $actions = $db->fetchAll($union);
     // Process ids
     $ids = array();
     if (in_array($actionFilter, array('all', 'posts')) && $action_id) {
         $tag_ids = Engine_Api::_()->ynfeed()->getTaggedBaseActionIds($user, array('min' => $min_id, 'max' => $max_id));
         if (in_array($action_id, $tag_ids)) {
             $ids[] = $action_id;
         }
     }
     // No visible actions
     if (empty($actions) && empty($ids)) {
         return null;
     }
     // Process ids
     foreach ($actions as $data) {
         $ids[] = $data['action_id'];
     }
     $ids = array_unique($ids);
     // Finally get activity
     return $this->fetchAll($this->select()->where('action_id IN(' . join(',', $ids) . ')')->order('action_id DESC')->limit($limit));
 }