Commits

Insolita Donna  committed 7fc672f

New Qtoggle action - allow switch between range value - not (0,1) only

  • Participants
  • Parent commits 5723c08

Comments (0)

Files changed (2)

File JToggleColumn.php

      * @see sortable
      */
     public $name;
+    /**
+     * @var array with range of available values for qtoggle and it`s label (support text,image-url or html) @see labeltype
+     * for-ex: array('client'=>'image1.png','manager'=>'image2.png','boss'=>'image3.png','operator'=>'image4.png')
+     * OR array('0'=>'Draft','1'=>'Premoderation','2'=>'Public')
+     */
+    public $queue=array();
+    /**
+     * @var array with range of available values for qtoggle and it`s titles (for link title attribute)
+     * for-ex: array('client'=>'Client.Switch to manager','manager'=>'Manager.Switch to boss','boss'=>'Boss.Switch to operator','operator'=>'Operator.Switch to client')
+     * OR array('0'=>'Draft','1'=>'Premoderation','2'=>'Public')
+     */
+    public $queueTitles=array();
 
     /**
      * @var array the HTML options for the data cell tags.
     public $filter;
 
     /**
-     * @var string Name of the action
+     * @var string Name of the action ('toggle','switch' or 'qtoggle')
      */
     public $action;
 
             $this->sortable = false;
         if ($this->name === null)
             throw new CException(Yii::t('toggle_column', 'Model attribute ("name") must be specified for CToggleColumn.'));
+        if($this->action == 'qtoggle')
+        {
+            if  (empty($this->queue))
+            throw new CException(Yii::t('toggle_column', 'Please set correct queue array!!!'));
+            if  (empty($this->queueTitles))
+                throw new CException(Yii::t('toggle_column', 'Please set correct queueTitles array!!!'));
+             $archeq=array_diff_key($this->queue,$this->queueTitles);
+            if  (!empty($archeq))
+                throw new CException(Yii::t('toggle_column', 'Keys of array $queue and keys of array $queueTitles must be identical '));
+
+
+        }
 
         $this->initDefaultButtons();
 
         if ($this->action === null)
             $this->action = 'toggle';
 
-        $this->toggle_button = array(
-            'url' => 'Yii::app()->controller->createUrl("' . $this->action . '",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
-            'options' => array('class' => $this->name . '_toggle'),
-        );
+        if($this->action=='qtoggle'){
+            $range=array_keys($this->queue);
+            $range=implode(',',$range);
+            $this->toggle_button = array(
+                'url' => 'Yii::app()->controller->createUrl("' . $this->action . '",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '","range"=>"'.$range.'"))',
+                'options' => array('class' => $this->name . '_toggle'),
+            );
+        }else{
+            $this->toggle_button = array(
+                'url' => 'Yii::app()->controller->createUrl("' . $this->action . '",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
+                'options' => array('class' => $this->name . '_toggle'),
+            );
+        }
+
+
 
         if (Yii::app()->request->enableCsrfValidation) {
             $csrfTokenName = Yii::app()->request->csrfTokenName;
         if ($this->name !== null)
             $checked = CHtml::value($data, $this->name);
 
-        $button['title'] = $checked ? $this->checkedButtonTitle : $this->uncheckedButtonTitle;
-        $alt =CHtml::encode($button['title']);
-        $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
-        $options = isset($button['options']) ? $button['options'] : array();
-        $options=(!isset($options['title']))?array_merge($options,array('title'=>$alt)):$options;
-        switch($this->labeltype){
-            case 'image':{
-            $button['imageUrl'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
-            echo CHtml::link(CHtml::image($button['imageUrl'], $alt), $url, $options);
-            break;
-            }
-            case 'html':{
-            $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
-            echo CHtml::link($button['label'], $url, $options);
-            break;
+        if($this->action=='qtoggle'){
+            if(!isset($this->queueTitles[$checked])||!isset($this->queue[$checked])){
+                throw new CException(Yii::t('toggle_column', 'Unexpected Value - check jtogglecolumn settings for queue and queueTitles '));
             }
-            default:{
-            $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
-            echo CHtml::link(CHtml::encode($button['label']), $url, $options);
+            $button['title'] = $this->queueTitles[$checked];
+            $alt =CHtml::encode($button['title']);
+            $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
+            $options = isset($button['options']) ? $button['options'] : array();
+            $options=(!isset($options['title']))?array_merge($options,array('title'=>$alt)):$options;
+            switch($this->labeltype){
+                case 'image':{
+                echo CHtml::link(CHtml::image($this->queue[$checked], $alt), $url, $options);
+                break;
+                }
+                case 'html':{
+                echo CHtml::link($this->queue[$checked], $url, $options);
+                break;
+                }
+                default:{
+                echo CHtml::link(CHtml::encode($this->queue[$checked]), $url, $options);
+                }
             }
 
+        }else{
+            $button['title'] = $checked ? $this->checkedButtonTitle : $this->uncheckedButtonTitle;
+            $alt =CHtml::encode($button['title']);
+            $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
+            $options = isset($button['options']) ? $button['options'] : array();
+            $options=(!isset($options['title']))?array_merge($options,array('title'=>$alt)):$options;
+            switch($this->labeltype){
+                case 'image':{
+                $button['imageUrl'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
+                echo CHtml::link(CHtml::image($button['imageUrl'], $alt), $url, $options);
+                break;
+                }
+                case 'html':{
+                $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
+                echo CHtml::link($button['label'], $url, $options);
+                break;
+                }
+                default:{
+                $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
+                echo CHtml::link(CHtml::encode($button['label']), $url, $options);
+                }
+            }
         }
 
 
+
+
     }
 
     /**

File QtoggleAction.php

+<?php
+
+/**
+ * CToggleColumn class file.
+ *
+ * @author DonnaInsolita
+ * @copyright Copyright &copy; 2012 DonnaInsolita
+ * @license http://www.yiiframework.com/license/
+ */
+class QtoggleAction extends CAction {
+	 
+    public function run($id,$attribute,$range) {
+		
+        if(Yii::app()->request->isPostRequest)
+        {
+            // we only allow deletion via POST request
+            $model = $this->controller->loadModel($id);
+            $range=explode(',',$range);
+            $curr=array_search($model->$attribute,$range);
+            $next=(isset($range[$curr+1]))?$range[$curr+1]:$range[0];
+            $model->$attribute = $next;
+            $model->save(false);
+            
+            // 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.');
+    }
+ 
+}
+
+?>