Advertisement
AmourSpirit

Joomla form behavior for Select2 4.0

Aug 6th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2. // see also: http://pastebin.com/ih2gLVg2 for joomla form example usage
  3. /**
  4.  * @package     Joomla.Libraries
  5. * @subpackage  HTML
  6. *
  7. * @copyright   Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  8. * @license     GNU General Public License version 2 or later; see LICENSE
  9. */
  10.  
  11. defined('JPATH_PLATFORM') or die;
  12.  
  13. // place this file in /libraries/cms/html/formbehaviourselect2.php
  14.  
  15. /**
  16.  * Utility class for form related behaviors
  17.  *
  18.  * @package     Joomla.Libraries
  19.  * @subpackage  HTML
  20.  * @since       3.0
  21.  */
  22. abstract class JHtmlFormbehaviourselect2
  23. {
  24.     /**
  25.      * @var    array  Array containing information for loaded files
  26.      * @since  3.0
  27.      */
  28.     protected static $loaded = array();
  29.  
  30.     /**
  31.      * Method to load the Select2 JavaScript framework and supporting CSS into the document head
  32.      *
  33.      * @param   string  $selector  Class for Chosen elements. [optional]
  34.      * @param   string  $option    options for Select2 elements. [optional]
  35.      * @param   mixed   $debug     Is debugging mode on? [optional]
  36.      *
  37.      * @return  void
  38.      *
  39.      * @since   3.0
  40.     */
  41.     public static function select2($selector = '.advancedSelect', $option = '', $debug = null)
  42.     {
  43.         if (isset(static::$loaded[__METHOD__][$selector]))
  44.         {
  45.             return;
  46.         }
  47.  
  48.         // Include jQuery
  49.         JHtml::_('jquery.framework');
  50.  
  51.         // If no debugging value is set, use the configuration setting
  52.         if ($debug === null)
  53.         {
  54.             $config = JFactory::getConfig();
  55.             $debug  = (boolean) $config->get('debug');
  56.         }
  57.  
  58.         $http = JHtmlFormbehaviourselect2::isSecure() ? 'https' :'http';
  59.         //JHtml::_('script', 'jui/select2.min.js', false, true, false, false, $debug);
  60.         //JHtml::_('script', 'jui/select2_locale_cs.js', false, true, false, false, $debug);
  61.         JHtml::_('script', $http . '://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js', false, true, false, false, $debug);
  62.  
  63.         //JHtml::_('stylesheet', 'jui/select2.css', false, true);
  64.         JHtml::_('stylesheet', $http . '://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css', false, true);
  65.         JFactory::getDocument()->addScriptDeclaration("
  66.                 jQuery(document).ready(function (){
  67.                     jQuery('" . $selector . "').select2({
  68.                         " .$option. "
  69.                     });
  70.                 });
  71.             "
  72.         );
  73.  
  74.         static::$loaded[__METHOD__][$selector] = true;
  75.  
  76.         return;
  77.     }
  78.  
  79.     protected static function isSecure() {
  80.         return
  81.         (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  82.         || $_SERVER['SERVER_PORT'] == 443;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement