add import card rfid key
This commit is contained in:
parent
39ae361505
commit
d2bc8076ca
@ -5,168 +5,247 @@ 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() {
|
||||||
|
return [
|
||||||
public function behaviors()
|
'access' => [
|
||||||
{
|
'class' => \yii\filters\AccessControl::className (),
|
||||||
return [
|
'rules' => [
|
||||||
'access' => [
|
// allow authenticated users
|
||||||
'class' => \yii\filters\AccessControl::className(),
|
[
|
||||||
'rules' => [
|
'actions' => [
|
||||||
// allow authenticated users
|
'create',
|
||||||
[
|
'index',
|
||||||
'actions' => ['create','index','view','update','list'],
|
'view',
|
||||||
'allow' => true,
|
'update',
|
||||||
'roles' => ['@'],
|
'list' ,
|
||||||
],
|
'import-rfid'
|
||||||
// everything else is denied
|
],
|
||||||
],
|
'allow' => true,
|
||||||
],
|
'roles' => [
|
||||||
|
'@'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
// 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
|
*
|
||||||
* @return mixed
|
* @param integer $id
|
||||||
*/
|
* @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 ( [
|
||||||
}else{
|
'create'
|
||||||
return $this->redirect(['view', 'id' => $model->id_card]);
|
] );
|
||||||
}
|
} else {
|
||||||
} else {
|
return $this->redirect ( [
|
||||||
return $this->render('create', [
|
'view',
|
||||||
'model' => $model,
|
'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 ( [
|
||||||
|
'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 ();
|
||||||
|
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
public function actionImportRfid() {
|
||||||
|
$model = new CardImportRfidForm ();
|
||||||
|
$arr = [];
|
||||||
|
|
||||||
|
if (Yii::$app->request->isPost) {
|
||||||
|
$model->file = UploadedFile::getInstance($model, 'file');
|
||||||
|
|
||||||
/**
|
// print_r($model->file);
|
||||||
* Updates an existing Card model.
|
// $model->message = "ok";
|
||||||
* If update is successful, the browser will be redirected to the 'view' page.
|
$file = $model->file->tempName;
|
||||||
* @param integer $id
|
|
||||||
* @return mixed
|
$file = fopen ( $file , "r" );
|
||||||
*/
|
|
||||||
public function actionUpdate($id)
|
$trans = null;
|
||||||
{
|
$i = 0;
|
||||||
$model = $this->findModel($id);
|
$j = 0;
|
||||||
|
while ( ($data = fgetcsv ( $file, 0, "," )) != null ) {
|
||||||
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
// if ($i == 0) {
|
||||||
return $this->redirect(['index']);
|
// $i ++;
|
||||||
// return $this->redirect(Yii::$app->request->referrer);
|
// continue;
|
||||||
} else {
|
// }
|
||||||
return $this->render('update', [
|
$j++;
|
||||||
'model' => $model,
|
$number = $key = false;
|
||||||
]);
|
if ( isset($data[0]) ){
|
||||||
}
|
$number = $data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ( isset($data[1]) ){
|
||||||
* Deletes an existing Card model.
|
$key = $data[1];
|
||||||
* If deletion is successful, the browser will be redirected to the 'index' page.
|
}
|
||||||
* @param integer $id
|
|
||||||
* @return mixed
|
if ( isset($number) && isset($key)){
|
||||||
*/
|
$item = [];
|
||||||
public function actionDelete($id)
|
$item['number'] = $number;
|
||||||
{
|
$item['key'] = Helper::fixAsciiChars( $key);
|
||||||
$this->findModel($id)->delete();
|
$arr[] = $item;
|
||||||
|
}
|
||||||
return $this->redirect(['index']);
|
|
||||||
}
|
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Finds the Card model based on its primary key value.
|
$failed = [];
|
||||||
* If the model is not found, a 404 HTTP exception will be thrown.
|
$sqls = [];
|
||||||
* @param integer $id
|
foreach ($arr as $item ){
|
||||||
* @return Card the loaded model
|
$card = Card::find()->andWhere(['number' => $item['number']])->one();
|
||||||
* @throws NotFoundHttpException if the model cannot be found
|
if ( $card != null ){
|
||||||
*/
|
$card->rfid_key = $item['key'];
|
||||||
protected function findModel($id)
|
$sql = "update card set rfid_key = '" .$item['key'] ."' where id_card = " .$card->id_card .";";
|
||||||
{
|
$sqls[] = $sql;
|
||||||
if (($model = Card::findOne($id)) !== null) {
|
$i++;
|
||||||
return $model;
|
}else{
|
||||||
} else {
|
$failed [] = $item;
|
||||||
throw new NotFoundHttpException('The requested page does not exist.');
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
$model->message = "rows read: " .$j ." / ". "updated cards: " .$i;
|
||||||
|
$model->message .= "<br> array size: " . count($arr);
|
||||||
/**
|
$model->message .= "<br> failed: " . print_r($failed,true);
|
||||||
* Your controller action to fetch the list
|
$model->message .= "<br>sql:";
|
||||||
*/
|
$model->message .= "<br>". implode("<br>", $sqls);
|
||||||
public function actionList($search = null) {
|
|
||||||
$query = new Query();
|
}
|
||||||
|
|
||||||
$query->select ( [
|
return $this->render ( 'importRfid.php', [
|
||||||
'card.number as number',
|
'model' => $model
|
||||||
'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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
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