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/filtering/SearchCondition.php
<?php
namespace app\components\filtering;

use app\components\BaseModel;
use yii\db\Expression;

/**
 * Class SearchCondition
 * @package app\components\filtering
 */
class SearchCondition extends BaseCondition
{
    /**
     * @inheritdoc
     */
    public static function get($attribute, $value, $modelClass)
    {
        /** @var BaseModel $modelClass */
        $concats = [];
        $attributes = $modelClass::getTableSchema()->columns;
        $spaceExpr = "' '";

        foreach ($attributes as $attr) {
            if (static::canSearchBy($attr->name)) {
                switch ($attr->type) {
                    case 'string':
                        $concats[] = self::getTextClause($attr->name, $value);
                        $concats[] = $spaceExpr;
                        break;
                    case 'timestamp':
                        $concats[] = self::getDateTimeClause($attr->name, $value, $modelClass);
                        $concats[] = $spaceExpr;
                        break;
                    default:
                }
            }
        }

        if (!$concats) {
            return [];
        }

        $conactField = 'concat(' . implode(',', $concats) . ')';
        $expression = new Expression($conactField);

        $conditions = [];
        foreach (explode(' ', $value) as $item) {
            $conditions[] = ['LIKE', $expression, trim($item)];
        }

        return array_merge(['AND'], $conditions);
    }

    /**
     * Checks if it's possible to search by the attribute
     * @param string $attribute attribute name
     * @return bool
     */
    protected static function canSearchBy($attribute)
    {
        $internalAttributes = [
            BaseModel::CreatedAt_Attribute,
            BaseModel::UpdatedAt_Attribute,
            BaseModel::CreatedId_Attribute,
            BaseModel::UpdatedId_Attribute,
        ];

        return !in_array($attribute, $internalAttributes);
    }

    /**
     * Returns a clause for a string/text attributes
     * @param string $attribute attribute name
     * @param string $value value
     * @return array
     */
    protected static function getTextClause($attribute, $value)
    {
        return "IFNULL({$attribute}, '')";
    }

    /**
     * Returns a clause for a timestamp/date attributes
     * @param string $attribute attribute name
     * @param string $value value
     * @return array
     */
    protected static function getDateTimeClause($attribute, $value, $modelClass)
    {
        return "IFNULL(DATE_FORMAT({$attribute}, '%d/%m/%Y'), '')";
    }

    /**
     * @inheritdoc
     */
    public static function is($attribute, $value)
    {
        return 'Search' === $attribute && is_scalar($value);
    }
}