add rest application and discount-status rest method
This commit is contained in:
99
rest/controllers/CustomerController.php
Normal file
99
rest/controllers/CustomerController.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: rocho
|
||||
* Date: 2018.08.29.
|
||||
* Time: 21:58
|
||||
*/
|
||||
|
||||
namespace rest\controllers;
|
||||
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Ticket;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class CustomerController extends RestController
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $number
|
||||
* @param int $lastXDays default 0. Search for valid tickets also in the last x days.
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionDiscountStatus($number , $lastXDays = 0 ){
|
||||
|
||||
$number = Helper::fixAsciiChars( $number );
|
||||
|
||||
$query = Card::find();
|
||||
$query->andWhere(['or',
|
||||
['and',[ 'in','card.number' , [$number]],"trim(coalesce(card.number, '')) <>'' " ],
|
||||
['and', ['in','card.rfid_key' ,[ $number] ],"trim(coalesce(card.rfid_key, '')) <>'' "],
|
||||
|
||||
]);
|
||||
|
||||
$card = $query->one();
|
||||
|
||||
if ( !isset($card)){
|
||||
throw new NotFoundHttpException("Kártya nem található");
|
||||
}
|
||||
|
||||
$customer = $card->customer;
|
||||
|
||||
if ( !isset($customer) ){
|
||||
throw new NotFoundHttpException("Vendég nem található");
|
||||
}
|
||||
|
||||
if ( isset($lastXDays) ){
|
||||
if (!is_numeric($lastXDays)){
|
||||
throw new BadRequestHttpException("lastXDays paraméter hibás");
|
||||
}
|
||||
if ( $lastXDays > 6 || $lastXDays < 1){
|
||||
throw new BadRequestHttpException("lastXDays paraméter érték hibás");
|
||||
}
|
||||
}
|
||||
|
||||
// check if has valid ticket today
|
||||
/** @var \common\models\Card $card */
|
||||
$tickets = Ticket::readActive($card );
|
||||
$hasValidTicket = count($tickets) > 0;
|
||||
|
||||
// try to find any valid ticket in the lastXDays
|
||||
$minusDay = 1;
|
||||
while ( !$hasValidTicket && $minusDay <= $lastXDays ){
|
||||
/** @var integer $minusDay */
|
||||
$day = $this->getDateMinusDays($minusDay);
|
||||
$tickets = Ticket::readActive($card, $day );
|
||||
$hasValidTicket = count($tickets) > 0;
|
||||
$minusDay = $minusDay + 1;
|
||||
}
|
||||
|
||||
|
||||
$result = [
|
||||
'discount' => $hasValidTicket
|
||||
];
|
||||
|
||||
if ( isset($customer) ){
|
||||
$result['card_number'] = $card->number;
|
||||
$result['name'] = $customer->name;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $minusDays
|
||||
* @return \DateTime
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function getDateMinusDays($minusDays){
|
||||
$date = new \DateTime('now');
|
||||
$date->sub(new \DateInterval('P'.$minusDays.'D'));
|
||||
$date->setTime(0,0,0);
|
||||
return $date;
|
||||
}
|
||||
|
||||
}
|
||||
37
rest/controllers/RestController.php
Normal file
37
rest/controllers/RestController.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace rest\controllers;
|
||||
|
||||
|
||||
use common\models\User;
|
||||
use yii\filters\auth\HttpBasicAuth;
|
||||
use yii\rest\Controller;
|
||||
|
||||
class RestController extends Controller
|
||||
{
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
$behaviors['authenticator'] = [
|
||||
'class' => HttpBasicAuth::className(),
|
||||
'auth' => [$this, 'auth']
|
||||
];
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function auth($username, $password)
|
||||
{
|
||||
try {
|
||||
$user = User::findOne(['username' => $username]);
|
||||
if ($user->validatePassword($password)) {
|
||||
return $user;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Yii::error("Failed to load user: " . $e->getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
124
rest/controllers/SiteController.php
Normal file
124
rest/controllers/SiteController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace rest\controllers;
|
||||
|
||||
use Yii;
|
||||
use common\models\LoginForm;
|
||||
use yii\web\Controller;
|
||||
use yii\filters\VerbFilter;
|
||||
use yii\filters\AccessControl;
|
||||
use common\models\User;
|
||||
use common\components\Helper;
|
||||
use common\models\Log;
|
||||
|
||||
/**
|
||||
* Site controller
|
||||
*/
|
||||
class SiteController extends Controller
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
'access' => [
|
||||
'class' => AccessControl::className(),
|
||||
'only' => ['logout' ],
|
||||
'rules' => [
|
||||
|
||||
[
|
||||
'actions' => ['logout'],
|
||||
'allow' => true,
|
||||
'roles' => ['@'],
|
||||
],
|
||||
],
|
||||
],
|
||||
'verbs' => [
|
||||
'class' => VerbFilter::className(),
|
||||
'actions' => [
|
||||
'logout' => ['post'],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function actions()
|
||||
{
|
||||
return [
|
||||
'error' => [
|
||||
'class' => 'yii\web\ErrorAction',
|
||||
],
|
||||
'captcha' => [
|
||||
'class' => 'yii\captcha\CaptchaAction',
|
||||
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays homepage.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in a user.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionLogin()
|
||||
{
|
||||
if (!\Yii::$app->user->isGuest) {
|
||||
return $this->goHome();
|
||||
}
|
||||
|
||||
$model = new LoginForm();
|
||||
if ($model->load(Yii::$app->request->post()) && $model->login()) {
|
||||
|
||||
$geoip = Helper::getGeoIp();
|
||||
|
||||
$message = "";
|
||||
$user = User::findOne(\Yii::$app->user->id);
|
||||
if ( isset($geoip)){
|
||||
$ip = isset( $geoip->ip ) ? $geoip->ip : "";
|
||||
$city = isset( $geoip->city ) ? $geoip->city : "";
|
||||
$message = "Bejelentkezés: " .$user->username. " Ip cím:". $ip . " Város: " . $city;
|
||||
}
|
||||
|
||||
Log::log([
|
||||
'type' =>Log::$TYPE_LOGIN,
|
||||
'message' => $message
|
||||
]);
|
||||
|
||||
return $this->redirect(['account/select']);
|
||||
} else {
|
||||
return $this->render('login', ['model' => $model,]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Logs out the current user.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionLogout()
|
||||
{
|
||||
Yii::$app->user->logout();
|
||||
|
||||
return $this->goHome();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user