123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- namespace Attr\Src;
- use Dever;
- class Api
- {
- /**
- * 获取属性配置
- *
- * @return mixed
- */
- public function get()
- {
- $cate = Dever::db('attr/cate')->state();
- if ($cate) {
- foreach ($cate as $k => $v) {
- $cate[$k]['child'] = Dever::db('attr/info')->state(array('cate_id' => $v['id']));
- }
- }
- $cate['state'] = 1;
- return $cate;
- }
- /**
- * 获取属性详细信息,有类别
- *
- * @return mixed
- */
- public function getInfo($ids, $value)
- {
- $result = array();
- if ($ids) {
- $where['ids'] = $ids;
- $cate = Dever::db('attr/cate')->state();
- $data = Dever::db('attr/info')->getAllByIds($where);
- $ids = explode(',', $ids);
- $value = explode(',', $value);
- if ($data) {
- foreach ($ids as $k => $v) {
- if (isset($data[$v])) {
- $cate_id = $data[$v]['cate_id'];
- if (!isset($result[$cate_id])) {
- $result[$cate_id] = array
- (
- 'name' => $cate[$cate_id]['name'],
- 'id' => $cate_id
- );
- }
- $result[$cate_id]['attr'][$v] = array
- (
- 'name' => $data[$v]['name'],
- 'value' => $value[$k],
- 'id' => $v
- );
- }
- }
- }
- }
- return $result;
- }
- /**
- * 设置数据库的结构信息
- *
- * @return mixed
- */
- public function setDatabaseConfig($attr, &$config)
- {
- if ($attr) {
- foreach ($attr as $k => $v) {
- $k = 'attr_' . $v['id'];
- $config['struct'][$k] = array();
- $option = array();
- if ($v['type_option']) {
- $v['type_option'] = explode("\n", $v['type_option']);
- foreach ($v['type_option'] as $k1 => $v1) {
- $option[$k1+1] = $v1;
- }
- }
- $config['struct'][$k]['name'] = $v['name'];
- $config['struct'][$k]['default'] = '';
- $config['struct'][$k]['desc'] = $v['name'];
- switch ($v['type']) {
- case 3:
- $config['struct'][$k]['update'] = 'textarea';
- $config['struct'][$k]['search'] = 'fulltext';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- break;
- case 4:
- $config['struct'][$k]['type'] = 'text-255';
- $config['struct'][$k]['update'] = 'editor';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['key'] = 1;
- case 5:
- $config['struct'][$k]['update'] = 'image';
- $config['struct'][$k]['type'] = 'varchar-150';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['key'] = 1;
- break;
- case 6:
- $config['struct'][$k]['update'] = 'images';
- $config['struct'][$k]['type'] = 'text-255';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['key'] = 1;
- break;
- case 7:
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['update'] = 'linkage';
- $config['struct'][$k]['search'] = 'linkage';
- $config['struct'][$k]['option'] = Dever::url('area/api.get');
- //$config['struct'][$k]['list'] = 'Dever::load("area/api.string", "{area}")';
- break;
- case 10:
- $config['struct'][$k]['update'] = 'radio';
- $config['struct'][$k]['type'] = 'varchar-11';
- $config['struct'][$k]['match'] = 'is_numeric';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '1';
- break;
- case 11:
- $config['struct'][$k]['update'] = 'checkbox';
- $config['struct'][$k]['type'] = 'varchar-150';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '1';
- break;
- case 12:
- $config['struct'][$k]['update'] = 'select';
- $config['struct'][$k]['type'] = 'varchar-11';
- $config['struct'][$k]['match'] = 'is_numeric';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '1';
- break;
-
- default:
- $config['struct'][$k]['update'] = 'text';
- $config['struct'][$k]['search'] = 'fulltext';
- if ($v['data_type'] == 1 || $v['type'] == 1) {
- $config['struct'][$k]['type'] = 'varchar-11';
- $config['struct'][$k]['match'] = 'is_numeric';
- } else {
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- }
- }
- if ($v['option_type'] && $v['option_type'] == 2) {
- $config['struct'][$k]['match'] = 'option';
- }
- if ($v['value']) {
- //$config['struct'][$k]['default'] = $v['value'];
- }
- if ($v['state'] == 2) {
- unset($config['struct'][$k]['update']);
- }
- # 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
- }
- }
- }
- /**
- * 根据属性id,获取属性列表
- *
- * @return mixed
- */
- public function getList($ids, $value, $order = 'all')
- {
- $result = array();
- if ($ids) {
- $where['ids'] = $ids;
- $method = 'getAllByIds';
- if ($order == 'list') {
- $method = 'getListByIds';
- } elseif ($order == 'search') {
- $method = 'getSearchByIds';
- } elseif ($order == 'view') {
- $method = 'getViewByIds';
- }
- $data = Dever::db('attr/info')->$method($where);
- $ids = explode(',', $ids);
- $value = explode(',', $value);
- if ($data) {
- foreach ($ids as $k => $v) {
- if (isset($data[$v])) {
- if (isset($value) && $value[$k]) {
- $data[$v]['value'] = $value[$k];
- } else {
- $data[$v]['value'] = '';
- }
- }
- }
- }
- }
- return $data;
- }
- /**
- * 获取属性的值
- *
- * @return mixed
- */
- public function getValue($info)
- {
- # 地区
- if ($info['type'] == 7) {
- $info['value'] = Dever::load("area/api.string", $info['value']);
- } elseif ($info['type'] >= 10 && $info['value']) {
- $temp = explode("\n", $info['type_option']);
- if ($temp) {
- $info['value'] = $info['value'] - 1;
- if (isset($temp[$info['value']])) {
- $info['value'] = $temp[$info['value']];
- if (strstr($info['value'], ',')) {
- $temp = explode(',', $info['value']);
- $info['value'] = $temp[0];
- }
- }
- }
-
- }
- if ($info['unit']) {
- $info['value'] = $info['value'] . $info['unit'];
- }
- return $info['value'];
- }
- /**
- * 获取属性的名称,字符类型
- *
- * @return mixed
- */
- public function string($ids, $value = '')
- {
- $result = $this->getInfo($ids, $value);
- $table = array();
- foreach ($result as $k => $v) {
- $table[$v['name']] = '';
- foreach ($v['attr'] as $k1 => $v1) {
- $table[$v['name']] .= $v1['name'] . " ";
- }
- }
- return Dever::table($table);
- }
- private function attrSort(&$attr, $reorder = 'list_reorder')
- {
- $order = Dever::config('base')->attrOrder;
- if ($order && $order != $reorder) {
- $reorder = $order;
- }
- if ($attr) {
- foreach ($attr as $k => $v) {
- $sort[$k] = $v[$reorder];
- }
- array_multisort($sort, SORT_DESC, $attr);
- }
- }
- public function getAttrByCate($category, $order = 'list_reorder')
- {
- $data = array();
- if ($category && Dever::project('category')) {
- $array = explode(',', $category);
- $cate = array();
- $total = count($array)-1;
- foreach ($array as $k => $v) {
- $cate[$k] = $v;
- if ($k < $total) {
- $cate[] = '-1';
- }
- $where['category'] = $cate;
- $info = Dever::db('category/attr')->one($where);
- if ($info) {
- $data += Dever::load('attr/api')->getList($info['attr'], $info['attr_input']);
- }
- }
- $this->attrSort($data, $order);
- }
- return $data;
- }
- /**
- * 获取某个分类下的搜索列表
- *
- * @return mixed
- */
- public function getSearch($ids, $cate = true, $city = false, $search_value = false)
- {
- $value = array();
- $search_value = Dever::preInput('attr_', $search_value);
- if ($cate && $ids && Dever::project('category')) {
- $data = $this->getAttrByCate($ids, 'search_reorder');
- if (!$data) {
- return array('search' => array(), 'value' => $value);
- }
- } else {
- $where['ids'] = $ids;
- $where['search_reorder'] = 1;
- $data = Dever::db('attr/info')->getSearch($where);
- }
- if ($data) {
- foreach ($data as $k => $v) {
- if (isset($ids_input[$v['id']]) && $ids_input[$v['id']]) {
- $data[$k]['name'] = $ids_input[$v['id']];
- }
- $data[$k]['key'] = 'attr_' . $v['id'];
- if ($v['type'] == 7 && $city > 0) {
- # 获取地区,获取最后一个级别的地区
- $data[$k]['option'] = Dever::db('area/county')->state(array('city_id' => $city));
- } elseif ($v['type_option']) {
- $temp = explode("\n", $v['type_option']);
- foreach ($temp as $k1 => $v1) {
- $v1 = explode(',', $v1);
- $k1 = $k1+1;
- $data[$k]['option'][$k1] = array
- (
- 'id' => $k1,
- 'name' => $v1[0],
- );
- if (isset($v1[1])) {
- $data[$k]['option_match'][$k1] = $v1[1];
- }
- }
- }
- if (isset($search_value[$data[$k]['key']])) {
- $data[$k]['option_value'] = $search_value[$data[$k]['key']];
- $value[$data[$k]['key']] = $data[$k]['option_value'];
- }
- }
- }
-
- return array('search' => $data, 'value' => $value);
- }
- }
|