add import card rfid key
This commit is contained in:
parent
39ae361505
commit
d2bc8076ca
@ -5,85 +5,97 @@ 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'],
|
||||
'actions' => [
|
||||
'create',
|
||||
'index',
|
||||
'view',
|
||||
'update',
|
||||
'list' ,
|
||||
'import-rfid'
|
||||
],
|
||||
'allow' => true,
|
||||
'roles' => ['@'],
|
||||
],
|
||||
'roles' => [
|
||||
'@'
|
||||
]
|
||||
]
|
||||
]
|
||||
// everything else is denied
|
||||
],
|
||||
],
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all Card models.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionIndex()
|
||||
{
|
||||
public function actionIndex() {
|
||||
$searchModel = new CardSearch ();
|
||||
$dataProvider = $searchModel->search ( Yii::$app->request->queryParams );
|
||||
|
||||
return $this->render ( 'index', [
|
||||
'searchModel' => $searchModel,
|
||||
'dataProvider' => $dataProvider,
|
||||
'dataProvider' => $dataProvider
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a single Card model.
|
||||
*
|
||||
* @param integer $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionView($id)
|
||||
{
|
||||
public function actionView($id) {
|
||||
return $this->render ( 'view', [
|
||||
'model' => $this->findModel($id),
|
||||
'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()
|
||||
{
|
||||
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' ]);
|
||||
return $this->redirect ( [
|
||||
'create'
|
||||
] );
|
||||
} else {
|
||||
return $this->redirect(['view', 'id' => $model->id_card]);
|
||||
return $this->redirect ( [
|
||||
'view',
|
||||
'id' => $model->id_card
|
||||
] );
|
||||
}
|
||||
} else {
|
||||
return $this->render ( 'create', [
|
||||
'model' => $model,
|
||||
'model' => $model
|
||||
] );
|
||||
}
|
||||
}
|
||||
@ -91,19 +103,21 @@ class CardController extends \backend\controllers\BackendController
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
public function actionUpdate($id) {
|
||||
$model = $this->findModel ( $id );
|
||||
|
||||
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) {
|
||||
return $this->redirect(['index']);
|
||||
return $this->redirect ( [
|
||||
'index'
|
||||
] );
|
||||
// return $this->redirect(Yii::$app->request->referrer);
|
||||
} else {
|
||||
return $this->render ( 'update', [
|
||||
'model' => $model,
|
||||
'model' => $model
|
||||
] );
|
||||
}
|
||||
}
|
||||
@ -111,25 +125,27 @@ class CardController extends \backend\controllers\BackendController
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
protected function findModel($id) {
|
||||
if (($model = Card::findOne ( $id )) !== null) {
|
||||
return $model;
|
||||
} else {
|
||||
@ -137,7 +153,6 @@ class CardController extends \backend\controllers\BackendController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Your controller action to fetch the list
|
||||
*/
|
||||
@ -147,7 +162,7 @@ class CardController extends \backend\controllers\BackendController
|
||||
$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 ",
|
||||
"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') {
|
||||
@ -161,12 +176,76 @@ class CardController extends \backend\controllers\BackendController
|
||||
$out [] = [
|
||||
'number' => $d ['number'],
|
||||
'name' => $d ['name'],
|
||||
'txt' => $d ['txt'],
|
||||
'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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$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
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
30
backend/models/CardImportRfidForm.php
Normal file
30
backend/models/CardImportRfidForm.php
Normal 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']
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
16
backend/views/card/importRfid.php
Normal file
16
backend/views/card/importRfid.php
Normal 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() ?>
|
||||
Loading…
Reference in New Issue
Block a user