mBrandPageResult = []; $this->mBaseInfo = []; $this->mAlphaList = []; } static public function instance() { if (self::$stInstance == null) { self::$stInstance = new brand_helper(); } if(StatesHelper::fetch_state('brands')) { Log::record("brand_helper reload data.",Log::DEBUG); self::$stInstance->init(); } return self::$stInstance; } public function findByname($name) { $name = trim($name); if(array_key_exists($name,$this->mNameID)) { $brand_id = $this->mNameID[$name]; return $this->mBaseInfo[$brand_id]; } else { return false; } } public function name($brand_id) { $brand_id = intval($brand_id); if(array_key_exists($brand_id,$this->mBaseInfo)) { return $this->mBaseInfo[$brand_id]['brand_name']; } else { return ''; } } public function country($brand_id) { $brand_id = intval($brand_id); if(array_key_exists($brand_id,$this->mBaseInfo)) { return $this->mBaseInfo[$brand_id]['brand_country']; } else { return ''; } } public function country_logo($brand_id) { $brand_id = intval($brand_id); if(array_key_exists($brand_id,$this->mBaseInfo)) { return $this->mBaseInfo[$brand_id]['brand_country_logo']; } else { return ''; } } public function author_desc($brand_id) { $brand_id = intval($brand_id); if(array_key_exists($brand_id,$this->mBaseInfo)) { return $this->mBaseInfo[$brand_id]['author_desc']; } else { return ''; } } public function alpha_list() { return $this->mAlphaList; } static function alpha_compare($left,$right) { $l = $left['brand_prefix'][0]; $r = $right['brand_prefix'][0]; if($l > $r) { return 1; } elseif($l == $r) { $l_name = $left['brand_name']; $r_name = $right['brand_name']; return strcmp($l_name,$r_name); } else { return -1; } } private function init() { $data = $this->read_cache(); $this->mBrandPageResult = $data; $this->mBaseInfo = $data['base_info']; $alpha_list = []; foreach ($this->mBaseInfo as $item) { $name = trim($item['brand_name']); if(!empty($name)) { $this->mNameID[$name] = intval($item['brand_id']); } $alpha = Pinyin::getAlpha($name); if(empty($alpha)) continue; $alpha = strtoupper($alpha); $item['brand_prefix'] = $alpha; $alpha_list[] = $item; } usort($alpha_list,['brand_helper','alpha_compare']); $brand_series = []; $this->mAlphaList = []; foreach ($alpha_list as $item) { $val = []; $val['brand_name'] = $item['brand_name']; $val['brand_prefix'] = $item['brand_prefix']; $val['brand_logo'] = $item['brand_logo']; $val['brand_id'] = $item['brand_id']; $this->mAlphaList[] = $val; } } private function read_cache() { $ret = rcache('brandex', 'mb_'); if (!empty($ret)) { return unserialize($ret['data']); } global $config; $exbrands = $config['exclude_brands']; $brands = []; $base_info = []; $model = Model('brand'); $items = $model->field('brand_id,brand_logo,brand_name,brand_country,brand_country_logo,author_desc') ->where(array('brand_apply' => '1')) ->order('brand_sort asc') ->limit(false) ->select(); foreach ($items as $item) { $image = UPLOAD_SITE_URL . $item['brand_logo']; $ret = util::imgsize($image); if ($ret != false) { $item['brand_logo'] = $image; } else { Log::record("cant get {$item['brand_name']}." ,Log::ERR); continue; } if(empty($item['brand_country_logo'])) { $conuntry_logo = ''; Log::record("cant get {$item['brand_name']} brand_country_logo." ,Log::ERR); } else { $conuntry_logo = UPLOAD_SITE_URL . $item['brand_country_logo']; } $item['brand_country_logo'] = $conuntry_logo; $brand_id = intval($item['brand_id']); $base_info[$brand_id] = $item; if(!empty($exbrands) && is_array($exbrands) && in_array($brand_id,$exbrands)) { continue; } else { $brands[] = $item; } } $block = special_formater::format_grid_brands($brands); $result = array("brands" => $brands, "block" => $block,'base_info' => $base_info); wcache("brandex", array("data" => serialize($result)), 'mb_'); return $result; } public function brandex() { return $this->mBrandPageResult; } //////////for old interface///////////////////////////////////////////////////////////////////////////////////////// static function brands() { $prefix = 'mb_'; $ret = rcache('brand', $prefix); if (empty($ret)) { $model = Model(); $brand_area_list = $model->table('brand_area')->order('area_sort asc')->limit(false)->select(); $filed = 'brand_id,brand_name,brand_pic,brand_img_bg,brand_img_logo,brand_area_id'; $brand_c_list = $model->table('brand')->field($filed)->where(array('brand_apply' => '1'))->order('brand_sort asc')->limit(false)->select(); $brands = array(); foreach ($brand_area_list as $brand_area) { foreach ($brand_c_list as $brand) { $brand['brand_pic'] = image_helper::format_brand_img_rect($brand['brand_pic']); $brand['brand_img_bg'] = image_helper::format_brand_img_bg($brand['brand_img_bg']); $brand['brand_img_logo'] = image_helper::format_brand_img_circle($brand['brand_img_logo']); if ($brand['brand_area_id'] == $brand_area['area_id']) { if (empty($brand_area['data'])) { $brand_area['data'] = array(); } array_push($brand_area['data'], $brand); } } if (!empty($brand_area['data'])) { array_push($brands, $brand_area); } } wcache("brand", array("brands" => serialize($brands)), $prefix); } else { $brands = unserialize($ret['brands']); } return $brands; } }