HEX
Server: LiteSpeed
System: Linux premium260.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: aliazzsr (627)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/aliazzsr/api.crm.vqode.com/components/BaseModel.php
<?php
namespace app\components;

use app\components\behaviors\ModifierBehavior;
use app\components\filtering\ConditionFactory;
use app\components\helpers\ValueHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\ExpressionInterface;
use yii\helpers\ArrayHelper;

/**
 * Class BaseModel
 * @package app\components
 */
class BaseModel extends ActiveRecord
{
    const SCENARIO_STRICT_VALIDATE = 'strict validate';

    /** @var array attributes list needs to refresh after model saved */
    protected static $attributesToRefresh = [];

    const CreatedAt_Attribute = 'created_at';
    const UpdatedAt_Attribute = 'updated_at';

    const CreatedId_Attribute = 'created_id';
    const UpdatedId_Attribute = 'updated_id';

    const CONSOLE_USER = 1;

    public static $extraFields = [];

    /**
     * @inheritdoc
     */
    public function extraFields()
    {
        return static::$extraFields;
    }

    public static function findWithJoin()
    {
        return parent::find();
    }

    public static function hasRelation($relation)
    {
        return in_array($relation, static::$extraFields);
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => static::CreatedAt_Attribute,
            'updatedAtAttribute' => static::UpdatedAt_Attribute,
            'value' => ValueHelper::now(),
        ];

        $userId = is_a(\Yii::$app,'yii\console\Application') ? self::CONSOLE_USER : \Yii::$app->user->id;
        $behaviors[] = [
            'class' => ModifierBehavior::className(),
            'createdIdAttribute' => static::CreatedId_Attribute,
            'updatedIdAttribute' => static::UpdatedId_Attribute,
            'value' =>  $userId,
        ];

        return $behaviors;
    }

    public function fields()
    {
        $additionalFields = array_keys(static::getAdditionalFields());
        $fields = ArrayHelper::merge(
            parent::fields(),
            array_combine($additionalFields, $additionalFields)
        );

        unset(
            $fields[static::CreatedAt_Attribute],
            $fields[static::UpdatedAt_Attribute],
            $fields[static::CreatedId_Attribute],
            $fields[static::UpdatedId_Attribute]
        );

        return $fields;
    }

    public function getJoinWith()
    {
        return [];
    }

    public static function getAdditionalFields()
    {
        return [];
    }

    /**
     * Checks if attribute is valid attributes name
     * @param string $name attribute name
     * @return boolean
     */
    public static function isValidAttribute($name)
    {
        return !!preg_match('/^[a-zA-z\_0-9\-\.]+$/', $name);
    }

    /**
     * Returns condition for where clause
     * @param string $attribute
     * @param mixed $value
     * @return string|array|ExpressionInterface
     */
    public static function getConditionClause($attribute, $value)
    {
        return ConditionFactory::createCondition($attribute, $value, static::className());
    }

    /**
     * @inheritdoc
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);

        if ($this->shouldRefresh($changedAttributes)) {
            $this->refresh();
        }
    }

    /**
     * Checks if attributes required to refresh are changed
     * @param $changedAttributes
     * @return bool
     */
    protected function shouldRefresh($changedAttributes)
    {
        $keys = array_keys($changedAttributes);
        $intr = array_intersect($keys, static::$attributesToRefresh);

        return !empty($intr);
    }
}