diff --git a/backend/controllers/CardController.php b/backend/controllers/CardController.php
index 07409dc..e8ced89 100644
--- a/backend/controllers/CardController.php
+++ b/backend/controllers/CardController.php
@@ -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()
- {
- return [
- 'access' => [
- 'class' => \yii\filters\AccessControl::className(),
- 'rules' => [
- // allow authenticated users
- [
- 'actions' => ['create','index','view','update','list'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- // everything else is denied
- ],
- ],
+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' ,
+ '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);
-
- 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();
+
+ /**
+ * 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,
- ]);
- }
- }
+ 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 ( [
+ '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');
- /**
- * 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 );
- }
-
-
-
+// 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 .= "
array size: " . count($arr);
+ $model->message .= "
failed: " . print_r($failed,true);
+ $model->message .= "
sql:";
+ $model->message .= "
". implode("
", $sqls);
+
+ }
+
+ return $this->render ( 'importRfid.php', [
+ 'model' => $model
+ ] );
+ }
}
diff --git a/backend/models/CardImportRfidForm.php b/backend/models/CardImportRfidForm.php
new file mode 100644
index 0000000..d623b4e
--- /dev/null
+++ b/backend/models/CardImportRfidForm.php
@@ -0,0 +1,30 @@
+
+
+ ['enctype' => 'multipart/form-data']]) ?>
+
+ = $form->field($model, 'file')->fileInput() ?>
+
+
+ message);
+ ?>
+
+
+
\ No newline at end of file