Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\controllers;
- use app\components\AccountFilter;
- use Yii;
- use app\models\Contactperson;
- use yii\data\ActiveDataProvider;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\web\UploadedFile;
- /**
- * AccountContactpersonController implements the CRUD actions for Contactperson model.
- */
- class AccountContactpersonController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['admin', 'manager'],
- ],
- ],
- 'denyCallback' => function($rule, $action){
- return $this->redirect('login');
- }
- ],
- 'verbs' => [
- 'class' => VerbFilter::class,
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- 'account' => [
- 'class' => AccountFilter::class
- ],
- ];
- }
- /**
- * Creates a new Contactperson model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new Contactperson();
- $model->account_id = Yii::$app->account->data->id;
- if( $model->load(Yii::$app->request->post()) ){
- $model->scenario = Contactperson::SCENARIO_SAVE;
- //which button?
- //upload submit button
- if( Yii::$app->request->post("upload") === "true" ){
- //upload image only
- $model->scenario = Contactperson::SCENARIO_UPLOAD_ONLY;
- $model->image = UploadedFile::getInstance($model, 'image');
- $model->upload();
- //save submit button triggert, as only btn left
- }else{
- //image already uploaded OR has changed after upload?
- if( !$model->uploaded ){ //OR isset($model->image) ){
- $model->image = UploadedFile::getInstance($model, 'image');
- $model->upload();
- }
- if( $model->save() ) {
- return $this->redirect('/contactperson/' . $model->id);
- }
- }
- }
- Yii::debug($model);
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- /**
- * Updates an existing Contactperson 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);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect('/contactperson/' . $model->id);
- }
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- /**
- * Lists all Contactperson models.
- * @return mixed
- */
- public function actionIndex(){
- $dataProvider = new ActiveDataProvider([
- 'query' => Contactperson::find(),
- ]);
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Contactperson 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),
- ]);
- }
- /**
- * Deletes an existing Contactperson 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 Contactperson model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Contactperson the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id){
- if (($model = Contactperson::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement