Update.php 22 KB

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