123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- <?php
- namespace Product\Lib;
- use Dever;
- class Attr
- {
- /**
- * 根据分类id,获取属性更新信息,用于后台
- *
- * @return mixed
- */
- public function getManageByCate_api()
- {
- $id = Dever::input('id');
- $data = array();
- if ($id) {
- $data = Dever::db('product/info_attr')->getDataByInfoId(array('info_id' => $id));
- }
- $where['category_id'] = Dever::input('category', 2);
- $attr = Dever::db('product/category_attr')->getCateAll($where);
- if (!$attr) {
- return '';
- }
- $config = array();
- $this->setDatabaseConfig($attr, $config, $data);
- $data = array();
- $result = Dever::load('manage/database')->update_struct($config, false, $data, -1, 'product-info_attr-', true);
- return implode('', $result);
- }
- /**
- * 根据分类id,获取所有属性
- *
- * @return mixed
- */
- public function getInfoByCate($cate_id)
- {
- $where['category_id'] = $cate_id;
- $data = Dever::db('product/category_attr')->select($where);
- if ($data) {
- $name = array();
- foreach ($data as $k => $v) {
- $name[] = $v['name'];
- }
- return implode('、', $name);
- }
- return '';
- }
- /**
- * 根据json格式获取信息
- *
- * @return mixed
- */
- public function getInfoByJson($data)
- {
- $result = array();
- $string = array();
- foreach ($data as $k => $v) {
- $info = Dever::db('product/category_attr')->find($v['attr_id']);
- $result[$k]['id'] = $info['id'];
- $result[$k]['name'] = $info['name'];
- if ($v['id']) {
- $attr = Dever::db('product/category_attr_value')->find($v['id']);
- if ($attr) {
- $result[$k]['value_id'] = $attr['id'];
- $result[$k]['value_name'] = $attr['name'];
- $string[] = $result[$k]['value_name'];
- }
- }
- }
- return array('data' => $result, 'string' => implode(',', $string));
- }
- /**
- * 获取属性详细信息,有类别
- *
- * @return mixed
- */
- public function getInfo($id, $value)
- {
- $info = Dever::db('product/category_attr')->getOne($id);
- if ($info) {
- $info['value'] = $value;
- $info = $this->getValue($info);
- }
- return $info;
- }
- /**
- * 获取属性的值
- *
- * @return mixed
- */
- public function getValue($info)
- {
- # 地区
- if ($info['type'] == 7) {
- $info['value'] = Dever::load("area/api.string", $info['value']);
- /*
- $info['value'] = explode(',', $info['value']);
- $temp = end($info['value']);
- $info['value'] = $temp;
- */
- } elseif ($info['type'] == 9 && $info['value']) {
- $info['value'] = explode(',', $info['value']);
- $info['value'] = $info['value'][0] . $info['unit'];
- } elseif ($info['type'] > 9 && $info['value']) {
- $value = Dever::db('product/category_attr_value')->getData(array('ids' => $info['value']));
- if ($value) {
- $name = array();
- foreach ($value as $k => $v) {
- $name[] = $v['name'] . $info['unit'];
- }
- $info['value'] = implode(',', $name);
- }
- } elseif ($info['unit']) {
- $info['value'] = $info['value'] . $info['unit'];
- }
- unset($info['unit']);
- return $info;
- }
- /**
- * 设置数据库的结构信息
- *
- * @return mixed
- */
- public function setDatabaseConfig($attr, &$config, $data = array())
- {
- if ($attr) {
- foreach ($attr as $k => $v) {
- if ($v['type'] == 9) {
- $k = 'attr_' . $v['id'] . '_s';
- $config['struct'][$k]['name'] = $v['name'];
- $config['struct'][$k]['default'] = '';
- $config['struct'][$k]['desc'] = $v['name'];
- $config['struct'][$k]['update'] = 'hidden';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_numeric';
- $k = 'attr_' . $v['id'] . '_e';
- $config['struct'][$k]['name'] = $v['name'];
- $config['struct'][$k]['default'] = '';
- $config['struct'][$k]['desc'] = $v['name'];
- $config['struct'][$k]['update'] = 'hidden';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_numeric';
- }
- $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;
- }
- }*/
- $option = Dever::db('product/category_attr_value')->getData(array('attr_id' => $v['id']));
- $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-800';
- $config['struct'][$k]['match'] = 'is_numeric';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '';
- break;
- case 11:
- $config['struct'][$k]['update'] = 'checkbox';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '';
- break;
- case 12:
- $config['struct'][$k]['update'] = 'select';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_numeric';
- $config['struct'][$k]['option'] = $option;
- $config['struct'][$k]['search'] = 'select';
- $config['struct'][$k]['default'] = '';
- break;
-
- default:
- $config['struct'][$k]['update'] = 'text';
- $config['struct'][$k]['search'] = 'fulltext';
- if ($v['type'] == 9) {
- $config['struct'][$k]['name'] .= '-这是一个区间数值,后台填写请用半角逗号,隔开';
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- } elseif ($v['data_type'] == 1 || $v['type'] == 1) {
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_numeric';
- } else {
- $config['struct'][$k]['type'] = 'varchar-800';
- $config['struct'][$k]['match'] = 'is_string';
- }
- }
- if ($v['is_must'] && $v['is_must'] == 2) {
- $config['struct'][$k]['match'] = 'option';
- }
- /*
- if ($v['value']) {
- $config['struct'][$k]['default'] = $v['value'];
- }*/
- if ($data && isset($data[$v['id']])) {
- $config['struct'][$k]['default'] = $data[$v['id']]['attr_value'];
- }
- if ($v['state'] == 2) {
- unset($config['struct'][$k]['update']);
- }
- # 还要根据搜索来建立索引,另外,如果数据结构改了怎么办。暂时用varchar
- }
- }
- }
- /**
- * 根据属性id,获取属性列表
- *
- * @return mixed
- */
- public function getList($ids, $value, $order = 'list')
- {
- $data = array();
- if ($ids) {
- $where['ids'] = $ids;
- $method = 'getAllByIds';
- if ($order == 'list') {
- $method = 'getListByIds';
- } elseif ($order == 'search') {
- $method = 'getSearchByIds';
- } elseif ($order == 'search_reorder') {
- $method = 'getSearchByIds';
- $where['search_reorder'] = 1;
- } elseif ($order == 'view') {
- $method = 'getViewByIds';
- }
- $data = Dever::db('product/category_attr')->$method($where);
- $ids = explode(',', $ids);
- $value = explode(',', $value);
- if ($data) {
- foreach ($ids as $k => $v) {
- if (isset($data[$v])) {
- if (isset($value) && isset($value[$k]) && $value[$k]) {
- $data[$v]['value'] = $value[$k];
- } else {
- $data[$v]['value'] = '';
- }
- }
- }
- }
- }
- return $data;
- }
- /**
- * 获取属性的名称,字符类型
- *
- * @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')
- {
- if ($reorder == 'list') {
- $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];
- $attr[$k]['option'] = Dever::db('product/category_attr_value')->getData(array('attr_id' => $v['id']));
- /*
- if ($v['type_option']) {
- $attr[$k]['option'] = explode("\n", $v['type_option']);
- }
- */
- }
- array_multisort($sort, SORT_DESC, $attr);
- }
- }
- public function getAttrByCate($category, $order = 'list_reorder', $total = false)
- {
- $data = array();
- //print_r(Dever::db('category/attr')->all());
- if ($category && Dever::project('category')) {
- $array = explode(',', $category);
- $cate = array();
- $total = $total ? $total : count($array);
- $total -= 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'], $order);
- }
- }
- $this->attrSort($data, $order);
- }
- return $data;
- }
- /**
- * 获取某个分类下的搜索列表
- *
- * @return mixed
- */
- public function getSearch($ids, $cate = true, $city = false, $search_value = false, $total = 3)
- {
- $value = array();
- $search_value = Dever::preInput('attr_', array(), $search_value);
- if ($cate && $ids && Dever::project('category')) {
- $data = $this->getAttrByCate($ids, 'search_reorder', $total);
- if (!$data) {
- return array('search' => array(), 'value' => $value);
- }
- } else {
- $where['ids'] = $ids;
- $where['search_reorder'] = 1;
- $data = Dever::db('product/category_attr')->getSearchByIds($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'] == 1 || $v['type'] == 9) {
- $data[$k]['option'] = Dever::db('product/category_attr_search')->getData(array('attr_id' => $v['id']));
- } else {
- $data[$k]['option'] = Dever::db('product/category_attr_value')->getData(array('attr_id' => $v['id']));
- }
- /*
- 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);
- }
- /**
- * 获取搜索sql
- *
- * @return mixed
- */
- public function getSql($attr, $where, $sql = array(), $tname = 't_1')
- {
- if (!$attr) {
- return $sql;
- }
- foreach ($attr as $k => $v) {
- $key = 'attr_' . $v['id'];
- if (isset($where[$key]) && $where[$key]) {
- //$where['option'][$key] = array('yes', '=');
- if ($v['type'] == 7) {
- # 地区有点复杂,暂时先这样,后续优化一下
- $county = Dever::db('area/county')->one($where[$key]);
- if ($county) {
- $city = Dever::db('area/city')->one($county['city_id']);
- if ($city) {
- $province = Dever::db('area/province')->one($city['province_id']);
- if ($province) {
- $value = $province['id'] . ',' . $city['id'] . ',' . $county['id'];
- //$where[$key] = $value;
- $sql[] = ' '.$tname.'.'.$key.' = "'.$value.'"';
- }
- }
- }
- //} elseif ($v['type'] == 1 && $v['type_option']) {
- } elseif ($v['type'] == 1) {
- $search = Dever::db('product/category_attr_search')->getData(array('attr_id' => $v['id']));
- //print_r($where[$key]);die;
- /*
- $temp = explode("\n", $v['type_option']);
- if (isset($temp[$where[$key]-1])) {
- $temp = explode(',', $temp[$where[$key]-1]);
- $match = $temp[1];
- //$where['option'][$key] = array('yes', '<=');
- //$where[$key] = 100;
- $match = str_replace('{v}', $tname . '.' . $key, $match);
- $sql[] = $match;
- }
- */
- //} elseif ($v['type'] == 9 && $v['type_option']) {
- } elseif ($v['type'] == 9 && $v['type_option']) {
- $search = Dever::db('product/category_attr_search')->getData(array('attr_id' => $v['id']));
- //print_r($where[$key]);die;
- /*
- $temp = explode("\n", $v['type_option']);
- if (isset($temp[$where[$key]-1])) {
- $temp = explode(',', $temp[$where[$key]-1]);
- $match = $temp[1];
- //$where['option'][$key] = array('yes', '<=');
- //$where[$key] = 100;
- $match = str_replace('{s}', $tname . '.' . $key . '_s', $match);
- $match = str_replace('{e}', $tname . '.' . $key . '_e', $match);
- $match = str_replace('{v}', $tname . '.' . $key . '_e', $match);
- $sql[] = $match;
- }
- */
- } else {
- $sql[] = ' '.$tname.'.'.$key.' = "'.$where[$key].'"';
- }
- }
- }
- return $sql;
- }
- }
|