fitness-web/backend/controllers/CardController.php
Roland Schneider b6b5193120 Fix customer backend forms , fix site\index
backend create\update customer forms must display the same as the frontend
forms

site\index action should be friendly and related to this app
2015-11-02 18:41:36 +01:00

172 lines
4.5 KiB
PHP

<?php
namespace backend\controllers;
use Yii;
use common\models\Card;
use backend\models\CardSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\base\Object;
use yii\db\Query;
use common\models\Customer;
use yii\helpers\Json;
/**
* CardController implements the CRUD actions for Card model.
*/
class CardController extends \backend\controllers\BackendController
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
// allow authenticated users
[
'actions' => ['create','index','view','update','list'],
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
/**
* Lists all Card models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new CardSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Card model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Card model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Card();
$model->status = Card::STATUS_ACTIVE;
$model->type = Card::TYPE_RFID;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
\Yii::$app->session->setFlash( 'success','Card created!' );
if ( isset($_POST['create_next'])){
return $this->redirect(['create' ]);
}else{
return $this->redirect(['view', 'id' => $model->id_card]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Card model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_card]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Card 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 Card model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Card the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Card::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* Your controller action to fetch the list
*/
public function actionList($search = null) {
$query = new Query();
$query->select ( [
'card.number as number',
'customer.name as name',
"concat( card.number , case when customer.name is null then '' else customer.name end ) as txt ",
] )->from (Card::tableName() )->join("left join", Customer::tableName(), 'card.id_card = customer.id_customer_card')->where ( ' lower(number) LIKE "%' . strtolower ( $search ) . '%"' )->orderBy ( 'number' ) ;
if ( isset($_GET['onlyFree']) && $_GET['onlyFree'] == '1'){
$query->andWhere( 'customer.id_customer is null' );
}
$command = $query->createCommand ();
$data = $command->queryAll ();
$out = [ ];
foreach ( $data as $d ) {
$out [] = [
'number' => $d ['number'],
'name' => $d ['name'],
'txt' => $d ['txt'],
];
}
echo Json::encode ( $out );
}
}