Page.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. # 通用页面
  4. class Page extends Auth
  5. {
  6. protected $db;
  7. protected $id = false;
  8. protected $config = array();
  9. public function __construct($key = '')
  10. {
  11. parent::__construct();
  12. $this->id = Dever::input('id');
  13. list($app, $table) = explode('/', ltrim(Dever::input('load'), '/'));
  14. $this->db = Dever::db($table, $app);
  15. if (empty($this->db->config['struct'])) {
  16. Dever::error('无效信息');
  17. }
  18. $app = Dever::project($app);
  19. $manage = $app['path'] . 'table/manage/'.$table.'.php';
  20. if (is_file($manage)) {
  21. $this->db->config['manage'] = include $manage;
  22. if ($key) {
  23. $this->config = $this->db->config['manage'][$key] ?? array();
  24. }
  25. }
  26. }
  27. protected function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
  28. {
  29. if (empty($this->config[$key]) && $struct) {
  30. $this->config[$key] = $this->db->config['struct'];
  31. }
  32. if (empty($this->config[$key])) {
  33. return;
  34. }
  35. $setting = $this->config[$key];
  36. if (is_string($setting)) {
  37. $setting = explode(',', $setting);
  38. }
  39. $field = Dever::input('field');
  40. $result = array();
  41. foreach ($setting as $k => $v) {
  42. if ($field) {
  43. if (!Dever::check($field, $k)) {
  44. continue;
  45. }
  46. }
  47. if (!is_array($v)) {
  48. if (is_string($k) && isset($this->db->config['struct'][$k])) {
  49. $v = array('name' => $this->db->config['struct'][$k]['name'], 'type' => $v);
  50. } else {
  51. $k = $v;
  52. $v = array();
  53. }
  54. } else {
  55. if (isset($v['only'])) {
  56. if ($v['only'] == 'edit' && !$this->id) {
  57. continue;
  58. } elseif ($v['only'] == 'add' && $this->id) {
  59. continue;
  60. }
  61. }
  62. }
  63. $result[] = $this->data($data, $k, $v, $type, $disable);
  64. }
  65. return $result;
  66. }
  67. private function data(&$data, $key, $value = array(), $type = 'show', $disable = false)
  68. {
  69. $value['key'] = $key;
  70. $this->setName($value);
  71. $this->setType($value, $type);
  72. $this->setDisable($value, $disable);
  73. if ($type != 'show') {
  74. # 一般为更新页面需要的参数
  75. $this->setForm($value);
  76. $this->setRules($value);
  77. }
  78. $data[] = $value;
  79. return $value['name'];
  80. }
  81. private function setName(&$value)
  82. {
  83. if ($value['key'] == 'id') {
  84. $value['name'] = 'id';
  85. } elseif ($value['key'] == 'cdate') {
  86. $value['name'] = '创建时间';
  87. } elseif (empty($value['name'])) {
  88. $value['name'] = $this->db->config['struct'][$value['key']]['name'];
  89. }
  90. }
  91. private function setType(&$value, $type)
  92. {
  93. if (empty($value['type'])) {
  94. $value['type'] = $type;
  95. }
  96. if (strpos($value['type'], '(')) {
  97. $value['type'] = $type;
  98. }
  99. }
  100. private function setDisable(&$value, $disable)
  101. {
  102. if (isset($value['disable'])) {
  103. $disable = $value['disable'];
  104. }
  105. $value['disable'] = $disable;
  106. }
  107. private function setForm(&$value)
  108. {
  109. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  110. if ($option = $this->db->value($value['key'])) {
  111. if ($value['type'] == 'checkbox') {
  112. $value['value'] = $value['value'] ? explode(',', $value['value']) : array();
  113. }
  114. $value['option'] = $option;
  115. if ($value['type'] == 'text') {
  116. $value['type'] = 'select';
  117. }
  118. }
  119. }
  120. private function setRules(&$value)
  121. {
  122. if (isset($value['rules']) && $value['rules'] && is_array($value['rules'])) {
  123. foreach ($value['rules'] as $k => $v) {
  124. if (isset($v['only'])) {
  125. if ($v['only'] == 'edit' && !$this->id) {
  126. unset($value['rules'][$k]);
  127. break;
  128. } elseif ($v['only'] == 'add' && $this->id) {
  129. unset($value['rules'][$k]);
  130. break;
  131. }
  132. }
  133. }
  134. if (!isset($value['rules'][0])) {
  135. $value['rules'] = array_values($value['rules']);
  136. }
  137. }
  138. }
  139. # 通用的规则验证 一般为更新数据时使用
  140. protected function checkRules($set, $data)
  141. {
  142. if ($set['rules']) {
  143. foreach ($set['rules'] as $k => $v) {
  144. if (isset($v['required']) && $v['required'] && !$data && $data !== 0) {
  145. Dever::error($v['message']);
  146. }
  147. if (isset($v['pattern']) && $v['pattern'] && $data && !preg_match('/' . $v['pattern'] . '/', $data)) {
  148. Dever::error($v['message']);
  149. }
  150. if (isset($v['type']) && $v['type']) {
  151. if ($v['type'] == 'number' && !is_numeric($data)) {
  152. Dever::error($v['message']);
  153. } elseif ($v['type'] == 'array' && !is_array($data)) {
  154. Dever::error($v['message']);
  155. } elseif ($v['type'] == 'integer' && !is_int($data)) {
  156. Dever::error($v['message']);
  157. } elseif ($v['type'] == 'float' && !is_float($data)) {
  158. Dever::error($v['message']);
  159. } elseif ($v['type'] == 'string' && !is_string($data)) {
  160. Dever::error($v['message']);
  161. } elseif ($v['type'] == 'boolean' && !is_bool($data)) {
  162. Dever::error($v['message']);
  163. } elseif ($v['type'] == 'url' && !filter_var($data, FILTER_VALIDATE_URL)) {
  164. Dever::error($v['message']);
  165. } elseif ($v['type'] == 'email' && !filter_var($data, FILTER_VALIDATE_EMAIL)) {
  166. Dever::error($v['message']);
  167. } elseif ($v['type'] == 'enum' && isset($v['enum']) && !in_array($data, $v['enum'])) {
  168. Dever::error($v['message']);
  169. }
  170. }
  171. if (isset($v['len']) && $v['len'] && strlen($data) > $v['len']) {
  172. Dever::error($v['message']);
  173. }
  174. if (isset($v['min']) && $v['min'] && strlen($data) < $v['min']) {
  175. Dever::error($v['message']);
  176. }
  177. if (isset($v['max']) && $v['max'] && strlen($data) > $v['max']) {
  178. Dever::error($v['message']);
  179. }
  180. }
  181. }
  182. }
  183. }