Update.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 = '', $input = true)
  8. {
  9. parent::__construct('update', $load, $input);
  10. }
  11. public function get(&$value = array(), &$option = array())
  12. {
  13. $func = $this->checkFunc();
  14. $remote = $show = $spec = $source = $default = 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' || $v['type'] == 'cascader' || $v['type'] == 'checkbox' || isset($v['multiple'])) {
  19. $v['value'] = array();
  20. }
  21. if (isset($v['source'])) {
  22. $source[$v['key']] = $v['source'];
  23. }
  24. if (isset($v['remote'])) {
  25. $remote[$v['key']] = array($k, $v['remote'], $v['key']);
  26. if (isset($v['remote_default']) && !$v['remote_default']) {
  27. unset($remote[$v['key']][2]);
  28. }
  29. }
  30. if (isset($v['spec_data'])) {
  31. $spec[$v['key']] = array($k, $v['spec_data'], $v['key'] . '_spec');
  32. }
  33. if (isset($v['show']) && is_string($v['show'])) {
  34. $show[$v['key']] = array($k, $v['show']);
  35. }
  36. if (isset($v['default']) && is_string($v['default']) && strstr($v['default'], '{')) {
  37. $default[$v['key']] = array($k, $v['default']);
  38. }
  39. $data['field'][$v['key']] = $v['value'];
  40. if ($v['type'] == 'sku') {
  41. $data['field'][$v['key'] . '_spec'] = [];
  42. }
  43. if (isset($v['option'])) {
  44. $data['option'][$v['key']] = $v['option'];
  45. unset($data['update'][$k]['option']);
  46. }
  47. }
  48. $this->column($data);
  49. $data['info_id'] = false;
  50. if (!$this->info && $data['column'] && $data['column']['where'] == 'id') {
  51. $this->info = $this->db->find($data['column']['active']);
  52. if ($this->info) {
  53. $data['info_id'] = $this->info['id'];
  54. if (!$func) {
  55. $func = $this->getFunc('edit', '编辑', 1);
  56. if (!$func && $this->menu && $this->menu['show'] == 1) {
  57. Dever::error('无操作权限');
  58. }
  59. }
  60. }
  61. } elseif (!$func) {
  62. $func = $this->getFunc('update', '更新', 1);
  63. if (!$func && $this->menu && $this->menu['show'] == 1) {
  64. Dever::error('无操作权限');
  65. }
  66. }
  67. if ($this->info) {
  68. $info = $this->info;
  69. $this->setInfo($info, $data, $remote, $show, $source, $default);
  70. if ($spec) {
  71. foreach ($spec as $k => $v) {
  72. $data['update'][$spec[$k][0]]['remote'] = Dever::url($spec[$k][1]);
  73. $result = Dever::call($spec[$k][1], array($this->info['id']));
  74. if ($result) {
  75. $data['field'][$spec[$k][2]] = $result;
  76. }
  77. }
  78. }
  79. } elseif ($value) {
  80. foreach ($value as $k => &$v) {
  81. $this->setInfo($v, $data, $remote, $show, $source, $default, 2);
  82. $option[$k] = $data['option'];
  83. }
  84. if (isset($data['reset'])) {
  85. foreach ($data['reset'] as $k1 => $v1) {
  86. if (isset($data['option'][$k1])) {
  87. unset($data['option'][$k1]);
  88. $data['field'][$k1] = $v1;
  89. }
  90. }
  91. }
  92. } else {
  93. if ($remote) {
  94. $info = array();
  95. foreach ($remote as $k => $v) {
  96. $data['update'][$v[0]]['remote'] = Dever::url($v[1]);
  97. if (isset($v[2]) && isset($data['option'][$v[2]]) && $data['option'][$v[2]] && $m = Dever::issets($data['option'][$v[2]][0], 'id')) {
  98. $result = Dever::call($v[1], array($m), 'api');
  99. if ($result) {
  100. $this->setUpdate($info, $data, $result);
  101. }
  102. }
  103. }
  104. }
  105. if ($show) {
  106. foreach ($show as $k => $v) {
  107. $data['update'][$v[0]]['show'] = true;
  108. }
  109. }
  110. }
  111. $data['desc'] = $this->config['desc'] ?? '';
  112. $data['drag'] = $this->config['drag'] ?? false;
  113. $this->layout($data);
  114. $this->control($data);
  115. $this->tab($data, 'step');
  116. if (!$data['step']) {
  117. $this->tab($data);
  118. }
  119. return $data;
  120. }
  121. private function setInfo(&$info, &$data, $remote, $show, $source, $default, $type = 1)
  122. {
  123. if ($source) {
  124. foreach ($source as $k => $v) {
  125. $t = array();
  126. foreach ($v as $v1) {
  127. $t[] = $info[$v1] ?? '';
  128. }
  129. $info[$k] = implode(',', $t);
  130. }
  131. }
  132. foreach ($info as $k => $v) {
  133. if ($v == null) {
  134. $v = '';
  135. }
  136. if (isset($data['field'][$k])) {
  137. if (is_array($data['field'][$k])) {
  138. if ($v) {
  139. $v = explode(',', $v);
  140. foreach ($v as $k1 => $v1) {
  141. if (is_numeric($v1)) {
  142. $v[$k1] = (float) $v1;
  143. }
  144. }
  145. } else {
  146. $v = array();
  147. }
  148. $info[$k] = $v;
  149. }
  150. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['update'])) {
  151. $v = $this->config['field'][$k]['update'];
  152. }
  153. if ($k == 'cdate') {
  154. $this->config['field'][$k]['type'] = 'date';
  155. }
  156. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['type']) && $this->config['field'][$k]['type'] == 'date' && $v) {
  157. $v = date('Y-m-d H:i:s', $v);
  158. }
  159. if ($type == 1) {
  160. $data['field'][$k] = $v;
  161. }
  162. if (isset($remote[$k])) {
  163. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]);
  164. $result = Dever::call($remote[$k][1], array($v), 'api');
  165. if ($result) {
  166. $this->setUpdate($info, $data, $result);
  167. }
  168. }
  169. if (isset($show[$k])) {
  170. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
  171. }
  172. }
  173. }
  174. if ($default) {
  175. foreach ($default as $k => $v) {
  176. $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
  177. $data['field'][$k] = $this->getShow($v[1], $info);
  178. }
  179. }
  180. }
  181. private function setUpdate(&$info, &$data, $result, $remote = array())
  182. {
  183. foreach ($data['update'] as $k => $v) {
  184. if (isset($result[$v['key']])) {
  185. # 批量更新时,默认数据需要重置
  186. if (!isset($data['reset'][$v['key']])) {
  187. $data['reset'][$v['key']] = $data['field'][$v['key']];
  188. }
  189. if (isset($result[$v['key']]['option'])) {
  190. $data['option'][$v['key']] = $result[$v['key']]['option'];
  191. unset($result[$v['key']]['option']);
  192. }
  193. if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
  194. if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
  195. $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
  196. } else {
  197. $data['field'][$v['key']] = $result[$v['key']]['value'];
  198. }
  199. }
  200. if (isset($result[$v['key']]['set']) && $info) {
  201. $info = array_merge($info, $result[$v['key']]['set']);
  202. }
  203. $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
  204. }
  205. }
  206. }
  207. private function control(&$data)
  208. {
  209. $data['control'] = array();
  210. if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
  211. foreach ($data['control'] as $k => $v) {
  212. foreach ($data['update'] as $k1 => $v1) {
  213. if ($v1['key'] == $k) {
  214. $show = true;
  215. foreach ($v as $k2 => $v2) {
  216. if (is_array($v2)) {
  217. $temp = false;
  218. foreach ($v2 as $k3 => $v3) {
  219. if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
  220. $temp = true;
  221. } elseif ($data['field'][$k2] == $v3) {
  222. $temp = true;
  223. }
  224. }
  225. $show = $temp;
  226. } else {
  227. if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
  228. $show = false;
  229. } elseif ($data['field'][$k2] != $v2) {
  230. $show = false;
  231. }
  232. }
  233. }
  234. $data['update'][$k1]['show'] = $show;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. private function tab(&$data, $type = 'tab')
  241. {
  242. $field = $this->input('field', '');
  243. $data[$type] = array();
  244. if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  245. foreach ($this->config[$type] as $k => $v) {
  246. if (is_string($v)) {
  247. $field = array();
  248. $data[$type][] = array
  249. (
  250. 'name' => $k,
  251. 'update' => $this->getUpdate($v, $data['update'], $field),
  252. 'field' => $field,
  253. );
  254. } else {
  255. $field = array();
  256. $result = array();
  257. $result['name'] = $k;
  258. foreach ($v as $v1) {
  259. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  260. }
  261. $result['field'] = $field;
  262. $data[$type][] = $result;
  263. }
  264. }
  265. $data['update'] = array();
  266. }
  267. }
  268. private function layout(&$data)
  269. {
  270. $field = $this->input('field', '');
  271. $data['layout'] = array();
  272. if (!$field && isset($this->config['layout'])) {
  273. foreach ($this->config['layout'] as $k => $v) {
  274. $field = array();
  275. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  276. }
  277. $data['update'] = array();
  278. }
  279. }
  280. private function getUpdate($set, $update, &$field)
  281. {
  282. $result = array();
  283. if (is_string($set)) {
  284. $set = explode(',', $set);
  285. foreach ($set as $k => $v) {
  286. foreach ($update as $value) {
  287. if ($value['key'] == $v) {
  288. $result[] = $value;
  289. $field[] = $v;
  290. }
  291. }
  292. }
  293. } else {
  294. foreach ($set as $k => $v) {
  295. foreach ($update as $value) {
  296. if ($value['key'] == $k) {
  297. $result[] = array('span' => $v, 'update' => array($value));
  298. $field[] = $k;
  299. }
  300. }
  301. }
  302. }
  303. return $result;
  304. }
  305. public function do_commit(){}
  306. public function do()
  307. {
  308. $this->checkFunc();
  309. $update = array();
  310. $this->setting('field', $update, true, 'text');
  311. if ($update) {
  312. $data = $other = $sku = array();
  313. $input = Dever::input();
  314. $id = false;
  315. if (isset($input['id']) && $input['id'] > 0) {
  316. $id = $input['id'];
  317. }
  318. foreach ($update as $k => $v) {
  319. if (isset($input[$v['key']])) {
  320. if (isset($v['rules'])) {
  321. $this->checkRules($v, $input[$v['key']]);
  322. }
  323. if ($v['type'] == 'sku') {
  324. $sku[$v['key']] = array($v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]);
  325. } elseif (strpos($v['key'], '/')) {
  326. $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
  327. } else {
  328. $this->doData($data, $v['key'], $input[$v['key']]);
  329. }
  330. } elseif ($id) {
  331. $data[$v['key']] = '';
  332. }
  333. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  334. unset($data[$v['key']]);
  335. }
  336. }
  337. if (!$data && !$other && !$sku) {
  338. Dever::error('无效数据');
  339. }
  340. if ($data) {
  341. $this->exists($id, $data);
  342. $this->start($id, $data);
  343. if ($id) {
  344. $info = $this->db->find($id);
  345. if ($info) {
  346. $state = $this->db->update($info['id'], $data);
  347. if ($state) {
  348. $id = $info['id'];
  349. }
  350. } else {
  351. $data['id'] = $id;
  352. $id = $this->db->insert($data);
  353. }
  354. } else {
  355. $id = $this->db->insert($data);
  356. }
  357. }
  358. if (!$id) {
  359. Dever::error('操作失败');
  360. }
  361. $this->other($id, $data, $other);
  362. $this->sku($id, $data, $sku);
  363. $this->end($id, $data);
  364. return '操作成功';
  365. }
  366. }
  367. private function doData(&$data, $key, $value)
  368. {
  369. if (is_array($value)) {
  370. if (isset($value[0])) {
  371. $value = ltrim(implode(',', $value), ',');
  372. } else {
  373. $value = Dever::json_encode($value);
  374. }
  375. }
  376. if ($value && isset($this->config['field'][$key]) && $handle = Dever::issets($this->config['field'][$key], 'handle')) {
  377. $value = Dever::call($handle, array($value));
  378. if (is_array($value) && isset($value[$key])) {
  379. foreach ($value as $k => $v) {
  380. $data[$k] = trim($v);
  381. }
  382. return;
  383. }
  384. } elseif (isset($this->config['field'][$key]) && isset($this->config['field'][$key]['type']) && $this->config['field'][$key]['type'] == 'date' && $value) {
  385. $value = \Dever\Helper\Date::mktime($value);
  386. }
  387. $data[$key] = trim($value);
  388. }
  389. private function exists($id, $data)
  390. {
  391. if (isset($this->config['check']) && $this->config['check']) {
  392. $check = explode(',', $this->config['check']);
  393. $where = array();
  394. $name = array();
  395. foreach ($check as $k => $v) {
  396. if (isset($data[$v])) {
  397. if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) {
  398. $n = $this->config['field'][$v]['name'];
  399. } elseif (isset($this->db->config['struct'][$v])) {
  400. $n = $this->db->config['struct'][$v]['name'];
  401. } else {
  402. $n = $v;
  403. }
  404. $where[$v] = $data[$v];
  405. $name[] = $n;
  406. }
  407. }
  408. if ($where) {
  409. if ($id) {
  410. $where['id'] = array('!=', $id);
  411. }
  412. $info = $this->db->find($where);
  413. if ($info) {
  414. $name = implode('、', $name);
  415. Dever::error($name . '已存在');
  416. }
  417. }
  418. }
  419. }
  420. private function start($id, &$data)
  421. {
  422. if (isset($this->config['start']) && $this->config['start']) {
  423. $data['id'] = $id;
  424. $data = Dever::call($this->config['start'], array($data));
  425. }
  426. }
  427. private function end($id, $data)
  428. {
  429. if (isset($this->config['end']) && $this->config['end']) {
  430. $data['id'] = $id;
  431. Dever::call($this->config['end'], array($data));
  432. }
  433. }
  434. private function other($rid, $data, $other)
  435. {
  436. if ($other) {
  437. foreach ($other as $k => $v) {
  438. if (strpos($k, '#')) {
  439. $k = str_replace('#', '', $k);
  440. }
  441. $common = $v[0];
  442. $update = $v[1];
  443. $drag = $v[2];
  444. $input = $v[3];
  445. $value = array();
  446. foreach ($input as $k1 => $v1) {
  447. if (isset($v1['id'])) {
  448. $value['id'] = $v1['id'];
  449. }
  450. foreach ($common as $k2 => $v2) {
  451. if (!is_array($v2)) {
  452. if ($v2 == 'id') {
  453. $value[$k2] = $rid;
  454. } elseif (isset($data[$v2])) {
  455. $value[$k2] = $data[$v2];
  456. } else {
  457. $value[$k2] = $v2;
  458. }
  459. }
  460. }
  461. foreach ($update as $k2 => $v2) {
  462. if (isset($v1[$k2])) {
  463. $this->doData($value, $k2, $v1[$k2]);
  464. } else {
  465. $value[$k2] = '';
  466. }
  467. }
  468. if ($drag) {
  469. $value[$drag] = $k1+1;
  470. }
  471. if (isset($value['id']) && $value['id'] > 0) {
  472. $id = $value['id'];
  473. unset($value['id']);
  474. Dever::db($k)->update($id, $value);
  475. } else {
  476. Dever::db($k)->insert($value);
  477. }
  478. }
  479. }
  480. }
  481. }
  482. private function sku($rid, $data, $sku)
  483. {
  484. if ($sku) {
  485. foreach ($sku as $k => $v) {
  486. if (strpos($k, '#')) {
  487. $k = str_replace('#', '', $k);
  488. }
  489. $common = $v[0];
  490. $update = $v[1];
  491. $spec_table = $v[2];
  492. $spec_value_table = $spec_table . '_value';
  493. $spec_field = $v[3];
  494. $spec = $v[4];
  495. $input = $v[5];
  496. $value = $spec_value = array();
  497. Dever::db($spec_table)->update(array($spec_field => $rid), array('state' => 2));
  498. Dever::db($spec_value_table)->update(array($spec_field => $rid), array('state' => 2));
  499. foreach ($spec as $k1 => &$v1) {
  500. $spec_data = array();
  501. $spec_data['state'] = 1;
  502. $spec_data[$spec_field] = $rid;
  503. $spec_data['name'] = $v1['name'];
  504. $spec_data['sort'] = $k1+1;
  505. if (isset($v1['id']) && $v1['id']) {
  506. Dever::db($spec_table)->update($v1['id'], $spec_data);
  507. } else {
  508. $v1['id'] = Dever::db($spec_table)->insert($spec_data);
  509. }
  510. if ($v1['id']) {
  511. foreach ($v1['value'] as $k2 => &$v2) {
  512. $spec_value_data = array();
  513. $spec_value_data['state'] = 1;
  514. $spec_value_data[$spec_field] = $rid;
  515. $spec_value_data['spec_id'] = $v1['id'];
  516. $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
  517. $spec_value_data['sort'] = $k2+1;
  518. $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
  519. if (isset($v2['id']) && $v2['id']) {
  520. Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
  521. } else {
  522. $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
  523. }
  524. $spec_value[$v1['key']][$spec_value_data['value']] = array($v2['id'], $spec_data['sort']);
  525. }
  526. }
  527. }
  528. Dever::db($spec_table)->delete(array($spec_field => $rid, 'state' => 2));
  529. Dever::db($spec_value_table)->delete(array($spec_field => $rid, 'state' => 2));
  530. Dever::db($k)->update(array($spec_field => $rid), array('state' => 2));
  531. foreach ($input as $k1 => $v1) {
  532. if (isset($v1['id'])) {
  533. $value['id'] = $v1['id'];
  534. }
  535. foreach ($common as $k2 => $v2) {
  536. if (!is_array($v2)) {
  537. if ($v2 == 'id') {
  538. $value[$k2] = $rid;
  539. } elseif (isset($data[$v2])) {
  540. $value[$k2] = $data[$v2];
  541. } else {
  542. $value[$k2] = $v2;
  543. }
  544. }
  545. }
  546. foreach ($update as $k2 => $v2) {
  547. if (isset($v1[$k2])) {
  548. $value[$k2] = $v1[$k2];
  549. }
  550. }
  551. $value['key'] = array();
  552. foreach ($v1 as $k2 => $v2) {
  553. if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
  554. $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
  555. }
  556. }
  557. if ($value['key']) {
  558. $value['key'] = implode(',' , $value['key']);
  559. }
  560. $value['state'] = 1;
  561. if (isset($value['id']) && $value['id'] > 0) {
  562. $id = $value['id'];
  563. unset($value['id']);
  564. Dever::db($k)->update($id, $value);
  565. } else {
  566. Dever::db($k)->insert($value);
  567. }
  568. }
  569. Dever::db($k)->delete(array($spec_field => $rid, 'state' => 2));
  570. }
  571. }
  572. }
  573. }