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,85 +5,97 @@ namespace backend\controllers;
use Yii; use Yii;
use common\models\Card; use common\models\Card;
use backend\models\CardSearch; use backend\models\CardSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\base\Object;
use yii\db\Query; use yii\db\Query;
use common\models\Customer; use common\models\Customer;
use yii\helpers\Json; use yii\helpers\Json;
use backend\models\CardImportRfidForm;
use yii\web\UploadedFile;
use common\components\Helper;
/** /**
* CardController implements the CRUD actions for Card model. * CardController implements the CRUD actions for Card model.
*/ */
class CardController extends \backend\controllers\BackendController class CardController extends \backend\controllers\BackendController {
{ public function behaviors() {
public function behaviors()
{
return [ return [
'access' => [ 'access' => [
'class' => \yii\filters\AccessControl::className (), 'class' => \yii\filters\AccessControl::className (),
'rules' => [ 'rules' => [
// allow authenticated users // allow authenticated users
[ [
'actions' => ['create','index','view','update','list'], 'actions' => [
'create',
'index',
'view',
'update',
'list' ,
'import-rfid'
],
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => [
], '@'
]
]
]
// everything else is denied // everything else is denied
],
], ]
]; ];
} }
/** /**
* Lists all Card models. * Lists all Card models.
*
* @return mixed * @return mixed
*/ */
public function actionIndex() public function actionIndex() {
{
$searchModel = new CardSearch (); $searchModel = new CardSearch ();
$dataProvider = $searchModel->search ( Yii::$app->request->queryParams ); $dataProvider = $searchModel->search ( Yii::$app->request->queryParams );
return $this->render ( 'index', [ return $this->render ( 'index', [
'searchModel' => $searchModel, 'searchModel' => $searchModel,
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider
] ); ] );
} }
/** /**
* Displays a single Card model. * Displays a single Card model.
*
* @param integer $id * @param integer $id
* @return mixed * @return mixed
*/ */
public function actionView($id) public function actionView($id) {
{
return $this->render ( 'view', [ return $this->render ( 'view', [
'model' => $this->findModel($id), 'model' => $this->findModel ( $id )
] ); ] );
} }
/** /**
* Creates a new Card model. * Creates a new Card model.
* If creation is successful, the browser will be redirected to the 'view' page. * If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed * @return mixed
*/ */
public function actionCreate() public function actionCreate() {
{
$model = new Card (); $model = new Card ();
$model->status = Card::STATUS_ACTIVE; $model->status = Card::STATUS_ACTIVE;
$model->type = Card::TYPE_RFID; $model->type = Card::TYPE_RFID;
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) { if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
\Yii::$app->session->setFlash ( 'success', 'Card created!' ); \Yii::$app->session->setFlash ( 'success', 'Card created!' );
if (isset ( $_POST ['create_next'] )) { if (isset ( $_POST ['create_next'] )) {
return $this->redirect(['create' ]); return $this->redirect ( [
'create'
] );
} else { } else {
return $this->redirect(['view', 'id' => $model->id_card]); return $this->redirect ( [
'view',
'id' => $model->id_card
] );
} }
} else { } else {
return $this->render ( 'create', [ return $this->render ( 'create', [
'model' => $model, 'model' => $model
] ); ] );
} }
} }
@ -91,19 +103,21 @@ class CardController extends \backend\controllers\BackendController
/** /**
* Updates an existing Card model. * Updates an existing Card model.
* If update is successful, the browser will be redirected to the 'view' page. * If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $id * @param integer $id
* @return mixed * @return mixed
*/ */
public function actionUpdate($id) public function actionUpdate($id) {
{
$model = $this->findModel ( $id ); $model = $this->findModel ( $id );
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) { if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
return $this->redirect(['index']); return $this->redirect ( [
'index'
] );
// return $this->redirect(Yii::$app->request->referrer); // return $this->redirect(Yii::$app->request->referrer);
} else { } else {
return $this->render ( 'update', [ return $this->render ( 'update', [
'model' => $model, 'model' => $model
] ); ] );
} }
} }
@ -111,25 +125,27 @@ class CardController extends \backend\controllers\BackendController
/** /**
* Deletes an existing Card model. * Deletes an existing Card model.
* If deletion is successful, the browser will be redirected to the 'index' page. * If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param integer $id * @param integer $id
* @return mixed * @return mixed
*/ */
public function actionDelete($id) public function actionDelete($id) {
{
$this->findModel ( $id )->delete (); $this->findModel ( $id )->delete ();
return $this->redirect(['index']); return $this->redirect ( [
'index'
] );
} }
/** /**
* Finds the Card model based on its primary key value. * Finds the Card model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown. * If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id * @param integer $id
* @return Card the loaded model * @return Card the loaded model
* @throws NotFoundHttpException if the model cannot be found * @throws NotFoundHttpException if the model cannot be found
*/ */
protected function findModel($id) protected function findModel($id) {
{
if (($model = Card::findOne ( $id )) !== null) { if (($model = Card::findOne ( $id )) !== null) {
return $model; return $model;
} else { } else {
@ -137,7 +153,6 @@ class CardController extends \backend\controllers\BackendController
} }
} }
/** /**
* Your controller action to fetch the list * Your controller action to fetch the list
*/ */
@ -147,7 +162,7 @@ class CardController extends \backend\controllers\BackendController
$query->select ( [ $query->select ( [
'card.number as number', 'card.number as number',
'customer.name as name', 'customer.name as name',
"concat( card.number , case when customer.name is null then '' else customer.name end ) as txt ", "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' ); ] )->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') { if (isset ( $_GET ['onlyFree'] ) && $_GET ['onlyFree'] == '1') {
@ -161,12 +176,76 @@ class CardController extends \backend\controllers\BackendController
$out [] = [ $out [] = [
'number' => $d ['number'], 'number' => $d ['number'],
'name' => $d ['name'], 'name' => $d ['name'],
'txt' => $d ['txt'], 'txt' => $d ['txt']
]; ];
} }
echo Json::encode ( $out ); 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;
}
} }
$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() ?>