166 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			166 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
class Block extends Base
 | 
						|
{
 | 
						|
    const BLOCK = 1;
 | 
						|
    const IMG = 2;
 | 
						|
    const TEXT = 3;
 | 
						|
    const GROUP = 4;
 | 
						|
    const VIDEO = 5;
 | 
						|
    const CODE = 6;
 | 
						|
 | 
						|
    //获取类型键值对
 | 
						|
    public static function getTypes()
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            '1' => 'block',
 | 
						|
            '2' => 'img',
 | 
						|
            '3' => 'text',
 | 
						|
            '4' => 'group',
 | 
						|
            '5' => 'video',
 | 
						|
            '6' => 'code'
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    //根据键值和类目获取数据
 | 
						|
    public static function getByKeyword($keyword, $categoryId)
 | 
						|
    {
 | 
						|
        return self::where('keyword', $keyword)->where('category_id', $categoryId)->findOrEmpty()->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    //根据栏目ID获取块列表
 | 
						|
    public static function getByCategoryId($categoryId)
 | 
						|
    {
 | 
						|
        
 | 
						|
        if($categoryId <= 0){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $category = Category::getById($categoryId);
 | 
						|
        if(empty($category)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $items = self::where('category_id', $categoryId)
 | 
						|
        ->order('sort asc')
 | 
						|
        ->select()
 | 
						|
        ->toArray();
 | 
						|
        if(empty($items)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            $data[$item['keyword']] = $item;
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function onAfterInsert($item)
 | 
						|
    {
 | 
						|
        $item->sort  = $item->id;
 | 
						|
        $item->save();
 | 
						|
    }
 | 
						|
 | 
						|
    //获取在使用中的文件
 | 
						|
    public static  function getFilesInUse()
 | 
						|
    {
 | 
						|
        $items = self::whereIn('type', [self::IMG, self::GROUP, self::VIDEO, self::TEXT])
 | 
						|
        ->select()
 | 
						|
        ->toArray();
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            if($item['type'] == self::IMG){
 | 
						|
                $file = trim($item['value']);
 | 
						|
                if(!empty($file)){
 | 
						|
                    $key = getKeyByPath($file);
 | 
						|
                    $data[$key] = $file;
 | 
						|
                }
 | 
						|
            }elseif($item['type'] == self::GROUP){
 | 
						|
                $val = trim($item['value']);
 | 
						|
                if (!empty($val) && $val != '[]' && $val != 'null') {
 | 
						|
                    $files = json_decode($val, true);
 | 
						|
                    foreach($files as $file){
 | 
						|
                        $src = trim($file['src']);
 | 
						|
                        if(!empty($src)){
 | 
						|
                            $key = getKeyByPath($src);
 | 
						|
                            $data[$key] = $src;
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }elseif($item['type'] == self::VIDEO){
 | 
						|
                $video = trim($item['value']);
 | 
						|
                if(!empty($video)){
 | 
						|
                    $key = getKeyByPath($video);
 | 
						|
                    $data[$key] = $video;
 | 
						|
                }
 | 
						|
                $img = trim($item['img']);
 | 
						|
                if(!empty($img)){
 | 
						|
                    $key = getKeyByPath($img);
 | 
						|
                    $data[$key] = $img;
 | 
						|
                }
 | 
						|
            }elseif($item['type'] == self::TEXT){
 | 
						|
                $imgs = getImageUrlFromText($item['value']);
 | 
						|
                if(!empty($imgs)){
 | 
						|
                    $data = array_merge($data, $imgs);
 | 
						|
                }
 | 
						|
                $videos = getVideoUrlFromText($item['value']);
 | 
						|
                if(!empty($videos)){
 | 
						|
                    $data = array_merge($data, $videos);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    // 解析单页块元素内容,把JSON化的内容转为数组格式
 | 
						|
    public static function analysisBlock(array $items)
 | 
						|
    {
 | 
						|
        $arrayToJsonTypes = [4];
 | 
						|
        if(count($items) > 0) {
 | 
						|
            foreach ($items as &$item) {
 | 
						|
                if(in_array($item['type'], $arrayToJsonTypes)) {
 | 
						|
                    $item['value'] = empty($item['value']) ? [] : json_decode($item['value'], true);
 | 
						|
                }
 | 
						|
            }
 | 
						|
            unset($item);
 | 
						|
        }
 | 
						|
        return $items;
 | 
						|
    }
 | 
						|
 | 
						|
    // 按块元素的栏目分类和关键字进行分组
 | 
						|
    public static function convertGroupByCategory(array $items)
 | 
						|
    {
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            $data[$item['category_id']][$item['keyword']] = $item;
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    // 按块元素的关键字进行分组
 | 
						|
    public static function convertGroupByKeyword(array $items)
 | 
						|
    {
 | 
						|
        $data = [];
 | 
						|
        foreach($items as $item){
 | 
						|
            $data[$item['keyword']] = $item;
 | 
						|
        }
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    //根据栏目ID获取块列表
 | 
						|
    public static function getByCategoryIds($categoryIds)
 | 
						|
    {
 | 
						|
        if(!is_array($categoryIds) || empty($categoryIds)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        $items = self::whereIn('category_id', $categoryIds)
 | 
						|
            ->order('category_id', 'asc')
 | 
						|
            ->order('sort', 'asc')
 | 
						|
            ->select()
 | 
						|
            ->toArray();
 | 
						|
        if(empty($items)){
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
        return self::convertGroupByCategory($items);
 | 
						|
    }
 | 
						|
}
 |