PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/protected/extensions/bootstrap/components/Bootstrap.php

https://bitbucket.org/santool/santool-cms
PHP | 471 lines | 198 code | 52 blank | 221 comment | 15 complexity | 6f7899220791a1775882bdd2184d712a MD5 | raw file
  1. <?php
  2. /**
  3. * Bootstrap class file.
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7. * @version 1.0.0
  8. */
  9. /**
  10. * Bootstrap application component.
  11. */
  12. class Bootstrap extends CApplicationComponent
  13. {
  14. // Bootstrap plugins.
  15. const PLUGIN_ALERT = 'alert';
  16. const PLUGIN_BUTTON = 'button';
  17. const PLUGIN_CAROUSEL = 'carousel';
  18. const PLUGIN_COLLAPSE = 'collapse';
  19. const PLUGIN_DROPDOWN = 'dropdown';
  20. const PLUGIN_MODAL = 'modal';
  21. const PLUGIN_POPOVER = 'popover';
  22. const PLUGIN_SCROLLSPY = 'scrollspy';
  23. const PLUGIN_TAB = 'tab';
  24. const PLUGIN_TOOLTIP = 'tooltip';
  25. const PLUGIN_TRANSITION = 'transition';
  26. const PLUGIN_TYPEAHEAD = 'typeahead';
  27. const PLUGIN_DATEPICKER = 'bdatepicker';
  28. const PLUGIN_REDACTOR = 'redactor';
  29. const PLUGIN_MARKDOWNEDITOR = 'markdowneditor';
  30. const PLUGIN_AFFIX = 'affix';
  31. const PLUGIN_DATERANGEPICKER = 'daterangepicker';
  32. const PLUGIN_HTML5EDITOR = 'wysihtml5';
  33. const PLUGIN_COLORPICKER = 'colorpicker';
  34. /**
  35. * @var boolean whether to register the Bootstrap core CSS (bootstrap.min.css).
  36. * Defaults to true.
  37. */
  38. public $coreCss = true;
  39. /**
  40. * @var boolean whether to register the Bootstrap responsive CSS (bootstrap-responsive.min.css).
  41. * Defaults to false.
  42. */
  43. public $responsiveCss = false;
  44. /**
  45. * @var boolean whether to register the Yii-specific CSS missing from Bootstrap.
  46. * @since 0.9.12
  47. */
  48. public $yiiCss = true;
  49. /**
  50. * @var boolean whether to register the JQuery-specific CSS missing from Bootstrap.
  51. */
  52. public $jqueryCss = true;
  53. /**
  54. * @var boolean whether to register jQuery and the Bootstrap JavaScript.
  55. * @since 0.9.10
  56. */
  57. public $enableJS = true;
  58. /**
  59. * @var array plugin initial options (name=>options).
  60. * Each array key-value pair represents the initial options for a single plugin class,
  61. * with the array key being the plugin name, and array value being the initial options array.
  62. * @since 0.9.8
  63. */
  64. public $plugins = array();
  65. /**
  66. * @var string default popover CSS selector.
  67. * @since 0.10.0
  68. */
  69. public $popoverSelector = 'a[rel="popover"]';
  70. /**
  71. * @var string default tooltip CSS selector.
  72. * @since 0.10.0
  73. */
  74. public $tooltipSelector = 'a[rel="tooltip"]';
  75. /**
  76. * @var bool whether to enable bootbox messages or not. Default value is true.
  77. * @since YiiBooster 1.0.5
  78. */
  79. public $enableBootboxJS = true;
  80. protected $_assetsUrl;
  81. /**
  82. * Initializes the component.
  83. */
  84. public function init()
  85. {
  86. // Register the bootstrap path alias.
  87. if (Yii::getPathOfAlias('bootstrap') === false)
  88. Yii::setPathOfAlias('bootstrap', realpath(dirname(__FILE__) . '/..'));
  89. // Prevents the extension from registering scripts and publishing assets when ran from the command line.
  90. if (Yii::app() instanceof CConsoleApplication)
  91. return;
  92. if ($this->coreCss !== false)
  93. $this->registerCoreCss();
  94. if ($this->responsiveCss !== false)
  95. $this->registerResponsiveCss();
  96. if ($this->yiiCss !== false)
  97. $this->registerYiiCss();
  98. if($this->jqueryCss !== false)
  99. $this->registerJQueryCss();
  100. if ($this->enableJS !== false)
  101. $this->registerCoreScripts();
  102. parent::init();
  103. }
  104. /**
  105. * Registers the Bootstrap CSS.
  106. */
  107. public function registerCoreCss()
  108. {
  109. $this->registerAssetCss('bootstrap' . (!YII_DEBUG ? '.min' : '') . '.css');
  110. }
  111. /**
  112. * Registers the Bootstrap responsive CSS.
  113. * @since 0.9.8
  114. */
  115. public function registerResponsiveCss()
  116. {
  117. /** @var CClientScript $cs */
  118. $cs = Yii::app()->getClientScript();
  119. $cs->registerMetaTag('width=device-width, initial-scale=1.0', 'viewport');
  120. $cs->registerCssFile($this->getAssetsUrl() . '/css/bootstrap-responsive' . (!YII_DEBUG ? '.min' : '') . '.css');
  121. }
  122. /**
  123. * Registers the Yii-specific CSS missing from Bootstrap.
  124. * @since 0.9.11
  125. */
  126. public function registerYiiCss()
  127. {
  128. $this->registerAssetCss('bootstrap-yii.css');
  129. }
  130. /**
  131. * Registers the JQuery-specific CSS missing from Bootstrap.
  132. */
  133. public function registerJQueryCss()
  134. {
  135. Yii::app()->getClientScript()->scriptMap['jquery-ui.css'] = $this->getAssetsUrl() . '/css/jquery-ui-bootstrap.css';
  136. $this->registerAssetCss('jquery-ui-bootstrap.css');
  137. }
  138. /**
  139. * Registers a specific css in the asset's css folder
  140. * @param string $cssFile the css file name to register
  141. * @param string $media the media that the CSS file should be applied to. If empty, it means all media types.
  142. */
  143. public function registerAssetCss($cssFile, $media = '')
  144. {
  145. Yii::app()->getClientScript()->registerCssFile($this->getAssetsUrl() . "/css/{$cssFile}", $media);
  146. }
  147. /**
  148. * Registers the core JavaScript.
  149. * @since 0.9.8
  150. */
  151. public function registerCoreScripts()
  152. {
  153. $this->registerJS(Yii::app()->clientScript->coreScriptPosition);
  154. $this->registerTooltip();
  155. $this->registerPopover();
  156. }
  157. /**
  158. * Registers the Bootstrap JavaScript.
  159. * @param int $position the position of the JavaScript code.
  160. * @see CClientScript::registerScriptFile
  161. */
  162. public function registerJS($position = CClientScript::POS_HEAD)
  163. {
  164. /** @var CClientScript $cs */
  165. $cs = Yii::app()->getClientScript();
  166. $cs->registerCoreScript('jquery');
  167. /** enable bootboxJS? */
  168. if($this->enableBootboxJS)
  169. {
  170. $cs->registerScriptFile($this->getAssetsUrl() . '/js/bootstrap.bootbox.min.js', $position);
  171. }
  172. $cs->registerScriptFile($this->getAssetsUrl() . '/js/bootstrap' . (!YII_DEBUG ? '.min' : '') . '.js', $position);
  173. }
  174. /**
  175. * Register a specific js file in the asset's js folder
  176. * @param string $jsFile
  177. * @param int $position the position of the JavaScript code.
  178. * @see CClientScript::registerScriptFile
  179. */
  180. public function registerAssetJs($jsFile, $position = CClientScript::POS_END)
  181. {
  182. Yii::app()->getClientScript()->registerScriptFile($this->getAssetsUrl() . "/js/{$jsFile}", $position);
  183. }
  184. /**
  185. * Registers the Bootstrap alert plugin.
  186. * @param string $selector the CSS selector
  187. * @param array $options the plugin options
  188. * @see http://twitter.github.com/bootstrap/javascript.html#alerts
  189. * @since 0.9.8
  190. */
  191. public function registerAlert($selector = null, $options = array())
  192. {
  193. $this->registerPlugin(self::PLUGIN_ALERT, $selector, $options);
  194. }
  195. /**
  196. * Registers the Bootstrap buttons plugin.
  197. * @param string $selector the CSS selector
  198. * @param array $options the plugin options
  199. * @see http://twitter.github.com/bootstrap/javascript.html#buttons
  200. * @since 0.9.8
  201. */
  202. public function registerButton($selector = null, $options = array())
  203. {
  204. $this->registerPlugin(self::PLUGIN_BUTTON, $selector, $options);
  205. }
  206. /**
  207. * Registers the Bootstrap carousel plugin.
  208. * @param string $selector the CSS selector
  209. * @param array $options the plugin options
  210. * @see http://twitter.github.com/bootstrap/javascript.html#carousel
  211. * @since 0.9.8
  212. */
  213. public function registerCarousel($selector = null, $options = array())
  214. {
  215. $this->registerPlugin(self::PLUGIN_CAROUSEL, $selector, $options);
  216. }
  217. /**
  218. * Registers the Bootstrap collapse plugin.
  219. * @param string $selector the CSS selector
  220. * @param array $options the plugin options
  221. * @see http://twitter.github.com/bootstrap/javascript.html#collapse
  222. * @since 0.9.8
  223. */
  224. public function registerCollapse($selector = null, $options = array())
  225. {
  226. $this->registerPlugin(self::PLUGIN_COLLAPSE, $selector, $options, '.collapse');
  227. }
  228. /**
  229. * Registers the Bootstrap dropdowns plugin.
  230. * @param string $selector the CSS selector
  231. * @param array $options the plugin options
  232. * @see http://twitter.github.com/bootstrap/javascript.html#dropdowns
  233. * @since 0.9.8
  234. */
  235. public function registerDropdown($selector = null, $options = array())
  236. {
  237. $this->registerPlugin(self::PLUGIN_DROPDOWN, $selector, $options, '.dropdown-toggle[data-dropdown="dropdown"]');
  238. }
  239. /**
  240. * Registers the Bootstrap modal plugin.
  241. * @param string $selector the CSS selector
  242. * @param array $options the plugin options
  243. * @see http://twitter.github.com/bootstrap/javascript.html#modal
  244. * @since 0.9.8
  245. */
  246. public function registerModal($selector = null, $options = array())
  247. {
  248. $this->registerPlugin(self::PLUGIN_MODAL, $selector, $options);
  249. }
  250. /**
  251. * Registers the Bootstrap scrollspy plugin.
  252. * @param string $selector the CSS selector
  253. * @param array $options the plugin options
  254. * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy
  255. * @since 0.9.8
  256. */
  257. public function registerScrollSpy($selector = null, $options = array())
  258. {
  259. $this->registerPlugin(self::PLUGIN_SCROLLSPY, $selector, $options);
  260. }
  261. /**
  262. * Registers the Bootstrap popover plugin.
  263. * @param string $selector the CSS selector
  264. * @param array $options the plugin options
  265. * @see http://twitter.github.com/bootstrap/javascript.html#popover
  266. * @since 0.9.8
  267. */
  268. public function registerPopover($selector = null, $options = array())
  269. {
  270. $this->registerTooltip(); // Popover requires the tooltip plugin
  271. $this->registerPlugin(self::PLUGIN_POPOVER, $selector, $options, $this->popoverSelector);
  272. }
  273. /**
  274. * Registers the Bootstrap tabs plugin.
  275. * @param string $selector the CSS selector
  276. * @param array $options the plugin options
  277. * @see http://twitter.github.com/bootstrap/javascript.html#tabs
  278. * @since 0.9.8
  279. */
  280. public function registerTabs($selector = null, $options = array())
  281. {
  282. $this->registerPlugin(self::PLUGIN_TAB, $selector, $options);
  283. }
  284. /**
  285. * Registers the Bootstrap tooltip plugin.
  286. * @param string $selector the CSS selector
  287. * @param array $options the plugin options
  288. * @see http://twitter.github.com/bootstrap/javascript.html#tooltip
  289. * @since 0.9.8
  290. */
  291. public function registerTooltip($selector = null, $options = array())
  292. {
  293. $this->registerPlugin(self::PLUGIN_TOOLTIP, $selector, $options, $this->tooltipSelector);
  294. }
  295. /**
  296. * Registers the Bootstrap typeahead plugin.
  297. * @param string $selector the CSS selector
  298. * @param array $options the plugin options
  299. * @see http://twitter.github.com/bootstrap/javascript.html#typeahead
  300. * @since 0.9.8
  301. */
  302. public function registerTypeahead($selector = null, $options = array())
  303. {
  304. $this->registerPlugin(self::PLUGIN_TYPEAHEAD, $selector, $options);
  305. }
  306. /**
  307. * Register the Bootstrap datepicker plugin.
  308. * IMPORTANT: if you register a selector via this method you wont be able to attach events to the plugin.
  309. * @param string $selector the CSS selector
  310. * @param array $options the plugin options
  311. * @see http://www.eyecon.ro/bootstrap-datepicker/
  312. *
  313. */
  314. public function registerDatePicker($selector = null, $options = array())
  315. {
  316. $this->registerPlugin(self::PLUGIN_DATEPICKER, $selector, $options);
  317. }
  318. /**
  319. * Registers the RedactorJS plugin.
  320. * @param null $selector
  321. * @param $options
  322. */
  323. public function registerRedactor($selector = null, $options = array())
  324. {
  325. $this->registerPlugin(self::PLUGIN_REDACTOR, $selector, $options);
  326. }
  327. /**
  328. * Registers the Bootstrap-whysihtml5 plugin.
  329. * @param null $selector
  330. * @param $options
  331. */
  332. public function registerHtml5Editor($selector = null, $options = array())
  333. {
  334. $this->registerPlugin(self::PLUGIN_HTML5EDITOR, $selector, $options);
  335. }
  336. /**
  337. * Registers the Bootstrap-colorpicker plugin.
  338. * @param null $selector
  339. * @param $options
  340. */
  341. public function registerColorPicker($selector = null, $options = array())
  342. {
  343. $this->registerPlugin(self::PLUGIN_COLORPICKER, $selector, $options);
  344. }
  345. /**
  346. * Registers the affix plugin
  347. * @param null $selector
  348. * @param array $options
  349. * @see http://twitter.github.com/bootstrap/javascript.html#affix
  350. */
  351. public function registerAffix($selector = null, $options = array())
  352. {
  353. $this->registerPlugin(self::PLUGIN_AFFIX, $selector, $options);
  354. }
  355. /**
  356. * Registers the Bootstrap daterange plugin
  357. * @param string $selector the CSS selector
  358. * @param array $options the plugin options
  359. * @param $callback the javascript callback function
  360. * @see http://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/
  361. * @since 1.1.0
  362. */
  363. public function registerDateRangePlugin($selector, $options = array(), $callback = null)
  364. {
  365. $key = __CLASS__ . '.' . md5(self::PLUGIN_DATERANGEPICKER . $selector . serialize($options) . $callback);
  366. Yii::app()->clientScript->registerScript($key, '$("' . $selector . '").daterangepicker(' . CJavaScript::encode($options) . ($callback ? ', ' . CJavaScript::encode($callback) : '') . ');');
  367. }
  368. /**
  369. * Registers a Bootstrap JavaScript plugin.
  370. * @param string $name the name of the plugin
  371. * @param string $selector the CSS selector
  372. * @param array $options the plugin options
  373. * @param string $defaultSelector the default CSS selector
  374. * @since 0.9.8
  375. */
  376. protected function registerPlugin($name, $selector = null, $options = array(), $defaultSelector = null)
  377. {
  378. if (!isset($selector) && empty($options))
  379. {
  380. // Initialization from extension configuration.
  381. $config = isset($this->plugins[$name]) ? $this->plugins[$name] : array();
  382. if (isset($config['selector']))
  383. $selector = $config['selector'];
  384. if (isset($config['options']))
  385. $options = $config['options'];
  386. if (!isset($selector))
  387. $selector = $defaultSelector;
  388. }
  389. if (isset($selector))
  390. {
  391. $key = __CLASS__ . '.' . md5($name . $selector . serialize($options) . $defaultSelector);
  392. $options = !empty($options) ? CJavaScript::encode($options) : '';
  393. Yii::app()->clientScript->registerScript($key, "jQuery('{$selector}').{$name}({$options});");
  394. }
  395. }
  396. /**
  397. * Returns the URL to the published assets folder.
  398. * @return string the URL
  399. */
  400. public function getAssetsUrl()
  401. {
  402. if (isset($this->_assetsUrl))
  403. return $this->_assetsUrl;
  404. else
  405. {
  406. $assetsPath = Yii::getPathOfAlias('bootstrap.assets');
  407. $assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, YII_DEBUG);
  408. return $this->_assetsUrl = $assetsUrl;
  409. }
  410. }
  411. /**
  412. * Returns the extension version number.
  413. * @return string the version
  414. */
  415. public function getVersion()
  416. {
  417. return '1.0.5';
  418. }
  419. }