View file mooSocial v2.3.0 - PHP Social Network/upload/app/Controller/SearchController.php

File size: 18.39Kb
<?php

/**
 * mooSocial - The Web 2.0 Social Network Software
 * @website: http://www.moosocial.com
 */
class SearchController extends AppController {


    public function index($keyword = null, $plugin = null) {
        if (!isset($keyword)) {
            $this->redirect('/');
        } else {
            $keyword = urldecode($keyword);

            if ($plugin != null) { //result by filter
                $this->keyword = $keyword;
                $this->plugin = $plugin;
                $results = $this->getEventManager()->dispatch(new CakeEvent("Controller.Search.search", $this));
            } else { //all results
                $uid = $this->Auth->user('id');

                if (!Configure::read('core.guest_search') && empty($uid))
                    $this->_checkPermission();

                $this->loadModel('Friend');


                $this->loadModel('User');
                $params = array('User.active' => 1,
                    'MATCH(User.name) AGAINST(? IN BOOLEAN MODE)' => $keyword
                );

                $users = $this->User->getUsers(1, $params, 2);
                if (empty($users)) {
                    $users = $this->Friend->searchFriends($uid, $keyword);

                    unset($users['Friends']);
                    if (empty($users))
                        $users = $this->User->getAllUser($keyword, array(), 5, 1);
                    else {
                        $idList = Hash::extract($users, '{n}.User.id');
                        $users = array_merge($users, $this->User->getAllUser($keyword, $idList));
                    }
                }
                if(count($users) > 5)
                    $users = array_slice($users,0,5);
                $this->set('users', $users);
                $this->set('keyword', $keyword);
                $this->set('title_for_layout', $keyword);

                //other plugins
                $this->keyword = $keyword;
                $searches = $this->getEventManager()->dispatch(new CakeEvent('Controller.Search.search', $this));
                
                $this->set('searches', $searches->result);
            }
        }
    }

    //search suggestion
    public function suggestion($type = 'all', $searchValue = null) {
        $this->loadModel('User');
        if (!empty($type)) {
            if ($searchValue === null)
                $searchVal = $this->request->data['searchVal'];
            else
                $searchVal = $searchValue;
            $uid = $this->Auth->user('id');

            if ($type != 'all') { //result by filter
                $event = new CakeEvent('Controller.Search.suggestion', $this, array('type' => $type, 'searchVal' => $searchVal));
                $result = $this->getEventManager()->dispatch($event);
                $this->set('other_header', $result->result);
            } else { //all result
                $event = new CakeEvent('Controller.Search.suggestion', $this, array('type' => 'all', 'searchVal' => $searchVal));
                $result = $this->getEventManager()->dispatch($event);

                $this->set('other_suggestion', $result->result);
            }
            if ($type == 'all' || $type == 'user') {
                $page = (!empty($this->request->named['page'])) ? $this->request->named['page'] : 1;

                $this->loadModel('Friend');
                $params = array('User.active' => 1,
                    'MATCH(User.name) AGAINST(? IN BOOLEAN MODE)' => $searchVal
                );
                $users = $this->User->getUsers($page, $params);
                if (empty($users)) {
                    if($page == 1)
                    {
                        $users = $this->Friend->searchFriends($uid, $searchVal);
                        if (empty($users))
                            $users = $this->User->getAllUser($searchVal, array(), RESULTS_LIMIT, $page);
                        else {
                            $idList = Hash::extract($users, '{n}.User.id');
                            $users = array_merge($users, $this->User->getAllUser($searchVal, $idList,RESULTS_LIMIT,$page));
                        }
                    }
                    else{
                        $users_friend = $this->Friend->searchFriends($uid, $searchVal);
                        if(!empty($users_friend))
                        {
                            $idList = Hash::extract($users_friend, '{n}.User.id');
                            $users = $this->User->getAllUser($searchVal, $idList,RESULTS_LIMIT,$page);
                        }
                        else
                            $users = $this->User->getAllUser($searchVal, array(), RESULTS_LIMIT, $page);
                    }
                }
            }

            if ($type != 'all') {
                if ($type == 'user') {
                    $this->set('users', $users);
                    $this->set('result', 1);
                    $this->set('more_url', '/search/suggestion/user/' . $this->params['pass'][1] . '/page:' . ( $page + 1 ));
                    $this->set('element_list_path', "lists/users_list");
                    $this->set('page',$page);
                }
                $this->set('keyword', $searchValue);
                if (!empty($this->request->named['page']) && $this->request->named['page'] > 1) {
                    $this->set('more_link', true);
                }

                $this->set('type', $type);
                $this->render('/Search/suggestion_filter');
            } else {
                if (count($users) > 2) {
                    $users = array_slice($users, 0, 2);
                }
                $this->set('users', $users);
            }
            $this->set('searchVal', $searchVal);
        }
    }

    public function hashtags($keyword = null,$type = 'all', $filter = null){
        $this->loadModel('Activity');
        $this->loadModel('ActivityComment');
        $this->loadModel('Comment');
        $this->loadModel('Hashtag');
        //$keyword = strtolower($keyword);
        $keyword1 = strtolower($keyword);
        if (!empty($type))
        {
            $search_keyword = str_replace('_','',$keyword1);
            $uid = MooCore::getInstance()->getViewer(true);
            $items = $this->Hashtag->find('all',array(
                'conditions' => array(
                    'hashtags LIKE "%'.$search_keyword.'%"'
                )
            ));
            foreach($items as $key => &$item)
            {
                $aHash = explode(',',$item['Hashtag']['hashtags']);
                if(!in_array($search_keyword,$aHash))
                    unset($items[$key]);
            }
            if(!empty($items))
                $item_groups = Hash::combine($items,'{n}.Hashtag.id','{n}.Hashtag.item_id','{n}.Hashtag.item_table');
            else
                $item_groups = '';
            
            if ($type != 'all') { //result by filter
                if ($filter){
                    $event = new CakeEvent('Controller.Search.hashtags_filter', $this, array('search_keyword' => $search_keyword, 'type' => $type, 'item_ids' => !empty($item_groups[$type]) ? $item_groups[$type] : '' ) );
                }
                else {
                    $event = new CakeEvent('Controller.Search.hashtags', $this, array('search_keyword' => $search_keyword, 'type' => $type, 'item_ids' => !empty($item_groups[$type]) ? $item_groups[$type] : '' ) );
                }
                
                $result = $this->getEventManager()->dispatch($event);
                $this->set('other_suggestion', $result->result);
            } else { //all result
                if ($filter){
                    $event = new CakeEvent('Controller.Search.hashtags_filter', $this, array('search_keyword' => $search_keyword, 'type' => 'all', 'item_groups' => $item_groups));
                }
                else {
                    $event = new CakeEvent('Controller.Search.hashtags', $this, array('search_keyword' => $search_keyword, 'type' => 'all', 'item_groups' => $item_groups));
                }
                
                $result = $this->getEventManager()->dispatch($event);

                $this->set('other_suggestion', $result->result);
            }

            $page = (!empty($this->request->named['page'])) ? $this->request->named['page'] : 1;
            if ($type == 'all' || $type == 'activities')
            {
                $item_ids = !empty($item_groups['activities']) ? $item_groups['activities'] : array();

                $activities = $this->Activity->getActivityHashtags($item_ids, RESULTS_LIMIT,$page);
                
                // get activity likes
                if (!empty($uid)) {
                    $this->loadModel('Like');

                    $activity_likes = $this->Like->getActivityLikes($activities, $uid);
                    $this->set('activity_likes', $activity_likes);
                }

                if ($type == 'activities')
                {
                    $this->set('activities', $activities);
                    $this->set('result', 1);
                    $this->set('more_url', '/search/hashtags/' . $this->params['pass'][0] . '/activities/page:' . ( $page + 1 ));
                    $this->set('element_list_path', "activities");
                    $this->set('page',$page);
                }
                else
                {
                    if (count($activities) > 5) {
                        $activities = array_slice($activities, 0, 5);
                    }
                    $this->set('activities', $activities);
                }
            }
            if ($type == 'all' || $type == 'comments')
            {
                $item_ids = !empty($item_groups['comments']) ? $item_groups['comments'] : '';
                $comments = $this->Comment->getCommentHashtags($item_ids, RESULTS_LIMIT,$page);

                $activity_comment_ids = !empty($item_groups['activity_comments']) ? $item_groups['activity_comments'] : '';
                $activity_comments = $this->ActivityComment->getActivityCommentHashtags($activity_comment_ids, RESULTS_LIMIT,$page);

                foreach($comments as $key => &$comment)
                {
                    $aCore = array('Photo_Photo','Photo_Album','Blog_Blog','Topic_Topic','Event_Event','Group_Group','Video_Video');
                    if(in_array($comment['Comment']['type'],$aCore))
                    {
                        list($plugin,$controller) = explode('_',$comment['Comment']['type']);
                        
                        $comment['Comment']['view_link'] = $this->request->base.'/'.lcfirst($controller).'s/view/'.$comment['Comment']['target_id'];
                    }
                    else
                    {
                        $result = $this->getEventManager()->dispatch(new CakeEvent("Controller.Search.setCommentViewLink", $this));
                        $comment['Comment']['view_link'] = $result->result['view_link'].$comment['Comment']['target_id'];
                    }
                }

                if ($type == 'comments')
                {
                    $this->set('comments', $comments);
                    $this->set('activity_comments', $activity_comments);
                    $this->set('result', 1);
                    $this->set('more_url', '/search/hashtags/' . $this->params['pass'][0] . '/comments/page:' . ( $page + 1 ));
                    $this->set('element_list_path', "lists/comments_list");
                    $this->set('page',$page);
                }
                else
                {
                    if (count($comments) > 5) {
                        $comments = array_slice($comments, 0, 5);
                        $this->set('comments', $comments);
                        $this->set('activity_comments', array());
                    }else{
                        $activity_comment_count = 5 - count($comments);
                        $activity_comments = array_slice($activity_comments, 0, $activity_comment_count);
                        $this->set('comments', $comments);
                        $this->set('activity_comments', $activity_comments);
                    }

                }
            }

            if ($type != 'all') {
                if ($page > 1) {
                    $this->set('more_link', true);
                }
            }
            $this->set('filter',$filter);
            $this->set('type', $type);
            $this->set('keyword', $keyword);
            if(isset($this->request->named['tabs']))
                $this->set('tabs',$this->request->named['tabs']);
        }
    }
    
    public function hashtags_filter($keyword = null,$type = 'all'){
        $this->loadModel('Activity');
        $this->loadModel('ActivityComment');
        $this->loadModel('Comment');
        $this->loadModel('Hashtag');
        if (!empty($type))
        {
            $search_keyword = str_replace('_','',$keyword);
            $uid = MooCore::getInstance()->getViewer(true);
            $items = $this->Hashtag->find('all',array(
                'conditions' => array(
                    'hashtags LIKE "%'.$search_keyword.'%"'
                )
            ));
            foreach($items as $key => &$item)
            {
                $aHash = explode(',',$item['Hashtag']['hashtags']);
                if(!in_array($search_keyword,$aHash))
                    unset($items[$key]);
            }
            if(!empty($items))
                $item_groups = Hash::combine($items,'{n}.Hashtag.id','{n}.Hashtag.item_id','{n}.Hashtag.item_table');
            else
                $item_groups = '';
            
            if ($type != 'all') { //result by filter
                $event = new CakeEvent('Controller.Search.hashtags', $this, array('search_keyword' => $search_keyword, 'type' => $type, 'item_ids' => !empty($item_groups[$type]) ? $item_groups[$type] : '' ) );
                $result = $this->getEventManager()->dispatch($event);
                $this->set('other_header', $result->result);
            } else { //all result
                $event = new CakeEvent('Controller.Search.hashtags', $this, array('search_keyword' => $search_keyword, 'type' => 'all', 'item_groups' => $item_groups));
                $result = $this->getEventManager()->dispatch($event);
                
                $this->set('other_suggestion', $result->result);
            }

            $page = (!empty($this->request->named['page'])) ? $this->request->named['page'] : 1;
            if ($type == 'all' || $type == 'activities')
            {
                $item_ids = !empty($item_groups['activities']) ? $item_groups['activities'] : array();

                $activities = $this->Activity->getActivityHashtags($item_ids, RESULTS_LIMIT,$page);

                // get activity likes
                if (!empty($uid)) {
                    $this->loadModel('Like');

                    $activity_likes = $this->Like->getActivityLikes($activities, $uid);
                    $this->set('activity_likes', $activity_likes);
                }

                if ($type == 'activities')
                {
                    $this->set('activities', $activities);
                    $this->set('result', 1);
                    $this->set('more_url', '/search/hashtags/' . $this->params['pass'][0] . '/activities/page:' . ( $page + 1 ));
                    $this->set('element_list_path', "activities");
                    $this->set('page',$page);
                }
                else
                {
                    if (count($activities) > 5) {
                        $activities = array_slice($activities, 0, 5);
                    }
                    $this->set('activities', $activities);
                }
            }
            if ($type == 'all' || $type == 'comments')
            {
                $item_ids = !empty($item_groups['comments']) ? $item_groups['comments'] : '';
                $comments = $this->Comment->getCommentHashtags($item_ids, RESULTS_LIMIT,$page);

                $activity_comment_ids = !empty($item_groups['activity_comments']) ? $item_groups['activity_comments'] : '';
                $activity_comments = $this->ActivityComment->getActivityCommentHashtags($activity_comment_ids, RESULTS_LIMIT,$page);

                foreach($comments as $key => &$comment)
                {
                    $aCore = array('Photo_Photo','Photo_Album','Blog_Blog','Topic_Topic','Event_Event','Group_Group','Video_Video');
                    if(in_array($comment['Comment']['type'],$aCore))
                    {
                        list($plugin,$controller) = explode('_',$comment['Comment']['type']);
                        
                        $comment['Comment']['view_link'] = $this->request->base.'/'.lcfirst($controller).'s/view/'.$comment['Comment']['target_id'];
                    }
                    else
                    {
                        $result = $this->getEventManager()->dispatch(new CakeEvent("Controller.Search.setCommentViewLink", $this));
                        $comment['Comment']['view_link'] = $result->result['view_link'].$comment['Comment']['target_id'];
                    }
                }

                if ($type == 'comments')
                {
                    $this->set('comments', $comments);
                    $this->set('activity_comments', $activity_comments);
                    $this->set('result', 1);
                    $this->set('more_url', '/search/hashtags/' . $this->params['pass'][0] . '/comments/page:' . ( $page + 1 ));
                    $this->set('element_list_path', "lists/comments_list");
                    $this->set('page',$page);
                }
                else
                {
                    if (count($comments) > 5) {
                        $comments = array_slice($comments, 0, 5);
                        $this->set('comments', $comments);
                        $this->set('activity_comments', array());
                    }else{
                        $activity_comment_count = 5 - count($comments);
                        $activity_comments = array_slice($activity_comments, 0, $activity_comment_count);
                        $this->set('comments', $comments);
                        $this->set('activity_comments', $activity_comments);
                    }

                }
            }

            if ($type != 'all') {
                if ($page > 1) {
                    $this->set('more_link', true);
                }
            }
            $this->set('type', $type);
            $this->set('keyword', $keyword);
            if(isset($this->request->named['tabs']))
                $this->set('tabs',$this->request->named['tabs']);
        }
    }
}