Update.php 14 KB

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