<?php
/**
 * @package    YouTubeR
 * @license  https://allforjoomla.ru/license
 *
 * Created by Oleg Micriucov for Joomla! 3.x
 * https://allforjoomla.ru
 *
 */
defined( '_JEXEC' ) or die(':)');
 
class plgSystemYoutuber extends JPlugin{
	
	protected $autoloadLanguage = true;
    private static $scriptsLoaded = false;
 	
	public function onAfterInitialise(){
		$app = JFactory::getApplication();
		$input = $app->input;
		$action = $input->get('action','');
		if($action!='youtuber'){
			return true;
		}
		try{
            $this->_loadLang();
			JResponse::setHeader('Content-Type', 'application/json', true);
			parse_str(urldecode($input->getString('params','')),$attribs);
			$attribs = $this->shortcodeAttribs($attribs);
			$attribs['pageToken'] = $input->getString('pageToken','');
			
			$view = $this->getView($attribs);
			
			$result = new stdclass;
			$result->success = 1;
			$result->pageToken = (isset($view->playlist->nextPageToken)?$view->playlist->nextPageToken:'');
			$result->html = $view->render();
			
			echo json_encode($result);
		}
		catch(Exception $e){
			JResponse::setHeader('Content-Type', 'text/html', true);
			echo '<p><strong>YouTubeR '.JText::_('PLG_SYSTEM_YOUTUBER_ERROR').':</strong> '.$e->getMessage().'</p>';
		}
		$app->close();
	}
	
	public function onAfterRender(){
		$app = JFactory::getApplication();
		if($app->isAdmin()) return;	
		$doc = JFactory::getDocument();
		if($doc->getType()!='html') return;
		$html = JResponse::getBody();
		if(strpos($html,'[mx_youtuber')===false){
			return true;
		}
		$html = preg_replace_callback('~\[(mx_youtube[^ ]+)([^\]]+)]~',array($this,'renderShortcode'),$html);
		JResponse::setBody($html);
		return true;
	}
	
	public function onBeforeCompileHead(){
		$app = JFactory::getApplication();
		$doc = JFactory::getDocument();
		if($doc->getType()!='html') return;
        $this->_loadLang();
		$mediaURI = JURI::root().'media/plg_system_youtuber/';
		JHtml::_('jquery.framework');
        if(self::$scriptsLoaded){
            return;
        }
        $loadFancybox = (int)$this->params->get('load_fancybox', 1);
        self::$scriptsLoaded = true;
		if($app->isAdmin() && $this->params->get('googleOAuthKey')!=''){
			JHtml::script($mediaURI.'assets/js/media-uploader.js',false,true);
			JHtml::script($mediaURI.'assets/js/mxyoutube.js',false,true);
			JHtml::script('https://apis.google.com/js/client.js',false,true);
			JHtml::stylesheet($mediaURI.'assets/css/backend.css',array(),true);
			$doc->addScriptDeclaration('
				jQuery(document).ready(function(){
					if(typeof mxYouTubeRBtnClick !="function"){
						return;
					}
					if(window.wzYoutube==undefined){
						alert("'.JText::_('PLG_SYSTEM_YOUTUBER_ACTIVATE_PLUGIN').'");
                        jQuery(".mxYouTuberBtn").attr("onclick","return false;").css("opacity","0.5");
					}
					if("'.$this->params->get('googleOAuthKey').'"==""){
						alert("'.JText::_('PLG_SYSTEM_YOUTUBER_SET_OAUTH_ID').'");
						jQuery(".mxYouTuberBtn").attr("onclick","return false;").css("opacity","0.5");
					}
					else{
						window.wzYoutube.lang.authorize_account = "'.JText::_('PLG_SYSTEM_YOUTUBER_AUTHORIZE_YOUTUBE_ACCOUNT').'";
						window.wzYoutube.lang.upload_video = "'.JText::_('PLG_SYSTEM_YOUTUBER_UPLOAD_VIDEO').'";
						window.wzYoutube.lang.list_videos = "'.JText::_('PLG_SYSTEM_YOUTUBER_VIDEOS_LIST').'";
						window.wzYoutube.lang.more_videos = "'.JText::_('PLG_SYSTEM_YOUTUBER_MORE_VIDEOS').'";
						window.wzYoutube.lang.title = "'.JText::_('PLG_SYSTEM_YOUTUBER_TITLE').'";
						window.wzYoutube.lang.video_title = "'.JText::_('PLG_SYSTEM_YOUTUBER_VIDEO_TITLE').'";
						window.wzYoutube.lang.description = "'.JText::_('PLG_SYSTEM_YOUTUBER_DESCRIPTION').'";
						window.wzYoutube.lang.video_description = "'.JText::_('PLG_SYSTEM_YOUTUBER_VIDEO_DESCRIPTION').'";
						window.wzYoutube.lang.tags = "'.JText::_('PLG_SYSTEM_YOUTUBER_TAGS').'";
						window.wzYoutube.lang.video_tags = "'.JText::_('PLG_SYSTEM_YOUTUBER_TAGS_SEPARATED_BY_COMMA').'";
						window.wzYoutube.lang.privacy_status = "'.JText::_('PLG_SYSTEM_YOUTUBER_PRIVACY_STATUS').'";
						window.wzYoutube.lang.upload = "'.JText::_('PLG_SYSTEM_YOUTUBER_UPLOAD').'";
						window.wzYoutube.lang.privacy_public = "'.JText::_('PLG_SYSTEM_YOUTUBER_PUBLIC').'";
						window.wzYoutube.lang.privacy_ulnisted = "'.JText::_('PLG_SYSTEM_YOUTUBER_UNLISTED').'";
						window.wzYoutube.lang.privacy_private = "'.JText::_('PLG_SYSTEM_YOUTUBER_PRIVATE').'";
						window.wzYoutube.lang.enter_video_title = "'.JText::_('PLG_SYSTEM_YOUTUBER_PLEASE_ENTER_VIDEO_TITLE').'";
						window.wzYoutube.lang.choose_video_file = "'.JText::_('PLG_SYSTEM_YOUTUBER_PLEASE_CHOOSE_VIDEO_FILE').'";
						window.wzYoutube.appID = "'.$this->params->get('googleOAuthKey').'";
					}
				});
			');
		}
		else if(!$app->isAdmin()){
            if($loadFancybox==1){
                JHtml::script($mediaURI.'assets/fancybox/jquery.fancybox.min.js',false,true);
                JHtml::stylesheet($mediaURI.'assets/fancybox/jquery.fancybox.min.css',array(),true);
            }
            if((int)$this->params->get('load_gfont', 1)){
                JHtml::stylesheet('https://fonts.googleapis.com/css?family=Roboto:400,400italic,500,500italic,700,700italic&subset=latin,cyrillic');
            }
			JHtml::stylesheet($mediaURI.'assets/css/frontend.css',array(),true);
			$doc->addScriptDeclaration('
				window.mxYouTubeR = {ajax_url:"'.JURI::root().'",lang:{"more":"'.JText::_('PLG_SYSTEM_YOUTUBER_MORE').'","less":"'.JText::_('PLG_SYSTEM_YOUTUBER_LESS').'"}};
			');
            JHtml::script($mediaURI.'assets/js/frontend.js',false,true);
		}
	}
	
	public function getView($attribs){
		$viewPath = JPATH_ROOT.'/plugins/system/youtuber/';
		$viewName = 'mxYouTuberView_'.$attribs['type'];
		$classFile = $viewPath.'views/'.$attribs['type'].'.php';
		if(is_file($classFile)){
			require_once($viewPath.'/data.php');
			require_once($classFile);
			$view = new $viewName($attribs,$this);
			return $view;
		}
		else{
			throw new Exception('Incorrect shortcode attribute type "'.$attribs['type'].'".');
		}
		return false;
	}
	
	public function shortcodeAttribs($rawAttribs){
		$attribs = array();
		if(is_array($rawAttribs)){
			$attribs = $rawAttribs;
		}
		else{
			preg_match_all('~([a-zA-Z0-9_\-]+)=\&quot;(.*?)(?=\&quot;)\&quot;~',$rawAttribs,$mchs);
			foreach($mchs[1] as $k=>$v){
				$attribs[$v] = $mchs[2][$k];
			}
            preg_match_all('~([a-zA-Z0-9_\-]+)="([^"]+)"~',$rawAttribs,$mchs);
			foreach($mchs[1] as $k=>$v){
				$attribs[$v] = $mchs[2][$k];
			}
		}
		$defaults = array(
			'type' => 'video',
			'id' => '',
			'videos' => '',
			'display' => 'header,title,date,channel,description,meta,header,playlists',
			'mode' => $this->params->get('mode'),
			'theme' => $this->params->get('theme'),
			'ytp_params' => '',
			'size' => '',
			'width' => '100%',
			'height' => '300',
			'cols' => ((isset($attribs['type'])&&$attribs['type']=='channel')?1:(int)$this->params->get('cols')),
			'rows' => (int)$this->params->get('rows'),
			'responsive_limit' => $this->params->get('responsive_limit'),
			'max_words' => (int)$this->params->get('max_words'),
			'infinite_scroll' => 'false',
			'load_more' => 'true',
			'load_more_text' => JText::_('PLG_SYSTEM_YOUTUBER_LOAD_MORE'),
			'pageToken' => '',
			'suggested_videos' => 'false',
			'order_by' => $this->params->get('order_by','default'),
			'order_dir' => $this->params->get('order_dir','asc'),
            'playlist_id' => '',
            'playlists' => '',
            'user' => '',
            'except_videos' => '',
            'title' => '',
            'description' => '',
		);
		$result = array_merge($defaults,$attribs);
		$result['limit'] =  (int)$result['cols']*(int)$result['rows'];
		return $result;
	}
	
	public function renderShortcode($matches){
        $this->_loadLang();
		$shortCode = $matches[1];
		$attribs = $this->shortcodeAttribs($matches[2]);
		if($shortCode=='mx_youtuber_video'){
			$attribs['type'] = 'video';
		}
		else if($shortCode=='mx_youtuber_playlist'){
			$attribs['type'] = 'playlist';
		}
		else if($shortCode=='mx_youtuber_channel'){
			$attribs['type'] = 'channel';
		}
		try{
			$view = $this->getView($attribs);
			return $view->render();
		}
		catch( Exception $e){
			if(JDEBUG || (int)$this->params->get('debug',0)==1){
				return '<p><strong>YouTubeR '.JText::_('PLG_SYSTEM_YOUTUBER_ERROR').':</strong> '.$e->getMessage().'</p>';
			}
			else return '<p><strong>YouTubeR '.JText::_('PLG_SYSTEM_YOUTUBER_ERROR_NO_DEBUG').'</strong>.</p>';
		}
		return '';
	}
    
    public function onExtensionAfterSave($context, $table, $isNew=true){
        if($context!='com_plugins.plugin' || !isset($table->type) || !isset($table->element) || $table->type!='plugin' || $table->element!='youtuber'){
            return;
        }
        $params = new JRegistry($table->params);
        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
                ->update('#__update_sites')
                ->set('extra_query='.$db->quote('purchase_code='.$params->get('purchase_code','')))
                ->where('name='.$db->quote('YouTubeR'));
        $db->setQuery($query);
        $db->execute();
    }
	
	public function getTemplatePaths($theme=''){
		$app    = JFactory::getApplication();
		$mainThemePath = JPATH_ROOT.'/templates/'.$app->getTemplate().'/html/plg_system_youtuber/';
		$mediaPath = JPATH_ROOT.'/media/plg_system_youtuber/';
		$theme = ($theme!=''?$theme:$this->params->get('theme'));
		$paths = array();
		$paths[] = $mainThemePath.$theme;
		$paths[] = $mainThemePath.'default';
		$paths[] = $mediaPath.'themes/'.$theme;
		$paths[] = $mediaPath.'themes/default';
		return $paths;
	}
    
    private function _loadLang(){
        $lang = JFactory::getLanguage();
        $lang->load('plg_system_youtuber', JPATH_ADMINISTRATOR);
    }
}