1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 Yii::import('bootstrap.helpers.TbArray');
16
17 class WhGallery extends CWidget
18 {
19 20 21 22
23 public $htmlOptions = array();
24
25 26 27 28
29 public $pluginOptions = array();
30
31 32 33 34 35 36 37 38 39 40 41 42 43 44
45 public $items = array();
46
47 48 49
50 public $displayControls = false;
51
52 53 54
55 public function init()
56 {
57 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
58 $this->pluginOptions['container'] = '#' . $this->htmlOptions['id'] . '-gallery';
59 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
60 parent::init();
61 }
62
63 64 65 66
67 public function run()
68 {
69 if (empty($this->items)) {
70 return null;
71 }
72 $this->renderLinks();
73 $this->renderTemplate();
74 $this->registerClientScript();
75 }
76
77 78 79
80 public function renderLinks()
81 {
82 echo CHtml::openTag('div', $this->htmlOptions);
83 foreach ($this->items as $item) {
84 $url = TbArray::getValue('url', $item, '#');
85 $src = TbArray::getValue('src', $item, '#');
86 $options = TbArray::getValue('options', $item );
87 echo CHtml::link(CHtml::image($src), $url, $options);
88 }
89 echo CHtml::closeTag('div');
90 }
91
92 93 94
95 public function renderTemplate()
96 {
97 $options = array(
98 'id' => $this->htmlOptions['id'] . '-gallery',
99 'class' => 'blueimp-gallery'
100 );
101 if($this->displayControls) {
102 TbHtml::addCssClass('blueimp-gallery-controls', $options);
103 }
104 echo CHtml::openTag('div', $options);
105 echo '<div class="slides"></div>
106 <h3 class="title"></h3>
107 <a class="prev">‹</a>
108 <a class="next">›</a>
109 <a class="close">×</a>
110 <a class="play-pause"></a>
111 <ol class="indicator"></ol>';
112 echo CHtml::closeTag('div');
113 }
114
115 116 117
118 public function registerGalleryScriptFiles()
119 {
120
121 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
122 $assetsUrl = $this->getAssetsUrl($path);
123
124
125 $cs = Yii::app()->getClientScript();
126
127 $cs->registerScriptFile($assetsUrl . '/js/blueimp-gallery.min.js', CClientScript::POS_END);
128 $cs->registerScriptFile($assetsUrl . '/js/blueimp-gallery-indicator.js', CClientScript::POS_END);
129 $cs->registerCssFile($assetsUrl . '/css/blueimp-gallery.min.css');
130 }
131
132 133 134
135 public function registerClientScript()
136 {
137 $this->registerGalleryScriptFiles();
138 $selector = $this->htmlOptions['id'];
139
140 $options = CJavaScript::encode($this->pluginOptions);
141 $js = "
142 ;var galleryLinks = [];
143 $(document).on('click', '#{$selector} a', function(e){
144 var links = $(this).parent()[0].getElementsByTagName('a');
145 var options = {$options};
146 options.index = $(this)[0];
147 blueimp.Gallery(links, options);
148 return false;
149 });
150 ";
151
152 Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->getId(), $js);
153 }
154
155 }
156