123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php namespace Manage\Lib;
- use Dever;
- # 通用页面
- class Page extends Auth
- {
- protected $db;
- protected $id = false;
- protected $config = array();
- public function __construct($key = '')
- {
- parent::__construct();
- $this->id = Dever::input('id');
- list($app, $table) = explode('/', ltrim(Dever::input('load'), '/'));
- $this->db = Dever::db($table, $app);
- if (empty($this->db->config['struct'])) {
- Dever::error('无效信息');
- }
- $app = Dever::project($app);
- $manage = $app['path'] . 'table/manage/'.$table.'.php';
- if (is_file($manage)) {
- $this->db->config['manage'] = include $manage;
- if ($key) {
- $this->config = $this->db->config['manage'][$key] ?? array();
- }
- }
- }
- protected function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
- {
- if (empty($this->config[$key]) && $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 = Dever::input('field');
- $result = array();
- foreach ($setting as $k => $v) {
- if ($field) {
- if (!Dever::check($field, $k)) {
- continue;
- }
- }
- if (!is_array($v)) {
- if (is_string($k) && isset($this->db->config['struct'][$k])) {
- $v = array('name' => $this->db->config['struct'][$k]['name'], 'type' => $v);
- } else {
- $k = $v;
- $v = array();
- }
- } else {
- if (isset($v['only'])) {
- if ($v['only'] == 'edit' && !$this->id) {
- continue;
- } elseif ($v['only'] == 'add' && $this->id) {
- continue;
- }
- }
- }
- $result[] = $this->data($data, $k, $v, $type, $disable);
- }
- return $result;
- }
- private function data(&$data, $key, $value = array(), $type = 'show', $disable = false)
- {
- $value['key'] = $key;
- $this->setName($value);
- $this->setType($value, $type);
- $this->setDisable($value, $disable);
- if ($type != 'show') {
- # 一般为更新页面需要的参数
- $this->setForm($value);
- $this->setRules($value);
- }
- $data[] = $value;
- return $value['name'];
- }
- private function setName(&$value)
- {
- if ($value['key'] == 'id') {
- $value['name'] = 'id';
- } elseif ($value['key'] == 'cdate') {
- $value['name'] = '创建时间';
- } elseif (empty($value['name'])) {
- $value['name'] = $this->db->config['struct'][$value['key']]['name'];
- }
- }
- private function setType(&$value, $type)
- {
- if (empty($value['type'])) {
- $value['type'] = $type;
- }
- if (strpos($value['type'], '(')) {
- $value['type'] = $type;
- }
- }
- 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 ($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'] && is_array($value['rules'])) {
- 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 checkRules($set, $data)
- {
- if ($set['rules']) {
- foreach ($set['rules'] as $k => $v) {
- if (isset($v['required']) && $v['required'] && !$data && $data !== 0) {
- Dever::error($v['message']);
- }
- if (isset($v['pattern']) && $v['pattern'] && $data && !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']);
- }
- }
- }
- }
- }
|