Update.php 24 KB

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