SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Jquery.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17.  
  18.  
  19. /**
  20.  * Simple class to ensure uniform jQuery version(s) used
  21.  */
  22. class Jquery {
  23.  
  24. /**
  25.   * Gets a <script> tag to include jQuery or jQuery UI
  26.   * @param string $lib Library to include: 'jquery' or 'jqueryui'
  27.   * @param string $loc Location: 'front' or 'admin'
  28.   * @param string $min Specify 'min' to get a minified version
  29.   */
  30. public static function script($lib, $loc, $min = 'min')
  31. {
  32. if (!in_array($loc, ['front', 'admin'])) {
  33. throw new \Exception('Invalid $loc');
  34. }
  35.  
  36. $lib = strtolower($lib);
  37. if (!in_array($lib, ['jquery', 'jqueryui'])) {
  38. throw new \Exception('Invalid $lib');
  39. }
  40.  
  41. $version = Kohana::config("sprout.{$lib}_{$loc}");
  42.  
  43. $file = preg_replace('/ui$/', '-ui', $lib);
  44. $ext = ($min == 'min' ? '.min.js' : '.js');
  45.  
  46. $script = '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/';
  47. $script .= $lib . '/' . $version . '/' . $file . $ext . '"></script>';
  48.  
  49. return $script;
  50. }
  51.  
  52. }
  53.