Commits

Blue Spy committed 44b401d

Added two properties to let checkedValue and uncheckedValue
could be others than 1 and 0.

Comments (0)

Files changed (3)

JToggleColumn.php

      */
     public $uncheckedButtonLabel;
 
+	public $checkedValue = 1;
+
+	public $uncheckedValue = 0;
+
     /**
      * @var string the image URL for the toggle button. If not set, an integrated image will be used.
      * You may set this property to be false to render a text link instead.
         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->toggle_button !== null)
+		{
+			$this->toggle_button = array(
+				'url' => 'Yii::app()->controller->createUrl("' . $this->action . '",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
+				'options' => array('class' => $this->name . '_toggle'),
+			);
+		} else
+		{
+			$this->toggle_button = array(
+				'options' => array('class' => $this->name . '_toggle'),
+			);
+		}
+
+		$data = "'checkedValue':'".$this->checkedValue."','uncheckedValue':'".$this->uncheckedValue."',";
 
         if (Yii::app()->request->enableCsrfValidation) {
             $csrfTokenName = Yii::app()->request->csrfTokenName;
             $csrfToken = Yii::app()->request->csrfToken;
-            $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
+            $data .= "'$csrfTokenName':'$csrfToken',";
         }
-        else
-            $csrf = '';
+
+		$data = "data:{" . substr($data,0,-1) . "},";
 
         $this->toggle_button['click'] = <<<EOD
 function() {
 	var th=this;
+	if(!confirm('Are you sure you want to change this item value?')) return false;
 	$.fn.yiiGridView.update('{$this->grid->id}', {
 		type:'POST',
-		url:$(this).attr('href'),$csrf
+		url:$(this).attr('href'),$data
 		success:function(data) {
 			$.fn.yiiGridView.update('{$this->grid->id}');
 		}
         if ($this->name !== null)
             $checked = CHtml::value($data, $this->name);
 
-        $button['imageUrl'] = $checked ? $this->checkedButtonImageUrl : $this->uncheckedButtonImageUrl;
-        $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
+        $button['imageUrl'] = $checked == $this->checkedValue ? $this->checkedButtonImageUrl : $this->uncheckedButtonImageUrl;
+        $button['label'] = $checked == $this->checkedValue ? $this->checkedButtonLabel : $this->uncheckedButtonLabel;
 
         $label = $button['label'];
         $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
         {
             // we only allow deletion via POST request
             $model = $this->controller->loadModel($id);
-            $model->updateAll(array($attribute=>0));
-            $model->$attribute = 1;
+            $model->updateAll(array($attribute=>$_POST['checkedValue']));
+            $model->$attribute = $_POST['uncheckedValue'];
             $model->save(false);
             
             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         {
             // we only allow deletion via POST request
             $model = $this->controller->loadModel($id);
-            $model->$attribute = ($model->$attribute==0)?1:0;
+            $model->$attribute = ($model->$attribute==$_POST['uncheckedValue'])?$_POST['checkedValue']:$_POST['uncheckedValue'];
             $model->save(false);
             
             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser