File: //home/aliazzsr/api.crm.vqode.com/models/core/Stage.php
<?php
namespace app\models\core;
use app\components\BaseModel;
use app\components\validators\ColorValidator;
use app\components\validators\PercentageValidator;
use yii\behaviors\AttributeTypecastBehavior;
class Stage extends BaseModel
{
const ICON_COLOR_DEFAULT = '000000';
const ICON_PERCENTAGE_DEFAULT = 0;
public static function tableName()
{
return 'stage';
}
public function rules()
{
return [
[['name', 'description', 'icon_color'], 'trim'],
[['icon_color'], 'default', 'value' => self::ICON_COLOR_DEFAULT],
[['icon_percentage'], 'default', 'value' => self::ICON_PERCENTAGE_DEFAULT],
[['icon_color'], ColorValidator::className()],
[['icon_percentage',], PercentageValidator::className()],
[['name'], 'required'],
[['name'], 'string', 'max' => 255],
[['name'], 'unique'],
[['active'], 'boolean'],
[['name', 'description', 'icon_color', 'icon_percentage'], 'safe'],
];
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['typecast'] = [
'class' => AttributeTypecastBehavior::className(),
'attributeTypes' => [
'active' => AttributeTypecastBehavior::TYPE_BOOLEAN,
'icon_percentage' => AttributeTypecastBehavior::TYPE_FLOAT,
],
'typecastAfterValidate' => true,
'typecastBeforeSave' => true,
'typecastAfterFind' => true,
];
return $behaviors;
}
/**
* @inherit
*/
public static function getConditionClause($attribute, $value)
{
switch($attribute) {
case 'active':
$clause = ['=', 'active', 'true' === $value ? 1 : 0];
break;
default:
$clause = parent::getConditionClause($attribute, $value);
}
return $clause;
}
/**
* @inheritdoc
*/
public function beforeDelete()
{
Project::updateAll(['stage_id' => null], ['stage_id' => $this->id]);
return parent::beforeDelete();
}
}
/**
* @SWG\Definition(
* definition="Stage",
* type="object",
* description="Stage model",
* allOf={
* @SWG\Schema(
* required={"name"},
* @SWG\Property(property="id", type="integer", example="3", description="Unique identifier"),
* @SWG\Property(property="name", type="string", example="Needs Discovered", description="Stage name"),
* @SWG\Property(property="icon_color", type="string", description="Icon hex color", example="23EFA9"),
* @SWG\Property(property="icon_percentage", type="decimal", example=75, description="Icon percentage"),
* @SWG\Property(property="active", type="boolean", example=false, description="Are projects on the stage active or concluded."),
* @SWG\Property(property="description", type="string", description="Stage description"),
* )
* }
* )
*/