special_helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/6/16
  6. * Time: 下午2:07
  7. */
  8. //将乱七八糟的格式输出,整合成统一的格式输出
  9. class special_helper
  10. {
  11. private $special_id;
  12. private $filter_data;
  13. private $goods_ids;
  14. public function __construct($special_id)
  15. {
  16. $this->special_id = $special_id;
  17. $this->read_cache();
  18. }
  19. public static function format_brand($brands)
  20. {
  21. if(empty($brands)) return array();
  22. $result = [];
  23. if(count($brands) == 1)
  24. {
  25. $result['item_title'] = '';
  26. $result['item_type'] = 'home1';
  27. foreach ($brands as $val) {
  28. $item['image'] = "";
  29. $item['type'] = $val['type'];
  30. $item['data'] = $val['data'];
  31. $item['title'] = $val['title'];
  32. $result['items'][] = $item;
  33. }
  34. }
  35. else
  36. {
  37. $result['item_title'] = '';
  38. $result['item_type'] = 'homewords';
  39. foreach ($brands as $val) {
  40. $item['image'] = "";
  41. $item['type'] = $val['type'];
  42. $item['data'] = strval($val['data']);
  43. $item['title'] = $val['title'];
  44. $result['items'][] = $item;
  45. }
  46. }
  47. return $result;
  48. }
  49. public static function format_category($cats,$title = '')
  50. {
  51. $result = [];
  52. $result['item_title'] = $title;
  53. $result['item_type'] = 'homewords';
  54. foreach ($cats as $val) {
  55. $item['image'] = "";
  56. $item['type'] = $val['type'];
  57. $item['data'] = strval($val['data']);
  58. $item['title'] = $val['title'];
  59. $result['items'][] = $item;
  60. }
  61. return $result;
  62. }
  63. public static function format_keyword($words,$title='')
  64. {
  65. $result = [];
  66. $result['item_title'] = $title;
  67. $result['item_type'] = 'homewords';
  68. foreach ($words as $word) {
  69. $item['image'] = "";
  70. $item['type'] = 'keyword';
  71. $item['data'] = $word;
  72. $item['title'] = '';
  73. $result['items'][] = $item;
  74. }
  75. return $result;
  76. }
  77. public static function format_goods($goods_ids,$title)
  78. {
  79. $result = [];
  80. $result['item_title'] = empty($title) ? '' : $title;
  81. $result['item_type'] = 'home_goods';
  82. foreach ($goods_ids as $goods_id) {
  83. $item['image'] = "";
  84. $item['type'] = "goods";
  85. $item['data'] = strval($goods_id);
  86. $result['items'][] = $item;
  87. }
  88. return $result;
  89. }
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. public function format(&$goods_ids)
  92. {
  93. $goods_ids = $this->goods_ids;
  94. return $this->filter_data;
  95. }
  96. private function read_cache()
  97. {
  98. $mod_special = Model('mb_special');
  99. $specials = $mod_special->getMbSpecialItemUsableListByID($this->special_id);
  100. // unset($specials['mobile_filter_data'],$specials['mobile_goods_ids']);
  101. //
  102. // $this->filter_data = $this->filter($specials,$goods_ids);
  103. // $this->goods_ids = $goods_ids;
  104. //
  105. // $specials['mobile_filter_data'] = $this->filter_data;
  106. // $specials['mobile_goods_ids'] = $goods_ids;
  107. if(array_key_exists("mobile_filter_data",$specials)) {
  108. $this->filter_data = $specials['mobile_filter_data'];
  109. $this->goods_ids = $specials['mobile_goods_ids'];
  110. } else {
  111. $this->filter_data = $this->filter($specials,$goods_ids);
  112. $this->goods_ids = $goods_ids;
  113. $specials['mobile_filter_data'] = $this->filter_data;
  114. $specials['mobile_goods_ids'] = $goods_ids;
  115. $cache = array('special' => serialize($specials));
  116. wcache($this->special_id, $cache, 'mb_special');
  117. }
  118. }
  119. private function filter($specials,&$goods_ids)
  120. {
  121. $blocks = [];
  122. $goods_ids = array();
  123. foreach($specials as $key => $special)
  124. {
  125. $block = $this->filter_item($special,$goods_ids,$scale);
  126. if($scale != false) {
  127. $blocks[] = $block;
  128. }
  129. }
  130. return $blocks;
  131. }
  132. private function filter_item($special,&$goods_ids,&$scale)
  133. {
  134. $scale = false;
  135. $result = [];
  136. foreach($special as $key => $val)
  137. {
  138. $result['item_title'] = empty($val['title']) ? '' : $val['title'];
  139. $result['item_type'] = $key;
  140. if($key == 'home1') {
  141. $items = $this->filter_home1($val,$goods_ids,$scale);
  142. }
  143. elseif($key == 'home2') {
  144. $items = $this->filter_home2($val,$goods_ids,$scale);
  145. }
  146. elseif($key == 'home3') {
  147. $items = $val['item'];
  148. $items = $this->filter_home3($items,$goods_ids,$scale);
  149. }
  150. elseif($key == 'home4') {
  151. $items = $this->filter_home4($val,$goods_ids,$scale);
  152. }
  153. elseif($key == 'goods')
  154. {
  155. $result['item_type'] = 'home_goods';
  156. $items = $val['item'];
  157. $items = $this->filter_goods($items,$goods_ids,$scale);
  158. }
  159. elseif($key == 'home5')
  160. {
  161. $items = $val['item'];
  162. $items = $this->filter_adv($items,$goods_ids,$scale);
  163. }
  164. elseif($key == 'adv_list')
  165. {
  166. $items = $val['item'];
  167. $items = $this->filter_adv($items,$goods_ids,$scale);
  168. }
  169. else {
  170. continue;
  171. }
  172. $this->decode($items);
  173. $result['items'] = $items;
  174. $result['scale'] = $scale;
  175. }
  176. return $result;
  177. }
  178. /**
  179. * 处理type=url 时的&字符转义问题
  180. * @param array $data
  181. * @return array|bool
  182. */
  183. private function decode(&$items)
  184. {
  185. foreach($items as &$item)
  186. {
  187. if ($item['type'] == 'url' || $item['type'] == 'innerurl') {
  188. $item['data'] = htmlspecialchars_decode($item['data']);
  189. }
  190. }
  191. }
  192. private function scale($image)
  193. {
  194. $image = trim($image);
  195. if(empty($image)) {
  196. return false;
  197. }
  198. $ret = getimagesize($image,$info);
  199. if($ret != false) {
  200. $scale = floatval($ret[0]) / floatval($ret[1]);
  201. return $scale;
  202. } else {
  203. return false;
  204. }
  205. }
  206. private function safe_string($str)
  207. {
  208. if($str == null || empty($str)) {
  209. return "";
  210. }
  211. else {
  212. return $str;
  213. }
  214. }
  215. private function filter_home1($val,&$goods_ids,&$scale)
  216. {
  217. $items = [];
  218. $item['image'] = $val['image'];
  219. $item['type'] = $val['type'];
  220. $item['data'] = $val['data'];
  221. $item['title'] = $this->safe_string($val['title']);
  222. $scale = $this->scale($item['image']);
  223. $items[] = $item;
  224. if($item['type'] == 'goods')
  225. {
  226. $goods_id = intval($item['data']);
  227. if($goods_id > 0) {
  228. array_push($goods_ids,$goods_id);
  229. }
  230. }
  231. return $items;
  232. }
  233. private function filter_home2($val,&$goods_ids,&$scale)
  234. {
  235. $items = [];
  236. {
  237. $item['image'] = $val['square_image'];
  238. $item['type'] = $val['square_type'];
  239. $item['data'] = $val['square_data'];
  240. $item['title'] = $this->safe_string($val['square_title']);
  241. $items[] = $item;
  242. }
  243. {
  244. $item['image'] = $val['rectangle1_image'];
  245. $item['type'] = $val['rectangle1_type'];
  246. $item['data'] = $val['rectangle1_data'];
  247. $item['title'] = $this->safe_string($val['rectangle1_title']);
  248. $items[] = $item;
  249. $scale = $this->scale($item['image']);
  250. }
  251. {
  252. $item['image'] = $val['rectangle2_image'];
  253. $item['type'] = $val['rectangle2_type'];
  254. $item['data'] = $val['rectangle2_data'];
  255. $item['title'] = $this->safe_string($val['rectangle2_title']);
  256. $items[] = $item;
  257. }
  258. foreach ($items as $item)
  259. {
  260. if($item['type'] == 'goods')
  261. {
  262. $goods_id = intval($item['data']);
  263. if($goods_id > 0) {
  264. array_push($goods_ids,$goods_id);
  265. }
  266. }
  267. }
  268. return $items;
  269. }
  270. private function filter_home3($items,&$goods_ids,&$scale)
  271. {
  272. $result = [];
  273. $has_set = false;
  274. foreach($items as $item)
  275. {
  276. $result[] = $item;
  277. if($has_set == false) {
  278. $scale = $this->scale($item['image']);
  279. if($scale != false) {
  280. $scale = $scale * 2;
  281. }
  282. $has_set = true;
  283. }
  284. if($item['type'] == 'goods')
  285. {
  286. $goods_id = intval($item['data']);
  287. if($goods_id > 0) {
  288. array_push($goods_ids,$goods_id);
  289. }
  290. }
  291. }
  292. return $result;
  293. }
  294. private function filter_home4($val,&$goods_ids,&$scale)
  295. {
  296. $items = [];
  297. {
  298. $item['image'] = $val['rectangle1_image'];
  299. $item['type'] = $val['rectangle1_type'];
  300. $item['data'] = $val['rectangle1_data'];
  301. $item['title'] = $this->safe_string($val['rectangle1_title']);
  302. $items[] = $item;
  303. }
  304. {
  305. $item['image'] = $val['rectangle2_image'];
  306. $item['type'] = $val['rectangle2_type'];
  307. $item['data'] = $val['rectangle2_data'];
  308. $item['title'] = $this->safe_string($val['rectangle2_title']);
  309. $scale = $this->scale($item['image']);
  310. $items[] = $item;
  311. }
  312. {
  313. $item['image'] = $val['square_image'];
  314. $item['type'] = $val['square_type'];
  315. $item['data'] = $val['square_data'];
  316. $item['title'] = $this->safe_string($val['square_title']);
  317. $items[] = $item;
  318. }
  319. foreach ($items as $item)
  320. {
  321. if($item['type'] == 'goods')
  322. {
  323. $goods_id = intval($item['data']);
  324. if($goods_id > 0) {
  325. array_push($goods_ids,$goods_id);
  326. }
  327. }
  328. }
  329. return $items;
  330. }
  331. private function filter_goods($items,&$goods_ids,&$scale)
  332. {
  333. $scale = 1;
  334. $result = [];
  335. foreach($items as $val)
  336. {
  337. $item['data'] = $val['goods_id'];
  338. $item['type'] = 'goods';
  339. $item['image'] = $val['goods_image'];
  340. $item['title'] = $val['goods_name'];
  341. $result[] = $item;
  342. $goods_id = intval($val['goods_id']);
  343. if($goods_id > 0) {
  344. array_push($goods_ids,$goods_id);
  345. }
  346. }
  347. return $result;
  348. }
  349. private function filter_adv($items,&$goods_ids,&$scale)
  350. {
  351. $result = [];
  352. $has_set = false;
  353. foreach($items as $item)
  354. {
  355. $val['image'] = $item['image'];
  356. $val['type'] = $item['type'];
  357. $val['data'] = $item['data'];
  358. $val['title'] = $this->safe_string($item['title']);
  359. $result[] = $val;
  360. if($has_set == false) {
  361. $scale = $this->scale($item['image']);
  362. $has_set = true;
  363. }
  364. if($item['type'] == 'goods')
  365. {
  366. $goods_id = intval($item['data']);
  367. if($goods_id > 0) {
  368. array_push($goods_ids,$goods_id);
  369. }
  370. }
  371. }
  372. return $result;
  373. }
  374. }