add property and property definition

This commit is contained in:
Roland Schneider 2021-10-11 22:32:43 +02:00
parent a2c828cc19
commit e6d4d0ffc4
13 changed files with 604 additions and 1 deletions

View File

@ -235,6 +235,28 @@ class AdminMenuStructure
];
/////////////////////////////
// Group Training
/////////////////////////////
$items = [];
// $items[] = ['label' => 'Felszerelés', 'url' => ['/event-equipment-type'], 'role' => [RoleDefinition::$ROLE_ADMIN]];
$items[] = [
'label' => 'Beállítások',
'url' => ['/settings/index'],
'role' => [
RoleDefinition::$ROLE_ADMIN,
]
];
$this->menuItems[] = [
'label' => 'Beállítások',
'url' => $this->emptyUrl,
'items' => $items,
'role' => [
RoleDefinition::$ROLE_ADMIN
]
];
/////////////////////////////
// Development
/////////////////////////////

View File

@ -0,0 +1,77 @@
<?php
namespace backend\controllers;
use common\helpers\AppArrayHelper;
use common\manager\PropertySettingsManager;
use common\models\PropertySettingModel;
class SettingsController extends BackendController
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::class,
'rules' => [
// allow authenticated users
[
'actions' => [
'index',
],
'allow' => true,
'roles' => [
'admin',
'employee',
'reception'
]
]
]
// everything else is denied
]
];
}
/**
* Lists all Ticket models.
*
* @return mixed
*/
public function actionIndex()
{
$settingsManager = new PropertySettingsManager();
$settings = $settingsManager->getPropertySettings();
$settingsMap = AppArrayHelper::objectArrayToMap($settings,
function ($setting) {
return $setting->definition->id;
},
function ($setting) {
return $setting;
});
$models = AppArrayHelper::mapValues($settings, function ($item) {
return PropertySettingModel::fromPropertySetting($item);
}
);
if (\Yii::$app->request->isPost) {
PropertySettingModel::loadMultiple($models, \Yii::$app->request->post());
foreach ($models as $model){
if ( isset($settingsMap[$model->id_definition])){
$setting = $settingsMap[$model->id_definition];
$setting->setValue($model->value);
}
}
$settingsManager->saveSettings(array_values($settingsMap));
}
return $this->render('index', [
'settings' => $models
]);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace backend\models;
use common\models\Property;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\PropertyDefinition;
/**
* PropertyDefinitionSearch represents the model behind the search form about `common\models\PropertyDefinition`.
*/
class SettingsSearch extends Model
{
/**
* @inheritdoc
*/
public function rules()
{
return [
// [['id'], 'integer'],
// [['name', 'label', 'type', 'config', 'created_at', 'updated_at'], 'safe'],
];
}
/**
* Creates data provider instance with search query applied
*
*
* @return ActiveDataProvider
*/
public function search()
{
return $dataProvider;
}
}

View File

@ -0,0 +1,42 @@
<?php
/** @var common\models\PropertySettingModel[] $settings */
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div class="room-form">
<?php $form = ActiveForm::begin(); ?>
<?php
$index = 0;
foreach ($settings as $setting) {
?>
<div class="row">
<div class="col-lg-3">
<?= $setting->label . ":" ?>
</div>
<div class="col-lg-3">
<?= $form->field($setting, "[$index]value")->textInput(['maxlength' => true, 'label' => false])->label(false) ?>
</div>
</div>
<?php
$index++;
} ?>
<div class="form-group">
<?= Html::submitButton("Mentés", ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -7,6 +7,14 @@ class AppArrayHelper
{
public static function mapValues($array, $func){
$result = [];
foreach ($array as $item){
$result[] = $func($item);
}
return $result;
}
public static function objectArrayToMap($array, $funcGetId, $funcGetValue){
$result = [];
@ -37,4 +45,11 @@ class AppArrayHelper
);
}
public static function getOrDefault($array, $key, $defaultValue = null){
if ( isset($array[$key])){
return $array[$key];
}
return $defaultValue;
}
}

View File

@ -3,11 +3,13 @@
namespace common\manager;
use common\components\Helper;
use common\helpers\AppArrayHelper;
use common\models\Card;
use common\models\CardEventRegistrationForm;
use common\models\Customer;
use common\models\Event;
use common\models\EventRegistration;
use common\models\PropertyDefinition;
use common\models\Ticket;
use customerapi\models\available\EventInterval;
use customerapi\models\registrations\EventRegistrationAvailable;
@ -247,7 +249,12 @@ class EventRegistrationManager extends BaseObject
if ($reason == EventRegistration::CANCEL_REASON_CUSTOMER) {
$timeUntilEventStart = $event->start - $now;
if ($timeUntilEventStart < Helper::getGroupTrainingRegistrationCancelLimitMinutes() * 60) {
$settingsManager = new PropertySettingsManager();
$limitMinutes = $settingsManager->getSetting(
PropertyDefinition::DEFINITION_GROUP_TRAINING_CUSTOMER_CANCEL_TIME_LIMIT,
Helper::getGroupTrainingRegistrationCancelLimitMinutes()
);
if ($timeUntilEventStart <= $limitMinutes* 60) {
throw new BadRequestHttpException('The reservation can\'t be deleted', self::CANCEL_TIME_LIMIT_REACHED);
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace common\manager;
use common\helpers\AppArrayHelper;
use common\models\Property;
use common\models\PropertyDefinition;
use common\models\PropertySetting;
use common\models\PropertySettingModel;
class PropertySettingsManager
{
/**
* @return PropertySetting[]
*/
public function getPropertySettings()
{
$settings = [];
$definitions = PropertyDefinition::find()->all();
$properties = Property::find()->all();
$propertyByDefinition = AppArrayHelper::objectArrayToMap($properties,
function ($property) {
return $property->id_property_definition;
},
function ($property) {
return $property;
}
);
foreach ($definitions as $definition) {
$setting = new PropertySetting([
'property' => AppArrayHelper::getOrDefault($propertyByDefinition, $definition->id),
'definition' => $definition
]);
$settings[] = $setting;
}
return $settings;
}
/**
* @param PropertySetting $setting
*/
public function saveSetting($setting ){
Property::deleteAll(['id_property_definition' =>$setting->definition->id ]);
$setting->property->save(false);
}
/**
* @param PropertySetting[] $setting
*/
public function saveSettings($settings ){
foreach ($settings as $setting){
Property::deleteAll(['id_property_definition' =>$setting->definition->id ]);
$setting->property->save(false);
}
}
public function getSettingsMap(){
$settings = $this->getPropertySettings();
return AppArrayHelper::objectArrayToMap($settings,
function ($setting){
return $setting->definition->name;
},
function ($setting){
return $setting->getValue();
});
}
public function getSettingFromSettingsMap($settingsMap,$key,$defaultValue = null){
return AppArrayHelper::getOrDefault(
$settingsMap,
$key,
$defaultValue
);
}
public function getSetting($key,$defaultValue = null){
$settingsMap = $this->getSettingsMap();
return $this->getSettingFromSettingsMap($settingsMap,$key,$defaultValue);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "property".
*
* @property integer $id
* @property integer $id_user
* @property integer $id_property_definition
* @property string $value
* @property string $created_at
* @property string $updated_at
*/
class Property extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'property';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_user', 'id_property_definition'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['value'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'id_user' => 'Id User',
'id_property_definition' => 'Id Property Definition',
'value' => 'Value',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "property_definition".
*
* @property integer $id
* @property string $name
* @property string $label
* @property string $type
* @property string $config
* @property string $created_at
* @property string $updated_at
*/
class PropertyDefinition extends \yii\db\ActiveRecord
{
const DEFINITION_GROUP_TRAINING_CUSTOMER_CANCEL_TIME_LIMIT = "GROUP_TRAINING_CUSTOMER_CANCEL_TIME_LIMIT";
/**
* @inheritdoc
*/
public static function tableName()
{
return 'property_definition';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'label', 'type'], 'required'],
[['created_at', 'updated_at'], 'safe'],
[['name', 'label', 'type'], 'string', 'max' => 100],
[['config'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'label' => 'Label',
'type' => 'Type',
'config' => 'Config',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace common\models;
use yii\base\BaseObject;
/**
* @property \common\models\PropertyDefinition $definition
* @property \common\models\Property $property
*/
class PropertySetting extends BaseObject
{
public $definition;
public $property;
public function getValue(){
$value = null;
if ( isset($this->property)){
$value = $this->property->value;
}
return $value;
}
public function setValue($value){
// @property integer $id
// * @property integer $id_user
// * @property integer $id_property_definition
// * @property string $value
$this->property = new Property(
[
'id_user' => \Yii::$app->user->id,
'id_property_definition' => $this->definition->id,
'value' => $value
]
);
}
public function getLabel(){
return $this->definition->label;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace common\models;
use yii\base\Model;
class PropertySettingModel extends Model
{
public $id_definition;
public $value;
public $label;
public function rules()
{
return [
[['id_definition',"value"], 'required'],
[['id_definition',"value"], 'integer'],
];
}
/**
* @param PropertySetting $setting
* @return PropertySettingModel
*/
public static function fromPropertySetting($setting){
$result = new PropertySettingModel();
$result->value = $setting->getValue();
$result->label = $setting->getLabel();
$result->id_definition = $setting->definition->id;
return $result;
}
}

View File

@ -0,0 +1,67 @@
<?php
use yii\db\Migration;
/**
* Class m211011_050352_add_properties
*/
class m211011_050352_add_properties extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%property_definition}}', [
'id' => $this->primaryKey(),
'name' => $this->string(100)->notNull(),
'label' => $this->string(100)->notNull(),
'type' => $this->string(100)->notNull(),
'config' => $this->string(),
'created_at' => $this->timestamp()->notNull(),
'updated_at' => $this->timestamp()->notNull(),
], $tableOptions);
$this->createTable('{{%property}}', [
'id' => $this->primaryKey(),
'id_user' => $this->integer(11),
'id_property_definition' => $this->integer(11),
'value' => $this->string(),
'created_at' => $this->timestamp()->notNull(),
'updated_at' => $this->timestamp()->notNull(),
], $tableOptions);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m211011_050352_add_properties cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m211011_050352_add_properties cannot be reverted.\n";
return false;
}
*/
}

View File

@ -0,0 +1,57 @@
<?php
use yii\db\Migration;
/**
* Class m211011_052008_add_property_group_training_customer_cancel_interval
*/
class m211011_052008_add_property_group_training_customer_cancel_interval extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->insert("property_definition",
[
'name' => 'GROUP_TRAINING_CUSTOMER_CANCEL_TIME_LIMIT',
'label' => 'Csoportos edzés - lemondás idő korlát (perc):',
'type' => 'integer'
]);
$insertId = Yii::$app->db->getLastInsertID();
$user = \common\models\User::find()->andWhere(['username' => 'admin'])->one();
$this->insert("property",[
'id_user' => $user->id,
'id_property_definition' => $insertId,
'value' => '60'
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m211011_052008_add_property_group_training_customer_cancel_interval cannot be reverted.\n";
return false;
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m211011_052008_add_property_group_training_customer_cancel_interval cannot be reverted.\n";
return false;
}
*/
}