<?php

/**
 * @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
 * @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
 * @author iJoomla.com <webmaster@ijoomla.com>
 * @url https://www.jomsocial.com/license-agreement
 * The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
 * More info at https://www.jomsocial.com/license-agreement
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Uri\Uri;

// no direct access
defined('_JEXEC') or die('Restricted access');

if (!class_exists('CommunityViewVideos')) {

    class CommunityViewVideos extends CommunityView
    {

        var $_videoLib = null;
        var $model = '';

        public function __construct()
        {
            $this->model = CFactory::getModel('videos');
            $this->videoLib = new CVideoLibrary();
        }

        private function _getVideosHTML($videos, $pagination = null)
        {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $videoEntries = array();

            if ($videos) {
                foreach ($videos as $videoEntry) {
                    $video = Table::getInstance('Video', 'CTable');
                    $video->bind($videoEntry);
                    $videoEntries[] = $video;
                }
            }

            $my = CFactory::getUser();
            $user = CFactory::getUser($jinput->getInt('userid', $my->id));

            // for featured/unfeatured link
            $featured = new CFeatured(FEATURED_VIDEOS);
            $featuredVideos = $featured->getItemIds();
            $featuredList = array();

            foreach ($featuredVideos as $videoId) {
                $featuredList[] = $videoId;
            }

            $allowManageVideos = true;
            $pageVideo = false;
            $pageId = $jinput->get->get('pageid', '', 'INT');

            $groupVideo = false;
            $groupId = $jinput->get->get('groupid', '', 'INT');

            $task = $jinput->get->get('task', '', 'WORD');
            $redirectUrl = CRoute::getURI(false);

            if (!empty($pageId)) {
                $allowManageVideos = CPageHelper::allowManageVideo($pageId);
                $pageVideo = true;
            } elseif (!empty($groupId)) {
                $allowManageVideos = CGroupHelper::allowManageVideo($groupId);
                $groupVideo = true;
            }

            $config = CFactory::getConfig();
            $tmpl = new CTemplate();
            return $tmpl->set('sort', $jinput->get('sort', 'latest', 'STRING'))
                ->set('currentTask', $jinput->get('task', ''))
                ->set('videos', $videoEntries)
                ->set('videoThumbWidth', CVideoLibrary::thumbSize('width'))
                ->set('videoThumbHeight', CVideoLibrary::thumbSize('height'))
                ->set('redirectUrl', $redirectUrl)
                ->set('my', $my)
                ->set('user', $user)
                ->set('featuredList', $featuredList)
                ->set('isCommunityAdmin', COwnerHelper::isCommunityAdmin())
                ->set('allowManageVideos', $allowManageVideos)
                ->set('pageVideo', $pageVideo)
                ->set('groupVideo', $groupVideo)
                ->set('pagination', $pagination)
                ->set('showFeatured', $config->get('show_featured'))
                ->fetch('videos/list');
        }

        /**
         *    Get Featured Videos
         *
         * @return        array    Objects of random featured videos
         * @since        1.5
         */
        private function _getFeatVideos()
        {
            $featured = new CFeatured(FEATURED_VIDEOS);
            $featuredVideos = $featured->getItemIds();
            $featuredList = array();

            foreach ($featuredVideos as $videoId) {
                $table = Table::getInstance('Video', 'CTable');
                $table->load($videoId);

                if (empty($table->id)) {
                    continue;
                }

                $featuredList[] = $table;
            }

            return $featuredList;
        }

        /**
         * Return sort options for videos
         */
        private function _getSortOptions()
        {
            $sortItems = array
            (
                'latest' => Text::_('COM_COMMUNITY_VIDEOS_SORT_LATEST'),
                'mostwalls' => Text::_('COM_COMMUNITY_VIDEOS_SORT_MOST_WALL_POST'),
                'mostviews' => Text::_('COM_COMMUNITY_VIDEOS_SORT_POPULAR'),
                'featured' => Text::_('COM_COMMUNITY_VIDEOS_SORT_FEATURED'),
                'title' => Text::_('COM_COMMUNITY_VIDEOS_SORT_TITLE')
            );

            return $sortItems;
        }

        /**
         *    Generate Featured Videos HTML
         *
         * @param        array    Array of video objects
         * @return        string    HTML
         * @since        1.2
         */
        private function _getFeatHTML($videos)
        {
            $config = CFactory::getConfig();
            $tmpl = new CTemplate();
            return $tmpl->set('videos', $videos)
                ->set('showFeatured', $config->get('show_featured'))
                ->set('isCommunityAdmin', COwnerHelper::isCommunityAdmin())
                ->set('videoThumbWidth', CVideoLibrary::thumbSize('width'))
                ->set('videoThumbHeight', CVideoLibrary::thumbSize('height'))
                ->fetch('videos.featured');
        }

        /**
         * Display all videos in the whole system
         * */
        public function display($id = null)
        {
            $mainframe = Factory::getApplication();
            $config = CFactory::getConfig();
            $jinput = $mainframe->input;
            $document = Factory::getDocument();
            $my = CFactory::getUser();
            $model = CFactory::getModel('videos');
            $tmpl = new CTemplate();

            // Get category id from the query string if there are any.
            $categoryId = $jinput->get('catid', 0, 'INT');
            $category = Table::getInstance('VideosCategory', 'CTable');
            $category->load($categoryId);
            
            $pageId = $jinput->get('pageid', 0, 'INT');
            $groupId = $jinput->get('groupid', 0, 'INT');
            $eventId = $jinput->get('eventid', 0, 'INT');

            if ($pageId) {
                if (!$config->get('enablepages')) {
                    echo Text::_('COM_COMMUNITY_PAGES_DISABLE');
                    return;
                }

                $page = Table::getInstance('Page', 'CTable');
                $page->load($pageId);

                // @rule: Test if the page is unpublished, don't display it at all.
                if (!$page->published) {
                    $this->_redirectUnpublishPage();
                    return;
                }

                $params = new CParameter($page->params);
                if ($params->get('videopermission', 0) <= 0) {
                    $mainframe->redirect(CRoute::_('index.php?option=com_community&view=pages&task=viewpage&pageid='.$pageId, false));
                }

                // Set pathway for page videos
                // Community > Pages > Page Name > Videos
                $this->addPathway(
                    Text::_('COM_COMMUNITY_PAGES'),
                    CRoute::_('index.php?option=com_community&view=pages')
                );
                $this->addPathway(
                    $page->name,
                    CRoute::_('index.php?option=com_community&view=pages&task=viewpage&pageid=' . $pageId)
                );
            } elseif ($groupId) {
                if (!$config->get('enablegroups')) {
                    echo Text::_('COM_COMMUNITY_GROUPS_DISABLE');
                    return;
                }

                $group = Table::getInstance('Group', 'CTable');
                $group->load($groupId);

                // @rule: Test if the group is unpublished, don't display it at all.
                if (!$group->published) {
                    $this->_redirectUnpublishGroup();
                    return;
                }

                $params = new CParameter($group->params);
                if ($params->get('videopermission', 0) <= 0) {
                    $mainframe->redirect(CRoute::_('index.php?option=com_community&view=groups&task=viewgroup&groupid='.$groupId, false));
                }

                // Set pathway for group videos
                // Community > Groups > Group Name > Videos
                $this->addPathway(
                    Text::_('COM_COMMUNITY_GROUPS'),
                    CRoute::_('index.php?option=com_community&view=groups')
                );
                $this->addPathway(
                    $group->name,
                    CRoute::_('index.php?option=com_community&view=groups&task=viewgroup&groupid=' . $groupId)
                );
            } elseif ($eventId){
                if (!$config->get('enableevents')) {
                    echo Text::_('COM_COMMUNITY_EVENTS_DISABLED');
                    return;
                }

                $event = Table::getInstance('Event', 'CTable');
                $event->load($eventId);

                $params = new CParameter($event->params);
                if ($params->get('videopermission', 0) <= 0) {
                    $mainframe->redirect(CRoute::_('index.php?option=com_community&view=events&task=viewevent&eventid='.$eventId, false));
                }
            }

            $this->_addSubmenu();

            // If we are browing by category, add additional breadcrumb and add
            // category name in the page title
            if ($categoryId != null) {
                if (!empty($pageId)) {
                    $this->addPathway(
                        Text::_('COM_COMMUNITY_VIDEOS'),
                        CRoute::_('index.php?option=com_community&view=videos&pageid=' . $pageId)
                    );
                } elseif (!empty($groupId)) {
                    $this->addPathway(
                        Text::_('COM_COMMUNITY_VIDEOS'),
                        CRoute::_('index.php?option=com_community&view=videos&groupid=' . $groupId)
                    );
                } else {
                    $this->addPathway(
                        Text::_('COM_COMMUNITY_VIDEOS'),
                        CRoute::_('index.php?option=com_community&view=videos')
                    );
                }

                $this->addPathway(Text::_($this->escape($category->name)), '');
                /**
                 * Opengraph
                 */
                CHeadHelper::setType(
                    'website',
                    Text::_('COM_COMMUNITY_VIDEOS_CATEGORIES') . ' : ' . str_replace(
                        '&amp;',
                        '&',
                        Text::_($this->escape($category->name))
                    )
                );
            } else {
                $this->addPathway(Text::_('COM_COMMUNITY_VIDEOS'));
                /**
                 * Opengraph
                 */
                CHeadHelper::setType('website', Text::_('COM_COMMUNITY_VIDEOS_BROWSE_ALL_VIDEOS'));

                if(!empty($groupId)){
                    /**
                     * Opengraph
                     */
                    CHeadHelper::setType('website', Text::sprintf('COM_COMMUNITY_GROUPS_VIDEO_LISTING',$group->name));
                } else if(!empty($pageId)){
                    /**
                     * Opengraph
                     */
                    CHeadHelper::setType('website', Text::sprintf('COM_COMMUNITY_PAGES_VIDEO_LISTING',$page->name));
                }
            }

            $groupLink = !empty($groupId) ? '&groupid=' . $groupId : '';
            $feedLink = CRoute::_('index.php?option=com_community&view=videos' . $groupLink . '&format=feed');
            $feed = '<link rel="alternate" type="application/rss+xml" title="' . Text::_(
                    'COM_COMMUNITY_SUBSCRIBE_ALL_VIDEOS_FEED'
                ) . '" href="' . $feedLink . '"/>';
            $document->addCustomTag($feed);

            // Featured Videos
            $featVideos = '';
            $featuredHTML = '';
            $sorted = $jinput->get('sort', 'latest', 'STRING');
            $limitstart = $jinput->get('limitstart', 0, 'INT');
            $permissions = ($my->id == 0) ? 10 : 20;
            // ACL
            $permissions = (CFactory::getUser()->authorise('community.videoeditstate', 'com_community') || CFactory::getUser()->authorise('community.videoedit', 'com_community') || CFactory::getUser()->authorise('community.videodelete', 'com_community')) ? 40 : $permissions;

            $cat_id = isset($category->id) ? $category->id : "";

            if ($pageId) {
                $isMember = $page->isMember($my->id);
                $isMine = ($my->id == $page->ownerid);
                $isBanned = $page->isBanned($my->id);

                if (!$isMember && !$isMine && !CFactory::getUser()->authorise('community.pageeditstate', 'com_community') && !CFactory::getUser()->authorise('community.pageedit', 'com_community') && !CFactory::getUser()->authorise('community.pagedelete', 'com_community') && $page->approvals == COMMUNITY_PRIVATE_PAGE
                ) {
                    $this->noAccess(Text::_('COM_COMMUNITY_PAGES_PRIVATE_NOTICE'));
                    return;
                }

                // cache video list.
                $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, null, null, $pageId);
                $videosHTML = $videosData['HTML'];

                $allVideosUrl = 'index.php?option=com_community&view=videos&pageid=' . $pageId;
                $catVideoUrl = 'index.php?option=com_community&view=videos&pageid=' . $pageId . '&catid=';
                $parentUrl = 'index.php?option=com_community&view=videos&pageid=' . $pageId;

                //check if there's any pending video
                $pendingVideos = $this->model->hasPendingVideos( $pageId , VIDEO_PAGE_TYPE );

                $tmpl->set('canCreate', $my->authorise('community.create', 'pages.videos.' . $pageId));
            }elseif ($groupId) {
                $isMember = $group->isMember($my->id);
                $isMine = ($my->id == $group->ownerid);
                $isBanned = $group->isBanned($my->id);

                if (!$isMember && !$isMine && !CFactory::getUser()->authorise('community.groupeditstate', 'com_community') && !CFactory::getUser()->authorise('community.groupedit', 'com_community') && !CFactory::getUser()->authorise('community.groupdelete', 'com_community') && $group->approvals == COMMUNITY_PRIVATE_GROUP
                ) {
                    $this->noAccess(Text::_('COM_COMMUNITY_GROUPS_PRIVATE_NOTICE'));
                    return;
                }

                // cache video list.
                $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, $groupId);
                $videosHTML = $videosData['HTML'];

                $allVideosUrl = 'index.php?option=com_community&view=videos&groupid=' . $groupId;
                $catVideoUrl = 'index.php?option=com_community&view=videos&groupid=' . $groupId . '&catid=';
                $parentUrl = 'index.php?option=com_community&view=videos&groupid=' . $groupId;

                //check if there's any pending video
                $pendingVideos = $this->model->hasPendingVideos( $groupId , VIDEO_GROUP_TYPE );

                $tmpl->set('canCreate', $my->authorise('community.create', 'groups.videos.' . $groupId));
            }elseif($eventId){
                //if this is a group event, check the access of the group
                if($event->type == 'group' && $event->contentid){
                    $group = Table::getInstance('Group', 'CTable');
                    $group->load($event->contentid);

                    $isMember = $group->isMember($my->id);
                    $isMine = ($my->id == $group->ownerid);

                    if (!$isMember && !$isMine && !COwnerHelper::isCommunityAdmin(
                        ) && $group->approvals == COMMUNITY_PRIVATE_GROUP
                    ) {
                        $this->noAccess(Text::_('COM_COMMUNITY_GROUPS_PRIVATE_NOTICE'));
                        return;
                    }
                } else if($event->type == 'page' && $event->contentid){
                    $page = Table::getInstance('Page', 'CTable');
                    $page->load($event->contentid);

                    $isMember = $page->isMember($my->id);
                    $isMine = ($my->id == $page->ownerid);

                    if (!$isMember && !$isMine && !COwnerHelper::isCommunityAdmin(
                        ) && $page->approvals == COMMUNITY_PRIVATE_PAGE
                    ) {
                        $this->noAccess(Text::_('COM_COMMUNITY_PAGES_PRIVATE_NOTICE'));
                        return;
                    }
                }

                // cache video list.
                $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, null, $eventId);
                $videosHTML = $videosData['HTML'];

                $allVideosUrl = 'index.php?option=com_community&view=videos&eventid=' . $eventId;
                $catVideoUrl = 'index.php?option=com_community&view=videos&eventid=' . $eventId . '&catid=';
                $parentUrl = 'index.php?option=com_community&view=videos&eventid=' . $eventId;

                //check if there's any pending video
                $pendingVideos = $this->model->hasPendingVideos( $eventId , VIDEOS_EVENT_TYPE );

                //$tmpl->set('canCreate', $my->authorise('community.create', 'groups.videos.' . $eventId));

            }else {
                $task = $jinput->request->get('task', '', 'WORD');
                
                if ($task == 'pagevideos') {
                    $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, null, null, null, false, false, true);

                    $allVideosUrl = 'index.php?option=com_community&view=videos&task=pagevideos';
                    $catVideoUrl = 'index.php?option=com_community&view=videos&task=pagevideos&catid=';
                    $parentUrl = 'index.php?option=com_community&view=videos&task=pagevideos';
                } else if ($task == 'groupvideos') {
                    $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, null, null, null, true);

                    $allVideosUrl = 'index.php?option=com_community&view=videos&task=groupvideos';
                    $catVideoUrl = 'index.php?option=com_community&view=videos&task=groupvideos&catid=';
                    $parentUrl = 'index.php?option=com_community&view=videos&task=groupvideos';
                } else if ($task == 'eventvideos') {

                    if (!$config->get('enableevents')) {
                        echo Text::_('COM_COMMUNITY_EVENTS_DISABLED');
                        return;
                    }
                    
                    $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted, null, null, null, false, true);

                    $allVideosUrl = 'index.php?option=com_community&view=videos&task=eventvideos';
                    $catVideoUrl = 'index.php?option=com_community&view=videos&task=eventvideos&catid=';
                    $parentUrl = 'index.php?option=com_community&view=videos&task=eventvideos';
                } else {
                    $videosData = $this->getVideosShowAllVideos($cat_id, $permissions, $sorted);

                    $allVideosUrl = 'index.php?option=com_community&view=videos';
                    $catVideoUrl = 'index.php?option=com_community&view=videos&catid=';
                    $parentUrl = 'index.php?option=com_community&view=videos';
                }

                // cache video list.
                $videosHTML = $videosData['HTML'];

                // Featured Videos
                // Cache featured video.
                // Hide featured videos if we're viewing inside a particular category
                if (empty($cat_id)) {
                    $featuredData = $this->_cachedCall(
                        'getVideosFeaturedList',
                        array(),
                        '',
                        array(COMMUNITY_CACHE_TAG_FEATURED)
                    );
                    $featuredHTML = $featuredData['HTML'];
                } else {
                    $featuredHTML = '';
                }

                $pendingVideos = $this->model->hasPendingVideos( $my->id , VIDEO_USER_TYPE );
            }

            //Cache for category
            //This is local file
           // $categories = $this->getVideosCategories($categoryId); // this will return the child only
            $categories = $this->getFullVideoCategories();
            $sortItems = $this->_getSortOptions();
            $featuredVideoUsers = $this->_getFeatVideos();

            $canSearch = 1;
            if($my->id == 0 && !$config->get('enableguestsearchvideos')) $canSearch = 0;

            $tmpl = new CTemplate();
            $my = CFactory::getUser();
            $tmpl->set('sort', $jinput->get('sort', 'latest', 'STRING'))
                ->set('currentTask', $jinput->get('task', ''))
                ->set('featuredHTML', $featuredHTML)
                ->set('categoryId', $categoryId)
                ->set('canSearch', $canSearch)
                ->set('pageId', (($pageId) ? $pageId : false) )
                ->set('groupId', (($groupId) ? $groupId : false) )
                ->set('eventId', (($eventId) ? $eventId : false) )
                ->set('videosHTML', $videosHTML)
                ->set('categories', $categories)
                ->set('category', $category)
                ->set('sortings', CFilterBar::getHTML(CRoute::getURI(), $sortItems, 'latest'))
                ->set('allVideosUrl', $allVideosUrl)
                ->set('catVideoUrl', $catVideoUrl)
                ->set('parentUrl', $parentUrl)
                ->set('featuredVideoUsers', $featuredVideoUsers)
                ->set('submenu', $this->showSubmenu(false))
                ->set('pendingVideos', $pendingVideos);

                if($pageId) {
                    $tmpl->set('canCreate', $my->authorise('community.create', 'pages.videos.' . $pageId));
                }elseif($groupId) {
                    $tmpl->set('canCreate', $my->authorise('community.create', 'groups.videos.' . $groupId));
                }elseif($eventId){
                    $tmpl->set('canCreate', $my->authorise('community.create', 'events.videos.' . $eventId));
                } else {
                    $tmpl->set('canCreate', $my->authorise('community.create', 'videos'));
                }

                echo $tmpl->fetch('videos/base');
        }

        public function getUserTotalVideos($userId)
        {
            $model = CFactory::getModel('videos');
            return count($model->getUserTotalVideos($userId));
        }

        /**
         * List All FEATURED VIDEO
         * */
        public function getVideosFeaturedList()
        {
            $featVideos = $this->_getFeatVideos();
            $featuredHTML['HTML'] = $this->_getFeatHTML($featVideos);
            return $featuredHTML;
        }

        /**
         * List All category
         * */
        public function getVideosCategories($categoryId)
        {
            $model = CFactory::getModel('videos');
            $categories = $model->getCategories($categoryId);
            return $categories;
        }

        /**
         * List all the category including the children and format it
         */
        public function getFullVideoCategories($id = 0, $level = 0, $categoryList = array()){
            $mainCategories = $this->getVideosCategories($id); // first level of video category

            if(count($mainCategories) > 0){
                foreach($mainCategories as $category){
                    $prefix = '';
                    for($i = 0; $i < $level; $i++){
                        $prefix = $prefix.'-'; // this will add the - in front of the category name
                    }

                    $category->name = $prefix.' '.Text::_($category->name);
                    $categoryList[] = $category;
                    $categoryList = $this->getFullVideoCategories($category->id, $level+1, $categoryList);
                }
            }

            return $categoryList;
        }

        /**
         * List All Videos
         * */
        public function getVideosShowAllVideos($category, $permissions, $sorted, $groupId = null, $eventId = null, $pageId = null, $groupsOnly = false, $eventsOnly = false, $pagesOnly = false)
        {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $my = CFactory::getUser();

            $model = CFactory::getModel('videos');
            $limitstart = $jinput->get('limitstart', 0, 'INT');

            // Get group in category and it's children.
            $categories = $model->getAllCategories();
            $categoryIds = CCategoryHelper::getCategoryChilds($categories, $category);

            $friendsVideos = true;

            if ((int)$category > 0) {
                $categoryIds[] = (int)$category;
                //$friendsVideos = false;
            }

            if($pageId) {
                $videos = $model->getPageVideos($pageId, $categoryIds);
            } elseif($groupId) {
                $videos = $model->getGroupVideos($groupId, $categoryIds);
            } elseif($eventId){
                $videos = $model->getEventVideos($eventId, $categoryIds);
            }else{
                $filters = array
                (
                    'status' => 'ready',
                    'category_id' => $categoryIds,
                    'permissions' => $permissions,
                    'or_group_privacy' => 0,
                    'or_page_privacy' => 0,
                    'sorting' => $sorted,
                    'limitstart' => $limitstart,
                    'friendsvideos' => $friendsVideos,
                    'groupsOnly' => $groupsOnly,
                    'eventsOnly' => $eventsOnly,
                    'pagesOnly' => $pagesOnly

                );
                $videos = $model->getVideos($filters);
            }

            $pagination = $model->getPagination();
            $videosHTML['HTML'] = $this->_getVideosHTML($videos, $pagination);

            return $videosHTML;
        }

        /**
         * Application full view
         * */
        public function appFullView()
        {
            $document = Factory::getDocument();
            /**
             * Opengraph
             */
            // CHeadHelper::setType('website', (Text::_('COM_COMMUNITY_VIDEOS_WALL_TITLE')));


            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;

            $applicationName = CStringHelper::strtolower($jinput->get->get('app', '', 'STRING'));

            if (empty($applicationName)) {
                Factory::getApplication()->enqueueMessage(Text::_('COM_COMMUNITY_APP_ID_REQUIRED'), 'error');
            }

            $output  = '<div class="joms-page">';
            $output .= '<h3 class="joms-page__title">' . Text::_('COM_COMMUNITY_VIDEOS_WALL_TITLE') . '</h3>';

            if ($applicationName == 'walls') {
                $limit = $jinput->request->get('limit', 5, 'INT');
                $limitstart = $jinput->request->get('limitstart', 0, 'INT');
                $videoId = $jinput->getInt('videoid', '');
                $my = CFactory::getUser();
                $config = CFactory::getConfig();

                $video = Table::getInstance('Video', 'CTable');
                $video->load($videoId);

                // Get the walls content
                $viewAllLink = false;
                $wallCount = false;
                if ($jinput->request->get('task', '') != 'app') {
                    $viewAllLink = CRoute::_(
                        'index.php?option=com_community&view=videos&task=app&videoid=' . $video->id . '&app=walls'
                    );
                    $wallCount = CWallLibrary::getWallCount('videos', $video->id);
                }

                $output .= CWallLibrary::getWallContents(
                    'videos',
                    $video->id,
                    (COwnerHelper::isCommunityAdmin() || COwnerHelper::isMine($my->id, $video->creator)),
                    $limit,
                    $limitstart
                );

                if (!$config->get('lockvideoswalls') || ($config->get('lockvideoswalls') && CFriendsHelper::isConnected(
                            $my->id,
                            $video->creator
                        )) || COwnerHelper::isCommunityAdmin()
                ) {
                    $output .= CWallLibrary::getWallInputForm(
                        $video->id,
                        'videos,ajaxSaveWall',
                        'videos,ajaxRemoveWall'
                    );
                }

                $output .= CWallLibrary::getViewAllLinkHTML($viewAllLink, $wallCount);


                jimport('joomla.html.pagination');
                $wallModel = CFactory::getModel('wall');
                $pagination = new JPagination($wallModel->getCount($video->id, 'videos'), $limitstart, $limit);

                $output .= '<div class="cPagination">' . $pagination->getPagesLinks() . '</div>';
            } else {
                $model = CFactory::getModel('apps');
                $applications = CAppPlugins::getInstance();
                $applicationId = $model->getUserApplicationId($applicationName);

                $application = $applications->get($applicationName, $applicationId);

                if (is_callable(array($application, 'onAppDisplay'), true)) {
                    // Get the parameters
                    $manifest = CPluginHelper::getPluginPath(
                            'community',
                            $applicationName
                        ) . '/' . $applicationName . '/' . $applicationName . '.xml';

                    $params = new CParameter($model->getUserAppParams($applicationId), $manifest);

                    $application->params = $params;
                    $application->id = $applicationId;

                    $output = $application->onAppDisplay($params);
                } else {
                    Factory::getApplication()->enqueueMessage(Text::_('COM_COMMUNITY_APPS_NOT_FOUND'), 'error');
                }
            }

            $output .= '</div>';
            echo $output;
        }

        /**
         * View to display the search form
         * */
        public function search()
        {
            $document = Factory::getDocument();
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $config = CFactory::getConfig();

            $this->addPathway(
                Text::_('COM_COMMUNITY_VIDEOS'),
                CRoute::_('index.php?option=com_community&view=videos')
            );
            $this->addPathway(Text::_('COM_COMMUNITY_VIDEOS_SEARCH_VIDEOS'), '');
            /**
             * Opengraph
             */
            CHeadHelper::setType('website', Text::_('COM_COMMUNITY_VIDEOS_SEARCH_VIDEOS'));


            $this->_addSubmenu();

            $search = $jinput->request->get('search-text', '', 'STRING');
            $result = array();
            $pagination = null;
            $total = 0;

            if (!empty($search)) {
                $searchModel = CFactory::getModel('Search');
                $result = $searchModel->searchVideo($search);
                $pagination = $searchModel->getPagination();
                $total = $searchModel->getTotal();
            }

            $searchLinks = parent::getAppSearchLinks('videos');

            //$pagination = is_null($pagination) ? '' : $pagination->getPagesLinks();

            $videosHTML = $this->_getVideosHTML($result, $pagination);

            $tmpl = new CTemplate();
            $my = CFactory::getUser();

            $canSearch = 1;
            if($my->id == 0 && !$config->get('enableguestsearchvideos')) $canSearch = 0;

            echo $tmpl->set('videosHTML', $videosHTML)
                ->set('pagination', $pagination)
                ->set('videosCount', $total)
                ->set('search', $search)
                ->set('canSearch', $canSearch)
                ->set('searchLinks', $searchLinks)
                ->set('canCreate', $my->authorise('community.create', 'videos'))
                ->set('submenu', $this->showSubmenu(false))
                ->fetch('videos/base');
        }

        public function myvideos($id = null)
        {
            $document = Factory::getDocument();
            $my = CFactory::getUser();
            $jinput = Factory::getApplication()->input;
            $userid = $jinput->getInt('userid', $my->id);
            $user = CFactory::getUser($userid);
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $categoryId = $jinput->get('catid', 0, 'INT');

            $categoryId = $jinput->get('catid', 0, 'INT');
            $category = Table::getInstance('VideosCategory', 'CTable');
            $category->load($categoryId);

            // Set document title
            $blocked = $user->isBlocked();

            if ($blocked && !COwnerHelper::isCommunityAdmin()) {
                $tmpl = new CTemplate();
                echo $tmpl->fetch('profile.blocked');
                return;
            }

            if ($my->id == $user->id)
            {
                $title = Text::_('COM_COMMUNITY_VIDEOS_MY');
            }
            else
            {
                $title = Text::sprintf('COM_COMMUNITY_VIDEOS_USERS_VIDEO_TITLE', $user->getDisplayName());
            }

            /**
             * Opengraph
             */
            CHeadHelper::setType('website', $title);

            // Set pathway
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $this->addPathway(
                Text::_('COM_COMMUNITY_VIDEOS'),
                CRoute::_('index.php?option=com_community&view=videos')
            );
            $this->addPathway($title);

            $feedLink = CRoute::_('index.php?option=com_community&view=videos&userid=' . $user->id . '&format=feed');
            $feed = '<link rel="alternate" type="application/rss+xml" title="' . Text::_(
                    'COM_COMMUNITY_SUBSCRIBE_MY_VIDEOS_FEED'
                ) . '" href="' . $feedLink . '"/>';
            $document->addCustomTag($feed);

            $this->attachMiniHeaderUser($user->id);

            // Display submenu
            $this->_addSubmenu();

            // Get data from DB
            $model = CFactory::getModel('videos');
            //CFactory::load( 'helpers' , 'friends' );
            if ($my->id == $user->id || COwnerHelper::isCommunityAdmin()) {
                $permission = 40;
            } elseif (CFriendsHelper::isConnected($my->id, $user->id)) {
                $permission = 30;
            } elseif ($my->id != 0) {
                $permission = 20;
            } else {
                $permission = 10;
            }

            // Get in category and it's children.
            $categories = $model->getAllCategories();
            $categoryIds = CCategoryHelper::getCategoryChilds($categories, $category->id);
            $categoryIds[] = $categoryId;

            $filters = array
            (
                'creator' => $user->id,
                'category_id' => $categoryIds,
                'status' => 'ready',
                'sorting' => $jinput->get('sort', 'latest', 'STRING'),
                'permissions' => $permission,
                'creator_type' => 'user'
            );
            $videos = $model->getVideos($filters);

            $sortItems = $this->_getSortOptions();

            //pagination
            $pagination = $model->getPagination();

            $videosHTML = $this->_getVideosHTML($videos, $pagination);

            //This is local file
            $categories = $this->getFullVideoCategories();
            $sortItems = $this->_getSortOptions();

            $allVideosUrl = 'index.php?option=com_community&view=videos';
            $catVideoUrl = 'index.php?option=com_community&view=videos&task=myvideos&userid='.$userid.'&catid=';

            $pendingVideos = $this->model->hasPendingVideos( $my->id , VIDEO_USER_TYPE );

            $tmpl = new CTemplate();
            $tmpl->set('user', $user)
                ->set('title', $title)
                ->set('sort', $jinput->get('sort', 'latest', 'STRING'))
                ->set('currentTask', $jinput->get('task', ''))
                ->set('isMyVideo', true)
                ->set('videosHTML', $videosHTML)
                ->set('$category', $category)
                ->set('categories', $categories)
                ->set('currentUrl', CRoute::getURI())
                ->set('allVideosUrl', $allVideosUrl)
                ->set('catVideoUrl', $catVideoUrl)
                ->set('categories', $this->getFullVideoCategories())
                ->set('category', $category)
                ->set('categoryId', $categoryId)
                ->set('pendingVideos', $pendingVideos)
                ->set('sortings', CFilterBar::getHTML(CRoute::getURI(), $sortItems, 'latest'))
                ->set('submenu', $this->showSubmenu(false))
                ->set('pagination', $pagination);

                if($userid == $my->id)
                {
                    $tmpl->set('canCreate', $my->authorise('community.create', 'videos'));
                }
                else
                {
                    $tmpl->set('canCreate', 0);
                }

                echo $tmpl->fetch('videos/base');
        }

        public function mypendingvideos($id = null)
        {
            $document = Factory::getDocument();
            $my = CFactory::getUser();

            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;

            $userid = $jinput->getInt('userid', '');
            $user = CFactory::getUser($userid);

            $this->_addSubmenu();

            // Set pathway
            $mainframe = Factory::getApplication();
            $pathway = $mainframe->getPathway();
            $pathway->addItem('My Pending Videos', '');

            // Get data from DB
            $model = CFactory::getModel('videos');

            // Group video pending
            $groupid = $jinput->get('groupid', '0', 'INT');
            if (!empty($groupid)) {
                $filters = array
                (
                    'groupid' => $groupid,
                    'status' => 'pending'
                );
            } else {
                $filters = array
                (
                    'creator' => $user->id,
                    'groupid' => 0,
                    'status' => 'pending'
                );
            }

            $pendingVideos = $model->getVideos($filters);

            // Substitute permission in text form
            foreach ($pendingVideos as $video) {
                //$video		= $this->_getExtra($video);
                $video->isOwner = COwnerHelper::isMine($my->id, $video->creator);
            }

            $videosHTML = $this->_getVideosHTML($pendingVideos);

            $pagination = $model->getPagination();


            $tmpl = new CTemplate();

            echo $tmpl->set('videosHTML', $videosHTML)
                ->set('sort', $jinput->get('sort', 'latest', 'STRING'))
                ->set('currentTask', $jinput->get('task', ''))
                ->set('pendingVideos', $pendingVideos)
                ->set('pagination', $pagination)
                ->set('submenu', $this->showSubmenu(false))
                ->set('params', $this->videoLib)
                ->set('canCreate', $my->authorise('community.create', 'videos'))
                ->fetch('videos/base');
        }

        /**
         * Method to display video
         * @return void
         */
        public function video()
        {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;

            $document = Factory::getDocument();
            $config = CFactory::getConfig();
            $my = CFactory::getUser();
            $requestUser = CFactory::getRequestUser();
            $videoId = $jinput->get('videoid', '', 'INT');
            $task = $jinput->getCmd('task');

            // Get show video location map by default
            $videoMapsDefault = $config->get('videosmapdefault');

            // Load window library
            CWindow::load();

            $video = Table::getInstance('Video', 'CTable');

            if (empty($videoId)) {
                if ($jinput->get('videoid', '', 'INT')) {
                    $videoId = $jinput->get('videoid', '', 'INT');
                } else {
                    $url = CRoute::_('index.php?option=com_community&view=videos', false);
                    $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_VIDEOS_ID_ERROR'), 'warning');
                    $mainframe->redirect($url);
                }
            }
            if (!$video->load($videoId)) {
                $url = CRoute::_('index.php?option=com_community&view=videos', false);
                $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_VIDEOS_NOT_AVAILABLE'), 'warning');
                $mainframe->redirect($url);
            }

            if($video->pageid){
                $jinput->set( 'pageid', $video->pageid );
            }elseif($video->groupid){
                $jinput->set( 'groupid', $video->groupid );
            }elseif($video->eventid){
                $jinput->set( 'eventid', $video->eventid );
            }

            if ($video->creator_type == VIDEO_EVENT_TYPE && !$config->get('enableevents')) {
                echo Text::_('COM_COMMUNITY_EVENTS_DISABLED');
                return;
            }

            if ($video->creator_type == VIDEO_GROUP_TYPE && !$config->get('enablegroups')) {
                echo Text::_('COM_COMMUNITY_GROUPS_DISABLE');
                return;
            }

            if (!$video->published) {
                $url = CRoute::_('index.php?option=com_community&view=videos', false);
                $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_VIDEOS_NOT_AVAILABLE'), 'warning');
                $mainframe->redirect($url);
            }

            // for featured/unfeatured link
            $featured = new CFeatured(FEATURED_VIDEOS);
            $featuredVideos = $featured->getItemIds();
            $featuredList = array();

            foreach ($featuredVideos as $videoId) {
                $featuredList[] = $videoId;
            }

            // Setting up the sub menu
            if (($my->id == $video->creator && ($my->id != 0) && CFactory::getUser()->authorise('community.videocreate', 'com_community')) || (CFactory::getUser()->authorise('community.videoedit', 'com_community') && ($my->id != 0))) {
                if($video->storage != 's3') { // cannot fetch thumbnail from s3 remote storage
                    $this->addSubmenuItem(
                        '',
                        Text::_('COM_COMMUNITY_VIDEOS_FETCH_THUMBNAIL'),
                        "joms.api.videoFetchThumbnail('" . $video->id . "');",
                        true
                    );
                }

                // Only add the set as profile video for video owner
                if ($my->id == $video->creator && $config->get('enableprofilevideo')) {
                    $this->addSubmenuItem(
                        '',
                        Text::_('COM_COMMUNITY_VIDEOS_SET_AS_PROFILE'),
                        "joms.api.videoLinkToProfile('" . $video->id . "');",
                        true
                    );
                }
                $redirectUrl = CRoute::getURI(false);
                $this->addSubmenuItem(
                    '',
                    Text::_('COM_COMMUNITY_EDIT'),
                    "joms.api.videoEdit('" . $video->id . "');",
                    true
                );
            }

            if($my->authorise('community.delete', 'videos', $video)){
                $this->addSubmenuItem(
                    '',
                    Text::_('COM_COMMUNITY_DELETE'),
                    "joms.api.videoRemove('" . $video->id . "', 'redirect');",
                    true
                );
            }

            if (COwnerHelper::isCommunityAdmin() && !$video->pageid && !$video->groupid && !$video->eventid && $config->get('show_featured') && ($video->permissions == 0 || $video->permissions == 10)) { 

                if (!in_array($video->id, $featuredList)) {
                    $this->addSubmenuItem('',
                        Text::_('COM_COMMUNITY_MAKE_FEATURED'),
                        'joms.api.videoAddFeatured('.$video->id.')',
                        true
                    );
                } else {
                    $this->addSubmenuItem('',
                        Text::_('COM_COMMUNITY_REMOVE_FEATURED'),
                        'joms.api.videoRemoveFeatured('.$video->id.')',
                        true
                    );
                }
            }
            
            $this->_addSubmenu();

            // Show the mini header when viewing other's photos
            if (($video->creator_type == VIDEO_USER_TYPE) && ($my->id != $video->creator)) {
                // $this->attachMiniHeaderUser($video->creator);
            }

            // Check permission
            $user = CFactory::getUser($video->creator);
            $blocked = $user->isBlocked();
            if ($blocked && !COwnerHelper::isCommunityAdmin()) {
                $tmpl = new CTemplate();
                echo $tmpl->fetch('profile.blocked');
                return;
            }


            $sorted = $jinput->get('sort', 'latest', 'STRING');
            $limit = $jinput->get('limitstart', 6, 'INT');
            $permissions = ($my->id == 0) ? 0 : 20;
            $cat_id = $jinput->get('cat_id', '', 'INT');
            $model = CFactory::getModel('videos');
            /* We get groupid from video table instead user input */
            $pageId = $video->pageid;
            $groupId = $video->groupid;

            if ($video->creator_type == VIDEO_PAGE_TYPE) {

                if (!CGroupHelper::allowViewMedia($pageId)) {
                    /**
                     * Opengraph
                     */
                    CHeadHelper::setType('website', Text::_('COM_COMMUNITY_RESTRICTED_ACCESS'));
                    $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_RESTRICTED_ACCESS', 'notice'));
                    echo Text::_('COM_COMMUNITY_PAGES_VIDEO_MEMBER_PERMISSION');
                    return;
                }

                $page = Table::getInstance('Page', 'CTable');
                $page->load($pageId);

                // Set pathway
                $pathway = $mainframe->getPathway();
                $pathway->addItem(
                    Text::_('COM_COMMUNITY_PAGES'),
                    CRoute::_('index.php?option=com_community&view=pages')
                );
                $pathway->addItem(
                    $page->name,
                    CRoute::_('index.php?option=com_community&view=pages&task=viewpage&pageid=' . $pageId)
                );
                $pathway->addItem(
                    Text::_('COM_COMMUNITY_VIDEOS'),
                    CRoute::_('index.php?option=com_community&view=videos&pageid=' . $pageId)
                );
                $pathway->addItem($video->getTitle(), '');


                $otherVideos = $model->getPageVideos($pageId, $cat_id, $limit);
            } else if ($video->creator_type == VIDEO_GROUP_TYPE) {

                if (!CGroupHelper::allowViewMedia($groupId)) {
                    /**
                     * Opengraph
                     */
                    CHeadHelper::setType('website', Text::_('COM_COMMUNITY_RESTRICTED_ACCESS'));
                    $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_RESTRICTED_ACCESS', 'notice'));
                    echo Text::_('COM_COMMUNITY_GROUPS_VIDEO_MEMBER_PERMISSION');
                    return;
                }

                $group = Table::getInstance('Group', 'CTable');
                $group->load($groupId);

                // Set pathway
                $pathway = $mainframe->getPathway();
                $pathway->addItem(
                    Text::_('COM_COMMUNITY_GROUPS'),
                    CRoute::_('index.php?option=com_community&view=groups')
                );
                $pathway->addItem(
                    $group->name,
                    CRoute::_('index.php?option=com_community&view=groups&task=viewgroup&groupid=' . $groupId)
                );
                $pathway->addItem(
                    Text::_('COM_COMMUNITY_VIDEOS'),
                    CRoute::_('index.php?option=com_community&view=videos&groupid=' . $groupId)
                );
                $pathway->addItem($video->getTitle(), '');


                $otherVideos = $model->getGroupVideos($groupId, $cat_id, $limit);
            } else {
                if (!$this->isPermitted($my->id, $video->creator, $video->permissions)) {
                    /**
                     * Opengraph
                     */
                    CHeadHelper::setType('website', Text::_('COM_COMMUNITY_RESTRICTED_ACCESS'));
                    $mainframe->enqueueMessage(Text::_('COM_COMMUNITY_RESTRICTED_ACCESS', 'notice'));

                    switch ($video->permissions) {
                        case '40':
                            $this->noAccess(Text::_('COM_COMMUNITY_VIDEOS_OWNER_ONLY', 'notice'));
                            break;
                        case '30':
                            $owner = CFactory::getUser($video->creator);
                            $this->noAccess(
                                Text::sprintf(
                                    'COM_COMMUNITY_VIDEOS_FRIEND_PERMISSION_MESSAGE',
                                    $owner->getDisplayName()
                                )
                            );
                            break;
                        default:
                            $this->noAccess();
                            break;
                    }
                    return;
                }
                // Set pathway
                $pathway = $mainframe->getPathway();
                $pathway->addItem('Video', CRoute::_('index.php?option=com_community&view=videos'));
                $pathway->addItem($video->getTitle(), '');

                $filters = array
                (
                    'status' => 'ready',
                    'category_id' => $cat_id,
                    'creator' => $user->id,
                    'permissions' => $permissions,
                    'or_group_privacy' => 0,
                    'sorting' => $sorted,
                    'limit' => $limit
                );
                $otherVideos = $model->getVideos($filters);
            }
            // Set the current user's active profile
            CFactory::setActiveProfile($video->creator);

            // Hit counter + 1
            $video->hit();

            // Get reporting html
            $reportHTML = '';

            $report = new CReportingLibrary();
            if ($user->id != $my->id) {
                $reportHTML = $report->getReportingHTML(
                    Text::_('COM_COMMUNITY_VIDEOS_REPORT_VIDEOS'),
                    'videos,reportVideo',
                    array($video->id)
                );
            }

            // Get bookmark html
            $bookmarks = new CBookmarks($video->getPermalink());
            $bookmarksHTML = $bookmarks->getHTML();

            // Get the walls
            $wallContent = CWallLibrary::getWallContents(
                'videos',
                $video->id,
                (COwnerHelper::isCommunityAdmin() || ($my->id == $video->creator && ($my->id != 0))),
                $config->get('stream_default_comments', 0),
                0,
                'wall/content',
                'videos,video'
            );
            $wallCount = CWallLibrary::getWallCount('videos', $video->id);

            $viewAllLink = CRoute::_('index.php?option=com_community&view=videos&task=app&videoid=' . $video->id . '&app=walls');

            $wallViewAll = '';
            if ( $wallCount > $config->get('stream_default_comments', 0) ) {
                $wallViewAll = CWallLibrary::getViewAllLinkHTML($viewAllLink, $wallCount);
            }

            $wallForm = '';

            if ($this->isPermitted($my->id, $video->creator, PRIVACY_FRIENDS) || !$config->get('lockvideoswalls')) {
                $wallForm = CWallLibrary::getWallInputForm(
                    $video->id,
                    'videos,ajaxSaveWall',
                    'videos,ajaxRemoveWall',
                    $viewAllLink
                );
            }

            $redirectUrl = CRoute::getURI(false);

            // Get like information.
            $like = new CLike();
            $likeCount = $like->getLikeCount('videos', $video->id);
            $likeLiked = $like->userLiked('videos', $video->id, $my->id) === COMMUNITY_LIKE;

            $tmpl = new CTemplate();

            if ($video->creator_type == VIDEO_PAGE_TYPE) {
                $page = Table::getInstance('Page', 'CTable');
                $page->load($pageId);

                $document = Factory::getDocument();
                $document->addHeadLink($page->getThumbAvatar(), 'image_src', 'rel');
            } else if ($video->creator_type == VIDEO_GROUP_TYPE) {
                $group = Table::getInstance('Group', 'CTable');
                $group->load($groupId);

                $document = Factory::getDocument();
                $document->addHeadLink($group->getThumbAvatar(), 'image_src', 'rel');
            }

            if ($video->creator_type != VIDEO_PAGE_TYPE && $video->creator_type != VIDEO_GROUP_TYPE && $video->creator_type != VIDEO_EVENT_TYPE) {
                $this->attachMiniHeaderUser($video->creator);
            }
            
            if ($video->location !== '' && $videoMapsDefault) {

                $zoomableMap = CMapping::drawZoomableMap(
                    $video->location,
                    220,
                    150,
                    $video->longitude,
                    $video->latitude
                );
            } else {
                $zoomableMap = "";
            }

            //friend list for video tag
            $tagging = new CVideoTagging();
            $taggedList = $tagging->getTaggedList($video->id);

            for ($t = 0; $t < count($taggedList); $t++) {
                $tagItem = $taggedList[$t];
                $tagUser = CFactory::getUser($tagItem->userid);

                $canRemoveTag = 0;
                // 1st we check the tagged user is the video owner.
                //	If yes, canRemoveTag == true.
                //	If no, then check on user is the tag creator or not.
                //	If yes, canRemoveTag == true
                //	If no, then check on user whether user is being tagged
                if (COwnerHelper::isMine($my->id, $video->creator) || COwnerHelper::isMine(
                        $my->id,
                        $tagItem->created_by
                    ) || COwnerHelper::isMine($my->id, $tagItem->userid)
                ) {
                    $canRemoveTag = 1;
                }

                $tagItem->user = $tagUser;
                $tagItem->canRemoveTag = $canRemoveTag;
            }
            if($video->type=="file"){
                $storage = CStorage::getStorage($video->storage);
                $video->path = $storage->getURI($video->path);
            }


            $config = CFactory::getConfig();
            $canSearch = 1;
            if($my->id == 0 && !$config->get('enableguestsearchvideos')) $canSearch = 0;

            CHeadHelper::addOpengraph('og:image', Uri::root() . $video->thumb, true);
            CHeadHelper::setType('website', $video->title);

            $video->tagged = $taggedList;
            echo $tmpl->setMetaTags('video', $video)
                ->set('user', $user)
                ->set('zoomableMap', $zoomableMap)
                ->set('likeCount', $likeCount)
                ->set('canSearch', $canSearch)
                ->set('likeLiked', $likeLiked)
                ->set('redirectUrl', $redirectUrl)
                ->set('wallContent', $wallContent)
                ->set('wallForm', $wallForm)
                ->set('wallCount', $wallCount)
                ->set('wallViewAll', $wallViewAll)
                ->set('bookmarksHTML', $bookmarksHTML)
                ->set('reportHTML', $reportHTML)
                ->set('video', $video)
                ->set('otherVideos', $otherVideos)
                ->set('videoMapsDefault', $videoMapsDefault)
                ->set('wallCount', $wallCount)
                ->set('isPage', ($pageId) ? true : false)
                ->set('isGroup', ($groupId) ? true : false)
                ->set('groupId', ($groupId) ? $groupId : null )
                ->set('submenu', $this->showSubmenu(false))
                    ->fetch('videos/single');
        }

        /**
         *    Check if permitted to play the video
         *
         * @param    int $myid The current user's id
         * @param    int $userid The active profile user's id
         * @param    int $permission The video's permission
         * @return    bool    True if it's permitted
         * @since    1.2
         */
        public function isPermitted($myid = 0, $userid = 0, $permissions = 0)
        {
            // ACL
            if (CFactory::getUser()->authorise('community.videoeditstate', 'com_community') || CFactory::getUser()->authorise('community.videoedit', 'com_community') || CFactory::getUser()->authorise('community.videodelete', 'com_community')) {
                return true;
            } else {
                return CPrivacy::isAccessAllowed($myid, $userid, 'custom', $permissions);
            }
        }

        public function _addSubmenu()
        {
            $my = CFactory::getUser();
            $config = CFactory::getConfig();
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $task = $jinput->request->get('task', '', 'WORD');
            $pageId = $jinput->get->get('pageid', 0, 'INT');
            $groupId = $jinput->get->get('groupid', 0, 'INT');
            $eventId = $jinput->get->get('eventid', 0, 'INT');

            if (!empty($pageId)) {
                $videos = $this->model->hasPendingVideos($pageId, VIDEO_PAGE_TYPE);

                if ($videos) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=mypendingvideos&pageid=' . $pageId,
                        Text::_('COM_COMMUNITY_VIDEOS_PAGE_PENDING'),
                        '',
                        SUBMENU_LEFT
                    );
                }

                $allowManageVideos = CPageHelper::allowManageVideo($pageId);


            } else if (!empty($groupId)) {
                /*
                $group = Table::getInstance('Group', 'CTable');
                $group->load($groupId);
                $isBanned = $group->isBanned($my->id);
                */

                $videos = $this->model->hasPendingVideos($groupId, VIDEO_GROUP_TYPE);

                if ($videos) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=mypendingvideos&groupid=' . $groupId,
                        Text::_('COM_COMMUNITY_VIDEOS_GROUP_PENDING'),
                        '',
                        SUBMENU_LEFT
                    );
                }

                $allowManageVideos = CGroupHelper::allowManageVideo($groupId);


            } elseif($eventId) {
                $videos = $this->model->hasPendingVideos($eventId, VIDEO_EVENT_TYPE);

                if ($videos) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=mypendingvideos&eventid=' . $eventId,
                        Text::_('COM_COMMUNITY_VIDEOS_EVENT_PENDING'),
                        '',
                        SUBMENU_LEFT
                    );
                }
            }else{
                if ($config->get('enablepages')) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=pagevideos',
                        Text::_('COM_COMMUNITY_VIDEOS_PAGE_DESC'), '' , false , '' , 'joms-right'
                    );
                }

                if ($config->get('enableevents')) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=eventvideos',
                        Text::_('COM_COMMUNITY_VIDEOS_EVENT_DESC'), '' , false , '' , 'joms-right'
                    );
                }

                if ($config->get('enablegroups')) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=groupvideos',
                        Text::_('COM_COMMUNITY_VIDEOS_GROUP_DESC'), '' , false , '' , 'joms-right'
                    );
                }

                $this->addSubmenuItem(
                    'index.php?option=com_community&view=videos',
                    Text::_('COM_COMMUNITY_VIDEOS_ALL_DESC'), '' , false , '' , 'joms-right'
                );

                if (!empty($my->id)) {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=myvideos&userid=' . $my->id,
                        Text::_('COM_COMMUNITY_VIDEOS_MY'), '' , false , '' , 'joms-right'
                    );
                    //$this->addSubmenuItem('', Text::_('COM_COMMUNITY_ADD'), 'joms.videos.addVideo()', SUBMENU_RIGHT);
                }

//                if ((!$config->get('enableguestsearchvideos') && COwnerHelper::isRegisteredUser()) || $config->get(
//                        'enableguestsearchvideos'
//                    )
//                ) {
//                    $tmpl = new CTemplate();
//                    $tmpl->set('url', CRoute::_('index.php?option=com_community&view=videos&task=search'));
//                    $html = $tmpl->fetch('videos.search.submenu');
//                    $this->addSubmenuItem(
//                        'index.php?option=com_community&view=videos&task=search',
//                        Text::_('COM_COMMUNITY_SEARCH'),
//                        'joms.videos.toggleSearchSubmenu(this)',
//                        SUBMENU_LEFT,
//                        $html
//                    );
//                }

                $videos = $this->model->hasPendingVideos($my->id, VIDEO_USER_TYPE);

                if (!empty($my->id) && $videos && $task != 'mypendingvideos') {
                    $this->addSubmenuItem(
                        'index.php?option=com_community&view=videos&task=mypendingvideos&userid=' . $my->id,
                        Text::_('COM_COMMUNITY_VIDEOS_PENDING'),
                        '',
                        SUBMENU_LEFT
                    );
                }
            }
        }

    }

}
