| Current Path : /var/www/html/protegev2/backend/modules/theme/controllers/ |
| Current File : /var/www/html/protegev2/backend/modules/theme/controllers/ThemeRuleController.php |
<?php
namespace backend\modules\theme\controllers;
use Yii;
use backend\modules\theme\models\ThemeRule;
use backend\modules\theme\models\ThemeRuleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\modules\theme\models\ThemeSetting;
use yii\helpers\Url;
/**
* ThemeRuleController implements the CRUD actions for ThemeRule model.
*/
class ThemeRuleController extends Controller {
/**
*
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => [
'POST'
]
]
]
];
}
/**
* Lists all ThemeRule models.
*
* @return mixed
*/
public function actionIndex() {
$searchModel = new ThemeRuleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
/**
* Displays a single ThemeRule model.
*
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id)
]);
}
/**
* Creates a new ThemeRule model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate() {
$model = new ThemeRule();
$model->rule_status = 1;
if ($model->load(Yii::$app->request->post())) {
$params = [];
$post = filter_var_array($_POST, FILTER_SANITIZE_STRING);
for ($i = 0; $i < count($post['param_name']); $i++) {
$default = $post['param_default'][$i];
if ($post['param_type'][$i] === 'unit') {
$default = [
'value' => '0',
'unit' => 'px'
];
$matches = null;
preg_match('/^([\d]+)([a-zA-Z%]+)/', $post['param_default'][$i], $matches);
$default = [
'value' => $matches[1],
'unit' => $matches[2]
];
}
$params[] = [
'param_name' => $post['param_name'][$i],
'param_default' => $default,
'param_type' => $post['param_type'][$i]
];
}
$model->rule_params = json_encode($params);
if ($model->save()) {
$this->getView()->registerJs("swal.fire({
title:'Saved',
icon: 'success',
text: 'Data has been saved successfully',
}).then(function(result){
window.location = '" . Url::toRoute(['index']) . "';
});");
}
}
return $this->render('create', [
'model' => $model
]);
}
/**
* Updates an existing ThemeRule model.
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$params = [];
$post = filter_var_array($_POST, FILTER_SANITIZE_STRING);
for ($i = 0; $i < count($post['param_name']); $i++) {
$default = $post['param_default'][$i];
if ($post['param_type'][$i] === 'unit') {
$default = [
'value' => '0',
'unit' => 'px'
];
$matches = null;
preg_match('/^([\d]+)([a-zA-Z%]+)/', $post['param_default'][$i], $matches);
if (!empty($matches)) {
$default = [
'value' => $matches[1],
'unit' => $matches[2]
];
}
}
$params[] = [
'param_name' => $post['param_name'][$i],
'param_default' => $default,
'param_type' => $post['param_type'][$i]
];
// update def / type on theme setting
$themeSetting = ThemeSetting::find()->where([
'setting_rule_id' => $id,
'setting_param_name' => $post['param_name'][$i]
])->one();
if (!empty($themeSetting)) {
$themeSetting->setting_param_type = $post['param_type'][$i];
$themeSetting->setting_param_default = json_encode($default);
$themeSetting->save();
}
}
$model->rule_params = json_encode($params);
if ($model->save()) {
$this->getView()->registerJs("swal.fire({
title:'Saved',
icon: 'success',
text: 'Data has been saved successfully',
}).then(function(result){
window.location = '" . Url::toRoute(['index']) . "';
});");
}
}
return $this->render('update', [
'model' => $model
]);
}
/**
* Deletes an existing ThemeRule model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}
/**
* Finds the ThemeRule model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
* @return ThemeRule the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = ThemeRule::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}