HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
Booster.php
Go to the documentation of this file.
1 <?php
42 class Booster extends CApplicationComponent {
43 
51  public $enableCdn = false;
52 
57  public $coreCss = true;
58 
63  public $bootstrapCss = true;
64 
69  public $responsiveCss = true;
70 
76  public $disableZooming = false;
77 
84  public $fontAwesomeCss = false;
85 
89  public $minify = true;
90 
97  public $yiiCss = true;
98 
102  public $jqueryCss = true;
103 
108  public $enableJS = true;
109 
114  public $enableBootboxJS = true;
115 
122  public $enableNotifierJS = true;
123 
128  public $ajaxCssLoad = false;
129 
134  public $ajaxJsLoad = false;
135 
143  public $forceCopyAssets = false;
144 
145  public $enablePopover = true;
146 
147  public $enableTooltip = true;
148 
158  public $popoverSelector = '[data-toggle=popover]';
159 
171  public $tooltipSelector = '[data-toggle=tooltip]';
172 
181  public $packages = array();
182 
187  public $cs;
188 
192  public $_assetsUrl;
193 
197  private static $_instance;
198 
202  public function init() {
203 
204  // Prevents the extension from registering scripts and publishing assets when ran from the command line.
205  if ($this->isInConsoleMode() && !$this->isInTests())
206  return;
207 
208  self::setBooster($this);
209 
210  $this->setRootAliasIfUndefined();
211 
213 
214  $this->includeAssets();
215 
216  parent::init();
217  }
218 
220  protected function isInConsoleMode() {
221 
222  return Yii::app() instanceof CConsoleApplication || PHP_SAPI == 'cli';
223  }
224 
226  protected function isInTests() {
227 
228  return defined('IS_IN_TESTS') && IS_IN_TESTS;
229  }
230 
234  protected function setRootAliasIfUndefined() {
235 
236  if (Yii::getPathOfAlias('booster') === false) {
237  Yii::setPathOfAlias('booster', realpath(dirname(__FILE__) . '/..'));
238  }
239  }
240 
244  protected function includeAssets() {
245 
247 
248  $this->addOurPackagesToYii();
249 
251 
253  }
254 
258  protected function appendUserSuppliedPackagesToOurs() {
259 
260  $bootstrapPackages = require(Yii::getPathOfAlias('booster.components') . '/packages.php');
261  $bootstrapPackages += $this->createBootstrapCssPackage();
262  $bootstrapPackages += $this->createSelect2Package();
263 
264  $this->packages = CMap::mergeArray(
265  $bootstrapPackages,
266  $this->packages
267  );
268  }
269 
273  protected function addOurPackagesToYii() {
274 
275  foreach ($this->packages as $name => $definition) {
276  $this->cs->addPackage($name, $definition);
277  }
278  $this->cs->scriptMap['jquery-ui.min.js'] = $this->getAssetsUrl() . '/js/jquery-ui-no-conflict.min.js';
279  }
280 
284  protected function registerCssPackagesIfEnabled() {
285 
286  if (!$this->coreCss)
287  return;
288 
289  if (!$this->ajaxCssLoad && Yii::app()->request->isAjaxRequest)
290  return;
291 
292  if ($this->bootstrapCss)
293  $this->registerBootstrapCss();
294 
295  if ($this->fontAwesomeCss)
296  $this->registerFontAwesomeCss();
297 
298  if ($this->responsiveCss)
300 
301  if ($this->yiiCss !== false)
302  $this->registerYiiCss();
303 
304  if ($this->jqueryCss !== false)
305  $this->registerJQueryCss();
306  }
307 
313  public function registerYiiCss() {
314 
315  $this->registerPackage('bootstrap-yii');
316  }
317 
321  public function registerJQueryCss() {
322 
323  $this->registerPackage('jquery-css')->scriptMap['jquery-ui.css'] = $this->getAssetsUrl(
324  ) . '/css/jquery-ui-bootstrap.css';
325  }
326 
330  protected function registerJsPackagesIfEnabled() {
331 
332  if (!$this->enableJS)
333  return;
334 
335  if (!$this->ajaxJsLoad && Yii::app()->request->isAjaxRequest)
336  return;
337 
338  $this->registerPackage('bootstrap.js');
339  $this->registerPackage('bootstrap-noconflict');
340 
341  if ($this->enableBootboxJS)
342  $this->registerPackage('bootbox');
343 
344  if ($this->enableNotifierJS)
345  $this->registerPackage('notify');
346 
347  if($this->enablePopover)
348  $this->registerPopoverJs();
349 
350  if($this->enableTooltip)
351  $this->registerTooltipJs();
352  }
353 
354 
359  public function getVersion() {
360 
361  return '4.0.1';
362  }
363 
373  public function registerPackage($name) {
374 
375  return $this->cs->registerPackage($name);
376  }
377 
386  public function registerAssetCss($name, $media = '') {
387 
388  $this->cs->registerCssFile($this->getAssetsUrl() . "/css/{$name}", $media);
389  }
390 
399  public function registerAssetJs($name, $position = CClientScript::POS_END) {
400 
401  $this->cs->registerScriptFile($this->getAssetsUrl() . "/js/{$name}", $position);
402  }
403 
408  public function getAssetsUrl() {
409 
410  if (isset($this->_assetsUrl)) {
411  return $this->_assetsUrl;
412  } else {
413  return $this->_assetsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('booster.assets'), false, -1, $this->forceCopyAssets);
414  }
415  }
416 
417  protected function setAssetsRegistryIfNotDefined() {
418 
419  if (!$this->cs) {
420  $this->cs = Yii::app()->getClientScript();
421  }
422  }
423 
424  public function registerBootstrapCss() {
425 
426  $this->cs->registerPackage('bootstrap.css');
427  }
428 
435  protected function createBootstrapCssPackage() {
436 
437  return array('bootstrap.css' => array(
438  'baseUrl' => $this->enableCdn ? '//netdna.bootstrapcdn.com/bootstrap/3.1.1/' : $this->getAssetsUrl() . '/bootstrap/',
439  'css' => array( ($this->minify || $this->enableCdn) ? 'css/bootstrap.min.css' : 'css/bootstrap.css' ),
440  ));
441  }
442 
447  protected function createSelect2Package() {
448 
449  $jsFiles = array($this->minify ? 'select2.min.js' : 'select2.js');
450 
451  if (strpos(Yii::app()->language, 'en') !== 0) {
452  $locale = 'select2_locale_'. substr(Yii::app()->language, 0, 2). '.js';
453  if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
454  $jsFiles[] = $locale;
455  } else {
456  $locale = 'select2_locale_'. Yii::app()->language . '.js';
457  if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
458  $jsFiles[] = $locale;
459  }else{
460  $locale = 'select2_locale_'. substr(Yii::app()->language, 0, 2) . '-' . strtoupper(substr(Yii::app()->language, 3, 2)) . '.js';
461  if (@file_exists(Yii::getPathOfAlias('booster.assets.select2') . DIRECTORY_SEPARATOR . $locale )) {
462  $jsFiles[] = $locale;
463  }
464  }
465  }
466  }
467 
468  return array('select2' => array(
469  'baseUrl' => $this->getAssetsUrl() . '/select2/',
470  'js' => $jsFiles,
471  'css' => array('select2.css', 'select2-bootstrap.css'),
472  'depends' => array('jquery'),
473  ));
474  }
475 
479  protected function registerMetadataForResponsive() {
480  if($this->disableZooming)
481  $this->cs->registerMetaTag('width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', 'viewport');
482  else
483  $this->cs->registerMetaTag('width=device-width, initial-scale=1', 'viewport');
484  }
485 
490  public function registerFontAwesomeCss() {
491 
492  $this->registerPackage('font-awesome');
493  }
494 
495  public function registerPopoverJs() {
496  $this->cs->registerScript($this->getUniqueScriptId(), "jQuery('[data-toggle=popover]').popover();");
497  }
498 
499  public function registerTooltipJs() {
500  $this->cs->registerScript($this->getUniqueScriptId(), "jQuery('[data-toggle=tooltip]').tooltip();");
501  }
502 
508  public function getUniqueScriptId() {
509  return uniqid(__CLASS__ . '#', true);
510  }
511 
517  protected function tryGetSelectorForPlugin($name) {
518 
519  return $this->tryGetInfoForPlugin($name, 'selector');
520  }
521 
526  protected function tryGetOptionsForPlugin($name) {
527 
528  return $this->tryGetInfoForPlugin($name, 'options');
529  }
530 
535  public static function setBooster($value) {
536 
537  if ($value instanceof Booster) {
538  self::$_instance = $value;
539  }
540  }
541 
546  public static function getBooster() {
547 
548  if (null === self::$_instance) {
549  // Lets find inside current module
550  $module = Yii::app()->getController()->getModule();
551  if ($module) {
552  if ($module->hasComponent('booster')) {
553  self::$_instance = $module->getComponent('booster');
554  }
555  }
556  // Still nothing?
557  if (null === self::$_instance) {
558  if (Yii::app()->hasComponent('booster')) {
559  self::$_instance = Yii::app()->getComponent('booster');
560  }
561  }
562  }
563  return self::$_instance;
564  }
565 }