<?php namespace App\Modules\Backend\Articles\Models;

use config;
use \App\Modules\Backend\Categories\Models\Category;

class Article extends \Eloquent {



	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'articles';

	protected $fillable = array('title', 'intro_text', 'full_text', 'state', 'created_by', 'created_by_alias', 'category_id', 'created', 'publish_up', 'publish_down', 'metakey', 'metadesc', 'language', 'intro_image', 'large_image', 'gallery_items');


    /**
     * Se agrega la fechas de publicación para que se usen con el componente carbon
     */
    public function getDates()
    {
        return array_merge(parent::getDates(), array('created', 'publish_up', 'publish_down'));
    }

	public static function getStates()
	{
		return array(
					0 => trans('articles::label.unpublished'),
					1 => trans('articles::label.published')
					);
	}

	// Obtiene el autor real del artículo
	public function user()
    {
        return $this->belongsTo('User');
    }

    public function category()
    {
        return $this->belongsTo('\App\Modules\Backend\Categories\Models\Category');
    }
/**/


    /**
     * FRONTEND
     */
    
    public function getGalleryAttribute()
    {
        if(!empty($this->gallery_items) and $this->gallery_items != '{}')
            return json_decode($this->gallery_items);
        else
            return array();
    }

    public function getHashAttribute()
    {
        return sanatizeText($this->title);
    }
    
    /*
    
    public function getCategoryNameAttribute()
    {
        return sanatizeText($this->category->title);
    }

    

    
    
    public function scopeOffers($query)
    {
        return $query->setUp()->where('category_id', 8);
    }

    public function scopeFishingReport($query)
    {
    	return $query->setUp()->where('category_id', 2);
    }

    public function scopeRestaurants($query)
    {
        return $query->setUp()->where('category_id', 1);
    }

    public function scopeGroundTransfers($query)
    {
        return $query->setUp()->where('category_id', 9);
    }

    public function scopeFishingPages($query)
    {
        return $query->setUp()->where('category_id', 10);
    } 

    public function scopeTournamentsPages($query)
    {
        return $query->setUp()->where('category_id', 12);
    }

    public function scopeTournaments2Pages($query)
    {
        return $query->setUp()->where('category_id', 13);
    }

    public function scopeActivities($query)
    {
        return $query->setUp()->where('category_id', 11);
    }*/


    public function scopeNews($query)
    {
        return $query->setUp()->where('category_id', 1)->orderBy('created', 'desc');
    }

    public function getHumanDateAttribute()
    {
        $dates = explode(' ', $this->created);
        $dates = explode('-', $dates[0]);
        $month = strtolower(date("F", strtotime($this->created)));
        return $dates[2] . ' de ' . trans('dates.' . $month) . ' de ' . $dates[0];
    }

    /**
     * Prepara la consulta para que cumplan las siguientes condiciones:
     * - Idioma actual o todos
     * - El estado sea publicado
     * - Si tienes fechas de publicación debera respetarlas (publish_up and publish_down.
     */
    public function scopeSetUp($query)
    {
        return $query->whereIn('language', ['all', Config::get('app.locale')])
                    ->where('state', 1)
                    ->where(function($query){
                        $query->whereRaw('CURDATE() BETWEEN publish_up AND publish_down')
                                ->orWhere(function($query){
                                    $query->where('publish_up', '0000-00-00')
                                        ->orWhere('publish_down', '0000-00-00');
                                });
                    });
    }

}