Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/controllers/
Upload File :
Current File : /var/www/html/sprm/backend/controllers/CorruptionWantedController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\CorruptionWanted;
use backend\models\CorruptionWantedSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;
use yii\web\UploadedFile;
use backend\models\CorruptionWantedLawyer;
use backend\models\CorruptionWantedCase;

/**
 * CorruptionWantedController implements the CRUD actions for CorruptionWanted model.
 */
class CorruptionWantedController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CorruptionWanted models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new CorruptionWantedSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
    }

    /**
     * Displays a single CorruptionWanted model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', ['model' => $this->findModel($id),]);
    }

    /**
     * Creates a new CorruptionWanted model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        
        
        $model = new CorruptionWanted();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            $model->img_url = UploadedFile::getInstance($model, 'img_url');
            if (!empty($model->img_url)) {
                $file_name = str_replace(' ', '-', strtolower($model->name . ' ' . date("dmY")));
                $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $file_name);
                $img_name = $file_name . '.' . $model->img_url->extension;
                if ($model->img_url->saveAs('uploads/wanted/' . $img_name))
                    $model->img_url = $img_name;
            }
            
            if ($model->save()) {
                return $this->redirect(['index']);
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing CorruptionWanted model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $img_original = $model->img_url;
        if ($model->load(Yii::$app->request->post())) {
            $model->img_url = UploadedFile::getInstance($model, 'img_url');
            if (!empty($model->img_url)) {
                $file_name = str_replace(' ', '-', strtolower($model->name . ' ' . date("dmY")));
                $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $file_name);
                $img_name = $file_name . '.' . $model->img_url->extension;
                if ($model->img_url->saveAs('uploads/wanted/' . $img_name))
                    $model->img_url = $img_name;
            } else {
                $model->img_url = $img_original;
            }
            
            if ($model->save()) {
                return $this->redirect(['index']);
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing CorruptionWanted model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the CorruptionWanted model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CorruptionWanted the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = CorruptionWanted::findOne($id)) !== null) {
            return $model;
        }
        throw new NotFoundHttpException('The requested page does not exist.');
    }

}