Advertisement
Guest User

Controller (example)

a guest
Dec 30th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.84 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\components\AccountFilter;
  6. use Yii;
  7. use app\models\Contactperson;
  8. use yii\data\ActiveDataProvider;
  9. use yii\filters\AccessControl;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. use yii\filters\VerbFilter;
  13. use yii\web\UploadedFile;
  14.  
  15. /**
  16.  * AccountContactpersonController implements the CRUD actions for Contactperson model.
  17.  */
  18. class AccountContactpersonController extends Controller
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function behaviors()
  24.     {
  25.         return [
  26.             'access' => [
  27.                 'class' => AccessControl::class,
  28.                 'rules' => [
  29.                     [
  30.                         'allow' => true,
  31.                         'roles' => ['admin', 'manager'],
  32.                     ],
  33.                 ],
  34.                 'denyCallback' => function($rule, $action){
  35.                     return $this->redirect('login');
  36.                 }
  37.             ],
  38.             'verbs' => [
  39.                 'class' => VerbFilter::class,
  40.                 'actions' => [
  41.                     'delete' => ['POST'],
  42.                 ],
  43.             ],
  44.             'account' => [
  45.                 'class' => AccountFilter::class
  46.             ],
  47.         ];
  48.     }
  49.  
  50.  
  51.  
  52.     /**
  53.      * Creates a new Contactperson model.
  54.      * If creation is successful, the browser will be redirected to the 'view' page.
  55.      * @return mixed
  56.      */
  57.     public function actionCreate()
  58.     {
  59.         $model = new Contactperson();
  60.         $model->account_id = Yii::$app->account->data->id;
  61.  
  62.         if( $model->load(Yii::$app->request->post()) ){
  63.             $model->scenario = Contactperson::SCENARIO_SAVE;
  64.             //which button?
  65.             //upload submit button
  66.             if( Yii::$app->request->post("upload") === "true" ){
  67.                 //upload image only
  68.                 $model->scenario = Contactperson::SCENARIO_UPLOAD_ONLY;
  69.                 $model->image = UploadedFile::getInstance($model, 'image');
  70.                 $model->upload();
  71.                 //save submit button triggert, as only btn left
  72.             }else{
  73.                 //image already uploaded OR has changed after upload?
  74.                 if( !$model->uploaded ){ //OR isset($model->image) ){
  75.                     $model->image = UploadedFile::getInstance($model, 'image');
  76.                     $model->upload();
  77.                 }
  78.                 if( $model->save() ) {
  79.                     return $this->redirect('/contactperson/' . $model->id);
  80.                 }
  81.             }
  82.         }
  83.         Yii::debug($model);
  84.  
  85.         return $this->render('create', [
  86.             'model' => $model,
  87.         ]);
  88.     }
  89.  
  90.  
  91.  
  92.     /**
  93.      * Updates an existing Contactperson model.
  94.      * If update is successful, the browser will be redirected to the 'view' page.
  95.      * @param integer $id
  96.      * @return mixed
  97.      * @throws NotFoundHttpException if the model cannot be found
  98.      */
  99.     public function actionUpdate($id)
  100.     {
  101.         $model = $this->findModel($id);
  102.  
  103.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  104.             return $this->redirect('/contactperson/' . $model->id);
  105.         }
  106.  
  107.         return $this->render('update', [
  108.             'model' => $model,
  109.         ]);
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.     /**
  119.      * Lists all Contactperson models.
  120.      * @return mixed
  121.      */
  122.     public function actionIndex(){
  123.         $dataProvider = new ActiveDataProvider([
  124.             'query' => Contactperson::find(),
  125.         ]);
  126.  
  127.         return $this->render('index', [
  128.             'dataProvider' => $dataProvider,
  129.         ]);
  130.     }
  131.  
  132.     /**
  133.      * Displays a single Contactperson model.
  134.      * @param integer $id
  135.      * @return mixed
  136.      * @throws NotFoundHttpException if the model cannot be found
  137.      */
  138.     public function actionView($id){
  139.         return $this->render('view', [
  140.             'model' => $this->findModel($id),
  141.         ]);
  142.     }
  143.  
  144.     /**
  145.      * Deletes an existing Contactperson model.
  146.      * If deletion is successful, the browser will be redirected to the 'index' page.
  147.      * @param integer $id
  148.      * @return mixed
  149.      * @throws NotFoundHttpException if the model cannot be found
  150.      */
  151.     public function actionDelete($id){
  152.         $this->findModel($id)->delete();
  153.  
  154.         return $this->redirect(['index']);
  155.     }
  156.  
  157.     /**
  158.      * Finds the Contactperson model based on its primary key value.
  159.      * If the model is not found, a 404 HTTP exception will be thrown.
  160.      * @param integer $id
  161.      * @return Contactperson the loaded model
  162.      * @throws NotFoundHttpException if the model cannot be found
  163.      */
  164.     protected function findModel($id){
  165.         if (($model = Contactperson::findOne($id)) !== null) {
  166.             return $model;
  167.         }
  168.  
  169.         throw new NotFoundHttpException('The requested page does not exist.');
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement