Add key controller to add/update keys , add bakcend skin param
This commit is contained in:
@@ -33,7 +33,7 @@ class KeyController extends Controller {
|
||||
'rules' => [
|
||||
// allow authenticated users
|
||||
[
|
||||
'actions' => ['create','index','view','update'],
|
||||
'actions' => ['create','index','view','update','import'],
|
||||
'allow' => true,
|
||||
'roles' => ['admin','employee','reception'],
|
||||
],
|
||||
@@ -156,10 +156,18 @@ class KeyController extends Controller {
|
||||
public function actionImport() {
|
||||
$model = new KeyImportForm();
|
||||
$arr = [ ];
|
||||
$updated = 0;
|
||||
$inserted = 0;
|
||||
$failed = 0;
|
||||
$found = 0;
|
||||
|
||||
if (Yii::$app->request->isPost) {
|
||||
|
||||
|
||||
$model->file = UploadedFile::getInstance ( $model, 'file' );
|
||||
|
||||
if ( isset($model->file)){
|
||||
|
||||
// print_r($model->file);
|
||||
// $model->message = "ok";
|
||||
$file = $model->file->tempName;
|
||||
@@ -191,25 +199,48 @@ class KeyController extends Controller {
|
||||
$arr [] = $item;
|
||||
}
|
||||
}
|
||||
$found = count($arr);
|
||||
|
||||
foreach ( $arr as $item ) {
|
||||
try {
|
||||
$key = new Key ();
|
||||
$key->number = $item ['number'];
|
||||
$key->rfid_key = $item ['key'];
|
||||
$key->status = Key::STATUS_ACTIVE;
|
||||
$key->type = Key::TYPE_NORMAL;
|
||||
$key->save ( false );
|
||||
$key = Key::findOne(['number' => $item ['number']]);
|
||||
if ( !isset($key)){
|
||||
$key = new Key ();
|
||||
$key->number = $item ['number'];
|
||||
$key->rfid_key = $item ['key'];
|
||||
$key->status = Key::STATUS_ACTIVE;
|
||||
$key->type = Key::TYPE_NORMAL;
|
||||
$key->save ( false );
|
||||
$inserted++;
|
||||
}else{
|
||||
$key->rfid_key = $item['key'];
|
||||
$key->update(false);
|
||||
$updated++;
|
||||
}
|
||||
} catch ( \Exception $e ) {
|
||||
\Yii::error ( "Failed to save key: " . $key->number );
|
||||
$failed++;
|
||||
|
||||
}
|
||||
}
|
||||
$message = "Fájlnév: " . $model->file->name;
|
||||
$message .= "<br> Fájlból beolvasva: $found ";
|
||||
$message .= "<br> Új kulcs sikeresen importálva: $inserted ";
|
||||
$message .= "<br> Rendszerben lévő kulcs sikeresen módosítva: $updated ";
|
||||
$message .= "<br> Sikertelen kulcs importálás/módosítás: $failed ";
|
||||
\Yii::$app->session->setFlash('success',$message);
|
||||
|
||||
$this->redirect ( [
|
||||
$model->message = $message;
|
||||
|
||||
\Yii::$app->session->set('mymessage',$model->message);
|
||||
|
||||
return $this->redirect ( [
|
||||
'import'
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
$model->message = \Yii::$app->session->get('mymessage');
|
||||
\Yii::$app->session->remove('mymessage');
|
||||
return $this->render ( 'import', [
|
||||
'model' => $model
|
||||
] );
|
||||
|
||||
Reference in New Issue
Block a user