Update.php 30 KB

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