123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- <?php namespace Manage\Lib;
- use Dever;
- # 通用页面
- class Page extends Auth
- {
- protected $db;
- protected $key;
- protected $id = 0;
- protected $input = false;
- protected $menu = array();
- protected $config = array();
- protected $field = array();
- public $info = array();
- public function __construct($key = '', $load = '', $input = true)
- {
- parent::__construct();
- $this->key = $key;
- $this->input = $input;
- $this->id = $this->input('id', 0);
- if (!$load) {
- $load = Dever::input('load');
- }
- list($this->db, $this->menu) = Dever::load('common', 'manage')->db($load);
- if ($this->menu && $this->menu['show'] == 1) {
- $this->checkMenu($this->menu['id'], false);
- }
- $this->config = $this->db->config['manage'][$key] ?? array();
- if ($this->id && !strstr($this->id, ',')) {
- $this->info = $this->db->find($this->id);
- }
- }
- public function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
- {
- if (empty($this->config[$key]) && $struct && isset($this->db->config['struct']) && $this->db->config['struct']) {
- $this->config[$key] = $this->db->config['struct'];
- }
- if (empty($this->config[$key])) {
- return;
- }
- $setting = $this->config[$key];
- if (is_string($setting)) {
- $setting = explode(',', $setting);
- }
- $field = $this->input('field', '');
- return $this->setData($setting, $data, $field, $type, $disable);
- }
- # 获取某个数据的具体展示值
- public function getValue($key, $value, $data, $field = array())
- {
- if ($show = Dever::isset($field, 'show')) {
- $value = $this->getShow($show, $data);
- } elseif ($value && isset($this->db->config['struct'][$key]['value']) && $this->db->config['struct'][$key]['value']) {
- $value = $this->db->value($key, $value);
- } elseif ($key == 'cdate') {
- if (strstr($value, 'T')) {
- $value = date('Y-m-d H:i:s', strtotime($value));
- } elseif ($value) {
- $value = date('Y-m-d H:i:s', $value);
- } else {
- $value = '-';
- }
- }
- return $value;
- }
- # 获取关联数据
- public function getOther($key, $set, $data)
- {
- $where = array();
- foreach ($set as $k => $v) {
- if (!is_array($v) && isset($data[$v])) {
- $where[$k] = $data[$v];
- } else {
- $where[$k] = $v;
- }
- }
- if ($where) {
- return Dever::db($key)->select($where);
- }
- return array();
- }
- public function getShow($show, $data)
- {
- if (strpos($show, '{') !== false && strpos($show, '{"') === false) {
- $func = function ($r) use ($data) {
- if (isset($data[$r[1]])) {
- return $data[$r[1]];
- }
- return false;
- };
- $show = preg_replace_callback('/{(.*?)}/', $func, $show);
- }
- $eval = '$show = ' . $show . ';';
- @eval($eval);
- return $show;
- }
- # 获取菜单标题
- public function getTitle()
- {
- return $this->menu['name'];
- }
- # 获取左侧分栏
- protected function column(&$data, $name = '左侧分栏')
- {
- $data['column'] = false;
- if (isset($this->config['column'])) {
- $data['column'] = $this->config['column'];
- if (isset($this->config['column']['add'])) {
- $data['column']['add'] = array('name' => $this->config['column']['add'], 'func' => $this->getFunc('column_add', $name . '-' . $this->config['column']['add'], 101));
- }
- if (isset($this->config['column']['edit'])) {
- $data['column']['edit'] = array('name' => '编辑', 'func' => $this->getFunc('column_edit', $name . '-编辑', 102));
- }
- if (isset($this->config['column']['delete'])) {
- $data['column']['delete'] = array('name' => '删除', 'func' => $this->getFunc('column_delete', $name . '-删除', 103));
- }
- $data['column']['data'] = Dever::call($this->config['column']['data']);
- $data['height'] = '100%';
- }
- }
- # 通用的规则验证 一般为更新数据时使用
- protected function checkRules($set, $data)
- {
- if ($set['rules']) {
- if (!is_array($set['rules'])) {
- $set['rules'] = array
- (
- array
- (
- 'required' => true,
- 'trigger' => 'blur',
- 'message' => $set['name'] . '不能为空',
- ),
- );
- }
- foreach ($set['rules'] as $k => $v) {
- if (isset($v['required']) && $v['required'] && !$data && $data !== 0) {
- Dever::error($v['message']);
- }
- if ($data || $data === 0) {
- if (isset($v['pattern']) && $v['pattern'] && !preg_match('/' . $v['pattern'] . '/', $data)) {
- Dever::error($v['message']);
- }
- if (isset($v['type']) && $v['type']) {
- if ($v['type'] == 'number' && !is_numeric($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'array' && !is_array($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'integer' && !is_int($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'float' && !is_float($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'string' && !is_string($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'boolean' && !is_bool($data)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'url' && !filter_var($data, FILTER_VALIDATE_URL)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'email' && !filter_var($data, FILTER_VALIDATE_EMAIL)) {
- Dever::error($v['message']);
- } elseif ($v['type'] == 'enum' && isset($v['enum']) && !in_array($data, $v['enum'])) {
- Dever::error($v['message']);
- }
- }
- if (isset($v['len']) && $v['len'] && strlen($data) > $v['len']) {
- Dever::error($v['message']);
- }
- if (isset($v['min']) && $v['min'] && strlen($data) < $v['min']) {
- Dever::error($v['message']);
- }
- if (isset($v['max']) && $v['max'] && strlen($data) > $v['max']) {
- Dever::error($v['message']);
- }
- }
- }
- }
- }
- private function setData($setting, &$data, $field, $type, $disable)
- {
- $result = array();
- foreach ($setting as $k => $v) {
- if (!is_array($v)) {
- if (is_numeric($k)) {
- $k = $v;
- $v = $type;
- }
- if ($k == 'id') {
- $v = array('name' => 'ID', 'type' => $v);
- } elseif ($k == 'cdate') {
- $v = array('name' => '创建时间', 'type' => $v);
- } elseif(isset($this->db->config['struct'][$k])) {
- $v = array('name' => $this->db->config['struct'][$k]['name'], 'type' => $v);
- } else {
- $v = array('name' => $v);
- }
- } else {
- if (isset($v['only'])) {
- if ($v['only'] == 'edit' && !$this->id) {
- continue;
- } elseif ($v['only'] == 'add' && $this->id) {
- continue;
- }
- }
- }
- if ($field) {
- if (is_array($field)) {
- if (isset($field['param'])) {
- if (isset($field['param']['set'])) {
- $field = array_merge($field, $field['param']['set']);
- }
- if (isset($field['param']['search'])) {
- $field = array_merge($field, $field['param']['search']);
- }
- } elseif (isset($field['field']) && !Dever::check($field['field'], $k)) {
- continue;
- }
- if (isset($field[$k])) {
- $v['default'] = $field[$k];
- $v['type'] = 'hidden';
- }
- } elseif (!Dever::check($field, $k)) {
- continue;
- }
- }
- $info = $this->setField($data, $k, $v, $field, $type, $disable);
- if ($info) {
- $result[] = $info;
- }
- }
- return $result;
- }
- private function setField(&$data, $key, $value = array(), $field, $type = 'show', $disable = false)
- {
- $value['key'] = $key;
- $this->setName($value);
- # 对每个字段进行权限设置
- if (isset($value['func']) && $value['func']) {
- $func = $this->getFunc('field_' . $value['key'], '字段-' . $value['name'], 200);
- if (!$func) {
- return false;
- }
- }
- if (strpos($key, '/') && $this->key == 'update') {
- $this->setType($value, 'array');
- $this->setShow($value);
- if (strpos($key, '#')) {
- $key = str_replace('#', '', $key);
- }
- $sku = $value['type'] == 'sku' ? true : false;
- $value['value'] = $this->getOther($key, $value['where'], $this->info);
- $update = new \Manage\Api\Page\Update($key, false);
- $value['option'] = array();
- $value['content'] = $update->get($value['value'], $value['option'], $sku);
- $data[] = $value;
- return $value['name'];
- } else {
- $this->setType($value, $type);
- $this->setDisable($value, $disable);
- if ($this->key == 'update') {
- # 一般为更新页面需要的参数
- $this->setShow($value);
- $this->setRules($value);
- } elseif (isset($value['remote'])) {
- $value['remote'] = Dever::url($value['remote']);
- }
- if ($type == 'show') {
- $in = array('switch', 'select', 'input');
- if (in_array($value['type'], $in)) {
- $value['func'] = $this->getFunc('list_edit_' . $value['key'], '列表更新-' . $value['name'], 104);
- if (!$value['func']) {
- $value['type'] = 'show';
- if (isset($value['show'])) {
- unset($value['show']);
- }
- }
- }
- if (isset($value['child'])) {
- $child = array();
- $this->setData($value['child'], $child, $field, $type, $disable);
- $value['child'] = $child;
- } else {
- $this->field[$key] = $value;
- }
- }
- $this->setForm($value);
- $data[] = $value;
- return $value['name'];
- }
- }
- private function setShow(&$value)
- {
- if ($value['type'] == 'hidden') {
- $value['show'] = false;
- } elseif (!isset($value['show'])) {
- $value['show'] = true;
- }
- }
- private function setName(&$value)
- {
- if (empty($value['name']) && isset($this->db->config['struct'][$value['key']])) {
- $value['name'] = $this->db->config['struct'][$value['key']]['name'];
- }
- if (empty($value['placeholder']) && isset($value['name'])) {
- $value['placeholder'] = $value['name'];
- }
- }
- private function setType(&$value, $type)
- {
- if (empty($value['type'])) {
- $value['type'] = $type;
- }
- if (strpos($value['type'], '(')) {
- $value['type'] = $type;
- }
- if (empty($value['width'])) {
- $value['width'] = 'auto';
- }
- if (isset($value['upload']) && Dever::project('upload')) {
- $value['url'] = Dever::url('upload/save.act', array('id' => $value['upload']));
- $value['config'] = Dever::load('save', 'upload')->get($value['upload']);
- if (isset($value['multiple']) && $value['multiple']) {
- $value['limit'] = 10;
- } else {
- $value['limit'] = 1;
- }
- }
- if (isset($value['editorMenu'])) {
- if (isset($value['editorMenu']['uploadImage'])) {
- if (!is_array($value['editorMenu']['uploadImage'])) {
- $value['editorMenu']['uploadImage'] = array('upload' => $value['editorMenu']['uploadImage']);
- }
- $value['editorMenu']['uploadImage']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadImage']['upload']));
- if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
- $value['editorMenu']['uploadImage']['fieldName'] = 'file';
- }
- }
- if (isset($value['editorMenu']['uploadVideo'])) {
- if (!is_array($value['editorMenu']['uploadVideo'])) {
- $value['editorMenu']['uploadVideo'] = array('upload' => $value['editorMenu']['uploadVideo']);
- }
- $value['editorMenu']['uploadVideo']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadVideo']['upload']));
- if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
- $value['editorMenu']['uploadImage']['fieldName'] = 'file';
- }
- }
- }
- if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
- $value['default_time'] = array(\Dever\Helper\Date::mktime(date('Y-m-d 00:00:00'))*1000, \Dever\Helper\Date::mktime(date('Y-m-d 23:59:59'))*1000);
- }
- }
- private function setDisable(&$value, $disable)
- {
- if (isset($value['disable'])) {
- $disable = $value['disable'];
- }
- $value['disable'] = $disable;
- }
- private function setForm(&$value)
- {
- $value['value'] = Dever::input('search')[$value['key']] ?? '';
- if (!$value['value']) {
- if (isset($value['default']) && !strstr($value['default'], '{')) {
- $value['value'] = $value['default'];
- } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
- $value['value'] = $this->db->config['struct'][$value['key']]['default'];
- }
- }
- if (isset($value['option']) && $value['option']) {
- $this->db->config['struct'][$value['key']]['value'] = $value['option'];
- }
- if ($option = $this->db->value($value['key'])) {
- if ($value['type'] == 'checkbox') {
- $value['value'] = $value['value'] ? explode(',', $value['value']) : array();
- }
- $value['option'] = $option;
- if ($value['type'] == 'text') {
- $value['type'] = 'select';
- }
- }
- }
- private function setRules(&$value)
- {
- if (isset($value['rules']) && $value['rules']) {
- if (!is_array($value['rules'])) {
- $value['rules'] = array
- (
- array
- (
- 'required' => true,
- 'trigger' => 'blur',
- 'message' => $value['name'] . '不能为空',
- ),
- );
- }
- foreach ($value['rules'] as $k => $v) {
- if (isset($v['only'])) {
- if ($v['only'] == 'edit' && !$this->id) {
- unset($value['rules'][$k]);
- break;
- } elseif ($v['only'] == 'add' && $this->id) {
- unset($value['rules'][$k]);
- break;
- }
- }
- }
- if (!isset($value['rules'][0])) {
- $value['rules'] = array_values($value['rules']);
- }
- }
- }
- protected function input($key, $value = '')
- {
- return $this->input ? Dever::input($key) : $value;
- }
- }
|