1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Facades\Mail; |
8
|
|
|
use SET\Handlers\DateFormat; |
9
|
|
|
use SET\Mail\SendNewsEmail; |
10
|
|
|
|
11
|
|
|
class News extends Model |
12
|
|
|
{ |
13
|
|
|
use DateFormat; |
14
|
|
|
|
15
|
|
|
protected $table = 'news'; |
16
|
|
|
public $timestamps = true; |
17
|
|
|
|
18
|
|
|
protected $fillable = ['title', 'description', 'publish_date', 'expire_date', 'author_id', 'send_email']; |
19
|
|
|
|
20
|
|
|
public function attachments() |
21
|
|
|
{ |
22
|
|
|
return $this->morphMany('SET\Attachment', 'imageable'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function author() |
26
|
|
|
{ |
27
|
|
|
return $this->belongsTo('SET\User', 'author_id'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $query |
32
|
|
|
*/ |
33
|
|
|
public function scopePublishedNews($query) |
34
|
|
|
{ |
35
|
|
|
return $query->where('publish_date', '<=', Carbon::today()) |
36
|
|
|
->where(function ($q) { |
37
|
|
|
$q->where('expire_date', '>=', Carbon::today()) |
38
|
|
|
->orWhere('expire_date', null); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $key |
44
|
|
|
* @param mixed $value |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function setAttribute($key, $value) |
49
|
|
|
{ |
50
|
|
|
if (is_scalar($value) && $value === '') { |
51
|
|
|
$value = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return parent::setAttribute($key, $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getPublishDateAttribute($value) |
58
|
|
|
{ |
59
|
|
|
return $this->dateFormat($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getExpirationDateAttribute($value) |
63
|
|
|
{ |
64
|
|
|
return $this->dateFormat($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Email the news on the publish_date when a news article is created or updated. |
69
|
|
|
*/ |
70
|
|
|
public function emailNews() |
71
|
|
|
{ |
72
|
|
|
$publishDate = Carbon::createFromFormat('Y-m-d', $this->publish_date); |
73
|
|
|
|
74
|
|
|
if ($this->send_email && $publishDate->eq(Carbon::now())) { |
75
|
|
|
$users = User::skipSystem()->active()->get(); |
76
|
|
|
Mail::bcc($users)->send(new SendNewsEmail($this)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|