Attr.php 16 KB

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