<?php

class BatchOperationAction extends CAction {

    public function run($operation) {
        if (isset($_POST['items']) && is_array($_POST['items'])) {
            foreach ($_POST['items'] as $comment) {
                $model = $this->controller->loadModel(abs(intval($comment)));
                if ($model !== NULL) {
                    switch ($operation) {
                        case 'delete':
                            $model->delete();
                            break;
                        case 'approve': {
                                $model->status = 1;
                                $model->save();
                                break;
                            }
                        case 'draft': {
                                $model->status = 0;
                                $model->save();
                                break;
                            }
                    }
                }
            }
        }
        if (Yii::app()->request->isAjaxRequest) {
            $this->controller->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
    }

}
