Repository URL to install this package:
|
Version:
3.4.1 ▾
|
<?php
namespace Evsmash\News;
use Evsmash\Core\Database\Eloquent;
use Evsmash\Core\Database\SearchableTrait;
use Evsmash\Core\Database\SoftDeleting;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Simpy\Params;
use Evsmash\Core\System\Map;
use Evsmash\News\Schemas\News as Schema;
class News extends Eloquent {
use SoftDeleting;
use SearchableTrait;
protected $searchable = [
'columns' => [
'name' => 10,
'sneak' => 5,
'content' => 5,
]
];
// validate
static public function validate() {
return Schema::validate();
}
// belongs to type
public function type() {
return $this->belongsTo('Evsmash\News\NewsType');
}
// params
public function scopeParams($query, $params = false) {
$scope = new Params();
$scope->scope($query, $params);
$scope->where('published');
$scope->where('type', 'type_id');
$scope->searchable();
$scope->order(['created_at' => 'desc']);
$scope->with(['type']);
return $scope->query;
}
// published
public function scopePublished($query, $params = false) {
return $query->where('published', 1);
}
// promoted
public function scopePromoted($query, $params = false) {
return $query->where('promoted', 1);
}
// link
public function getLinkAttribute() {
$config = cfg('evsmash-news-slugs');
if($config AND isset($config['news'])) {
return '/'.$this->slug;
}
return Route::map('evsmash/news/news/show').'/'.$this->id.'/'.$this->slug;
}
// save
public function save(array $options = array()) {
// save
parent::save($options);
// check slug
$check = News::where('slug', $this->slug)->where('id', '!=', $this->id)->first();
if(!is_null($check)) {
$news = News::find($this->id);
$news->slug = $this->slug.'-'.time();
$news->save();
}
// map refresh
Map::refresh('routes-http');
}
}