123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Repositories;
- use App\Models\Ad;
- use App\Models\AdCategory;
- use Prettus\Repository\Eloquent\BaseRepository;
- /**
- * Class AdRepositoryEloquent.
- *
- * @package namespace App\Repositories;
- */
- class AdRepository extends BaseRepository
- {
- protected $ads;
- protected $adCategory;
- /**
- * AdRepository constructor.
- * @param $ads
- */
- public function __construct(Ad $ads, AdCategory $adCategory)
- {
- $this->ads = $ads;
- $this->adCategory = $adCategory;
- }
- public function model()
- {
- return Ad::class;
- }
- public function getFilterAds($where, $whereIn = array())
- {
- $today_time = strtotime(date('Y-m-d', strtotime("+1 day")))-1;
- /*
- if ($whereIn) {
- $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias')
- ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
- ->where($where)
- ->where(function ($query) {
- $today = strtotime(date('Y-m-d', time()));
- $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
- })
- ->where('started_at', '<=', $today_time)
- ->whereIn('c.alias', $whereIn)
- ->orderBy('list_order', 'asc')
- ->orderBy('id', 'desc')
- ->get();
- } else {
- $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias')
- ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
- ->where($where)
- ->where(function ($query) {
- $today = strtotime(date('Y-m-d', time()));
- $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
- })
- ->where('started_at', '<=', $today_time)
- ->orderBy('list_order', 'asc')
- ->orderBy('id', 'desc')
- ->get();
- }
- */
- $res = $this->ads
- ->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height','c.alias','ads.target')
- ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
- ->where($where)
- ->where(function ($query) {
- $today = strtotime(date('Y-m-d', time()));
- $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
- })
- ->where('started_at', '<=', $today_time);
- if ($whereIn) {
- $res->whereIn('c.alias', $whereIn);
- }
- /*$res->when(get_subsite_id()>0, function ($query) {
- $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});
- });*/
- $res->whereHas('subsites', function ($query) {
- $query->where('subsite_id', get_subsite_id());
- });
- $rst = $res->orderBy('list_order', 'desc')->orderBy('ads.created_at', 'desc')->get();
- $res_new = $this->get_html($rst);
- return $res_new;
- }
- public function getAds($where, $limit)
- {
- $today_time = strtotime(date('Y-m-d', strtotime("+1 day")))-1;
- $res = $this->ads->select('ads.id','ads.category_id','ads.type_id','ads.title','ads.content','ads.url','ads.text_color','ads.uid','c.org','c.is_float','c.float_left','c.float_right','c.float_top','c.width','c.height')
- ->leftjoin($this->adCategory->getTable().' as c', 'ads.category_id', '=', 'c.id')
- ->where($where)
- ->where(function ($query) {
- $today = strtotime(date('Y-m-d', time()));
- $query->where('ended_at', '>=', $today)->orWhere('ended_at', '=', 0);
- })
- ->where('started_at', '<=', $today_time)
- ->when(get_subsite_id()>0, function ($query) {
- $query->whereHas('subsites', function ($query) {
- $query->where('subsite_id', get_subsite_id());
- });
- })
- ->orderBy('list_order', 'asc')
- ->orderBy('ads.created_at', 'desc')
- ->limit($limit)
- ->get();
- $res_new = $this->get_html($res);
- return $res_new;
- }
- /*
- * 处理广告数据
- * _get_html方法用于根据不同的广告类型生成对应的模板文件代码
- * 广告类型(1:文字,2:图片,3:代码,4:flash,5:视频)
- */
- private function get_html($ads)
- {
- if ($ads->ToArray()) {
- foreach ($ads as $k => $v) {
- if ($v->alias == 'AIX_index_five_recommend' && $v->uid ==0) {
- unset($ads[$k]);
- continue;
- }
- $html = '';
- $size_html = '';
- $href = '';
- $v->width && $size_html .= 'width="'.$v->width.'"';
- $v->height && $size_html .= ' height="'.$v->height.'"';
- if ($v->url) {
- $href = $v->url;
- } elseif ($v->uid) {
- //企业信息查看页面
- $href = route(url_rewrite('AIX_companyshow'), array('id'=>$v->uid));
- }
- !$href && $href = 'javascript:;';
- switch ($v->type_id) {
- case 1:
- $v->text_color && $style="color:{{$v->text_color}};";
- $v->width && $style.="width:{{$v->width}};";
- $v->height && $style.="height:{{$v->height}}";
- $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank" style="'.$style.'">'.$v->content.'</a>';
- break;
- case 2://生成广告为图片类型的代码
- $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
- $html .= '<img alt="'.$v->title.'" src="'.upload_asset($v->content).'" '.$size_html.'>';
- $html .= '</a>';
- break;
- case 3:
- $html = $v->content;
- break;
- case 4://生成广告为flash类型的代码
- $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
- $html .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '.$size_html.' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">';
- $html .= '<param name="movie" value="{{upload_asset($v->content)}}" />';
- $html .= '<param name="quality" value="autohigh" />';
- $html .= '<param name="wmode" value="opaque" />';
- $html .= '<embed src="{{upload_asset($v->content)}}" quality="autohigh" wmode="opaque" name="flashad" swliveconnect="TRUE" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" '.$size_html.'></embed>';
- $html .= '</object>';
- $html .= '</a>';
- break;
- case 5://生成视频广告
- if (preg_match("/^.+\.(flv|f4v)$/i", $v->content)) {
- $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">';
- $html .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '.$size_html.'>';
- $html .= '<param name="movie" value="__ROOT__/static/flvplayer.swf"/>';
- $html .= '<param name="quality" value="high"/>';
- $html .= '<param name="allowFullScreen" value="true" />';
- $html .= '<param name="FlashVars" value="vcastr_file='.upload_asset($v->content).'&IsAutoPlay=1&IsContinue=1" />';
- $html .= '<embed src="__ROOT__/static/flvplayer.swf" autostart=true loop=true allowFullScreen="true" FlashVars="vcastr_file='.upload_asset($v->content).'&IsAutoPlay=1&IsContinue=1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" '.$size_html.'></embed>';
- $html .= '</object>';
- $html .= '</a>';
- } else {
- //通用代码
- $v->content = preg_replace('/((width)[=]?[:]?[\"]?[0-9]+[\"]?)/i', 'width="'.$v->width.'"', $v->content);//宽
- $v->content = preg_replace('/((height)[=]?[:]?[\'"]?[0-9]+[\'"]?)/i', 'height="'.$v->height.'"', $v->content);//高
- $html = '<a title="'.$v->title.'" href="'.$href.'" target="_blank">'.$v->content.'</a>';
- }
- break;
- }
- $v->html = $html;
- $v->href = $href;
- }
- }
- return $ads;
- }
- }
|