123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/6/16
- * Time: 下午2:07
- */
- //将乱七八糟的格式输出,整合成统一的格式输出
- class brands_special
- {
- const publish_brands_special = 50;
- const local_brands_special = 50;
- const test_brands_special = 23;
- static private $stBImgContianers;
- public function brands_special()
- {
- if(self::$stBImgContianers == null) {
- self::$stBImgContianers = [];
- $this->init();
- }
- }
- public function info($brand_id)
- {
- if(array_key_exists($brand_id,self::$stBImgContianers)) {
- return self::$stBImgContianers[$brand_id];
- } else {
- return false;
- }
- }
- private function init()
- {
- $special_id = self::special_id();
- if($special_id == false) return false;
- $helper = new special_helper($special_id);
- $blocks = $helper->format($goods_ids);
- foreach ($blocks as $block)
- {
- $items = $block['items'];
- if(empty($items) || is_array($items) == false) continue;
- foreach ($items as $item)
- {
- $brand_id = intval($item['data']);
- $data = $item;
- $data['scale'] = $block['scale'];
- self::$stBImgContianers[$brand_id] = $data;
- }
- if(!empty($items) && is_array($items)) {
- }
- }
- }
- private static function special_id()
- {
- if(is_localdebug()) {
- return self::local_brands_special;
- }
- elseif (is_pushoms()) {
- return self::publish_brands_special;
- }
- else {
- return self::test_brands_special;
- }
- }
- }
- class special_helper
- {
- private $special_id;
- private $filter_data;
- private $goods_ids;
- public function __construct($special_id)
- {
- $this->special_id = $special_id;
- $this->read_cache();
- }
- public static function format_brand_sale($brand_id,$can_click)
- {
- if($brand_id <= 0) return array();
- $helper = new brands_special();
- $info = $helper->info($brand_id);
- if($info == false) return array();
- $result['item_title'] = '';
- $result['item_type'] = 'home1';
- $result['scale'] = $info['scale'];
- $item['image'] = $info['image'];
- $item['type'] = $info['type'];
- if($can_click == true) {
- $item['data'] = $info['data'];
- } else {
- $item['data'] = 0;
- }
- $item['title'] = $info['title'];
- $result['items'][] = $item;
- return $result;
- }
- public static function format_grid_brands($brands)
- {
- $result = [];
- $result['item_title'] = '';
- $result['item_type'] = 'home_grid';
- $result['scale'] = 3;
- $items = [];
- foreach ($brands as $brand) {
- $item['image'] = $brand['brand_logo'];
- $item['type'] = 'brand';
- $item['data'] = $brand['brand_id'];
- $item['title'] = $brand['brand_name'];
- $items[] = $item;
- }
- $result['items'] = $items;
- return $result;
- }
- public static function format_brand($brands)
- {
- if(empty($brands)) return array();
- if(count($brands) == 1)
- {
- $brand_id = intval($brands[0]['data']);
- return self::format_brand_sale($brand_id,true);
- }
- else
- {
- $result = [];
- $result['item_title'] = '';
- $result['item_type'] = 'homewords';
- foreach ($brands as $val) {
- $item['image'] = "";
- $item['type'] = $val['type'];
- $item['data'] = strval($val['data']);
- $item['title'] = $val['title'];
- $result['items'][] = $item;
- }
- return $result;
- }
- }
- public static function format_category($cats,$title = '')
- {
- if(empty($cats)) return array();
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'homewords';
- foreach ($cats as $val) {
- $item['image'] = "";
- $item['type'] = $val['type'];
- $item['data'] = strval($val['data']);
- $item['title'] = $val['title'];
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_keyword($words,$title='')
- {
- $result = [];
- $result['item_title'] = $title;
- $result['item_type'] = 'homewords';
- foreach ($words as $word) {
- $item['image'] = "";
- $item['type'] = 'keyword';
- $item['data'] = $word;
- $item['title'] = $word;
- $result['items'][] = $item;
- }
- return $result;
- }
- public static function format_goods($goods_ids,$title)
- {
- $result = [];
- $result['item_title'] = empty($title) ? '' : $title;
- $result['item_type'] = 'home_goods';
- foreach ($goods_ids as $goods_id) {
- $item['image'] = "";
- $item['type'] = "goods";
- $item['data'] = strval($goods_id);
- $result['items'][] = $item;
- }
- return $result;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public function format(&$goods_ids)
- {
- $goods_ids = $this->goods_ids;
- return $this->filter_data;
- }
- private function read_cache()
- {
- $mod_special = Model('mb_special');
- $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
- // unset($specials['mobile_filter_data'],$specials['mobile_goods_ids']);
- //
- // $this->filter_data = $this->filter($specials,$goods_ids);
- // $this->goods_ids = $goods_ids;
- //
- // $specials['mobile_filter_data'] = $this->filter_data;
- // $specials['mobile_goods_ids'] = $goods_ids;
- if(array_key_exists("mobile_filter_data",$specials)) {
- $this->filter_data = $specials['mobile_filter_data'];
- $this->goods_ids = $specials['mobile_goods_ids'];
- } else {
- $this->filter_data = $this->filter($specials,$goods_ids);
- $this->goods_ids = $goods_ids;
- $specials['mobile_filter_data'] = $this->filter_data;
- $specials['mobile_goods_ids'] = $goods_ids;
- $cache = array('special' => serialize($specials));
- wcache($this->special_id, $cache, 'mb_special');
- }
- }
- private function filter($specials,&$goods_ids)
- {
- $blocks = [];
- $goods_ids = array();
- foreach($specials as $key => $special)
- {
- $block = $this->filter_item($special,$goods_ids,$scale);
- if($scale != false && !empty($block)) {
- $blocks[] = $block;
- }
- }
- return $blocks;
- }
- private function filter_item($special,&$goods_ids,&$scale)
- {
- $scale = false;
- $result = [];
- foreach($special as $key => $val)
- {
- $block = [];
- $block['item_title'] = empty($val['title']) ? '' : $val['title'];
- $block['item_type'] = $key;
- if($key == 'home1') {
- $items = $this->filter_home1($val,$goods_ids,$scale);
- }
- elseif($key == 'home2') {
- $items = $this->filter_home2($val,$goods_ids,$scale);
- }
- elseif($key == 'home3') {
- $items = $val['item'];
- $items = $this->filter_home3($items,$goods_ids,$scale);
- }
- elseif($key == 'home4') {
- $items = $this->filter_home4($val,$goods_ids,$scale);
- }
- elseif($key == 'goods')
- {
- $block['item_type'] = 'home_goods';
- $items = $val['item'];
- $items = $this->filter_goods($items,$goods_ids,$scale);
- }
- elseif($key == 'home5')
- {
- $items = $val['item'];
- $items = $this->filter_adv($items,$goods_ids,$scale);
- }
- elseif($key == 'adv_list')
- {
- $items = $val['item'];
- $items = $this->filter_adv($items,$goods_ids,$scale);
- }
- else {
- continue;
- }
- if($items == false) {
- continue;
- } else {
- $this->decode($items);
- $block['items'] = $items;
- $block['scale'] = $scale;
- $result = $block;
- }
- }
- return $result;
- }
- /**
- * 处理type=url 时的&字符转义问题
- * @param array $data
- * @return array|bool
- */
- private function decode(&$items)
- {
- foreach($items as &$item)
- {
- if ($item['type'] == 'url' || $item['type'] == 'innerurl') {
- $item['data'] = htmlspecialchars_decode($item['data']);
- }
- }
- }
- private function scale($image)
- {
- $image = trim($image);
- if(empty($image)) {
- return false;
- }
- $ret = getimagesize($image,$info);
- if($ret != false) {
- $scale = floatval($ret[0]) / floatval($ret[1]);
- return $scale;
- } else {
- return false;
- }
- }
- private function safe_string($str)
- {
- if($str == null || empty($str)) {
- return "";
- }
- else {
- return $str;
- }
- }
- private function check_goods($ids)
- {
- if(empty($ids)) return true;
- $ids = array_unique($ids);
- $mod = Model('goods');
- $goods = $mod->getGoodsOnlineList(array("goods_id" => array("in",$ids)));
- $gids = [];
- foreach ($goods as $item) {
- $gids[] = intval($item['goods_id']);
- }
- if(count($ids) == count($gids)) {
- return true;
- } else {
- return false;
- }
- }
- private function filter_home1($val,&$goods_ids,&$scale)
- {
- $items = [];
- $item['image'] = $val['image'];
- $item['type'] = $val['type'];
- $item['data'] = $val['data'];
- $item['title'] = $this->safe_string($val['title']);
- $scale = $this->scale($item['image']);
- $ids = [];
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- if(self::check_goods($ids)) {
- array_push($goods_ids,$goods_id);
- $items[] = $item;
- return $items;
- } else {
- return false;
- }
- }
- private function filter_home2($val,&$goods_ids,&$scale)
- {
- $items = [];
- {
- $item['image'] = $val['square_image'];
- $item['type'] = $val['square_type'];
- $item['data'] = $val['square_data'];
- $item['title'] = $this->safe_string($val['square_title']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['rectangle1_image'];
- $item['type'] = $val['rectangle1_type'];
- $item['data'] = $val['rectangle1_data'];
- $item['title'] = $this->safe_string($val['rectangle1_title']);
- $items[] = $item;
- $scale = $this->scale($item['image']);
- }
- {
- $item['image'] = $val['rectangle2_image'];
- $item['type'] = $val['rectangle2_type'];
- $item['data'] = $val['rectangle2_data'];
- $item['title'] = $this->safe_string($val['rectangle2_title']);
- $items[] = $item;
- }
- $ids = [];
- foreach ($items as $item)
- {
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- }
- if(self::check_goods($ids)) {
- $goods_ids = array_merge($goods_ids,$ids);
- return $items;
- } else {
- return false;
- }
- }
- private function filter_home3($items,&$goods_ids,&$scale)
- {
- $result = [];
- $has_set = false;
- foreach($items as $item)
- {
- if($has_set == false) {
- $scale = $this->scale($item['image']);
- if($scale != false) {
- $scale = $scale * 2;
- }
- $has_set = true;
- }
- if($item['type'] == 'goods')
- {
- $goods_id = intval($item['data']);
- if(self::check_goods(array($goods_id))) {
- array_push($goods_ids,$goods_id);
- $result[] = $item;
- } else {
- continue;
- }
- }
- else {
- $result[] = $item;
- }
- }
- if(empty($result)) {
- return false;
- } else {
- return $result;
- }
- }
- private function filter_home4($val,&$goods_ids,&$scale)
- {
- $items = [];
- {
- $item['image'] = $val['rectangle1_image'];
- $item['type'] = $val['rectangle1_type'];
- $item['data'] = $val['rectangle1_data'];
- $item['title'] = $this->safe_string($val['rectangle1_title']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['rectangle2_image'];
- $item['type'] = $val['rectangle2_type'];
- $item['data'] = $val['rectangle2_data'];
- $item['title'] = $this->safe_string($val['rectangle2_title']);
- $scale = $this->scale($item['image']);
- $items[] = $item;
- }
- {
- $item['image'] = $val['square_image'];
- $item['type'] = $val['square_type'];
- $item['data'] = $val['square_data'];
- $item['title'] = $this->safe_string($val['square_title']);
- $items[] = $item;
- }
- $ids = [];
- foreach ($items as $item)
- {
- if($item['type'] == 'goods') {
- $ids[] = intval($item['data']);
- }
- }
- if(self::check_goods($ids)) {
- $goods_ids = array_merge($goods_ids,$ids);
- return $items;
- } else {
- return false;
- }
- }
- private function filter_goods($items,&$goods_ids,&$scale)
- {
- $scale = 1;
- $result = [];
- foreach($items as $val)
- {
- $item['data'] = $val['goods_id'];
- $item['type'] = 'goods';
- $item['image'] = $val['goods_image'];
- $item['title'] = $val['goods_name'];
- $goods_id = intval($val['goods_id']);
- if(self::check_goods(array($goods_id))) {
- array_push($goods_ids,$goods_id);
- $result[] = $item;
- } else {
- continue;
- }
- }
- if(empty($result)) {
- return false;
- } else {
- return $result;
- }
- }
- private function filter_adv($items,&$goods_ids,&$scale)
- {
- $result = [];
- $has_set = false;
- foreach($items as $item)
- {
- $val['image'] = $item['image'];
- $val['type'] = $item['type'];
- $val['data'] = $item['data'];
- $val['title'] = $this->safe_string($item['title']);
- if($has_set == false) {
- $scale = $this->scale($item['image']);
- $has_set = true;
- }
- if($item['type'] == 'goods')
- {
- $goods_id = intval($item['data']);
- if(self::check_goods(array($goods_id))) {
- array_push($goods_ids,$goods_id);
- $result[] = $val;
- } else {
- continue;
- }
- }
- else {
- $result[] = $val;
- }
- }
- return $result;
- }
- }
|