Attr.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace Scm_product\Lib;
  3. use Dever;
  4. class Attr
  5. {
  6. public function test_api()
  7. {
  8. $url = 'http://192.168.33.10/sellking/package/scm/module/scm_product/?l=lib/attr.getManageByCate&json=1';
  9. $data = Dever::curl($url, array('id' => 10000), 'post');
  10. print_r($data);die;
  11. }
  12. /**
  13. * 根据分类id,获取属性更新信息,用于后台
  14. *
  15. * @return mixed
  16. */
  17. public function getManageByCate_api()
  18. {
  19. $id = Dever::input('id');
  20. $data = array();
  21. if ($id) {
  22. $data = Dever::db('scm_product/info_attr')->getDataByInfoId(array('info_id' => $id));
  23. }
  24. $where['category_id'] = Dever::input('category', 2);
  25. $attr = Dever::db('scm_product/category_attr')->getCateAll($where);
  26. if (!$attr) {
  27. return '';
  28. }
  29. $config = array();
  30. $this->setDatabaseConfig($attr, $config, $data);
  31. $data = array();
  32. $result = Dever::load('manage/database')->update_struct($config, false, $data, -1, 'scm_product-info_attr-', true);
  33. return implode('', $result);
  34. }
  35. /**
  36. * 根据分类id,获取所有属性
  37. *
  38. * @return mixed
  39. */
  40. public function getInfoByCate($cate_id)
  41. {
  42. $where['category_id'] = $cate_id;
  43. $data = Dever::db('scm_product/category_attr')->select($where);
  44. if ($data) {
  45. $name = array();
  46. foreach ($data as $k => $v) {
  47. $name[] = $v['name'];
  48. }
  49. return implode('、', $name);
  50. }
  51. return '';
  52. }
  53. /**
  54. * 根据json格式获取信息
  55. *
  56. * @return mixed
  57. */
  58. public function getInfoByJson($data)
  59. {
  60. $result = array();
  61. $string = array();
  62. foreach ($data as $k => $v) {
  63. $info = Dever::db('scm_product/category_attr')->find($v['attr_id']);
  64. $result[$k]['id'] = $info['id'];
  65. $result[$k]['name'] = $info['name'];
  66. if ($v['id']) {
  67. $attr = Dever::db('scm_product/category_attr_value')->find($v['id']);
  68. if ($attr) {
  69. $result[$k]['value_id'] = $attr['id'];
  70. $result[$k]['value_name'] = $attr['name'];
  71. $string[] = $result[$k]['value_name'];
  72. }
  73. }
  74. }
  75. return array('data' => $result, 'string' => implode(',', $string));
  76. }
  77. /**
  78. * 获取属性详细信息,有类别
  79. *
  80. * @return mixed
  81. */
  82. public function getInfo($id, $value)
  83. {
  84. $info = Dever::db('scm_product/category_attr')->getOne($id);
  85. if ($info) {
  86. $info['value'] = $value;
  87. $info = $this->getValue($info);
  88. }
  89. return $info;
  90. }
  91. /**
  92. * 获取属性的值
  93. *
  94. * @return mixed
  95. */
  96. public function getValue($info)
  97. {
  98. # 地区
  99. if ($info['type'] == 7) {
  100. $info['value'] = Dever::load("area/api.string", $info['value']);
  101. /*
  102. $info['value'] = explode(',', $info['value']);
  103. $temp = end($info['value']);
  104. $info['value'] = $temp;
  105. */
  106. } elseif ($info['type'] == 9 && $info['value']) {
  107. $info['value'] = explode(',', $info['value']);
  108. $info['value'] = $info['value'][0] . $info['unit'];
  109. } elseif ($info['type'] > 9 && $info['value']) {
  110. $value = Dever::db('scm_product/category_attr_value')->getData(array('ids' => $info['value']));
  111. if ($value) {
  112. $name = array();
  113. foreach ($value as $k => $v) {
  114. $name[] = $v['name'] . $info['unit'];
  115. }
  116. $info['value'] = implode(',', $name);
  117. }
  118. } elseif ($info['unit']) {
  119. $info['value'] = $info['value'] . $info['unit'];
  120. }
  121. unset($info['unit']);
  122. return $info;
  123. }
  124. /**
  125. * 设置数据库的结构信息
  126. *
  127. * @return mixed
  128. */
  129. public function setDatabaseConfig($attr, &$config, $data = array())
  130. {
  131. if ($attr) {
  132. foreach ($attr as $k => $v) {
  133. if ($v['type'] == 9) {
  134. $k = 'attr_' . $v['id'] . '_s';
  135. $config['struct'][$k]['name'] = $v['name'];
  136. $config['struct'][$k]['default'] = '';
  137. $config['struct'][$k]['desc'] = $v['name'];
  138. $config['struct'][$k]['update'] = 'hidden';
  139. $config['struct'][$k]['type'] = 'varchar-800';
  140. $config['struct'][$k]['match'] = 'is_numeric';
  141. $k = 'attr_' . $v['id'] . '_e';
  142. $config['struct'][$k]['name'] = $v['name'];
  143. $config['struct'][$k]['default'] = '';
  144. $config['struct'][$k]['desc'] = $v['name'];
  145. $config['struct'][$k]['update'] = 'hidden';
  146. $config['struct'][$k]['type'] = 'varchar-800';
  147. $config['struct'][$k]['match'] = 'is_numeric';
  148. }
  149. $k = 'attr_' . $v['id'];
  150. $config['struct'][$k] = array();
  151. /*
  152. $option = array();
  153. if ($v['type_option']) {
  154. $v['type_option'] = explode("\n", $v['type_option']);
  155. foreach ($v['type_option'] as $k1 => $v1) {
  156. $option[$k1+1] = $v1;
  157. }
  158. }*/
  159. $option = Dever::db('scm_product/category_attr_value')->getData(array('attr_id' => $v['id']));
  160. $config['struct'][$k]['name'] = $v['name'];
  161. $config['struct'][$k]['default'] = '';
  162. $config['struct'][$k]['desc'] = $v['name'];
  163. switch ($v['type']) {
  164. case 3:
  165. $config['struct'][$k]['update'] = 'textarea';
  166. $config['struct'][$k]['search'] = 'fulltext';
  167. $config['struct'][$k]['type'] = 'varchar-800';
  168. $config['struct'][$k]['match'] = 'is_string';
  169. break;
  170. case 4:
  171. $config['struct'][$k]['type'] = 'text-255';
  172. $config['struct'][$k]['update'] = 'editor';
  173. $config['struct'][$k]['match'] = 'is_string';
  174. $config['struct'][$k]['key'] = 1;
  175. case 5:
  176. $config['struct'][$k]['update'] = 'image';
  177. $config['struct'][$k]['type'] = 'varchar-150';
  178. $config['struct'][$k]['match'] = 'is_string';
  179. $config['struct'][$k]['key'] = 1;
  180. break;
  181. case 6:
  182. $config['struct'][$k]['update'] = 'images';
  183. $config['struct'][$k]['type'] = 'text-255';
  184. $config['struct'][$k]['match'] = 'is_string';
  185. $config['struct'][$k]['key'] = 1;
  186. break;
  187. case 7:
  188. $config['struct'][$k]['type'] = 'varchar-800';
  189. $config['struct'][$k]['match'] = 'is_string';
  190. $config['struct'][$k]['update'] = 'linkage';
  191. $config['struct'][$k]['search'] = 'linkage';
  192. $config['struct'][$k]['option'] = Dever::url('area/api.get');
  193. //$config['struct'][$k]['list'] = 'Dever::load("area/api.string", "{area}")';
  194. break;
  195. case 10:
  196. $config['struct'][$k]['update'] = 'radio';
  197. $config['struct'][$k]['type'] = 'varchar-800';
  198. $config['struct'][$k]['match'] = 'is_numeric';
  199. $config['struct'][$k]['option'] = $option;
  200. $config['struct'][$k]['search'] = 'select';
  201. $config['struct'][$k]['default'] = '';
  202. break;
  203. case 11:
  204. $config['struct'][$k]['update'] = 'checkbox';
  205. $config['struct'][$k]['type'] = 'varchar-800';
  206. $config['struct'][$k]['match'] = 'is_string';
  207. $config['struct'][$k]['option'] = $option;
  208. $config['struct'][$k]['search'] = 'select';
  209. $config['struct'][$k]['default'] = '';
  210. break;
  211. case 12:
  212. $config['struct'][$k]['update'] = 'select';
  213. $config['struct'][$k]['type'] = 'varchar-800';
  214. $config['struct'][$k]['match'] = 'is_numeric';
  215. $config['struct'][$k]['option'] = $option;
  216. $config['struct'][$k]['search'] = 'select';
  217. $config['struct'][$k]['default'] = '';
  218. break;
  219. default:
  220. $config['struct'][$k]['update'] = 'text';
  221. $config['struct'][$k]['search'] = 'fulltext';
  222. if ($v['type'] == 9) {
  223. $config['struct'][$k]['name'] .= '-这是一个区间数值,后台填写请用半角逗号,隔开';
  224. $config['struct'][$k]['type'] = 'varchar-800';
  225. $config['struct'][$k]['match'] = 'is_string';
  226. } elseif ($v['data_type'] == 1 || $v['type'] == 1) {
  227. $config['struct'][$k]['type'] = 'varchar-800';
  228. $config['struct'][$k]['match'] = 'is_numeric';
  229. } else {
  230. $config['struct'][$k]['type'] = 'varchar-800';
  231. $config['struct'][$k]['match'] = 'is_string';
  232. }
  233. }
  234. if ($v['is_must'] && $v['is_must'] == 2) {
  235. $config['struct'][$k]['match'] = 'option';
  236. }
  237. /*
  238. if ($v['value']) {
  239. $config['struct'][$k]['default'] = $v['value'];
  240. }*/
  241. if ($data && isset($data[$v['id']])) {
  242. $config['struct'][$k]['default'] = $data[$v['id']]['attr_value'];
  243. }
  244. if ($v['state'] == 2) {
  245. unset($config['struct'][$k]['update']);
  246. }
  247. # 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
  248. }
  249. }
  250. }
  251. /**
  252. * 根据属性id,获取属性列表
  253. *
  254. * @return mixed
  255. */
  256. public function getList($ids, $value, $order = 'list')
  257. {
  258. $data = array();
  259. if ($ids) {
  260. $where['ids'] = $ids;
  261. $method = 'getAllByIds';
  262. if ($order == 'list') {
  263. $method = 'getListByIds';
  264. } elseif ($order == 'search') {
  265. $method = 'getSearchByIds';
  266. } elseif ($order == 'search_reorder') {
  267. $method = 'getSearchByIds';
  268. $where['search_reorder'] = 1;
  269. } elseif ($order == 'view') {
  270. $method = 'getViewByIds';
  271. }
  272. $data = Dever::db('scm_product/category_attr')->$method($where);
  273. $ids = explode(',', $ids);
  274. $value = explode(',', $value);
  275. if ($data) {
  276. foreach ($ids as $k => $v) {
  277. if (isset($data[$v])) {
  278. if (isset($value) && isset($value[$k]) && $value[$k]) {
  279. $data[$v]['value'] = $value[$k];
  280. } else {
  281. $data[$v]['value'] = '';
  282. }
  283. }
  284. }
  285. }
  286. }
  287. return $data;
  288. }
  289. /**
  290. * 获取属性的名称,字符类型
  291. *
  292. * @return mixed
  293. */
  294. public function string($ids, $value = '')
  295. {
  296. $result = $this->getInfo($ids, $value);
  297. $table = array();
  298. foreach ($result as $k => $v) {
  299. $table[$v['name']] = '';
  300. foreach ($v['attr'] as $k1 => $v1) {
  301. $table[$v['name']] .= $v1['name'] . "&nbsp;&nbsp;";
  302. }
  303. }
  304. return Dever::table($table);
  305. }
  306. private function attrSort(&$attr, $reorder = 'list_reorder')
  307. {
  308. if ($reorder == 'list') {
  309. $reorder = 'list_reorder';
  310. }
  311. $order = Dever::config('base')->attrOrder;
  312. if ($order && $order != $reorder) {
  313. $reorder = $order;
  314. }
  315. if ($attr) {
  316. foreach ($attr as $k => $v) {
  317. $sort[$k] = $v[$reorder];
  318. $attr[$k]['option'] = Dever::db('scm_product/category_attr_value')->getData(array('attr_id' => $v['id']));
  319. /*
  320. if ($v['type_option']) {
  321. $attr[$k]['option'] = explode("\n", $v['type_option']);
  322. }
  323. */
  324. }
  325. array_multisort($sort, SORT_DESC, $attr);
  326. }
  327. }
  328. public function getAttrByCate($category, $order = 'list_reorder', $total = false)
  329. {
  330. $data = array();
  331. //print_r(Dever::db('category/attr')->all());
  332. if ($category && Dever::project('category')) {
  333. $array = explode(',', $category);
  334. $cate = array();
  335. $total = $total ? $total : count($array);
  336. $total -= 1;
  337. foreach ($array as $k => $v) {
  338. $cate[$k] = $v;
  339. if ($k < $total) {
  340. $cate[] = '-1';
  341. }
  342. $where['category'] = $cate;
  343. $info = Dever::db('category/attr')->one($where);
  344. if ($info) {
  345. $data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input'], $order);
  346. }
  347. }
  348. $this->attrSort($data, $order);
  349. }
  350. return $data;
  351. }
  352. /**
  353. * 获取某个分类下的搜索列表
  354. *
  355. * @return mixed
  356. */
  357. public function getSearch($ids, $cate = true, $city = false, $search_value = false, $total = 3)
  358. {
  359. $value = array();
  360. $search_value = Dever::preInput('attr_', array(), $search_value);
  361. if ($cate && $ids && Dever::project('category')) {
  362. $data = $this->getAttrByCate($ids, 'search_reorder', $total);
  363. if (!$data) {
  364. return array('search' => array(), 'value' => $value);
  365. }
  366. } else {
  367. $where['ids'] = $ids;
  368. $where['search_reorder'] = 1;
  369. $data = Dever::db('scm_product/category_attr')->getSearchByIds($where);
  370. }
  371. if ($data) {
  372. foreach ($data as $k => $v) {
  373. if (isset($ids_input[$v['id']]) && $ids_input[$v['id']]) {
  374. $data[$k]['name'] = $ids_input[$v['id']];
  375. }
  376. $data[$k]['key'] = 'attr_' . $v['id'];
  377. if ($v['type'] == 7 && $city > 0) {
  378. # 获取地区,获取最后一个级别的地区
  379. $data[$k]['option'] = Dever::db('area/county')->state(array('city_id' => $city));
  380. } elseif ($v['type'] == 1 || $v['type'] == 9) {
  381. $data[$k]['option'] = Dever::db('scm_product/category_attr_search')->getData(array('attr_id' => $v['id']));
  382. } else {
  383. $data[$k]['option'] = Dever::db('scm_product/category_attr_value')->getData(array('attr_id' => $v['id']));
  384. }
  385. /*
  386. elseif ($v['type_option']) {
  387. $temp = explode("\n", $v['type_option']);
  388. foreach ($temp as $k1 => $v1) {
  389. $v1 = explode(',', $v1);
  390. $k1 = $k1+1;
  391. $data[$k]['option'][$k1] = array
  392. (
  393. 'id' => $k1,
  394. 'name' => $v1[0],
  395. );
  396. if (isset($v1[1])) {
  397. $data[$k]['option_match'][$k1] = $v1[1];
  398. }
  399. }
  400. }
  401. */
  402. if (isset($search_value[$data[$k]['key']])) {
  403. $data[$k]['option_value'] = $search_value[$data[$k]['key']];
  404. $value[$data[$k]['key']] = $data[$k]['option_value'];
  405. }
  406. }
  407. }
  408. return array('search' => $data, 'value' => $value);
  409. }
  410. /**
  411. * 获取搜索sql
  412. *
  413. * @return mixed
  414. */
  415. public function getSql($attr, $where, $sql = array(), $tname = 't_1')
  416. {
  417. if (!$attr) {
  418. return $sql;
  419. }
  420. foreach ($attr as $k => $v) {
  421. $key = 'attr_' . $v['id'];
  422. if (isset($where[$key]) && $where[$key]) {
  423. //$where['option'][$key] = array('yes', '=');
  424. if ($v['type'] == 7) {
  425. # 地区有点复杂,暂时先这样,后续优化一下
  426. $county = Dever::db('area/county')->one($where[$key]);
  427. if ($county) {
  428. $city = Dever::db('area/city')->one($county['city_id']);
  429. if ($city) {
  430. $province = Dever::db('area/province')->one($city['province_id']);
  431. if ($province) {
  432. $value = $province['id'] . ',' . $city['id'] . ',' . $county['id'];
  433. //$where[$key] = $value;
  434. $sql[] = ' '.$tname.'.'.$key.' = "'.$value.'"';
  435. }
  436. }
  437. }
  438. //} elseif ($v['type'] == 1 && $v['type_option']) {
  439. } elseif ($v['type'] == 1) {
  440. $search = Dever::db('scm_product/category_attr_search')->getData(array('attr_id' => $v['id']));
  441. //print_r($where[$key]);die;
  442. /*
  443. $temp = explode("\n", $v['type_option']);
  444. if (isset($temp[$where[$key]-1])) {
  445. $temp = explode(',', $temp[$where[$key]-1]);
  446. $match = $temp[1];
  447. //$where['option'][$key] = array('yes', '<=');
  448. //$where[$key] = 100;
  449. $match = str_replace('{v}', $tname . '.' . $key, $match);
  450. $sql[] = $match;
  451. }
  452. */
  453. //} elseif ($v['type'] == 9 && $v['type_option']) {
  454. } elseif ($v['type'] == 9 && $v['type_option']) {
  455. $search = Dever::db('scm_product/category_attr_search')->getData(array('attr_id' => $v['id']));
  456. //print_r($where[$key]);die;
  457. /*
  458. $temp = explode("\n", $v['type_option']);
  459. if (isset($temp[$where[$key]-1])) {
  460. $temp = explode(',', $temp[$where[$key]-1]);
  461. $match = $temp[1];
  462. //$where['option'][$key] = array('yes', '<=');
  463. //$where[$key] = 100;
  464. $match = str_replace('{s}', $tname . '.' . $key . '_s', $match);
  465. $match = str_replace('{e}', $tname . '.' . $key . '_e', $match);
  466. $match = str_replace('{v}', $tname . '.' . $key . '_e', $match);
  467. $sql[] = $match;
  468. }
  469. */
  470. } else {
  471. $sql[] = ' '.$tname.'.'.$key.' = "'.$where[$key].'"';
  472. }
  473. }
  474. }
  475. return $sql;
  476. }
  477. }