brand_helper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/12/28
  6. * Time: 下午12:31
  7. */
  8. require_once(BASE_ROOT_PATH . '/helper/img_helper.php');
  9. require_once(BASE_ROOT_PATH . '/helper/special_helper.php');
  10. require_once(BASE_ROOT_PATH . '/helper/time_interval.php');
  11. require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
  12. class brand_helper
  13. {
  14. static private $stInstance = null;
  15. private $mBrandPageResult;
  16. private $mBaseInfo;
  17. private function __construct()
  18. {
  19. $this->mBrandPageResult = [];
  20. $this->mBaseInfo = [];
  21. }
  22. static public function instance()
  23. {
  24. if (self::$stInstance == null) {
  25. self::$stInstance = new brand_helper();
  26. }
  27. if(StatesHelper::fetch_state('brands')) {
  28. Log::record("brand_helper reload data.",Log::DEBUG);
  29. self::$stInstance->init();
  30. }
  31. return self::$stInstance;
  32. }
  33. public function name($brand_id)
  34. {
  35. $brand_id = intval($brand_id);
  36. if(array_key_exists($brand_id,$this->mBaseInfo)) {
  37. return $this->mBaseInfo[$brand_id]['brand_name'];
  38. } else {
  39. return '';
  40. }
  41. }
  42. public function country($brand_id)
  43. {
  44. $brand_id = intval($brand_id);
  45. if(array_key_exists($brand_id,$this->mBaseInfo)) {
  46. return $this->mBaseInfo[$brand_id]['brand_country'];
  47. } else {
  48. return '';
  49. }
  50. }
  51. public function country_logo($brand_id)
  52. {
  53. $brand_id = intval($brand_id);
  54. if(array_key_exists($brand_id,$this->mBaseInfo)) {
  55. return $this->mBaseInfo[$brand_id]['brand_country_logo'];
  56. } else {
  57. return '';
  58. }
  59. }
  60. public function author_desc($brand_id)
  61. {
  62. $brand_id = intval($brand_id);
  63. if(array_key_exists($brand_id,$this->mBaseInfo)) {
  64. return $this->mBaseInfo[$brand_id]['author_desc'];
  65. } else {
  66. return '';
  67. }
  68. }
  69. private function init()
  70. {
  71. $data = $this->read_cache();
  72. $this->mBrandPageResult = $data;
  73. $this->mBaseInfo = $data['base_info'];
  74. }
  75. private function read_cache()
  76. {
  77. $ret = rcache('brandex', 'mb_');
  78. if (!empty($ret)) {
  79. return unserialize($ret['data']);
  80. }
  81. $brands = [];
  82. $base_info = [];
  83. $model = Model('brand');
  84. $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();
  85. foreach ($items as $item)
  86. {
  87. $image = UPLOAD_SITE_URL . $item['brand_logo'];
  88. $ret = util::imgsize($image);
  89. if ($ret != false) {
  90. $item['brand_logo'] = $image;
  91. } else {
  92. Log::record("cant get {$item['brand_name']}." ,Log::ERR);
  93. continue;
  94. }
  95. if(empty($item['brand_country_logo'])) {
  96. $conuntry_logo = '';
  97. Log::record("cant get {$item['brand_name']} brand_country_logo." ,Log::ERR);
  98. } else {
  99. $conuntry_logo = UPLOAD_SITE_URL . $item['brand_country_logo'];
  100. }
  101. $item['brand_country_logo'] = $conuntry_logo;
  102. $brand_id = intval($item['brand_id']);
  103. $brands[] = $item;
  104. $base_info[$brand_id] = $item;
  105. }
  106. $block = special_formater::format_grid_brands($brands);
  107. $result = array("brands" => $brands, "block" => $block,'base_info' => $base_info);
  108. wcache("brandex", array("data" => serialize($result)), 'mb_');
  109. return $result;
  110. }
  111. public function brandex()
  112. {
  113. return $this->mBrandPageResult;
  114. }
  115. //////////for old interface/////////////////////////////////////////////////////////////////////////////////////////
  116. static function brands()
  117. {
  118. $prefix = 'mb_';
  119. $ret = rcache('brand', $prefix);
  120. if (empty($ret)) {
  121. $model = Model();
  122. $brand_area_list = $model->table('brand_area')->order('area_sort asc')->limit(false)->select();
  123. $filed = 'brand_id,brand_name,brand_pic,brand_img_bg,brand_img_logo,brand_area_id';
  124. $brand_c_list = $model->table('brand')->field($filed)->where(array('brand_apply' => '1'))->order('brand_sort asc')->limit(false)->select();
  125. $brands = array();
  126. foreach ($brand_area_list as $brand_area) {
  127. foreach ($brand_c_list as $brand) {
  128. $brand['brand_pic'] = image_helper::format_brand_img_rect($brand['brand_pic']);
  129. $brand['brand_img_bg'] = image_helper::format_brand_img_bg($brand['brand_img_bg']);
  130. $brand['brand_img_logo'] = image_helper::format_brand_img_circle($brand['brand_img_logo']);
  131. if ($brand['brand_area_id'] == $brand_area['area_id']) {
  132. if (empty($brand_area['data'])) {
  133. $brand_area['data'] = array();
  134. }
  135. array_push($brand_area['data'], $brand);
  136. }
  137. }
  138. if (!empty($brand_area['data'])) {
  139. array_push($brands, $brand_area);
  140. }
  141. }
  142. wcache("brand", array("brands" => serialize($brands)), $prefix);
  143. } else {
  144. $brands = unserialize($ret['brands']);
  145. }
  146. return $brands;
  147. }
  148. }