1: <?php
2: Yii::import('zii.widgets.grid.CButtonColumn');
3:
4:
5: /**
6: * EButtonColumnWithClearFilters class.
7: *
8: * The EButtonColumnWithClearFilters extension adds up some functionality to the default
9: * possibilites of zii's CButtonColumn implementation.
10: *
11: * An image will be placed in the top column(on same line of AJAX filters). When clicked
12: * the filters will be cleared, the content will be refreshed with all items available.
13: *
14: *
15: * To use this extension, just copy this file to your components/ directory,
16: * add 'import' => 'application.components.EButtonColumnWithClearFilters', [...] to your
17: * config/main.php and use this column on each widget's Column array you would like to
18: * inherit the new possibilities:
19: *
20: * array(
21: * 'class'=>'EButtonColumnWithClearFilters',
22: * //'clearVisible'=>true,
23: * //'onClick_BeforeClear'=>'alert('this js fragment executes before clear');',
24: * //'onClick_AfterClear'=>'alert('this js fragment executes after clear');',
25: * //'clearHtmlOptions'=>array('class'=>'custom-clear'),
26: * //'imageUrl'=>'/path/to/custom/image/delete.png',
27: * //'url'=>'Yii::app()->controller->createUrl(Yii::app()->controller->action->ID,array("clearFilters"=>1))',
28: * //'label'=>'My Custom Label',
29: * ),
30: *
31: *
32: * In your controller in the same action the widget is displayed, you have to add
33: *
34: * if (intval(Yii::app()->request->getParam('clearFilters'))==1) {
35: * $model->unsetAttributes();
36: * $this->redirect(array($this->action->ID));
37: * }
38: *
39: * All posible customizations have been enumerated above, you shall comment out those that
40: * you won't override. The minial setup is just the class type for the Columns.
41: *
42: * clearVisible: a PHP expression for determining whether the button is visible
43: *
44: * onClick_BeforeClear: If you want to execute certain JS code before the filters are cleared out,
45: * use this property to pass your custom code. You are allowed to use 'return false;' only, when you want
46: * to stop the clear to happen. This will stop all further JS code, and HTTP request to be executed.
47: * You are not allowed to use 'return true;' it will break the components usage.
48: *
49: * onClick_AfterClear: If you want to execute certain JS code after clear, but before the AJAX call
50: * use this property to pass your custom code. You are allowed to use 'return false' only, when you want
51: * to stop the AJAX call to happen. This will stop the form to be reloaded.
52: * If you want to clear the form by classic GET request, and not by ajax you shall 'return true;' here.
53: *
54: * clearHtmlOptions: Associative array of html elements to be passed for the button
55: * default is: array('class'=>'clear','id'=>'cbcwr_clear','style'=>'text-align:center;display:block;');
56: *
57: * imageUrl: image URL of the button. If not set or false, a text link is used
58: * Default is: $this->grid->baseScriptUrl.'/delete.png'
59: *
60: * url: a PHP expression for generating the URL of the button
61: * Default is: Yii::app()->controller->createUrl(Yii::app()->controller->action->ID,array("clearFilters"=>1))
62: *
63: * label: Label tag to be used on the button when no URL is given
64: * Default is: Clear Filters
65: *
66: * This extension comes handy when you use Remember Filters extension for GridView
67: * http://www.yiiframework.com/extension/remember-filters-gridview
68: *
69: * Please VOTE this extension if helps you at:
70: * http://www.yiiframework.com/extension/clear-filters-gridview
71: *
72: * @author Marton Kodok http://www.yiiframework.com/forum/index.php?/user/8824-pentium10/
73: * @link http://www.yiiframework.com/
74: * @license http://opensource.org/licenses/bsd-license.php
75: * @version 1.0
76: * @package application.components
77: */
78: class EButtonColumnWithClearFilters extends CButtonColumn {
79:
80: /**
81: * Private member to store internally the button definition
82: *
83: * @var array
84: */
85: private $_clearButton;
86: /**
87: * Private member to store as a backup the template usage.
88: *
89: * @var string
90: */
91: private $_templateB;
92:
93: /**
94: * a PHP expression for determining whether the button is visible
95: *
96: * @var string
97: */
98: public $clearVisible;
99: /**
100: * JS code to be invoked when the button is clicked, this is invoked before clearing the form fields;
101: * Returning false from this code fragment prevents the AJAX to be executed. Only use 'return' block when you want to stop further steps execution.
102: *
103: * @var string
104: */
105: public $onClick_BeforeClear;
106: /**
107: * JS code to be invoked when the button is clicked, this is invoked after clearing the form fields, before AJAX;
108: * Returning false from this code fragment prevents the AJAX to be executed. Only use 'return' block when you want to stop further steps execution.
109: *
110: * @var string
111: */
112: public $onClick_AfterClear;
113:
114: /**
115: * Associative array of html elements to be passed for the button
116: * default is: array('class'=>'clear','id'=>'cbcwr_clear','style'=>'text-align:center;display:block;');
117: *
118: * @var array
119: */
120: public $clearHtmlOptions;
121:
122: /**
123: * image URL of the button. If not set or false, a text link is used
124: * Default is: $this->grid->baseScriptUrl.'/delete.png'
125: *
126: * @var string
127: */
128: public $imageUrl;
129:
130: /**
131: * a PHP expression for generating the URL of the button
132: * Default is: Yii::app()->controller->createUrl(Yii::app()->controller->action->ID,array("clearFilters"=>1))
133: *
134: * @var string
135: */
136: public $url;
137:
138: /**
139: * Label tag to be used on the button when no URL is given
140: * Default is: Clear Filters
141: *
142: * @var unknown_type
143: */
144: public $label;
145:
146: public function init()
147: {
148:
149: //initializ variables
150: $_customJS=null;
151: $_beforeAjax=null;
152: $_click=null;
153: $_visible=null;
154: $_options=null;
155: $_imageUrl=null;
156:
157: //define defaults
158: $_optionsDefault=array('class'=>'clear','id'=>'cbcwr_clear','style'=>'text-align:center;display:block;');
159:
160: // handle custom JS setup
161: if (!empty($this->onClick_BeforeClear)) {
162: $_customJS=$this->onClick_BeforeClear.';';
163: }
164: if (!empty($this->onClick_AfterClear)) {
165: $_beforeAjax=$this->onClick_AfterClear.";\r\n";
166: }
167: // turn custom setup into representative output
168: $_click="js:function() {{$_customJS} return cbcwr_clearFields() }";
169: $_visible=is_bool($this->clearVisible)?( ($this->clearVisible)?'true':'false'):$this->clearVisible;
170: if (empty($this->clearHtmlOptions)) {
171: $this->clearHtmlOptions=array();
172: }
173: $_options=@array_merge($_optionsDefault,$this->clearHtmlOptions);
174:
175: if (!empty($imageUrl)) {
176: $_imageUrl=$this->imageUrl;
177: } else {
178: $_imageUrl=$this->grid->baseScriptUrl.'/delete.png';
179: }
180:
181: if (!empty($this->url)) {
182: $_url=$this->url;
183: } else {
184: $_url='Yii::app()->controller->createUrl(Yii::app()->controller->action->ID,array("clearFilters"=>1))';
185: }
186:
187: if (!empty($this->label)) {
188: $_label=Yii::t('app',$this->label);
189: } else {
190: $_label=Yii::t('app','Clear Filters');
191: }
192:
193:
194: // define the button structure to be used
195: $this->_clearButton = array(
196: 'label'=>$_label, // text label of the button
197: 'url'=>$_url, // a PHP expression for generating the URL of the button
198: 'imageUrl'=>$_imageUrl, // image URL of the button. If not set or false, a text link is used
199: 'options'=>$_options, // HTML options for the button tag
200: 'click'=>$_click, // a JS function to be invoked when the button is clicked
201: 'visible'=>$_visible, // a PHP expression for determining whether the button is visible
202: );
203:
204:
205:
206: $this->buttons=array(
207: 'clear' => $this->_clearButton,
208:
209: );
210:
211: $this->_templateB=$this->template;
212: $this->template.="{clear}";
213:
214: $script=<<<HTMLEND
215: $.fn.clearFields = $.fn.clearInputs = function() {
216: return this.each(function() {
217: var t = this.type, tag = this.tagName.toLowerCase();
218: if (t == 'text' || t == 'password' || tag == 'textarea') {
219: this.value = '';
220: }
221: else if (t == 'checkbox' || t == 'radio') {
222: this.checked = false;
223: }
224: else if (tag == 'select') {
225: this.selectedIndex = -1;
226: }
227: });
228: };
229:
230: function cbcwr_clearFields() {
231: try
232: {
233: $('#{$this->grid->id} :input').clearFields(); // this will clear all input in the current grid
234: {$_beforeAjax} $('#{$this->grid->id} :input').first().trigger('change');// to submit the form
235: return false;
236: }
237: catch(cbwr_err)
238: {
239: return false;
240: }
241: }
242: HTMLEND;
243: Yii::app()->clientScript ->registerScript(__CLASS__.'clearFields',$script,CClientScript::POS_HEAD);
244:
245: // call parent to initialize other buttons
246: parent::init();
247: }
248:
249:
250: public function renderFilterCell()
251: {
252: // initialise variables
253: $row=null;
254: $data=null;
255: // restore template
256: $this->template=$this->_templateB;
257: // output
258: echo "<td>";
259: echo $this->renderButton('clear',$this->_clearButton,$row=array(),$data=array());
260: echo "</td>";
261: }
262:
263: /**
264: * Static method to check if a model uses a certain behavior class
265: *
266: * @param CModel $model
267: * @param string $behaviorClass
268: * @return boolean
269: */
270: private static function modelUsesBehavior($model,$behaviorClass) {
271: $behaviors=$model->behaviors();
272: if (is_array($behaviors)) {
273: foreach ($behaviors as $behavior => $behaviorDefine) {
274: if (is_array($behavior)) {
275: $className=$behavior['class'];
276: } else {
277: $className=$behavior;
278: }
279: if (strpos($className,$behaviorClass)!==false) {
280: return true;
281: }
282: }
283: }
284: return false;
285: }
286:
287: public static function clearFilters($controller,$model) {
288: $model->unsetAttributes();
289: if (EButtonColumnWithClearFilters::modelUsesBehavior($model,'ERememberFiltersBehavior')) {
290: try {
291: $model->unsetAllFilters();
292: }
293: catch (Exception $e) {
294:
295: }
296: }
297: if(isset($_GET['id'])){
298: $controller->redirect(array($controller->action->ID,'id'=>Yii::app()->request->getParam('id')));
299: }
300: else{
301: $controller->redirect(array($controller->action->ID));
302: }
303: }
304:
305: }
306: ?>
307: