1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenG\LaravelPages; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class is the core of the package. Everything is handles through here, although you might always use the Facade 'LPages'. |
7
|
|
|
* |
8
|
|
|
* @author JeroenG |
9
|
|
|
**/ |
10
|
|
|
class LaravelPages |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Checks if a page exists in the database. |
14
|
|
|
* |
15
|
|
|
* @param string $slug The slug to search for in the database. |
16
|
|
|
* @return bool Returns true if the page exists, false otherwise. |
17
|
|
|
**/ |
18
|
|
|
public function pageExists($slug) |
19
|
|
|
{ |
20
|
|
|
$count = Page::where('page_slug', '=', $slug)->count(); |
21
|
|
|
|
22
|
|
|
if ($count == 0) { |
23
|
|
|
return false; |
24
|
|
|
} else { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gets all the data of the page from the database, based on the slug. |
31
|
|
|
* |
32
|
|
|
* @param string $slug The slug to search for in the database. |
33
|
|
|
* @param bool $trashed Include trashed (soft deleted) pages? |
34
|
|
|
* @return array The data such as title, content and publishing date in an array. |
35
|
|
|
**/ |
36
|
|
|
public function getPage($slug, $trashed = false) |
37
|
|
|
{ |
38
|
|
|
if ($trashed) { |
39
|
|
|
return Page::withTrashed()->where('page_slug', '=', $slug)->first(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return Page::where('page_slug', '=', $slug)->first(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets all the data of the page from the database, based on the ID. |
47
|
|
|
* |
48
|
|
|
* @param string $id The id to search for in the database. |
49
|
|
|
* @param bool $trashed Include trashed (soft deleted) pages? |
50
|
|
|
* @return array The data such as title, content and publishing date in an array. |
51
|
|
|
**/ |
52
|
|
|
public function getPageById($id, $trashed = false) |
53
|
|
|
{ |
54
|
|
|
if ($trashed) { |
55
|
|
|
return Page::withTrashed()->find($id); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Page::find($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets only the id of the page that belongs to the given slug. |
63
|
|
|
* |
64
|
|
|
* @param string $slug The slug to search for in the database. |
65
|
|
|
* @return int The id of the page. |
66
|
|
|
**/ |
67
|
|
|
public function getPageId($slug) |
68
|
|
|
{ |
69
|
|
|
$query = Page::where('page_slug', '=', $slug)->withTrashed()->select('page_id')->first(); |
70
|
|
|
|
71
|
|
|
return $query->page_id; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a slug. |
76
|
|
|
* |
77
|
|
|
* @param string $slugify_this The piece of text to transform into a slug. |
78
|
|
|
* @return string A safe slug. |
79
|
|
|
**/ |
80
|
|
|
public function createSlug($slugify_this) |
81
|
|
|
{ |
82
|
|
|
return str_slug($slugify_this); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds a page to the database. |
87
|
|
|
* |
88
|
|
|
* @param string $page_title The title of the page. |
89
|
|
|
* @param text $page_content The content of the page. |
90
|
|
|
* @param string|null $custom_slug A custom slug, if not provided the page title is slugified. |
91
|
|
|
* @return void The page is saved. |
92
|
|
|
**/ |
93
|
|
|
public function addPage($page_title, $page_content, $custom_slug = null) |
94
|
|
|
{ |
95
|
|
|
$newPage = new Page; |
96
|
|
|
$newPage->page_title = $page_title; |
|
|
|
|
97
|
|
|
$newPage->page_content = $page_content; |
|
|
|
|
98
|
|
|
if (is_null($custom_slug) or empty($custom_slug)) { |
99
|
|
|
$newPage->page_slug = $this->createSlug($page_title); |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$newPage->page_slug = $this->createSlug($custom_slug); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $newPage->save(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Updates an existing page. |
109
|
|
|
* |
110
|
|
|
* @param int $page_id The id of the existing page. |
111
|
|
|
* @param string $page_title The (changed) title of the page. |
112
|
|
|
* @param text $page_content The (changed) content of the page. |
113
|
|
|
* @param string $page_slug The (changed) slug of the page. |
114
|
|
|
* @return void The page is saved. |
115
|
|
|
**/ |
116
|
|
|
public function updatePage($page_id, $page_title, $page_content, $page_slug) |
117
|
|
|
{ |
118
|
|
|
$page = Page::find($page_id); |
119
|
|
|
$page->page_title = $page_title; |
120
|
|
|
$page->page_content = $page_content; |
121
|
|
|
$page->page_slug = $this->createSlug($page_slug); |
122
|
|
|
$page->touch(); |
123
|
|
|
|
124
|
|
|
return $page->save(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Deletes a page, possibly even with force. |
129
|
|
|
* |
130
|
|
|
* If $forceDelete is set to true, the page will be removed from the database (for ever). |
131
|
|
|
* If kept at false, the page will get a 'deleted_at' value and does not show until restored. |
132
|
|
|
* |
133
|
|
|
* @param int $page_id The id of the page. |
134
|
|
|
* @param bool $forceDelete Whether to really remove the page from the database or not. |
135
|
|
|
* @return void The page is deleted. |
136
|
|
|
**/ |
137
|
|
|
public function deletePage($page_id, $forceDelete = false) |
138
|
|
|
{ |
139
|
|
|
$page = Page::withTrashed()->find($page_id); |
140
|
|
|
|
141
|
|
|
if ($forceDelete) { |
142
|
|
|
return $page->forceDelete($page_id); |
143
|
|
|
} else { |
144
|
|
|
return $page->delete($page_id); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Restores a previously soft-deleted page. |
150
|
|
|
* |
151
|
|
|
* The id of the page can be retrieved with the getPageId() function. |
152
|
|
|
* |
153
|
|
|
* @param int $page_id The id of the page. |
154
|
|
|
* @return void The page is back! |
155
|
|
|
**/ |
156
|
|
|
public function restorePage($page_id) |
157
|
|
|
{ |
158
|
|
|
$page = Page::withTrashed()->find($page_id); |
159
|
|
|
|
160
|
|
|
return $page->restore($page_id); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.