fitness-web/frontend/controllers/CustomerController.php

255 lines
6.9 KiB
PHP

<?php
namespace frontend\controllers;
use Yii;
use common\models\Customer;
use frontend\models\ReceptionForm;
use frontend\models\CustomerSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Card;
use frontend\models\CustomerUpdate;
use frontend\models\CustomerCreate;
use common\models\Image;
use yii\base\Exception;
use common\models\Log;
/**
* CustomerController implements the CRUD actions for Customer model.
*/
class CustomerController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['create', 'update','reception','contract'],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
public function actionReception($number = ""){
$model = new ReceptionForm();
$model->number = $number;
$model->readCard();
$model->mkDoorLog();
if ( $model->isFreeCard() ){
return $this->redirect([ 'create', 'number' => $model->card->number ]);
}else if ( $model->isCardWithKey()){
return $this->redirect([ 'product/sale', 'number' => $model->card->number ]);
}else if ( $model->isCustomerWithTicket()){
// return $this->redirect([ 'product/sale', 'number' => $model->card->number ]);
// return $this->redirect([ 'customer/reception', 'number' => $model->card->number ]);
}else if ( $model->isCardWithCustomer() ){
return $this->redirect([ 'ticket/create', 'number' => $model->card->number ]);
}
return $this->render('reception',['model' => $model]);
}
/**
* Lists all Customer models.
* @return mixed
*/
/*
public function actionIndex()
{
$searchModel = new CustomerSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
*/
/**
* Displays a single Customer model.
* @param integer $id
* @return mixed
*/
/*
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
*/
/**
* Creates a new Customer model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate($number = null)
{
$model = new CustomerCreate();
$model->country = "Magyarország";
$model->id_user = Yii::$app->user->id;
$model->warn_mail_ticket_expire_enabled = 1;
$model->newsletter = 0;
$receptionForm = new ReceptionForm();
$receptionForm->number = $number;
$receptionForm->readCard();
if ( isset($number)){
$model->cardNumber = $number;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$this->saveBinaryImage($model);
\Yii::$app->session->setFlash( 'success','Vendég létrehozva!' );
Log::log([
'type' =>Log::$TYPE_CREATE_CUSTOMER,
'message' => 'Új vendég:' .$model->name,
'id_customer' => $model->id_customer
]);
return $this->redirect(['update', 'number' => $model->cardNumber]);
} else {
return $this->render('create', [
'model' => $model,
'receptionForm' => $receptionForm
]);
}
}
/**
* Updates an existing Customer model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param null $number
* @return mixed
* @throws NotFoundHttpException
* @internal param int $id
*/
public function actionUpdate($number = null)
{
$card = null;
$model = null;
$receptionForm = new ReceptionForm();
$receptionForm->number = $number;
$receptionForm->readCard();
if ( $number != null ){
$card = Card::readCard($number);
if ( $card != null ){
$model = CustomerUpdate::find()->innerJoin(Card::tableName(), "customer.id_customer_card = card.id_card")->andWhere( [ 'customer.id_customer_card' => $card->id_card ])->one();
}
}
if ( $model == null) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$model->birthdate= isset($model->birthdate ) ? Yii::$app->formatter->asDate($model->birthdate) :'';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$this->saveBinaryImage($model);
\Yii::$app->session->setFlash( 'success','Vendég módosításai elmentve' );
$cardNumber = $card->number;
if ( isset( $model->replacementCard ) ){
$cardNumber = $model->replacementCard->number;
}
return $this->redirect(['update', 'number' => $cardNumber ]);
} else {
return $this->render('update', [
'model' => $model,
'receptionForm' => $receptionForm
]);
}
}
protected function saveBinaryImage($model){
if ( !empty($model->photo_data)){
$encoded_data = $model->photo_data;
$binary_data = base64_decode( $encoded_data );
// save to server (beware of permissions)
$path = \common\components\Image::saveBinary($binary_data,'profile');
$image = new Image();
$image->path = $path;
$image->save();
//todo delete old image
$model->id_image = $image->id_image;
$model->save(false);
}
}
// s
/**
* Deletes an existing Customer model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
/*
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
*/
/**
* Finds the Customer model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Customer the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Customer::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}