|
@@ -0,0 +1,191 @@
|
|
|
+<?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']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|