<?php

namespace DMZ\Models;
use DB;
use Illuminate\Database\Eloquent\Model;

class Contents extends Model
{
  protected $table ='contents';
  public $fillable = [
    'created_by',
    'updated_by',
    'created_by_alias',
    'publish_up',
    'publish_down',
    'status',
    'version',
    'ordering',
    'access',
    'hits',
    'tags',
    'featured',
    'language',
    'images',
    'title',
    'slug',
    'author',
    'text',
    'description'
  ];
  public $timestamps = true;

  public function scopeGetContentPublic($query, $category)
    {
        return $query->select('contents.*')
                     ->where('contents.status',1)
                     ->where('contents.category_id',$category)
                     ->orderBy('publish_up', 'desc')
                     ->orderBy('created_at', 'desc');
     }
  public function scopeGetContentPublicAll($query, $category)
    {
        return $query->select('contents.*') 
                     ->where('contents.status','>=',0)
                     ->where('contents.category_id',$category)
                     ->orderBy('publish_up', 'desc')
                     ->orderBy('created_at', 'desc');
     }
    public function scopeGetContentItem($query, $slug)
       {
           return $query->select('contents.*',
           DB::raw('(select slug from categories WHERE category_id=categories.id) as category_slug'),
           DB::raw('(select category from categories WHERE category_id=categories.id) as category') )
                       // ->where('contents.status',1)
                        ->where('contents.slug',$slug) ;
        }
    public function scopeGetContentItemId($query, $id)
       {
           return $query->select('contents.*',
           DB::raw('(select slug from categories WHERE category_id=categories.id) as category_slug'),
           DB::raw('(select category from categories WHERE category_id=categories.id) as category') )
                      //  ->where('contents.status',1)
                        ->where('contents.id',$id) ;
        }
    public function scopeGetContentLast($query, $category)
       {
           return $query->select('contents.*',
           DB::raw('(select slug from categories WHERE category_id=categories.id) as category') )
                       ->where('contents.status',1)
                       ->where('contents.category_id',$category)
                       ->orderBy('publish_up', 'desc');
       }
    public function scopeGetContentFeatured($query)
        {
            return $query->select('contents.*',
            DB::raw('(select slug from categories WHERE category_id=categories.id) as category') )
                      ->where('contents.status',1)
                      ->where('contents.category_id',32)
                       //->whereNotIn('contents.category_id', [33, 36, 37])
                      ->where('contents.featured',1)
                      ->orderBy('publish_up', 'desc');
       }
}
