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