add property and property definition
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
86
common/manager/PropertySettingsManager.php
Normal file
86
common/manager/PropertySettingsManager.php
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
53
common/models/Property.php
Normal file
53
common/models/Property.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
58
common/models/PropertyDefinition.php
Normal file
58
common/models/PropertyDefinition.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
44
common/models/PropertySetting.php
Normal file
44
common/models/PropertySetting.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
34
common/models/PropertySettingModel.php
Normal file
34
common/models/PropertySettingModel.php
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user