Commits

Nikola Trifunović committed ac0fddd

Added ability to change action(two included: toggle(default) and switch), now using CActions

Comments (0)

Files changed (4)

JToggleColumn.php

     public $filter;
     
     /**
+     * @var string Name of the action
+     */
+    public $action;
+    
+    /**
      * @var string Assets url
      */
     private $_assetsUrl;
             $this->checkedButtonImageUrl = $this->getAssetsUrl(). '/checked.png';
         if ($this->uncheckedButtonImageUrl === null)
             $this->uncheckedButtonImageUrl = $this->getAssetsUrl() . '/unchecked.png';
+            
+        if ($this->action === null)
+            $this->action = 'toggle';
 
         $this->toggle_button = array(
-            'url' => 'Yii::app()->controller->createUrl("toggle",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
+            'url' => 'Yii::app()->controller->createUrl("' . $this->action . '",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
             'options' => array('class' => $this->name . '_toggle'),
         );
 
 JToggleColumn
-=====
+====
 Column for CGridView which toggles the boolean ( TINYINT(1) ) value of model attribute. Tested with Yii 1.10.
 
 Example
 ====
 ![JToggleColumn](https://bitbucket.org/johonunu/jtogglecolumn/raw/6220c9674443/example.png)
 
+History
+====
+24.04.2012 - first release
+25.04.2012 - added filter option and is now using assets
+17.06.2012 - added ability to change action(two included: toggle(default) and switch), now using CActions
+
 Tutorial
 ====
 Extract downloaded zip to your components or extensions directory.
 
 Define a JToggleColumn in your CGridView widget:
 
-    <?php $this->widget('zii.widgets.grid.CGridView', array(
-        'id'=>'user-grid',
-        'dataProvider'=>$model->search(),
-        'filter'=>$model,
-        'columns'=>array(
-                'id',
-                'username',
-                'password',
-                'email',
-                'role',
-                array(
-                        'class'=>'JToggleColumn',
-                        'name'=>'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
-                        'filter' => array('0' => 'No', '1' => 'Yes'), // filter
-                        'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
-                ),
-                array(
-                        'class'=>'JToggleColumn',
-                        'name'=>'visible', // boolean model attribute (tinyint(1) with values 0 or 1)
-                        'filter' => array('0' => 'No', '1' => 'Yes'), // filter
-                        'checkedButtonImageUrl'=>'/auth/images/check.png', // checked image
-                        'uncheckedButtonImageUrl'=>'/auth/images/uncheck.png', // unchecked image
-                        'checkedButtonLabel'=>'Uncheck this', // tooltip
-                        'uncheckedButtonLabel'=>'Check this', // tooltip
-                        'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
-        ),
-        array(
-            'class'=>'CButtonColumn',
-        ),
-    ),
-    )); ?>
+    <?php 
+    
+    $this->widget('zii.widgets.grid.CGridView', array(
+            'id'=>'language-grid',
+            'dataProvider'=>$model->search(),
+            'filter'=>$model,
+            'columns'=>array(
+                    'id',
+                    'name',
+                    'lang_code',
+                    'time_format',
+                    array(
+                                    'class'=>'JToggleColumn',
+                                    'name'=>'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
+                                    'filter' => array('0' => 'No', '1' => 'Yes'), // filter
+                                    'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
+                    ),
+                    array(
+                                            'class'=>'JToggleColumn',
+                                            'name'=>'is_default', // boolean model attribute (tinyint(1) with values 0 or 1)
+                                            'filter' => array('0' => 'No', '1' => 'Yes'), // filter
+                                            'action'=>'switch', // other action, default is 'toggle' action
+                                            'checkedButtonImageUrl'=>'/images/toggle/yes.png', // checked image
+                                            'uncheckedButtonImageUrl'=>'/images/toggle/no.png', // unchecked image
+                                            'checkedButtonLabel'=>'No', // tooltip
+                                            'uncheckedButtonLabel'=>'Yes', // tooltip
+                                            'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
+                    ),
+                    array(
+                            'class'=>'CButtonColumn',
+                    ),
+            ),
+    )); 
+
+    ?>
  
-Create action in your controller:
+Add action(s) in your controller:
 
     <?php
     
-    public function actionToggle($id,$attribute)
-    {
-        if(Yii::app()->request->isPostRequest)
-        {
-            // we only allow deletion via POST request
-            $model = $this->loadModel($id);
-            $model->$attribute = ($model->$attribute==0)?1:0;
-            $model->save();
-            
-            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
-            if(!isset($_GET['ajax']))
-                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
-            }
-            else
-            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
-        }
+    public function actions(){
+        return array(
+                'toggle'=>'ext.jtogglecolumn.ToggleAction',
+                'switch'=>'ext.jtogglecolumn.SwitchAction', // only if you need it
+        );
+    }
     
     ?>
 
-Don't forget to add this action to controllers accessRules.
+Don't forget to add this action to controllers accessRules:
+
+    <?php
+
+    public function accessRules()
+    {
+            return array(
+                    array('allow',
+                            'actions'=>array('toggle','switch'),
+                            'users'=>array('admin'),
+                    )
+            );
+    }
+
+    ?>
+<?php
+
+/**
+ * CToggleColumn class file.
+ *
+ * @author Nikola Trifunovic <johonunu@gmail.com>
+ * @link http://www.trifunovic.me/
+ * @copyright Copyright &copy; 2012 Nikola Trifunovic
+ * @license http://www.yiiframework.com/license/
+ */
+class SwitchAction extends CAction {
+	 
+    public function run($id,$attribute) {
+		
+        if(Yii::app()->request->isPostRequest)
+        {
+            // we only allow deletion via POST request
+            $model = $this->controller->loadModel($id);
+            $model->updateAll(array($attribute=>0));
+            $model->$attribute = 1;
+            $model->save();
+            
+            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
+            if(!isset($_GET['ajax']))
+                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
+        }
+        else
+        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
+    }
+ 
+}
+
+?>
+<?php
+
+/**
+ * CToggleColumn class file.
+ *
+ * @author Nikola Trifunovic <johonunu@gmail.com>
+ * @link http://www.trifunovic.me/
+ * @copyright Copyright &copy; 2012 Nikola Trifunovic
+ * @license http://www.yiiframework.com/license/
+ */
+class ToggleAction extends CAction {
+	 
+    public function run($id,$attribute) {
+		
+        if(Yii::app()->request->isPostRequest)
+        {
+            // we only allow deletion via POST request
+            $model = $this->controller->loadModel($id);
+            $model->$attribute = ($model->$attribute==0)?1:0;
+            $model->save();
+            
+            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
+            if(!isset($_GET['ajax']))
+                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
+        }
+        else
+        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
+    }
+ 
+}
+
+?>