Don't like ads? PRO users don't see any ads ;-)
Guest

have fun ;)

By: a guest on Jul 24th, 2012  |  syntax: PHP  |  size: 8.33 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Joomla! System plugin - jQuery Fancybox
  4.  *
  5.  * @author Yireo (info@yireo.com)
  6.  * @copyright Copyright 2011 Yireo.com. All rights reserved
  7.  * @license GNU Public License
  8.  * @link http://www.yireo.com
  9.  
  10.  * CHANGELOG:
  11.  * [1.1.8] Extra parameter to disable loading of jQuery
  12.  * [1.1.8] Usage of $j variable in noConflict mode
  13.  * [1.1.9] Addition of Google API
  14.  * [1.2.3] New option "Content-type"
  15.  */
  16.  
  17. // Check to ensure this file is included in Joomla!
  18. defined('_JEXEC') or die( 'Restricted access' );
  19.  
  20. // Import the parent class
  21. jimport( 'joomla.plugin.plugin' );
  22.  
  23. /**
  24.  * Fancybox System Plugin
  25.  */
  26. class plgSystemFancybox extends JPlugin
  27. {
  28.     /**
  29.      * Event onAfterRender
  30.      *
  31.      * @access public
  32.      * @param null
  33.      * @return null
  34.      */
  35.     public function onAfterDispatch()
  36.     {
  37.         // Dot not load if this is not the right document-class
  38.         $document = JFactory::getDocument();
  39.         if($document->getType() != 'html') {
  40.             return false;
  41.         }
  42.  
  43.         // Perform actions on the frontend
  44.         $application = JFactory::getApplication();
  45.         if($application->isSite()) {
  46.  
  47.             $elements = $this->getElements();
  48.             if(empty($elements)) return null;
  49.  
  50.             // Get and parse the components from the plugin parameters
  51.             $components = $this->getParams()->get('exclude_components');
  52.             if(empty($components)) {
  53.                 $components= array();
  54.             } elseif(!is_array($components)) {
  55.                 $components = array($components);
  56.             }
  57.  
  58.             // Don't do anything if the current component is excluded
  59.             if(in_array(JRequest::getCmd('option'), $components)) {
  60.                 return;
  61.             }
  62.  
  63.             $js_folder = 'media/plg_fancybox/js/';
  64.             $transition = $this->getParams()->get('transition', '');
  65.             $namespace = $this->getParams()->get('namespace', '');
  66.  
  67.             $this->loadStylesheet('jquery.fancybox-1.3.4.css', $this->getParams()->get('load_css', 1));
  68.  
  69.             if(JFactory::getApplication()->get('jquery') == false) {
  70.                 $this->loadScript('jquery-1.6.4.min.js', $this->getParams()->get('load_jquery', 1));
  71.                 JFactory::getApplication()->set('jquery', true);
  72.             }
  73.             $this->loadScript('jquery.fancybox-1.3.4.pack.js', $this->getParams()->get('load_fancybox', 1));
  74.             if($this->getParams()->get('enable_mousewheel', 0) == 1 && $this->getParams()->get('load_mousewheel', 1) == 1) {
  75.                 $this->loadScript('jquery.mousewheel-3.0.4.pack.js');
  76.             }
  77.  
  78.             $options = array(
  79.                 'hideOnContentClick' => (bool)$this->getParams()->get('hide_on_click', true),
  80.                 'hideOnOverlayClick' => (bool)$this->getParams()->get('hide_on_click', true),
  81.                 'overlayShow' => false,
  82.             );
  83.  
  84.             $content_type = $this->getParams()->get('content_type');
  85.             if(!empty($content_type)) {
  86.                 $options['type'] = $content_type;
  87.             }
  88.  
  89.             if(!in_array($transition, array('', 'swing', 'linear', 'elastic'))) {
  90.            
  91.                 $this->loadScript('jquery.easing-1.3.pack.js', $this->getParams()->get('load_easing', 1));
  92.  
  93.                 $options['easingIn'] = 'easeIn'.ucfirst($transition);
  94.                 $options['easingOut'] = 'easeOut'.ucfirst($transition);
  95.                 $options['zoomSpeedIn'] = $this->getParams()->get('speed', 200);
  96.                 $options['zoomSpeedOut'] = $this->getParams()->get('speed', 200);
  97.  
  98.             } else {
  99.                 $options['transitionIn'] = $transition;
  100.                 $options['transitionOut'] = $transition;
  101.                 $options['speedIn'] = $this->getParams()->get('speed', 200);
  102.                 $options['speedOut'] = $this->getParams()->get('speed', 200);
  103.             }
  104.  
  105.             foreach($options as $name => $value) {
  106.                 if(is_bool($value)) {
  107.                     $bool = ($value) ? 'true' : 'false';
  108.                     $options[$name] = "'$name':$bool";
  109.                 } else {
  110.                     $options[$name] = "'$name':'$value'";
  111.                 }
  112.             }
  113.  
  114.             $script_lines = array('<!--//--><![CDATA[//><!--');
  115.             if(empty($namespace)) {
  116.                 $script_lines[] = 'jQuery.noConflict();';
  117.                 $script_lines[] = 'jQuery(document).ready(function() {';
  118.                 foreach($elements as $element) {
  119.                     $script_lines[] = 'if(jQuery("'.$element.'")) { jQuery("'.$element.'").fancybox({'.implode(',', $options).'}); }';
  120.                 }
  121.  
  122.             } else {
  123.                 $script_lines[] = $namespace.' = jQuery.noConflict();';
  124.                 $script_lines[] = $namespace.'(document).ready(function() {';
  125.                 foreach($elements as $element) {
  126.                     $script_lines[] = $namespace.'("'.$element.'").fancybox({'.implode(',', $options).'});';
  127.                 }
  128.             }
  129.             $script_lines[] = '});';
  130.             $script_lines[] = '//--><!]]>';
  131.  
  132.             $document = JFactory::getDocument();
  133.             $document->addScriptDeclaration(implode("\n", $script_lines));
  134.  
  135.         }
  136.     }
  137.  
  138.     /**
  139.      * Load a script
  140.      *
  141.      * @access private
  142.      * @param null
  143.      * @return null
  144.      */
  145.     private function loadScript($file = null, $condition = true)
  146.     {
  147.         if($condition == true) {
  148.  
  149.             if(preg_match('/^jquery-([0-9\.]+).min.js$/', $file, $match) && $this->getParams()->get('use_google_api', 0) == 1) {
  150.  
  151.                 if(JURI::getInstance()->isSSL() == true) {
  152.                     $script = 'https://ajax.googleapis.com/ajax/libs/jquery/'.$match[1].'/jquery.min.js';
  153.                 } else {
  154.                     $script = 'http://ajax.googleapis.com/ajax/libs/jquery/'.$match[1].'/jquery.min.js';
  155.                 }
  156.  
  157.                 JFactory::getDocument()->addScript($script);
  158.                 return;
  159.             }
  160.  
  161.             $folder = 'media/plg_fancybox/js/';
  162.  
  163.             // Check for overrides
  164.             $template = JFactory::getApplication()->getTemplate();
  165.             if(file_exists(JPATH_SITE.DS.'templates'.DS.$template.DS.'html'.DS.'plg_fancybox'.DS.'js'.DS.$file)) {
  166.                 $folder = 'templates/'.$template.'/html/plg_fancybox/js/';
  167.             }
  168.  
  169.             JHTML::script($file, $folder, false);
  170.         }
  171.     }
  172.  
  173.     /**
  174.      * Load a stylesheet
  175.      *
  176.      * @access private
  177.      * @param null
  178.      * @return null
  179.      */
  180.     private function loadStylesheet($file = null, $condition = true)
  181.     {
  182.         if($condition == true) {
  183.  
  184.             $folder = 'media/plg_fancybox/css/';
  185.  
  186.             // Check for overrides
  187.             $template = JFactory::getApplication()->getTemplate();
  188.             if(file_exists(JPATH_SITE.DS.'templates'.DS.$template.DS.'html'.DS.'plg_fancybox'.DS.'css'.DS.$file)) {
  189.                 $folder = 'templates/'.$template.'/html/plg_fancybox/css/';
  190.             }
  191.  
  192.             JHTML::stylesheet($file, $folder, false);
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Load the parameters
  198.      *
  199.      * @access private
  200.      * @param null
  201.      * @return JParameter
  202.      */
  203.     private function getParams()
  204.     {
  205.         jimport('joomla.version');
  206.         $version = new JVersion();
  207.         if(version_compare($version->RELEASE, '1.5', 'eq')) {
  208.             $plugin = JPluginHelper::getPlugin('system', 'fancybox');
  209.             $params = new JParameter($plugin->params);
  210.             return $params;
  211.         } else {
  212.             return $this->params;
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Get the HTML elements
  218.      *
  219.      * @access private
  220.      * @param null
  221.      * @return JParameter
  222.      */
  223.     private function getElements()
  224.     {
  225.         $elements = $this->getParams()->get('elements');
  226.         $elements = trim($elements);
  227.         $elements = explode(",", $elements);
  228.         if(!empty($elements)) {
  229.             foreach($elements as $index => $element) {
  230.                 $element = trim($element);
  231.                 $element = preg_replace('/([^a-zA-Z0-9\[\]\=\-\_\.\#\ ]+)/', '', $element);
  232.                 if(empty($element)) {
  233.                     unset($elements[$index]);
  234.                 } else {
  235.                     $elements[$index] = $element;
  236.                 }
  237.             }
  238.         }
  239.  
  240.         return $elements;
  241.     }
  242. }