114 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			114 lines
		
	
	
		
			3.3 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; | ||
|  |     } | ||
|  | } |