Update.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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($load = '', $id = false)
  8. {
  9. parent::__construct('update', $load, $id);
  10. }
  11. public function get()
  12. {
  13. $func = $this->checkFunc();
  14. $remote = $show = array();
  15. $data['update'] = $data['field'] = array();
  16. $this->setting('field', $data['update'], true, 'text');
  17. foreach ($data['update'] as $k => $v) {
  18. if ($v['type'] == 'tree') {
  19. $v['value'] = array();
  20. }
  21. if (isset($v['remote'])) {
  22. # 需要设置remote
  23. $remote[$v['key']] = array($k, $v['remote']);
  24. }
  25. if (isset($v['show']) && is_string($v['show'])) {
  26. $show[$v['key']] = array($k, $v['show']);
  27. }
  28. $data['field'][$v['key']] = $v['value'];
  29. }
  30. $this->column($data);
  31. $data['info_id'] = false;
  32. if (!$this->info && $data['column'] && $data['column']['where'] == 'id') {
  33. $this->info = $this->db->find($data['column']['active']);
  34. if ($this->info) {
  35. $data['info_id'] = $this->info['id'];
  36. if (!$func) {
  37. $func = $this->getFunc('edit', '编辑', 1);
  38. if (!$func) {
  39. Dever::error('无操作权限');
  40. }
  41. }
  42. }
  43. } elseif (!$func) {
  44. $func = $this->getFunc('update', '更新', 1);
  45. if (!$func) {
  46. Dever::error('无操作权限');
  47. }
  48. }
  49. if ($this->info) {
  50. $data['info'] = $this->info;
  51. foreach ($data['info'] as $k => $v) {
  52. if (isset($data['field'][$k])) {
  53. if (is_array($data['field'][$k])) {
  54. $v = explode(',', $v);
  55. }
  56. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['value'])) {
  57. $v = $this->config['field'][$k]['value'];
  58. }
  59. $data['field'][$k] = $v;
  60. if (isset($remote[$k])) {
  61. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]);
  62. $result = Dever::call($remote[$k][1], $v, 'api');
  63. if ($result) {
  64. $this->setUpdate($data['update'], $result);
  65. }
  66. }
  67. if (isset($show[$k])) {
  68. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $data['info']);
  69. }
  70. }
  71. }
  72. } else {
  73. if ($remote) {
  74. foreach ($remote as $k => $v) {
  75. $data['update'][$v[0]]['remote'] = Dever::url($v[1]);
  76. }
  77. }
  78. if ($show) {
  79. foreach ($show as $k => $v) {
  80. $data['update'][$v[0]]['show'] = true;
  81. }
  82. }
  83. }
  84. $data['desc'] = $this->config['desc'] ?? '';
  85. $data['drag'] = $this->config['drag'] ?? false;
  86. $this->layout($data);
  87. $this->tab($data, 'step');
  88. if (!$data['step']) {
  89. $this->tab($data);
  90. }
  91. $this->control($data);
  92. return $data;
  93. }
  94. private function setUpdate(&$update, $result)
  95. {
  96. foreach ($update as $k => $v) {
  97. if (isset($result[$v['key']])) {
  98. $update[$k] = array_merge($update[$k], $result[$v['key']]);
  99. }
  100. }
  101. }
  102. private function control(&$data)
  103. {
  104. $data['control'] = array();
  105. if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
  106. foreach ($data['control'] as $k => $v) {
  107. foreach ($data['update'] as $k1 => $v1) {
  108. if ($v1['key'] == $k) {
  109. $show = true;
  110. foreach ($v as $k2 => $v2) {
  111. if ($data['field'][$k2] != $v2) {
  112. $show = false;
  113. }
  114. }
  115. $data['update'][$k1]['show'] = $show;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. private function tab(&$data, $type = 'tab')
  122. {
  123. $field = Dever::input('field');
  124. $data[$type] = array();
  125. if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  126. foreach ($this->config[$type] as $k => $v) {
  127. if (is_string($v)) {
  128. $field = array();
  129. $data[$type][] = array
  130. (
  131. 'name' => $k,
  132. 'update' => $this->getUpdate($v, $data['update'], $field),
  133. 'field' => $field,
  134. );
  135. } else {
  136. $field = array();
  137. $result = array();
  138. $result['name'] = $k;
  139. foreach ($v as $v1) {
  140. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  141. }
  142. $result['field'] = $field;
  143. $data[$type][] = $result;
  144. }
  145. }
  146. $data['update'] = array();
  147. }
  148. }
  149. private function layout(&$data)
  150. {
  151. $field = Dever::input('field');
  152. $data['layout'] = array();
  153. if (!$field && isset($this->config['layout'])) {
  154. foreach ($this->config['layout'] as $k => $v) {
  155. $field = array();
  156. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  157. }
  158. $data['update'] = array();
  159. }
  160. }
  161. private function getUpdate($set, $update, &$field)
  162. {
  163. $result = array();
  164. if (is_string($set)) {
  165. $set = explode(',', $set);
  166. foreach ($set as $k => $v) {
  167. foreach ($update as $value) {
  168. if ($value['key'] == $v) {
  169. $result[] = $value;
  170. $field[] = $v;
  171. }
  172. }
  173. }
  174. } else {
  175. foreach ($set as $k => $v) {
  176. foreach ($update as $value) {
  177. if ($value['key'] == $k) {
  178. $result[] = array('span' => $v, 'update' => array($value));
  179. $field[] = $k;
  180. }
  181. }
  182. }
  183. }
  184. return $result;
  185. }
  186. public function do_commit(){}
  187. public function do()
  188. {
  189. $this->checkFunc();
  190. $update = array();
  191. $this->setting('field', $update, true, 'text');
  192. if ($update) {
  193. $data = $other = array();
  194. $input = Dever::input();
  195. $id = false;
  196. if (isset($input['id']) && $input['id'] > 0) {
  197. $id = $input['id'];
  198. }
  199. foreach ($update as $k => $v) {
  200. if (isset($input[$v['key']])) {
  201. if (isset($v['rules'])) {
  202. $this->checkRules($v, $input[$v['key']]);
  203. }
  204. if (strpos($v['key'], '/')) {
  205. $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
  206. } else {
  207. $this->doData($data, $v['key'], $input[$v['key']]);
  208. }
  209. } elseif ($id) {
  210. $data[$v['key']] = '';
  211. }
  212. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  213. unset($data[$v['key']]);
  214. }
  215. }
  216. if (!$data) {
  217. Dever::error('无效数据');
  218. }
  219. $this->exists($id, $data);
  220. $this->start($id, $data);
  221. if ($id) {
  222. $info = $this->db->find($id);
  223. if ($info) {
  224. $state = $this->db->update($info['id'], $data);
  225. if ($state) {
  226. $id = $info['id'];
  227. }
  228. } else {
  229. $data['id'] = $id;
  230. $id = $this->db->insert($data);
  231. }
  232. } else {
  233. $id = $this->db->insert($data);
  234. }
  235. if (!$id) {
  236. Dever::error('操作失败');
  237. }
  238. $this->end($id, $data);
  239. $this->other($id, $data, $other);
  240. return '操作成功';
  241. }
  242. }
  243. private function doData(&$data, $key, $value)
  244. {
  245. if (is_array($value)) {
  246. if (isset($value[0])) {
  247. $value = ltrim(implode(',', $value), ',');
  248. } else {
  249. $value = Dever::json_ecode($value);
  250. }
  251. }
  252. if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) {
  253. $value = Dever::call($handle, $value);
  254. if (is_array($value) && isset($value[$key])) {
  255. foreach ($value as $k => $v) {
  256. $data[$k] = $v;
  257. }
  258. return;
  259. }
  260. }
  261. $data[$key] = $value;
  262. }
  263. private function exists($id, $data)
  264. {
  265. if (isset($this->config['check']) && $this->config['check']) {
  266. $check = explode(',', $this->config['check']);
  267. $where = array();
  268. $name = array();
  269. foreach ($check as $k => $v) {
  270. if (isset($data[$v])) {
  271. if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) {
  272. $n = $this->config['field'][$v]['name'];
  273. } elseif (isset($this->db->config['struct'][$v])) {
  274. $n = $this->db->config['struct'][$v]['name'];
  275. } else {
  276. $n = $v;
  277. }
  278. $where[$v] = $data[$v];
  279. $name[] = $n;
  280. }
  281. }
  282. if ($where) {
  283. if ($id) {
  284. $where['id'] = array('!=', $id);
  285. }
  286. $info = $this->db->find($where);
  287. if ($info) {
  288. $name = implode('、', $name);
  289. Dever::error($name . '已存在');
  290. }
  291. }
  292. }
  293. }
  294. private function start($id, &$data)
  295. {
  296. if (isset($this->config['start']) && $this->config['start']) {
  297. $data['id'] = $id;
  298. $data = Dever::call($this->config['start'], $data);
  299. }
  300. }
  301. private function end($id, $data)
  302. {
  303. if (isset($this->config['end']) && $this->config['end']) {
  304. $data['id'] = $id;
  305. Dever::call($this->config['end'], $data);
  306. }
  307. }
  308. private function other($rid, $data, $other)
  309. {
  310. if ($other) {
  311. foreach ($other as $k => $v) {
  312. $common = $v[0];
  313. $update = $v[1];
  314. $drag = $v[2];
  315. $input = $v[3];
  316. $value = array();
  317. foreach ($input as $k1 => $v1) {
  318. if (isset($v1['id'])) {
  319. $value['id'] = $v1['id'];
  320. }
  321. foreach ($common as $k2 => $v2) {
  322. if ($v2 == 'id') {
  323. $value[$k2] = $rid;
  324. } elseif (isset($data[$v2])) {
  325. $value[$k2] = $data[$v2];
  326. }
  327. }
  328. foreach ($update as $k2 => $v2) {
  329. if (isset($v1[$k2])) {
  330. $value[$k2] = $v1[$k2];
  331. }
  332. }
  333. if ($drag) {
  334. $value[$drag] = $k1+1;
  335. }
  336. if (isset($value['id']) && $value['id'] > 0) {
  337. $id = $value['id'];
  338. unset($value['id']);
  339. Dever::db($k)->update($id, $value);
  340. } else {
  341. Dever::db($k)->insert($value);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }