add import card rfid key

This commit is contained in:
Roland Schneider 2016-01-07 16:31:06 +01:00
parent 39ae361505
commit d2bc8076ca
3 changed files with 276 additions and 151 deletions

View File

@ -5,168 +5,247 @@ 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;
use backend\models\CardImportRfidForm;
use yii\web\UploadedFile;
use common\components\Helper;
/**
* CardController implements the CRUD actions for Card model.
*/
class CardController extends \backend\controllers\BackendController
{
public function behaviors()
{
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
],
],
'access' => [
'class' => \yii\filters\AccessControl::className (),
'rules' => [
// allow authenticated users
[
'actions' => [
'create',
'index',
'view',
'update',
'list' ,
'import-rfid'
],
'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);
/**
* 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,
]);
}
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),
]);
}
/**
* 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();
/**
* 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,
]);
}
}
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);
/**
* 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(['index']);
// return $this->redirect(Yii::$app->request->referrer);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
return $this->redirect ( [
'index'
] );
// return $this->redirect(Yii::$app->request->referrer);
} 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();
/**
* 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']);
}
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.');
}
}
/**
* 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 );
}
public function actionImportRfid() {
$model = new CardImportRfidForm ();
$arr = [];
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
// print_r($model->file);
// $model->message = "ok";
$file = $model->file->tempName;
$file = fopen ( $file , "r" );
$trans = null;
$i = 0;
$j = 0;
while ( ($data = fgetcsv ( $file, 0, "," )) != null ) {
// if ($i == 0) {
// $i ++;
// continue;
// }
$j++;
$number = $key = false;
if ( isset($data[0]) ){
$number = $data[0];
}
if ( isset($data[1]) ){
$key = $data[1];
}
if ( isset($number) && isset($key)){
$item = [];
$item['number'] = $number;
$item['key'] = Helper::fixAsciiChars( $key);
$arr[] = $item;
}
/**
* 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 );
}
$failed = [];
$sqls = [];
foreach ($arr as $item ){
$card = Card::find()->andWhere(['number' => $item['number']])->one();
if ( $card != null ){
$card->rfid_key = $item['key'];
$sql = "update card set rfid_key = '" .$item['key'] ."' where id_card = " .$card->id_card .";";
$sqls[] = $sql;
$i++;
}else{
$failed [] = $item;
}
}
$model->message = "rows read: " .$j ." / ". "updated cards: " .$i;
$model->message .= "<br> array size: " . count($arr);
$model->message .= "<br> failed: " . print_r($failed,true);
$model->message .= "<br>sql:";
$model->message .= "<br>". implode("<br>", $sqls);
}
return $this->render ( 'importRfid.php', [
'model' => $model
] );
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Ticket;
use common\models\Account;
use yii\web\UploadedFile;
/**
* ContactForm is the model behind the contact form.
* @property \Yii\web\UploadedFile $file
*/
class CardImportRfidForm extends Model{
public $file;
public $message;
public function rules(){
return [
[['file'], 'file']
];
}
}

View File

@ -0,0 +1,16 @@
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file')->fileInput() ?>
<button>Submit</button>
<?php
echo ($model->message);
?>
<?php ActiveForm::end() ?>