File: /home/aliazzsr/api.crm.vqode.com/models/core/Note.php
<?php
namespace app\models\core;
use app\components\BaseModel;
use app\components\formatters\DateFormatter;
use app\models\enums\NoteRelTypeEnum;
use app\models\enums\NoteTypeEnum;
use yii\behaviors\AttributeTypecastBehavior;
/**
* Class Note
* @package app\models\core
* @property integer $id unique identifier
* @property integer $reltype_id model class the note related to NoteRelTypeEnum
* @property integer $rel_id the model the note related to, identifier
* @property integer $type_id note type, for future use
* @property string $dt
*/
class Note extends BaseModel
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'note';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['reltype_id', 'rel_id', 'dt', 'content'], 'required'],
['type_id', 'default', 'value' => NoteTypeEnum::DEFAULT_NOTE],
[['reltype_id', 'rel_id', 'type_id'], 'integer'],
['reltype_id', 'in', 'range' => NoteRelTypeEnum::getConstantsByName()],
['type_id', 'in', 'range' => NoteTypeEnum::getConstantsByName()],
['rel_id', 'exist', 'targetClass' => Project::className(), 'targetAttribute' => 'id', 'when' => function (Note $model) {
return $model->reltype_id === NoteRelTypeEnum::PROJECT_NOTE;
}],
['rel_id', 'exist', 'targetClass' => Company::className(), 'targetAttribute' => 'id', 'when' => function (Note $model) {
return $model->reltype_id === NoteRelTypeEnum::COMPANY_NOTE;
}],
['dt', 'date', 'format' => 'php:d/m/Y H:i:s'],
[['reltype_id', 'rel_id', 'type_id', 'dt', 'content'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'reltype_id' => 'Relation type',
'rel_id' => 'Relation model',
'type_id' => 'Note type',
'dt' => 'Date',
];
}
/**
* @inheritdoc
*/
public function fields()
{
$fields = parent::fields();
unset($fields['type_id']);
return $fields;
}
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = [
'typecast' => [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'id' => AttributeTypecastBehavior::TYPE_INTEGER,
'reltype_id' => AttributeTypecastBehavior::TYPE_INTEGER,
'rel_id' => AttributeTypecastBehavior::TYPE_INTEGER,
'type_id' => AttributeTypecastBehavior::TYPE_INTEGER,
],
'typecastAfterValidate' => true,
'typecastBeforeSave' => true,
'typecastAfterFind' => true,
],
'typecast_afterFind' => [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'dt' => [DateFormatter::className(), 'toStringWithTime'],
],
'typecastAfterValidate' => false,
'typecastBeforeSave' => false,
'typecastAfterFind' => true,
],
'typecast_beforeSave' => [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'dt' => [DateFormatter::className(), 'toMysqlWithTime'],
],
'typecastAfterValidate' => false,
'typecastBeforeSave' => true,
'typecastAfterFind' => false,
],
];
return array_merge(parent::behaviors(), $behaviors);
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$this->dt = $this->dt ? DateFormatter::toStringWithTime($this->dt) : null;
}
}
/**
* @SWG\Definition(
* definition="Note",
* type="object",
* description="Note model",
* required={"reltype_id", "rel_id", "dt", "content"},
* allOf={
* @SWG\Schema(
* @SWG\Property(property="id", type="integer", example=7, description="Unique note identifier"),
* @SWG\Property(property="reltype_id", type="integer", example=1,
* description="Relation type identifier. 1 - note related to Company, 2 - note related to Project."),
* @SWG\Property(property="rel_id", type="integer", example=35,
* description="Model identifier that note related to. FK Company.id or Project.id, depends on reltype_id"),
* @SWG\Property(property="dt", type="string", example="24/03/1987 23:15:00",
* description="Note date"),
* @SWG\Property(property="content", type="string", example="Lorem ipsum dolor sit amet, consectetur adipiscing elit",
* description="Note content")
* )
* }
* )
*/