| Current Path : /var/www/html/brspace/backend/modules/theme/controllers/ |
| Current File : /var/www/html/brspace/backend/modules/theme/controllers/ThemeSettingController.php |
<?php
namespace backend\modules\theme\controllers;
use Yii;
use backend\modules\theme\models\ThemeRule;
use backend\modules\theme\models\ThemeSetting;
use backend\modules\theme\models\ThemeSettingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use backend\modules\mimin\components\AccessControl;
use common\components\HelpersFunctions;
use yii\web\UploadedFile;
/**
* ThemeSettingController implements the CRUD actions for ThemeSetting model.
*/
class ThemeSettingController extends Controller {
/**
*
* {@inheritdoc}
*/
/**
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
]
];
}
/**
* Lists all ThemeSetting models.
*
* @return mixed
*/
public function actionIndex() {
$searchModel = new ThemeSettingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'setting' => ThemeSetting::settingArray()
]);
}
/**
* Displays a single ThemeSetting 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 ThemeSetting model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate() {
//static module side bg upload
if ($_FILES['file']['error'] === 0) {
$config = \Yii::$app->params['templateSetting'];
$configpath = $config['sideBgUpload'];
$filename = $config['sideBgFileName'];
if (empty($_POST['fileBase64'])) {
$source = $_FILES['file']['tmp_name'];
} else {
//convert base 64
$source = HelpersFunctions::base64ToImage($_POST['fileBase64'], $configpath, $filename);
}
//compress it
HelpersFunctions::compressImage($source, $configpath, $filename, 75);
}
//static module side bg upload
if ($_FILES['favicon']['error'] === 0) {
$source = $_FILES['favicon']['tmp_name'];
//compress it
HelpersFunctions::compressImage($source, './', 'favicon.ico', 75);
}
//static module login logo upload
if ($_FILES['inputloginlogo']['error'] === 0) {
$source = $_FILES['inputloginlogo']['tmp_name'];
//compress it
HelpersFunctions::compressImage($source, './images/', 'login_logo.png', false, 9);
}
//static module login background upload
if ($_FILES['inputloginbackground']['error'] === 0) {
$source = $_FILES['inputloginbackground']['tmp_name'];
//compress it
HelpersFunctions::compressImage($source, './images/', 'login_background.jpg', 75);
}
//if (!empty($_FILES["inputdashboardbanner"]["tmp_name"])) {
//$target_file = 'images/cmsbanner2.jpg';
//move_uploaded_file($_FILES["inputdashboardbanner"]["tmp_name"], $target_file);
//echo $_FILES["inputdashboardbanner"]["error"];
//die();
//}
$inputdashboardbanner = UploadedFile::getInstanceByName('inputdashboardbanner');
if (!empty($inputdashboardbanner)) {
$dashboardbannerpath = 'images/cmsbanner.jpg';
$inputdashboardbanner->saveAs($dashboardbannerpath);
}
//if ($_FILES['inputdashboardbanner']['error'] === 0) {
//$source = $_FILES['inputdashboardbanner']['tmp_name'];
//compress it
//HelpersFunctions::compressImage($source, './images/', 'cmsbanner.jpg', 75);
//}
if (count($_POST) > 0) {
$post = filter_var_array($_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
foreach ($post as $key => $arr) {
//adhoc theme setting
if (!strpos($key, '-id-')) {
continue;
}
$id = explode('-id-', $key);
$id = end($id); // last
if (is_array($arr) && !empty($id) && !empty($arr['param'])) {
foreach ($arr['param'] as $arrk => $val) {
$model = ThemeSetting::find()->where([
'setting_rule_id' => $id,
'setting_param_name' => $arrk
])->one();
if (empty($model)) {
$model = new ThemeSetting();
}
// process to json
$default = $arr['default'][$arrk];
if ($arr['type'][$arrk] === 'unit') {
$matches = null;
preg_match('/^([\d]+)([a-zA-Z%]+)/', $arr['default'][$arrk], $matches);
if (!empty($matches)) {
$default = json_encode([
'value' => $matches[1],
'unit' => $matches[2]
]);
}
$val = json_encode([
'value' => $val[0],
'unit' => $val[1]
]);
}
$model->setting_param_value = $val;
$model->setting_rule_id = $id;
$model->setting_param_name = $arrk;
$model->setting_param_default = $default;
$model->setting_param_type = $arr['type'][$arrk];
if (!$model->save()) {
}
}
}
}
$this->generateCss();
return $this->redirect([
'index'
]);
}
}
/**
* Updates an existing ThemeSetting 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()) && $model->save()) {
return $this->redirect([
'view',
'id' => $model->setting_id
]);
}
return $this->render('update', [
'model' => $model
]);
}
/**
* Deletes an existing ThemeSetting 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) {
$this->findModel($id)->delete();
return $this->redirect([
'index'
]);
}
/**
* Finds the ThemeSetting model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
* @return ThemeSetting the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = ThemeSetting::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
private function base64_to_jpeg($base64_string, $output_file) {
// open the output file for writing
$ifp = fopen($output_file, 'wb');
// split the string on commas
// $data[ 0 ] == "data:image/png;base64"
// $data[ 1 ] == <actual base64 string>
$data = explode(',', $base64_string);
// we could add validation here with ensuring count( $data ) > 1
fwrite($ifp, base64_decode($data[1]));
// clean up the file resource
fclose($ifp);
return $output_file;
}
private function generateCss() {
$path = dirname(Yii::getAlias('@app')) . DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'theme-module.css';
$cssrule = ThemeRule::find()->where([
'rule_status' => 1
])->all();
$content = '';
foreach ($cssrule as $css) {
$themes = ThemeSetting::settingArray()[$css->rule_id] ?? [];
$cconts = $css->rule_content;
foreach ($themes as $tkey => $tval) {
if (is_array($tval) && $tval['type'] === 'unit') {
$tval = $tval['value'] . $tval['unit'];
}
$cconts = strtr($cconts, [
"{{{$tkey}}}" => $tval
]);
}
$content .= PHP_EOL . $cconts . PHP_EOL;
}
file_put_contents($path, $content);
}
}