Files
sentcms/app/model/Config.php
tensent 5c320206fb auth
2020-02-16 10:49:43 +08:00

122 lines
3.2 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\model;
use think\facade\Cache;
use think\Model;
/**
* 设置模型
*/
class Config extends Model {
protected $type = array(
'id' => 'integer',
);
protected $auto = array('name', 'update_time', 'status' => 1);
protected $insert = array('create_time');
protected function setNameAttr($value) {
return strtolower($value);
}
protected function getTypeTextAttr($value, $data) {
$config = Cache::get('system_config');
$type = $config['config_type_list'];
$type_text = explode(',', $type[$data['type']]);
return $type_text[0];
}
protected function getGroupTextAttr($value, $data) {
$config = Cache::get('system_config');
$group = $config['config_group_list'];
return isset($group[$data['group']]) ? $group[$data['group']] : '未分组';
}
public function lists() {
$map = array('status' => 1);
$res = $this->where($map)->field('type,name,value')->select();
$config = array();
foreach ($res->toArray() as $value) {
$config[$value['name']] = $this->parse($value['type'], $value['value']);
}
return $config;
}
public function updateConfig($data) {
if (empty($data['config'])) {
$this->error = "无更新";
return false;
}
foreach ($data['config'] as $key => $value) {
self::update(['value' => $value], ['name' => $key], ['value']);
}
Cache::set('system_config', null);
return true;
}
/**
* 根据配置类型解析配置
* @param integer $type 配置类型
* @param string $value 配置值
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
private function parse($type, $value) {
switch ($type) {
case 'textarea': //解析数组
$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
if (strpos($value, ':')) {
$value = array();
foreach ($array as $val) {
$list = explode(':', $val);
if (isset($list[2])) {
$value[$list[0]] = $list[1] . ',' . $list[2];
} else {
$value[$list[0]] = $list[1];
}
}
} else {
$value = $array;
}
break;
}
return $value;
}
public function getThemesList() {
$files = array();
$files['pc'] = $this->getList('pc');
$files['mobile'] = $this->getList('mobile');
return $files;
}
protected function getList($type) {
$path = './template/';
$file = opendir($path);
while (false !== ($filename = readdir($file))) {
if (!in_array($filename, array('.', '..'))) {
$files = $path . $filename . '/info.php';
if (is_file($files)) {
$info = include $files;
if (isset($info['type']) && $info['type'] == $type) {
$info['id'] = $filename;
$info['img'] = '/template/' . $filename . '/' . $info['img'];
$list[] = $info;
} else {
continue;
}
}
}
}
return isset($list) ? $list : array();
}
}