Update.php 25 KB

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