Api.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Attr\Src;
  3. use Dever;
  4. class Api
  5. {
  6. /**
  7. * 获取属性配置
  8. *
  9. * @return mixed
  10. */
  11. public function get()
  12. {
  13. $cate = Dever::db('attr/cate')->state();
  14. if ($cate) {
  15. foreach ($cate as $k => $v) {
  16. $cate[$k]['child'] = Dever::db('attr/info')->state(array('cate_id' => $v['id']));
  17. }
  18. }
  19. $cate['state'] = 1;
  20. return $cate;
  21. }
  22. /**
  23. * 获取属性详细信息,有类别
  24. *
  25. * @return mixed
  26. */
  27. public function getInfo($ids, $value)
  28. {
  29. $result = array();
  30. if ($ids) {
  31. $where['ids'] = $ids;
  32. $cate = Dever::db('attr/cate')->state();
  33. $data = Dever::db('attr/info')->getAllByIds($where);
  34. $ids = explode(',', $ids);
  35. $value = explode(',', $value);
  36. if ($data) {
  37. foreach ($ids as $k => $v) {
  38. if (isset($data[$v])) {
  39. $cate_id = $data[$v]['cate_id'];
  40. if (!isset($result[$cate_id])) {
  41. $result[$cate_id] = array
  42. (
  43. 'name' => $cate[$cate_id]['name'],
  44. 'id' => $cate_id
  45. );
  46. }
  47. $result[$cate_id]['attr'][$v] = array
  48. (
  49. 'name' => $data[$v]['name'],
  50. 'value' => $value[$k],
  51. 'id' => $v
  52. );
  53. }
  54. }
  55. }
  56. }
  57. return $result;
  58. }
  59. /**
  60. * 设置数据库的结构信息
  61. *
  62. * @return mixed
  63. */
  64. public function setDatabaseConfig($attr, &$config)
  65. {
  66. if ($attr) {
  67. foreach ($attr as $k => $v) {
  68. $k = 'attr_' . $v['id'];
  69. $config['struct'][$k] = array();
  70. $option = array();
  71. if ($v['type_option']) {
  72. $v['type_option'] = explode("\n", $v['type_option']);
  73. foreach ($v['type_option'] as $k1 => $v1) {
  74. $option[$k1+1] = $v1;
  75. }
  76. }
  77. $config['struct'][$k]['name'] = $v['name'];
  78. $config['struct'][$k]['default'] = '';
  79. $config['struct'][$k]['desc'] = $v['name'];
  80. switch ($v['type']) {
  81. case 3:
  82. $config['struct'][$k]['update'] = 'textarea';
  83. $config['struct'][$k]['search'] = 'fulltext';
  84. $config['struct'][$k]['type'] = 'varchar-800';
  85. $config['struct'][$k]['match'] = 'is_string';
  86. break;
  87. case 4:
  88. $config['struct'][$k]['type'] = 'text-255';
  89. $config['struct'][$k]['update'] = 'editor';
  90. $config['struct'][$k]['match'] = 'is_string';
  91. $config['struct'][$k]['key'] = 1;
  92. case 5:
  93. $config['struct'][$k]['update'] = 'image';
  94. $config['struct'][$k]['type'] = 'varchar-150';
  95. $config['struct'][$k]['match'] = 'is_string';
  96. $config['struct'][$k]['key'] = 1;
  97. break;
  98. case 6:
  99. $config['struct'][$k]['update'] = 'images';
  100. $config['struct'][$k]['type'] = 'text-255';
  101. $config['struct'][$k]['match'] = 'is_string';
  102. $config['struct'][$k]['key'] = 1;
  103. break;
  104. case 7:
  105. $config['struct'][$k]['type'] = 'varchar-800';
  106. $config['struct'][$k]['match'] = 'is_string';
  107. $config['struct'][$k]['update'] = 'linkage';
  108. $config['struct'][$k]['search'] = 'linkage';
  109. $config['struct'][$k]['option'] = Dever::url('area/api.get');
  110. //$config['struct'][$k]['list'] = 'Dever::load("area/api.string", "{area}")';
  111. break;
  112. case 10:
  113. $config['struct'][$k]['update'] = 'radio';
  114. $config['struct'][$k]['type'] = 'varchar-11';
  115. $config['struct'][$k]['match'] = 'is_numeric';
  116. $config['struct'][$k]['option'] = $option;
  117. $config['struct'][$k]['search'] = 'select';
  118. $config['struct'][$k]['default'] = '1';
  119. break;
  120. case 11:
  121. $config['struct'][$k]['update'] = 'checkbox';
  122. $config['struct'][$k]['type'] = 'varchar-150';
  123. $config['struct'][$k]['match'] = 'is_string';
  124. $config['struct'][$k]['option'] = $option;
  125. $config['struct'][$k]['search'] = 'select';
  126. $config['struct'][$k]['default'] = '1';
  127. break;
  128. case 12:
  129. $config['struct'][$k]['update'] = 'select';
  130. $config['struct'][$k]['type'] = 'varchar-11';
  131. $config['struct'][$k]['match'] = 'is_numeric';
  132. $config['struct'][$k]['option'] = $option;
  133. $config['struct'][$k]['search'] = 'select';
  134. $config['struct'][$k]['default'] = '1';
  135. break;
  136. default:
  137. $config['struct'][$k]['update'] = 'text';
  138. $config['struct'][$k]['search'] = 'fulltext';
  139. if ($v['data_type'] == 1 || $v['type'] == 1) {
  140. $config['struct'][$k]['type'] = 'varchar-11';
  141. $config['struct'][$k]['match'] = 'is_numeric';
  142. } else {
  143. $config['struct'][$k]['type'] = 'varchar-800';
  144. $config['struct'][$k]['match'] = 'is_string';
  145. }
  146. }
  147. if ($v['option_type'] && $v['option_type'] == 2) {
  148. $config['struct'][$k]['match'] = 'option';
  149. }
  150. if ($v['value']) {
  151. //$config['struct'][$k]['default'] = $v['value'];
  152. }
  153. if ($v['state'] == 2) {
  154. unset($config['struct'][$k]['update']);
  155. }
  156. # 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
  157. }
  158. }
  159. }
  160. /**
  161. * 根据属性id,获取属性列表
  162. *
  163. * @return mixed
  164. */
  165. public function getList($ids, $value, $order = 'all')
  166. {
  167. $result = array();
  168. if ($ids) {
  169. $where['ids'] = $ids;
  170. $method = 'getAllByIds';
  171. if ($order == 'list') {
  172. $method = 'getListByIds';
  173. } elseif ($order == 'search') {
  174. $method = 'getSearchByIds';
  175. } elseif ($order == 'view') {
  176. $method = 'getViewByIds';
  177. }
  178. $data = Dever::db('attr/info')->$method($where);
  179. $ids = explode(',', $ids);
  180. $value = explode(',', $value);
  181. if ($data) {
  182. foreach ($ids as $k => $v) {
  183. if (isset($data[$v])) {
  184. if (isset($value) && $value[$k]) {
  185. $data[$v]['value'] = $value[$k];
  186. } else {
  187. $data[$v]['value'] = '';
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return $data;
  194. }
  195. /**
  196. * 获取属性的值
  197. *
  198. * @return mixed
  199. */
  200. public function getValue($info)
  201. {
  202. # 地区
  203. if ($info['type'] == 7) {
  204. $info['value'] = Dever::load("area/api.string", $info['value']);
  205. } elseif ($info['type'] >= 10 && $info['value']) {
  206. $temp = explode("\n", $info['type_option']);
  207. if ($temp) {
  208. $info['value'] = $info['value'] - 1;
  209. if (isset($temp[$info['value']])) {
  210. $info['value'] = $temp[$info['value']];
  211. if (strstr($info['value'], ',')) {
  212. $temp = explode(',', $info['value']);
  213. $info['value'] = $temp[0];
  214. }
  215. }
  216. }
  217. }
  218. if ($info['unit']) {
  219. $info['value'] = $info['value'] . $info['unit'];
  220. }
  221. return $info['value'];
  222. }
  223. /**
  224. * 获取属性的名称,字符类型
  225. *
  226. * @return mixed
  227. */
  228. public function string($ids, $value = '')
  229. {
  230. $result = $this->getInfo($ids, $value);
  231. $table = array();
  232. foreach ($result as $k => $v) {
  233. $table[$v['name']] = '';
  234. foreach ($v['attr'] as $k1 => $v1) {
  235. $table[$v['name']] .= $v1['name'] . "&nbsp;&nbsp;";
  236. }
  237. }
  238. return Dever::table($table);
  239. }
  240. private function attrSort(&$attr, $reorder = 'list_reorder')
  241. {
  242. $order = Dever::config('base')->attrOrder;
  243. if ($order && $order != $reorder) {
  244. $reorder = $order;
  245. }
  246. if ($attr) {
  247. foreach ($attr as $k => $v) {
  248. $sort[$k] = $v[$reorder];
  249. }
  250. array_multisort($sort, SORT_DESC, $attr);
  251. }
  252. }
  253. public function getAttrByCate($category, $order = 'list_reorder')
  254. {
  255. $data = array();
  256. if ($category && Dever::project('category')) {
  257. $array = explode(',', $category);
  258. $cate = array();
  259. $total = count($array)-1;
  260. foreach ($array as $k => $v) {
  261. $cate[$k] = $v;
  262. if ($k < $total) {
  263. $cate[] = '-1';
  264. }
  265. $where['category'] = $cate;
  266. $info = Dever::db('category/attr')->one($where);
  267. if ($info) {
  268. $data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input']);
  269. }
  270. }
  271. $this->attrSort($data, $order);
  272. }
  273. return $data;
  274. }
  275. /**
  276. * 获取某个分类下的搜索列表
  277. *
  278. * @return mixed
  279. */
  280. public function getSearch($ids, $cate = true, $city = false, $search_value = false)
  281. {
  282. $value = array();
  283. $search_value = Dever::preInput('attr_', $search_value);
  284. if ($cate && $ids && Dever::project('category')) {
  285. $data = $this->getAttrByCate($ids, 'search_reorder');
  286. if (!$data) {
  287. return array('search' => array(), 'value' => $value);
  288. }
  289. } else {
  290. $where['ids'] = $ids;
  291. $where['search_reorder'] = 1;
  292. $data = Dever::db('attr/info')->getSearch($where);
  293. }
  294. if ($data) {
  295. foreach ($data as $k => $v) {
  296. if (isset($ids_input[$v['id']]) && $ids_input[$v['id']]) {
  297. $data[$k]['name'] = $ids_input[$v['id']];
  298. }
  299. $data[$k]['key'] = 'attr_' . $v['id'];
  300. if ($v['type'] == 7 && $city > 0) {
  301. # 获取地区,获取最后一个级别的地区
  302. $data[$k]['option'] = Dever::db('area/county')->state(array('city_id' => $city));
  303. } elseif ($v['type_option']) {
  304. $temp = explode("\n", $v['type_option']);
  305. foreach ($temp as $k1 => $v1) {
  306. $v1 = explode(',', $v1);
  307. $k1 = $k1+1;
  308. $data[$k]['option'][$k1] = array
  309. (
  310. 'id' => $k1,
  311. 'name' => $v1[0],
  312. );
  313. if (isset($v1[1])) {
  314. $data[$k]['option_match'][$k1] = $v1[1];
  315. }
  316. }
  317. }
  318. if (isset($search_value[$data[$k]['key']])) {
  319. $data[$k]['option_value'] = $search_value[$data[$k]['key']];
  320. $value[$data[$k]['key']] = $data[$k]['option_value'];
  321. }
  322. }
  323. }
  324. return array('search' => $data, 'value' => $value);
  325. }
  326. }