Attr.php 16 KB

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