Update.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. # 更新页
  5. class Update extends Page
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct('update');
  10. }
  11. public function get()
  12. {
  13. $data['update'] = $data['field'] = array();
  14. $this->setting('field', $data['update'], true, 'text');
  15. foreach ($data['update'] as $k => $v) {
  16. $data['field'][$v['key']] = $v['value'];
  17. }
  18. if ($this->id) {
  19. $data['info'] = $this->db->find($this->id);
  20. if ($data['info']) {
  21. foreach ($data['info'] as $k => $v) {
  22. if (isset($data['field'][$k])) {
  23. if (is_array($data['field'][$k])) {
  24. $v = explode(',', $v);
  25. }
  26. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['value'])) {
  27. $v = $this->config['field'][$k]['value'];
  28. }
  29. $data['field'][$k] = $v;
  30. }
  31. }
  32. }
  33. }
  34. $data['desc'] = $this->config['desc'] ?? '';
  35. $this->layout($data);
  36. $this->tab($data, 'step');
  37. if (!$data['step']) {
  38. $this->tab($data);
  39. }
  40. return $data;
  41. }
  42. private function tab(&$data, $type = 'tab')
  43. {
  44. $field = Dever::input('field');
  45. $data[$type] = array();
  46. if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  47. foreach ($this->config[$type] as $k => $v) {
  48. if (is_string($v)) {
  49. $field = array();
  50. $data[$type][] = array
  51. (
  52. 'name' => $k,
  53. 'update' => $this->getUpdate($v, $data['update'], $field),
  54. 'field' => $field,
  55. );
  56. } else {
  57. $field = array();
  58. $result = array();
  59. $result['name'] = $k;
  60. foreach ($v as $v1) {
  61. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  62. }
  63. $result['field'] = $field;
  64. $data[$type][] = $result;
  65. }
  66. }
  67. $data['update'] = array();
  68. }
  69. }
  70. private function layout(&$data)
  71. {
  72. $field = Dever::input('field');
  73. $data['layout'] = array();
  74. if (!$field && isset($this->config['layout'])) {
  75. foreach ($this->config['layout'] as $k => $v) {
  76. $field = array();
  77. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  78. }
  79. $data['update'] = array();
  80. }
  81. }
  82. private function getUpdate($set, $update, &$field)
  83. {
  84. $result = array();
  85. if (is_string($set)) {
  86. $set = explode(',', $set);
  87. foreach ($set as $k => $v) {
  88. foreach ($update as $value) {
  89. if ($value['key'] == $v) {
  90. $result[] = $value;
  91. $field[] = $v;
  92. }
  93. }
  94. }
  95. } else {
  96. foreach ($set as $k => $v) {
  97. foreach ($update as $value) {
  98. if ($value['key'] == $k) {
  99. $result[] = array('span' => $v, 'update' => array($value));
  100. $field[] = $k;
  101. }
  102. }
  103. }
  104. }
  105. return $result;
  106. }
  107. public function do_commit(){}
  108. public function do()
  109. {
  110. $update = array();
  111. $this->setting('update', $update, true, 'text');
  112. if ($update) {
  113. $data = array();
  114. $input = Dever::input();
  115. $id = false;
  116. if (isset($input['id']) && $input['id'] > 0) {
  117. $id = $input['id'];
  118. }
  119. foreach ($update as $k => $v) {
  120. if (isset($input[$v['key']])) {
  121. if (isset($v['rule'])) {
  122. $this->checkRules($v, $input[$v['key']]);
  123. }
  124. $this->doData($data, $v['key'], $input[$v['key']]);
  125. } elseif ($id) {
  126. $data[$v['key']] = '';
  127. }
  128. }
  129. if (!$data) {
  130. Dever::error('无效数据');
  131. }
  132. $this->check($id, $data);
  133. $this->start($id, $data);
  134. if ($id) {
  135. $info = $this->db->find($id);
  136. if (!$info) {
  137. Dever::error('无效数据');
  138. }
  139. $state = $this->db->update($info['id'], $data);
  140. if ($state) {
  141. $id = $info['id'];
  142. }
  143. } else {
  144. $id = $this->db->insert($data);
  145. }
  146. if (!$id) {
  147. Dever::error('操作失败');
  148. }
  149. $this->end($id, $data);
  150. return '操作成功';
  151. }
  152. }
  153. private function doData(&$data, $key, $value)
  154. {
  155. if (is_array($value)) {
  156. if (isset($value[0])) {
  157. $value = ltrim(implode(',', $value), ',');
  158. } else {
  159. $value = Dever::json_ecode($value);
  160. }
  161. }
  162. if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) {
  163. $value = Dever::call($handle, $value);
  164. if (is_array($value) && isset($value[$key])) {
  165. foreach ($value as $k => $v) {
  166. $data[$k] = $v;
  167. }
  168. return;
  169. }
  170. }
  171. $data[$key] = $value;
  172. }
  173. private function check($id, $data)
  174. {
  175. if (isset($this->config['check']) && $this->config['check']) {
  176. $check = explode(',', $this->config['check']);
  177. $where = array();
  178. $name = array();
  179. foreach ($check as $k => $v) {
  180. if (isset($data[$v])) {
  181. if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) {
  182. $n = $this->config['field'][$v]['name'];
  183. } elseif (isset($this->db->config['struct'][$v])) {
  184. $n = $this->db->config['struct'][$v]['name'];
  185. } else {
  186. $n = $v;
  187. }
  188. $where[$v] = $data[$v];
  189. $name[] = $n;
  190. }
  191. }
  192. if ($where) {
  193. if ($id) {
  194. $where['id'] = array('!=', $id);
  195. }
  196. $info = $this->db->find($where);
  197. if ($info) {
  198. $name = implode('、', $name);
  199. Dever::error($name . '已存在');
  200. }
  201. }
  202. }
  203. }
  204. private function start($id, &$data)
  205. {
  206. if (isset($this->config['start']) && $this->config['start']) {
  207. echo $this->config['start'];die;
  208. }
  209. }
  210. private function end($id, $data)
  211. {
  212. if (isset($this->config['end']) && $this->config['end']) {
  213. echo $this->config['end'];die;
  214. }
  215. }
  216. }