You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB

6 years ago
<?php
/**
* Created by Error202
* Date: 04.06.2018
*/
namespace backend\components;
use yii\base\Action;
3 years ago
use yii\base\ExitException;
use yii\db\ActiveRecord;
6 years ago
use yii\db\Expression;
use yii\base\InvalidConfigException;
use yii\web\MethodNotAllowedHttpException;
use Yii;
3 years ago
use yii\web\Response;
6 years ago
class ToggleAction extends Action
{
6 years ago
/**
* @var string name of the model
*/
3 years ago
public string $modelClass;
6 years ago
/**
* @var string model attribute
*/
3 years ago
public string $attribute = 'active';
6 years ago
/**
* @var string scenario model
*/
3 years ago
public ?string $scenario = null;
6 years ago
/**
* @var string|array additional condition for loading the model
*/
3 years ago
public string|array $andWhere;
6 years ago
/**
* @var string|int|boolean|Expression what to set active models to
*/
3 years ago
public string|int|bool|Expression $onValue = 1;
6 years ago
/**
* @var string|int|boolean what to set inactive models to
*/
3 years ago
public string|int|bool $offValue = 0;
6 years ago
/**
* @var bool whether to set flash messages or not
*/
3 years ago
public bool $setFlash = false;
6 years ago
/**
* @var string flash message on success
*/
3 years ago
public string $flashSuccess = 'Model saved';
6 years ago
/**
* @var string flash message on error
*/
3 years ago
public string $flashError = 'Error saving Model';
6 years ago
/**
* @var string|array URL to redirect to
*/
3 years ago
public string|array $redirect;
6 years ago
/**
* @var string pk field name
*/
3 years ago
public string $primaryKey = 'id';
6 years ago
3 years ago
/**
* @param $id
* @return Response
* @throws InvalidConfigException
* @throws MethodNotAllowedHttpException
* @throws ExitException
*/
6 years ago
public function run($id)
{
if (!Yii::$app->request->getIsPost()) {
throw new MethodNotAllowedHttpException();
}
$id = (int)$id;
$result = null;
if (empty($this->modelClass) || !class_exists($this->modelClass)) {
throw new InvalidConfigException("Model class doesn't exist");
}
3 years ago
/* @var $modelClass ActiveRecord */
6 years ago
$modelClass = $this->modelClass;
$attribute = $this->attribute;
$model = $modelClass::find()->where([$this->primaryKey => $id]);
if (!empty($this->andWhere)) {
$model->andWhere($this->andWhere);
}
$model = $model->one();
if (!is_null($this->scenario)) {
$model->scenario = $this->scenario;
}
if (!$model->hasAttribute($this->attribute)) {
throw new InvalidConfigException("Attribute doesn't exist");
}
if ($model->$attribute == $this->onValue) {
$model->$attribute = $this->offValue;
} elseif ($this->onValue instanceof Expression && $model->$attribute != $this->offValue) {
$model->$attribute = $this->offValue;
} else {
$model->$attribute = $this->onValue;
}
if ($model->save()) {
if ($this->setFlash) {
Yii::$app->session->setFlash('success', $this->flashSuccess);
}
} else {
if ($this->setFlash) {
Yii::$app->session->setFlash('error', $this->flashError);
}
}
if (Yii::$app->request->getIsAjax()) {
Yii::$app->end();
}
/* @var $controller \yii\web\Controller */
$controller = $this->controller;
if (!empty($this->redirect)) {
return $controller->redirect($this->redirect);
}
return $controller->redirect(Yii::$app->request->getReferrer());
}
}