96 lines
2.3 KiB
PHP
96 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Created by IntelliJ IDEA.
|
|
* User: rocho
|
|
* Date: 2018.08.29.
|
|
* Time: 21:58
|
|
*/
|
|
|
|
namespace customerapi\controllers;
|
|
|
|
|
|
use common\manager\EventRegistrationManager;
|
|
use common\models\CardEventRegistrationForm;
|
|
use common\models\Customer;
|
|
use customerapi\models\available\EventInterval;
|
|
use customerapi\models\registrations\EventRegistrationAvailable;
|
|
use Exception;
|
|
use Throwable;
|
|
use Yii;
|
|
use yii\db\ActiveQuery;
|
|
use yii\web\Response;
|
|
|
|
/** @noinspection PhpUnused */
|
|
|
|
class EventRegistrationController extends CustomerApiController
|
|
{
|
|
|
|
/** @noinspection PhpUnused */
|
|
/**
|
|
* @return Response
|
|
* @throws Exception
|
|
*/
|
|
public function actionIndex()
|
|
{
|
|
$registrations =$this->prepareQueryFindRegistrationsForCustomer()->all();
|
|
return $this->asJson( $registrations );
|
|
}
|
|
|
|
/** @noinspection PhpUnused */
|
|
/***
|
|
* @param $id_registration
|
|
* @return Response
|
|
* @throws Exception
|
|
*/
|
|
public function actionRegistration($id_registration)
|
|
{
|
|
$registrations = $this->prepareQueryFindRegistrationsForCustomer()
|
|
->andWhere(['id_event_registration' => $id_registration])
|
|
->one();
|
|
return $this->asJson(
|
|
$registrations
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Prepare a query to get registrations for customer
|
|
*
|
|
* @return ActiveQuery
|
|
* @throws Exception
|
|
*/
|
|
private function prepareQueryFindRegistrationsForCustomer(){
|
|
$interval = EventInterval::createInterval();
|
|
return EventRegistrationAvailable::find()
|
|
->innerJoinWith('event')
|
|
->andWhere(['and',
|
|
['>=', 'event.start', $interval->firstActiveDate->getTimestamp()],
|
|
['id_customer' => Yii::$app->user->getId()]
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @noinspection PhpUnused
|
|
* @param $id_event
|
|
* @throws Throwable
|
|
*/
|
|
public function actionRegister($id_event) {
|
|
/** @var Customer $customer */
|
|
$customer = Yii::$app->user->getIdentity();
|
|
$card =$customer->card;
|
|
|
|
$form = new CardEventRegistrationForm();
|
|
$form->event_id = $id_event;
|
|
$form->card_number = $card->number;
|
|
|
|
$manager = new EventRegistrationManager();
|
|
$manager->registerCard($form);
|
|
|
|
return $form->registration;
|
|
|
|
}
|
|
|
|
|
|
}
|