Abstract Class luya\admin\base\Filter

Inheritanceluya\admin\base\Filter » yii\base\BaseObject
Implementsluya\admin\base\FilterInterface, yii\base\Configurable
Subclassesluya\admin\filters\LargeCrop, luya\admin\filters\LargeThumbnail, luya\admin\filters\MediumCrop, luya\admin\filters\MediumThumbnail, luya\admin\filters\SmallCrop, luya\admin\filters\SmallLandscape, luya\admin\filters\SmallThumbnail, luya\admin\filters\TinyCrop, luya\admin\filters\TinyThumbnail
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/base/Filter.php

Base class for all storage component filters.

The available effects for the chain are stored in the database, here the default effects which are provided when installing luya:

  • thumbnail: width, height, mode, saveOptions
  • crop: width, height, saveOptions

Public Properties

Hide inherited properties

Property Type Description Defined By
$chain array Each row of the array must have "effect_id" and "effect_json_values" key. luya\admin\base\Filter
$log array An array containing all log messages luya\admin\base\Filter

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
addLog() Add message to log array. luya\admin\base\Filter
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
chain() An array with represents the chain effects and the value of the defined effects. luya\admin\base\Filter
className() Returns the fully qualified name of this class. yii\base\BaseObject
findEffect() Find the effect model based on the effect identifier. If the effect could not found an exception will be thrown. luya\admin\base\Filter
findModel() Find the model based on the identifier. If the identifier does not exists in the database, create new record in the database. luya\admin\base\Filter
getChain() Returns a parsed effect chain for the current Filter. The method verifys if the provieded effect parameters are available in the effect definitions of luya. luya\admin\base\Filter
getEffectParamsList() Get an array with all the effect param options, based on the effect params definition. luya\admin\base\Filter
getLog() Return the log array. luya\admin\base\Filter
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
identifier() Unique identifier name for the effect, no special chars allowed. luya\admin\base\FilterInterface
init() Initializes the object. yii\base\BaseObject
name() Understandable Name expression for the effect. luya\admin\base\Filter
save() Update and save filter corresponding to the model, refresh chain values. luya\admin\base\Filter

Property Details

Hide inherited properties

$chain public read-only property

Each row of the array must have "effect_id" and "effect_json_values" key.

public array getChain ( )
$log public property

An array containing all log messages

public array $log = []

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
addLog() public method

Add message to log array.

public void addLog ( $message )
$message string

The message to log

                public function addLog($message)
{
    $this->log[] = $message;
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
chain() public abstract method

An array with represents the chain effects and the value of the defined effects.

public abstract array chain ( )
return array

Example response for chain() method:

return [
    [self::EFFECT_THUMBNAIL, [
        'width' => 100,
        'height' => 100,
    ]],
];

                abstract public function chain();

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
findEffect() public method

Find the effect model based on the effect identifier. If the effect could not found an exception will be thrown.

public array findEffect ( $effectIdentifier )
$effectIdentifier string

The name of effect, used EFFECT prefixed constants like

  • EFFECT_RESIZE
  • EFFECT_THUMBNAIL
  • EFFECT_CROP
return array

Contain an array with the effect properties.

throws luya\Exception

                public function findEffect($effectIdentifier)
{
    // find effect model based on the effectIdentifier
    $model = StorageEffect::find()->where(['identifier' => $effectIdentifier])->asArray()->one();
    // if the effect model could not found, throw Exception.
    if (!$model) {
        throw new Exception("The requested effect '$effectIdentifier' does not exist.");
    }
    // array
    return $model;
}

            
findModel() public method

Find the model based on the identifier. If the identifier does not exists in the database, create new record in the database.

public object findModel ( )
return object

\luya\admin\models\StorageFilter

                public function findModel()
{
    // find filter model based on the identifier
    $model = StorageFilter::find()->where(['identifier' => static::identifier()])->one();
    // if no model exists, create new record
    if (!$model) {
        $model = new StorageFilter();
        $model->setAttributes([
            'name' => $this->name(),
            'identifier' => static::identifier(),
        ]);
        $model->insert(false);
        $this->addLog("Added new filter '".static::identifier()."' with id '{$model->id}'.");
    }
    return $model;
}

            
getChain() public method

Returns a parsed effect chain for the current Filter. The method verifys if the provieded effect parameters are available in the effect definitions of luya.

public array getChain ( )
return array

Each row of the array must have "effect_id" and "effect_json_values" key.

throws luya\Exception

When effect option could be found in the effect definitions.

                public function getChain()
{
    $data = [];
    // get the chain from the effect, must be an array
    foreach ($this->chain() as $chainRow) {
        // set variables from chain array
        $effectIdentifier = $chainRow[0];
        $effectParams = $chainRow[1];
        // find the effect data for the effect identifier
        $effect = $this->findEffect($effectIdentifier);
        // get all params from the effect chain and verify if they are valid
        foreach ($effectParams as $effectParamVar => $effectParamValue) {
            if (!in_array($effectParamVar, $this->getEffectParamsList(Json::decode($effect['imagine_json_params'])))) {
                throw new Exception("Effect argument '$effectParamVar' does not exist in the effect definition of '{$effect['name']}'.");
            }
        }
        // create array with parsed effect id
        $data[] = ['effect_id' => $effect['id'], 'effect_json_values' => Json::encode($effectParams)];
    }
    return $data;
}

            
getEffectParamsList() public method

Get an array with all the effect param options, based on the effect params definition.

public array getEffectParamsList ( $effectParams )
$effectParams array
throws luya\Exception

When the vars key does not exists in the effect definition.

                public function getEffectParamsList($effectParams)
{
    if ($this->_effectParamsList === null) {
        // see if the effect definition contains a vars key
        if (!array_key_exists('vars', $effectParams)) {
            throw new Exception("Required 'vars' key not found in effect definition array.");
        }
        foreach ($effectParams['vars'] as $item) {
            $this->_effectParamsList[] = $item['var'];
        }
    }
    return $this->_effectParamsList;
}

            
getLog() public method

Return the log array.

public array getLog ( )
return array

Array with log messages.

                public function getLog()
{
    return $this->log;
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
identifier() public abstract static method

Defined in: luya\admin\base\FilterInterface::identifier()

Unique identifier name for the effect, no special chars allowed.

public abstract static string identifier ( )
return string

The identifier must match [a-zA-Z0-9\-]

                public static function identifier();

            
init() public method

Defined in: yii\base\BaseObject::init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
}

            
name() public abstract method

Understandable Name expression for the effect.

public abstract string name ( )
return string

A string containing the name to be listed in the admin area.

                abstract public function name();

            
save() public method

Update and save filter corresponding to the model, refresh chain values.

public boolean save ( )

                public function save()
{
    // get the filter model based on the current filter.
    $filterModel = $this->findModel();
    // update the name of the filter if changed
    if ($filterModel->name !== $this->name()) {
        $filterModel->setAttribute('name', $this->name());
        $filterModel->update(false);
        $this->addLog("Filter name '".$this->name()."' have been updated for identifier '".static::identifier()."'.");
    }
    // array containing the processed chain ids
    $processed = [];
    $removeImages = false;
    foreach ($this->getChain() as $chain) {
        // get filter chain for filter and effect
        $model = StorageFilterChain::find()->where(['filter_id' => $filterModel->id, 'effect_id' => $chain['effect_id']])->one();
        if ($model) {
            if (md5($chain['effect_json_values']) != md5(Json::encode($model->effect_json_values))) {
                $model->effect_json_values = $chain['effect_json_values'];
                if ($model->update(false)) {
                    $removeImages = true;
                    $this->addLog("[!] {$filterModel->name}: effect chain has changed.");
                }
            }
        } else {
            $model = new StorageFilterChain();
            $model->setAttributes(['filter_id' => $filterModel->id, 'effect_id' => $chain['effect_id'], 'effect_json_values' => $chain['effect_json_values']]);
            if ($model->save(false)) {
                $removeImages = true;
                $this->addLog("[+] {$filterModel->name}: Effect chain option/s have been added.");
            }
        }
        $processed[] = $model->id;
    }
    // remove not used chains for the current filter
    foreach (StorageFilterChain::find()->where(['not in', 'id', $processed])->andWhere(['=', 'filter_id', $filterModel->id])->all() as $deletion) {
        $this->addLog("[-] {$filterModel->name}: Effect chain option/s have been removed.");
        $deletion->delete();
        $removeImages = true;
    }
    if ($removeImages) {
        $this->addLog("[!] {$filterModel->name}: Remove images.");
        $removeLog = $filterModel->removeImageSources();
        foreach ($removeLog as $id => $sucess) {
            if ($sucess) {
                $this->addLog('✓ image ' . $id . ' sucessfull unlinked.');
            } else {
                $this->addLog('⨯ error while unlinking image id ' . $id);
            }
        }
    }
    return true;
}