Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
evsmash/articles / libs / Article.php
Size: Mime:
<?php

namespace Evsmash\Articles;

use Evsmash\Core\Database\Eloquent;

use Evsmash\Core\Database\SoftDeleting;
use Evsmash\Core\Helpers\Date;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Simpy\Params;
use Evsmash\Core\System\Map;

use Evsmash\Articles\Schemas\Articles as Schema;

class Article extends Eloquent {

	use SoftDeleting;

	// validate
	static public function validate() {

		return Schema::validate();

	}

	// belongs to type
	public function type() { 
		
		return $this->belongsTo('Evsmash\Articles\ArticlesType');
	
	}

	// belongs to many categories 
	public function categories() {
	
		return $this->belongsToMany('Evsmash\Articles\ArticlesCategory', 'articles_to_categories', 'article_id', 'category_id');

	}

	// params
	public function scopeParams($query, $params = false) {

		$scope = new Params();
		$scope->scope($query, $params);
		$scope->where('promoted');
		$scope->where('published');
		$scope->where('type', 'type_id');
		$scope->search(['name', 'slug', 'content', 'sneak']);
		$scope->order(['created_at' => 'desc']);
		$scope->with(['type', 'categories']);
		return $scope->query;

	}

	// published
	public function scopePublished($query, $params = false) {

		  return $query->where('published', 1)->where('created_at', '<=', Date::now('Y-m-d H:i:s'));

	}

	// promoted
	public function scopePromoted($query, $params = false) {

		  return $query->where('promoted', 1);

	}

	// month
	public function scopeMonth($query, $month) {

		if($month) {
			return $query->where('created_at', '>', Date::generate($month, 'Y-m-d H:i:s'))->where('created_at', '<', Date::modify($month, '+1month', 'Y-m-d H:i:s'));
		}
		return $query;

	}

	// link
	public function getLinkAttribute() {

		$config = cfg('evsmash-articles-slugs');
		if($config AND isset($config['articles'])) {
			return '/'.$this->slug;
		}
		return Route::map('evsmash/articles/articles/show').'/'.$this->id.'/'.$this->slug;

	}

	// months
	static public function months() {

		$output = [];
		foreach(Article::select('created_at')->published()->orderBy('created_at', 'desc')->get() as $row) {
			$output[Date::generate($row->created_at, 'Y-m')] = Date::show($row->created_at, 'F Y');
		}
		return $output;

	}

	// save
	public function save(array $options = array()) {

		// save
		parent::save($options);

		// check slug
		$check = Article::where('slug', $this->slug)->where('id', '!=', $this->id)->first();
		if(!is_null($check)) {
			$news = Article::find($this->id);
			$news->slug = $this->slug.'-'.time();
			$news->save();
		}

		// map refresh
		Map::refresh('routes-http');

	}

}