diff --git a/common/models/CardKeyAssignment.php b/common/models/CardKeyAssignment.php index cf60198..1ad8a4a 100644 --- a/common/models/CardKeyAssignment.php +++ b/common/models/CardKeyAssignment.php @@ -12,6 +12,7 @@ use yii\behaviors\TimestampBehavior; * @property integer $id_card * @property integer $id_key * @property integer $id_user + * @property string $virtual_key * @property string $created_at * @property string $updated_at */ diff --git a/console/migrations/m220220_190302_add_virtual_key_field_for_card_key_assignment_table.php b/console/migrations/m220220_190302_add_virtual_key_field_for_card_key_assignment_table.php new file mode 100644 index 0000000..7be6696 --- /dev/null +++ b/console/migrations/m220220_190302_add_virtual_key_field_for_card_key_assignment_table.php @@ -0,0 +1,44 @@ +addColumn('card_key_assignment','virtual_key', $this->string()); + $this->addColumn('card_key_assignment','direction_in_at', 'datetime'); + $this->addColumn('card_key_assignment','direction_out_at', 'datetime'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + echo "m220220_190302_add_virtual_key_field_for_card_key_assignment_table cannot be reverted.\n"; + + return false; + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m220220_190302_add_virtual_key_field_for_card_key_assignment_table cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/frontend/models/KeyToggleForm.php b/frontend/models/KeyToggleForm.php index 3e084fd..c9c2a24 100644 --- a/frontend/models/KeyToggleForm.php +++ b/frontend/models/KeyToggleForm.php @@ -117,6 +117,7 @@ class KeyToggleForm extends Model $assignment->id_key = $this->keyModel->id_key; /** @noinspection PhpUndefinedClassInspection */ $assignment->id_user = \Yii::$app->user->id; + $assignment->virtual_key = uniqid(null,true); $assignment->save(false); /** @noinspection PhpUndefinedClassInspection */ \Yii::$app->session->setFlash ( 'success', 'Kulcs kiadva!' ); diff --git a/mobileapi/controllers/ApiController.php b/mobileapi/controllers/ApiController.php index d2889f5..d2c4947 100644 --- a/mobileapi/controllers/ApiController.php +++ b/mobileapi/controllers/ApiController.php @@ -25,5 +25,14 @@ class ApiController extends RestController return $apiManager->getCardPage(); } + public function actionTicketPage(){ + $apiManager = new ApiManager(); + return $apiManager->getTicketPage(); + } + public function actionVirtualKeyPage(){ + + $apiManager = new ApiManager(); + return $apiManager->getVirtualKeyPage(); + } } diff --git a/mobileapi/manager/ApiManager.php b/mobileapi/manager/ApiManager.php index ed291b4..aa2a020 100644 --- a/mobileapi/manager/ApiManager.php +++ b/mobileapi/manager/ApiManager.php @@ -1,20 +1,26 @@ user->getIdentity(); - if ( $customer == null ){ + if (!isset($customer)) { throw new \yii\web\NotFoundHttpException(); } $card = Card::findOne($customer->id_customer_card); - if ( $card == null ){ + if (!isset($card)) { throw new \yii\web\NotFoundHttpException(); } @@ -27,4 +33,82 @@ class ApiManager ]; } + + function getTicketPage() + { + $customer = \Yii::$app->user->getIdentity(); + + if (!isset($customer)) { + throw new \yii\web\NotFoundHttpException(); + } + $card = Card::findOne($customer->id_customer_card); + + if (!isset($card)) { + throw new \yii\web\NotFoundHttpException(); + } + $tickets = Ticket::readActive($card); + + $result = []; + foreach ($tickets as $ticket) { + $result[] = [ + 'id_ticket' => $ticket->id_ticket, + 'type' => [ + 'id_type' => $ticket->ticketType->id_ticket_type, + 'name' => $ticket->ticketType->name, + ], + 'usage_count' => $ticket->usage_count, + 'start' => $ticket->start, + 'end' => $ticket->end + ]; + } + return $result; + } + + public function getVirtualKeyPage() + { + $customer = \Yii::$app->user->getIdentity(); + + if (!isset($customer)) { + throw new \yii\web\NotFoundHttpException(); + } + $card = Card::findOne($customer->id_customer_card); + + if (!isset($card)) { + throw new \yii\web\NotFoundHttpException(); + } + + $keyObject = null; + try { + $keyAssignment = CardKeyAssignment::findOne(['id_card' => $card->id_card]); + + if (!isset($keyAssignment)) { + throw new NotFoundHttpException(); + } + + $key = Key::findOne(['id_key' => $keyAssignment->id_key]); + + if (!isset($key)) { + throw new NotFoundHttpException(); + } + + $qrCode = new QrCode($keyAssignment->virtual_key ); + + $keyObject = [ + 'qrcode' => $qrCode->writeDataUri(), + 'created_at' => $card->created_at, + 'key' => $key->number, + 'id_key' => $key->id_key + ]; + } catch (\Exception $e) { + // failed to get key + \Yii::error('Failed to get virtual key: '.$e->getMessage()); + } + + return [ + 'key' => $keyObject + ]; + + } + + }