1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\Publishable; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withDrafts() |
7
|
|
|
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyDrafts() |
8
|
|
|
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyPublished() |
9
|
|
|
*/ |
10
|
|
|
trait Publishable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Boot the has-drafts trait for a model. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
3 |
|
public static function bootPublishable() |
18
|
|
|
{ |
19
|
3 |
|
static::addGlobalScope(new PublishedScope); |
20
|
3 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initialize this trait for an instance. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
3 |
|
public function initializePublishable() |
28
|
|
|
{ |
29
|
3 |
|
$this->dates[] = $this->getPublishedAtColumn(); |
|
|
|
|
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Save instance of this model as published. |
34
|
|
|
* |
35
|
|
|
* @param array $options |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
1 |
View Code Duplication |
public function publish(array $options = []) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
// https://laravel-news.com/laravel-model-events-getting-started |
42
|
|
|
// https://gist.github.com/scrubmx/7fc20663ce2b3ac103a2879915b572be |
43
|
|
|
// https://www.google.com/search?q=laravel+register+fire+model+event&oq=laravel+register+fire+model+event |
44
|
|
|
|
45
|
|
|
// If the "publishing" event returns false we bail out immediately and return false, |
46
|
|
|
// indicating that the save failed. This provides a chance for any |
47
|
|
|
// listeners to cancel save operations if validations fail or whatever. |
48
|
1 |
|
if ($this->fireModelEvent('publishing') === false) { |
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$this->{$this->getPublishedAtColumn()} = $this->freshTimestamp(); |
|
|
|
|
53
|
|
|
|
54
|
1 |
|
$saved = parent::save($options); |
|
|
|
|
55
|
|
|
|
56
|
1 |
|
if ($saved) { |
57
|
1 |
|
$this->fireModelEvent('published', false); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $saved; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Toogle model instance state to non-published. |
65
|
|
|
* |
66
|
|
|
* @param array $options |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
1 |
View Code Duplication |
public function unpublish(array $options = []) |
|
|
|
|
71
|
|
|
{ |
72
|
1 |
|
if ($this->fireModelEvent('unpublishing') === false) { |
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$this->{$this->getPublishedAtColumn()} = null; |
77
|
|
|
|
78
|
1 |
|
$saved = parent::save($options); |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
if ($saved) { |
81
|
1 |
|
$this->fireModelEvent('unpublished', false); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $saved; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Save instance of this model as a draft. |
89
|
|
|
* |
90
|
|
|
* @param array $options |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function draft(array $options = []) |
95
|
|
|
{ |
96
|
|
|
return $this->unpublish($options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Register a "publishing" model event callback with the dispatcher. |
101
|
|
|
* |
102
|
|
|
* @param \Closure|string $callback |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public static function publishing($callback) |
107
|
|
|
{ |
108
|
|
|
static::registerModelEvent('publishing', $callback); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register a "published" model event callback with the dispatcher. |
113
|
|
|
* |
114
|
|
|
* @param \Closure|string $callback |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public static function published($callback) |
119
|
|
|
{ |
120
|
|
|
static::registerModelEvent('published', $callback); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Register a "unpublishing" model event callback with the dispatcher. |
125
|
|
|
* |
126
|
|
|
* @param \Closure|string $callback |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public static function unpublishing($callback) |
131
|
|
|
{ |
132
|
|
|
static::registerModelEvent('unpublishing', $callback); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Register a "unpublished" model event callback with the dispatcher. |
137
|
|
|
* |
138
|
|
|
* @param \Closure|string $callback |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public static function unpublished($callback) |
143
|
|
|
{ |
144
|
|
|
static::registerModelEvent('unpublished', $callback); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Determine if the model instance is published. |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
1 |
|
public function isPublished() |
153
|
|
|
{ |
154
|
1 |
|
return ! is_null($this->{$this->getPublishedAtColumn()}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the name of the "published at" column. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
3 |
|
public function getPublishedAtColumn() |
163
|
|
|
{ |
164
|
3 |
|
return defined('static::PUBLISHED_AT') ? static::PUBLISHED_AT : 'published_at'; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the fully qualified "published at" column. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
1 |
|
public function getQualifiedPublishedAtColumn() |
173
|
|
|
{ |
174
|
1 |
|
return $this->qualifyColumn($this->getPublishedAtColumn()); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: