1<?php
2/**
3 * YiiWheels class file.
4 * @author Antonio Ramirez <amigo.cobos@gmail.com>
5 * @copyright Copyright &copy; 2amigos.us 2013-
6 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7 * @package yiiwheels
8 */
9
10class YiiWheels extends CApplicationComponent
11{
12    /**
13     * @var array the HTML options for the view container tag.
14     */
15    public $htmlOptions = array();
16
17    /**
18     * @var array $assetsJs of javascript library names to be registered when initializing the library.
19     */
20    public $assetsJs = array();
21
22    /**
23     * @var array $assetsCss of style library names to be registered when initializing the library.
24     */
25    public $assetsCss = array();
26
27    /**
28     * @var TbApi $_api
29     */
30    protected $_api;
31
32    /**
33     * @var string holds the published assets
34     */
35    protected $_assetsUrl;
36
37
38    /**
39     * Widget's initialization
40     * @throws CException
41     */
42    public function init()
43    {
44        $this->_api = Yii::app()->getComponent('bootstrap');
45
46        if (null === $this->_api) {
47            throw new CException(Yii::t('zii', '"YiiWheels" must work in conjunction with "YiiStrap".'));
48        }
49
50        /* ensure all widgets - plugins are accessible to the library */
51        Yii::import('bootstrap.widgets.*');
52        /* ensure common behavior is also accessible to the library */
53        Yii::import('yiiwheels.behaviors.WhPlugin');
54
55        /* register css assets */
56        foreach ($this->assetsCss as $css) {
57            $this->registerAssetCss($css);
58        }
59
60        /* register js assets */
61        foreach ($this->assetsJs as $js) {
62            $this->registerAssetJs($js);
63        }
64    }
65
66    /**
67     * Returns the core library (yiistrap) component
68     * @return TbApi
69     */
70    public function getApi()
71    {
72        return $this->_api;
73    }
74
75    /**
76     * Returns the assets URL.
77     * Assets folder has few orphan and very useful utility libraries.
78     * @return string
79     */
80    public function getAssetsUrl()
81    {
82        if (isset($this->_assetsUrl)) {
83            return $this->_assetsUrl;
84        } else {
85            $forceCopyAssets = $this->getApi()->forceCopyAssets;
86            $path            = Yii::getPathOfAlias('yiiwheels');
87            $assetsUrl       = Yii::app()->assetManager->publish(
88                $path . DIRECTORY_SEPARATOR . 'assets',
89                false,
90                -1,
91                $forceCopyAssets
92            );
93
94            return $this->_assetsUrl = $assetsUrl;
95        }
96    }
97
98    /**
99     * Register a specific js file in the asset's js folder
100     * @param string $jsFile
101     * @param int $position the position of the JavaScript code.
102     * @see CClientScript::registerScriptFile
103     * @return $this
104     */
105    public function registerAssetJs($jsFile, $position = CClientScript::POS_END)
106    {
107        Yii::app()->getClientScript()->registerScriptFile($this->getAssetsUrl() . "/js/{$jsFile}", $position);
108        return $this;
109    }
110
111    /**
112     * Registers a specific css in the asset's css folder
113     * @param string $cssFile the css file name to register
114     * @param string $media the media that the CSS file should be applied to. If empty, it means all media types.
115     * @return $this
116     */
117    public function registerAssetCss($cssFile, $media = '')
118    {
119        Yii::app()->getClientScript()->registerCssFile($this->getAssetsUrl() . "/css/{$cssFile}", $media);
120        return $this;
121    }
122}