Update.php 22 KB

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