News.class.php 5.13 KB
<?php
/**
 * 默认控制器
 */
class Controller_Admin_News extends Controller_Admin_Base 
{
    
    /**
     * Index
     */
    public function indexAction()
    {
        $limit = 15 ;
        $url_args = $conditions = array();
        $total = Facade_News::getTotal($conditions);
        $pagination = new Lib_Helper_Pagination($total,$limit);
        $pagination->setParames($url_args);
        $list = Facade_News::getList($conditions, $pagination->getOffset(),$limit);
        foreach ($list as $k => $v)
        {
            $list[$k]['content'] = Util_StringHelper::substr_cn(strip_tags($v['content']),30);
            if ($v['thumb'])
            {
                $list[$k]['thumb'] = Lib_Images::getImageUrl($v['thumb'],'0140x0060','fragmentimg');
            }
            if ($v['client_thumb'])
            {
            	$list[$k]['client_thumb'] = Lib_Images::getImageUrl($v['client_thumb'],'0140x0060','fragmentimg');
            }
            $list[$k]['tag'] = Util_StringHelper::substr_cn($v['tag'],30);
            $list[$k]['title'] = Util_StringHelper::substr_cn($v['title'],30);
        }
        $this->_view['list'] = $list ; 
        $this->_view['pagination'] = $pagination->getPagination() ;
        $this->_view['base_dir'] = $this->_request->baseDir() ;
        $this->_view['tags'] = array() ;
    }
    
    public function createAction()
    {
        $id = intval($this->_request->id );
        $info =  Array
        (
            'id' => 0,
            'title' => '',
        	'main_title_type' => 0,
        	'main_title'=>'',
            'content' => '',
            'thumb' =>'', 
            'tag' => '',
            'image' => '' ,
        	'sort'=> '0',
        	'client_thumb'=>'',
        	'client_image'=>'',
        	'pics' => array(),
            'brands'=> array(),
        );
        
        if ($id)
        {
            $info = Facade_News::getOneById($id);
            $brandNewsRela = Facade_Brand::getBrandNewsRelaByNewsID($id);
        
            $pics = array();
            if ($info['thumb'])
            {
                $info['image'] = Lib_Images::getImageUrl($info['thumb'],'0140x0060','fragmentimg');
            }
            if($info['client_thumb'])
            {
            	$info['client_image'] = Lib_Images::getImageUrl($info['client_thumb'],'0140x0060','fragmentimg');
            }
            if($info['pics'])
            {
            	$list = array_filter(explode('|', $info['pics']));
            	foreach($list  as $pic)
            	{
            		if(!empty($pic))
            		{
            			$pics[$pic] = Lib_Images::getImageUrl($pic,'0100x0100','fragmentimg');
            		}
            	}
            }
            $info['brands'] = array();
            if(!empty($brandNewsRela))
            {
                $brand = '';
            	foreach($brandNewsRela as $rela)
            	{
            		$brand = Facade_Brand::getBrandbyID($rela['brand_id']);
            		if(!empty($brand))
            		{
            		    $info['brands'][] = $brand;
            		}
            	}
            }
            $info['pics'] = $pics;
        }        
        $this->_view['key'] = Lib_Images::genKey('fragmentimg');
        $this->_view['tags'] = Facade_News::getTags();
        $this->_view['info'] = $info ;
        $this->_view['id'] = $id ;
        $this->_view['types'] = Facade_News::$types;
        $this->_view['title'] = ($id)?'更新信息':'添加信息';
    }
    
    /**
     * 保存
     *
     * @return json
     */
    public function doAction()
    {
        $id = intval($this->_request->id);
        do 
        {
            $data = $_POST;
            $ret = array('url' => url('news/index' ));
            if(empty($data))
            {
            	return $this->returnJson(true, 200, $ret,'数据不能为空');
            }
            $pics = array_filter(explode('|', $data['pics']));
            $brands = array_filter(explode('|', $data['brands']));
            unset($data['brands']);
            $data['tag'] = str_replace(',',',',$data['tag']);
            $width = $height = 0;
            $times = 0;
            while($width <= 0 && $height <= 0 && $times++<=5)
            {
                $info = Lib_Images::getImageInfo($data['thumb'], 'fragmentimg');
            	$width = intval($info['width']);
            	$height = intval($info['height']);
            }
            $data['thumb_size'] = json_encode(array('width'=> $width, 'height'=> $height));
            if ($id)
            {
                //更新
                Facade_News::setInfo($data, $id);
            }
            else
            {
                //新建
                $id = Facade_News::setInfo($data);
            }
            Facade_Brand::deleteBrandNewsRelaByNewsID($id);
            foreach($brands as $brandID)
            {
                Facade_Brand::setBrandNewsRela($id, $brandID);
            }
            return $this->returnJson(true, 200,$ret,'');
        }while (false);
        
    }
    
    /**
     * 删除
     *
     */
    public function delAction()
    {
        $id = intval($this->_request->id)  ;
        Facade_News::delInfo($id);
        return $this->returnJson(true, 200,'','');
    }
    
}