Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (success)
71.43%
5 / 7
CRAP
90.00% covered (success)
90.00%
36 / 40
ActivityAreaController
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (success)
71.43%
5 / 7
14.20
90.00% covered (success)
90.00%
36 / 40
 behaviors
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 actionIndex
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 actionView
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 actionCreate
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
10 / 10
 actionUpdate
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
2.31
57.14% covered (warning)
57.14%
4 / 7
 findModel
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
<?php
namespace backend\controllers;
use Yii;
use common\models\ActivityArea;
use backend\models\ActivityAreaSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/**
 * ActivityAreaController implements the CRUD actions for ActivityArea model.
 */
class ActivityAreaController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'view', 'create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Lists all ActivityArea models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ActivityAreaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single ActivityArea model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $itemsSearchModel = new ActivityAreaSearch(['parent_id' => $id]);
        $itemsDataProvider = $itemsSearchModel->search(Yii::$app->request->queryParams);
        return $this->render('view', [
            'model' => $this->findModel($id),
            'itemsSearchModel' => $itemsSearchModel,
            'itemsDataProvider' => $itemsDataProvider,
        ]);
    }
    /**
     * Creates a new ActivityArea model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($parent_id = null)
    {
        $model = new ActivityArea();
        $model->parent_id = $parent_id;
        $activityAreas = null;
        if ($parent_id === null) {
            $activityAreas = ActivityArea::find()->all();
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'activityAreas' => $activityAreas,
            ]);
        }
    }
    /**
     * Updates an existing ActivityArea model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $activityAreas = ActivityArea::find()->where(['not', ['id' => $id]])->all();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'activityAreas' => $activityAreas,
            ]);
        }
    }
    /**
     * Deletes an existing ActivityArea model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if ($model->getItems()->count() > 0) {
            Yii::$app->session->setFlash('error', Yii::t('app', 'Activity Area can not be removed.'));
            Yii::$app->session->setFlash('info', Yii::t('app', 'Remove all sub activity areas first.'));
            return $this->redirect(['view', 'id' => $id]);
        } else {
            $model->delete();
        }
        return $this->redirect(['index']);
    }
    /**
     * Finds the ActivityArea model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ActivityArea the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ActivityArea::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}