10 Yii::import(
'booster.components.JSONStorage',
true);
70 if (!$this->model instanceof CActiveRecord) {
71 throw new CException(Yii::t(
'zii',
'"model" attribute must be an CActiveRecord type of component'));
74 if (!$this->grid instanceof CGridView) {
75 throw new CException(Yii::t(
'zii',
'"grid" attribute must be an CGridView type of component'));
78 if (!$this->redirectRoute === null) {
79 throw new CException(Yii::t(
'zii',
'"redirectRoute" cannot be empty'));
82 $this->registry .=
'-' . $this->grid->id;
85 $this->jsonStorage->addRegistry($this->registry);
87 $this->filteredBy = array_filter(
88 $this->model->getAttributes(),
108 if ($key = Yii::app()->getRequest()->getParam($this->removeFilterVar)) {
109 if ($this->jsonStorage->removeData($key, $this->registry)) {
110 Yii::app()->getController()->redirect($this->redirectRoute);
125 $filterName = Yii::app()->getRequest()->getParam($this->saveFilterVar);
129 if (!count($this->filteredBy))
133 if ($this->jsonStorage->getData($key, $this->registry))
136 $data = array(
'name' => $filterName);
137 $data[
'options'] = array(get_class($this->model) => $this->filteredBy);
138 $this->jsonStorage->setData($key,
$data, $this->registry);
140 Yii::app()->getController()->redirect($this->redirectRoute);
153 if (!count($this->filteredBy) && !$this->jsonStorage->getLength($this->registry)) {
158 $cols = count($this->grid->columns);
159 echo
"<td colspan='{$cols}'>\n";
160 echo
"<div id='{$this->getId()}'>\n";
161 if (count($this->filteredBy)) {
182 $url = CHtml::normalizeUrl($this->redirectRoute);
184 Yii::app()->clientScript->registerScript(
185 __CLASS__ .
'#extended-filter' . $this->grid->id,
187 $(document).on(
'click',
'#{$this->grid->id} .btn-extended-filter-save',
function(e){
189 bootbox.prompt(
"How do you wish to save this filter?",
function(result){
190 if ($.trim(result).length > 0)
192 $(
'#{$this->grid->id}').yiiGridView(
'update',{data:{{$this->saveFilterVar}:result}});
196 $(document).on(
'click',
'#{$this->grid->id} .btn-extended-filter-apply',
function(e) {
198 var option = $(
'#{$this->getId()} select.select-extended-filter option:selected');
199 if (!option.length || !option.data(
'filter')) {
202 var data ={data:option.data(
'filter')};
203 if (option.val()==-1) {
206 $(
'#{$this->grid->id}').yiiGridView(
'update',data);
209 $(document).on(
'click',
'#{$this->grid->id} .btn-extended-filter-delete',
function(e) {
211 var option = $(
'#{$this->grid->id} select.select-extended-filter option:selected');
212 if (!option.length || !option.data(
'key') || option.val()==-1) {
215 bootbox.confirm(
'Delete "'+option.text()+
'" filter?',
function(confirmed){
217 $(
'#{$this->grid->id}').yiiGridView(
'update',{data:{{$this->removeFilterVar}:option.data(
'key')}});
234 if (null == $registryKey || $this->jsonStorage->getData($registryKey, $this->registry))
237 echo CHtml::openTag(
'p');
239 echo CHtml::link(
'save filter',
'#', array(
'class' =>
'btn btn-success btn-extended-filter-save'));
241 echo CHtml::closeTag(
'p');
253 if ($this->jsonStorage->getLength($this->registry)) {
254 $registry = $this->jsonStorage->getRegistry($this->registry);
255 echo
'<div class="row">';
256 echo
'<div class="col-md-6 col-sm-12" >';
257 echo
'<label class="label label-info">Saved Filters [select and click ok sign button]</label><br/>';
259 echo
'<div class="row" style="margin-top: 5px;">';
260 echo
'<div class="col-md-6 col-sm-12" >';
261 echo
'<select class="select-extended-filter">';
262 echo
'<option value="-1" data-filter="{}" ' . (!$registryKey ?
'selected' :
'') .
'>No Filters</option>';
263 foreach ($registry as $key => $filter) {
267 'data-filter' => CJSON::encode($filter[
'options']),
269 'selected' => ($key == $registryKey ?
'selected' : null)
272 echo $filter[
'name'];
275 echo
'</select> ';
278 '<i class="glyphicon glyphicon-ok glyphicon glyphicon-white"></i>',
280 array(
'class' =>
'btn btn-primary btn-extended-filter-apply')
283 echo CHtml::link(
'<i class="glyphicon glyphicon-trash"></i>',
'#', array(
'class' =>
'btn btn-warning btn-extended-filter-delete'));
297 if (!count($this->filteredBy)) {
300 return md5($this->grid->id . CJSON::encode($this->filteredBy));
315 foreach ($filteredBy as $key => $value) {
316 $values[] =
'<span class="label label-info">' . $key .
'</span> ' . $value;
318 return implode(
', ', $values);