<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/14
 * Time: 下午3:38
 */
require_once (BASE_ROOT_PATH . '/helper/special/block_filter.php');

class upgrade_helper extends block_filter
{
    public function __construct($block_type, $special_id, $items)
    {
        parent::__construct($block_type, $special_id, $items);
    }

    public function filter()
    {
        if($this->mItems == false) {
            return false;
        }

        if ($this->mType == 'goods' || $this->mType == 'home7')
        {
            $ids = [];
            foreach ($this->mItems['item'] as $goods_id)
            {
                if(is_array($goods_id)) {
                    return $this->mItems;
                }

                $ids[] = intval($goods_id);
            }
            return $this->goods($ids);
        }
        elseif($this->mType == 'home1') {
            return $this->home1();
        }
        elseif($this->mType == 'home2' ||  $this->mType == 'home4') {
            return $this->home2();
        }
        elseif($this->mType == 'adv_list' || $this->mType == 'home3' || $this->mType == 'home5' || $this->mType == 'home6') {
            return $this->normal();
        }
        else {
            return false;
        }
    }

    private function goods($ids)
    {
        $model_goods = Model('goods');
        $items = $model_goods->getGoodsList(array('goods_id' => array('in',$ids)),'goods_id,goods_name,goods_image');

        $result = [];
        foreach ($items as $item)
        {
            $val = [];
            $image = cthumb($item['goods_image'],360);
            if($this->to_local($image) == true) {
                $val['image'] = $image;
            } else {
                continue;
            }

            $val['title'] = $item['goods_name'];
            $val['type'] = 'goods';
            $val['data'] = $item['goods_id'];
            if($this->mType == 'goods') {
                $val['show_type'] = 'goods';
            }
            elseif($this->mType == 'goods_simple') {
                $val['show_type'] = 'goods_simple';
            }
            else {
                $val['show_type'] = 'goods_top';
            }
            $val['show_data'] = $val['data'];
            $result[$image] = $val;
        }

        if(empty($result)) {
            return false;
        } else {
            return array('item' => $result);
        }
    }
}