1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets\base;
9
10 use foundation\helpers\Foundation;
11
12 /**
13 * Class Widget
14 * @package foundation\widgets\base
15 */
16 class Widget extends \CWidget
17 {
18
19 /**
20 * @var array the HTML attributes for the breadcrumbs.
21 */
22 public $htmlOptions = array();
23 /**
24 * @var array list of links to appear in the breadcrumbs.
25 */
26 public $links = array();
27 /**
28 * @var array list of the assets the plugin requires. it holds the following format:
29 * <pre>
30 * 'assets' => array(
31 * 'css' => 'file.css',
32 * 'js' => 'file.js')
33 * ),
34 * ...
35 * 'assets' => array(
36 * 'css' => array(
37 * 'file1.css',
38 * 'file2.css'
39 * ),
40 * 'js' => array(
41 * 'file1.js',
42 * 'file2.js'
43 * )
44 * )
45 * </pre>
46 */
47 public $assets = array();
48
49
50 /**
51 * Initializes the widget
52 */
53 public function init()
54 {
55 Foundation::registerCoreCss();
56 Foundation::registerCoreScripts(false, !YII_DEBUG, \CClientScript::POS_HEAD);
57 $this->registerAssets();
58 }
59
60 /**
61 * Registers a specific plugin using the given selector and options.
62 * @param string $name the plugin name.
63 * @param string $selector the CSS selector.
64 * @param array $options the JavaScript options for the plugin.
65 * @param int $position the position of the JavaScript code.
66 */
67 public function registerPlugin($name, $selector, $options = array(), $position = \CClientScript::POS_READY)
68 {
69 Foundation::registerPlugin($name, $selector, $options, $position);
70 }
71
72 /**
73 * Registers events using the given selector.
74 * @param string $selector the CSS selector.
75 * @param string[] $events the JavaScript event configuration (name=>handler).
76 * @param int $position the position of the JavaScript code.
77 */
78 public function registerEvents($selector, $events, $position = \CClientScript::POS_READY)
79 {
80 Foundation::registerEvents($selector, $events, $position);
81 }
82
83 /**
84 * Registers the assets. Makes sure the assets pre-existed on the published folder prior registration.
85 */
86 public function registerAssets()
87 {
88 // make sure core hasn't been registered previously
89
90 $dirs = array('css', 'js');
91 foreach ($this->assets as $key => $files) {
92 if (in_array($key, $dirs)) {
93 $files = is_array($files) ? $files : array($files);
94 foreach ($files as $file) {
95 $filePath = Foundation::getAssetsPath() . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $file;
96 if (file_exists($filePath) && is_file($filePath)) {
97 $method = strcasecmp($key, 'css') === 0 ? 'registerCssFile' : 'registerScriptFile';
98 call_user_func(
99 array(\Yii::app()->clientScript, "$method"),
100 Foundation::getAssetsUrl() . "/$key/$file"
101 );
102 }
103 }
104 }
105 }
106
107 }
108
109 /**
110 * Ported from Yii2 widget's function. Creates a widget instance and runs it. We cannot use 'widget' name as it
111 * conflicts with CBaseController component.
112 *
113 * The widget rendering result is returned by this method.
114 * @param array $config name-value pairs that will be used to initialize the object properties
115 * @return string the rendering result of the widget.
116 */
117 public static function display($config = array())
118 {
119 ob_start();
120 ob_implicit_flush(false);
121 /** @var Widget $widget */
122 $config['class'] = get_called_class();
123 $widget = \Yii::createComponent($config);
124 $widget->init();
125 $widget->run();
126 return ob_get_clean();
127 }
128 }