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; } }