HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbExtendedFilter.php
Go to the documentation of this file.
1 <?php
10 Yii::import('booster.components.JSONStorage', true);
11 
20 class TbExtendedFilter extends CWidget {
21 
25  public $model;
26 
30  public $grid;
31 
35  protected $registry = 'extended-filter';
36 
40  public $saveFilterVar = 'saveFilterAs';
41 
45  public $removeFilterVar = 'removeFilter';
46 
50  public $filteredBy;
51 
56 
60  protected $jsonStorage;
61 
68  public function init() {
69 
70  if (!$this->model instanceof CActiveRecord) {
71  throw new CException(Yii::t('zii', '"model" attribute must be an CActiveRecord type of component'));
72  }
73 
74  if (!$this->grid instanceof CGridView) {
75  throw new CException(Yii::t('zii', '"grid" attribute must be an CGridView type of component'));
76  }
77 
78  if (!$this->redirectRoute === null) {
79  throw new CException(Yii::t('zii', '"redirectRoute" cannot be empty'));
80  }
81 
82  $this->registry .= '-' . $this->grid->id;
83 
84  $this->jsonStorage = new JSONStorage();
85  $this->jsonStorage->addRegistry($this->registry);
86 
87  $this->filteredBy = array_filter(
88  $this->model->getAttributes(),
89  function ($i) {
90  return $i != null;
91  }
92  );
93 
95  $this->checkRequestFilters();
96 
97  $this->registerClientScript();
98  }
99 
106  protected function checkRequestRemovalFilter() {
107 
108  if ($key = Yii::app()->getRequest()->getParam($this->removeFilterVar)) {
109  if ($this->jsonStorage->removeData($key, $this->registry)) {
110  Yii::app()->getController()->redirect($this->redirectRoute);
111  }
112  }
113  }
114 
123  protected function checkRequestFilters()
124  {
125  $filterName = Yii::app()->getRequest()->getParam($this->saveFilterVar);
126  if (!$filterName)
127  return false;
128 
129  if (!count($this->filteredBy))
130  return false;
131 
132  $key = $this->generateRegistryItemKey();
133  if ($this->jsonStorage->getData($key, $this->registry))
134  return false;
135 
136  $data = array('name' => $filterName);
137  $data['options'] = array(get_class($this->model) => $this->filteredBy);
138  $this->jsonStorage->setData($key, $data, $this->registry);
139 
140  Yii::app()->getController()->redirect($this->redirectRoute);
141  return true;
142  }
143 
149  public function run() {
150 
151  $registryKey = $this->generateRegistryItemKey($this->filteredBy);
152 
153  if (!count($this->filteredBy) && !$this->jsonStorage->getLength($this->registry)) {
154  return;
155  }
156 
157  echo "<tr>\n";
158  $cols = count($this->grid->columns);
159  echo "<td colspan='{$cols}'>\n";
160  echo "<div id='{$this->getId()}'>\n";
161  if (count($this->filteredBy)) {
162  echo '<p><span class="label label-success">Filtered by</span> ' . $this->displayExtendedFilterValues(
163  $this->filteredBy
164  ) . '</p>';
165  }
166 
167  $this->displaySaveButton($registryKey);
168  $this->displaySavedFilters($registryKey);
169 
170  echo "</div>\n";
171  echo "</td>\n";
172  echo "</tr>\n";
173  }
174 
180  public function registerClientScript() {
181 
182  $url = CHtml::normalizeUrl($this->redirectRoute);
183 
184  Yii::app()->clientScript->registerScript(
185  __CLASS__ . '#extended-filter' . $this->grid->id,
186  <<<EOD
187  $(document).on('click', '#{$this->grid->id} .btn-extended-filter-save', function(e){
188  e.preventDefault();
189  bootbox.prompt("How do you wish to save this filter?", function(result){
190  if ($.trim(result).length > 0)
191  {
192  $('#{$this->grid->id}').yiiGridView('update',{data:{{$this->saveFilterVar}:result}});
193  }
194  });
195  });
196  $(document).on('click', '#{$this->grid->id} .btn-extended-filter-apply', function(e) {
197  e.preventDefault();
198  var option = $('#{$this->getId()} select.select-extended-filter option:selected');
199  if (!option.length || !option.data('filter')) {
200  return false;
201  }
202  var data ={data:option.data('filter')};
203  if (option.val()==-1) {
204  data.url = "{$url}";
205  }
206  $('#{$this->grid->id}').yiiGridView('update',data);
207  });
208 
209  $(document).on('click', '#{$this->grid->id} .btn-extended-filter-delete', function(e) {
210  e.preventDefault();
211  var option = $('#{$this->grid->id} select.select-extended-filter option:selected');
212  if (!option.length || !option.data('key') || option.val()==-1) {
213  return false;
214  }
215  bootbox.confirm('Delete "'+option.text()+'" filter?', function(confirmed){
216  if (confirmed) {
217  $('#{$this->grid->id}').yiiGridView('update',{data:{{$this->removeFilterVar}:option.data('key')}});
218  }
219  });
220  });
221 EOD
222  );
223  }
224 
232  protected function displaySaveButton($registryKey) {
233 
234  if (null == $registryKey || $this->jsonStorage->getData($registryKey, $this->registry))
235  return;
236 
237  echo CHtml::openTag('p');
238 
239  echo CHtml::link('save filter', '#', array('class' => 'btn btn-success btn-extended-filter-save'));
240 
241  echo CHtml::closeTag('p');
242  }
243 
251  protected function displaySavedFilters($registryKey) {
252 
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/>';
258  echo '</div></div>';
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) {
264  echo CHtml::openTag(
265  'option',
266  array(
267  'data-filter' => CJSON::encode($filter['options']),
268  'data-key' => $key,
269  'selected' => ($key == $registryKey ? 'selected' : null)
270  )
271  );
272  echo $filter['name'];
273  echo '</option>';
274  }
275  echo '</select>&nbsp;';
276 
277  echo CHtml::link(
278  '<i class="glyphicon glyphicon-ok glyphicon glyphicon-white"></i>',
279  '#',
280  array('class' => 'btn btn-primary btn-extended-filter-apply')
281  );
282  echo '&nbsp;';
283  echo CHtml::link('<i class="glyphicon glyphicon-trash"></i>', '#', array('class' => 'btn btn-warning btn-extended-filter-delete'));
284  echo '</div></div>';
285  }
286  }
287 
295  protected function generateRegistryItemKey() {
296 
297  if (!count($this->filteredBy)) {
298  return null;
299  }
300  return md5($this->grid->id . CJSON::encode($this->filteredBy));
301  }
302 
312  protected function displayExtendedFilterValues($filteredBy) {
313 
314  $values = array();
315  foreach ($filteredBy as $key => $value) {
316  $values[] = '<span class="label label-info">' . $key . '</span> ' . $value;
317  }
318  return implode(', ', $values);
319  }
320 }