PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Database / Entity / Document.php

Document.php in Depicter — Popup & Slider Builder trunk, at app/src/Database/Entity/Document.php

283 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Database\Entity;
3
4 use Averta\WordPress\Database\Entity\Model;
5 use Averta\WordPress\Utility\Sanitize;
6
7 class Document extends Model
8 {
9 protected $idColumn = 'id';
10
11 /**
12 * Resource name.
13 *
14 * @var string
15 */
16 protected $resource = 'depicter_documents';
17
18 protected $routeResource = 'depicter-documents';
19
20 /**
21 * Determines what fields can be saved without be explicitly.
22 *
23 * @var array
24 */
25 protected $builtin = [
26 'id',
27 'name',
28 'slug',
29 'type',
30 'author',
31 'sections_count',
32 'created_at',
33 'modified_at',
34 'thumbnail',
35 'content',
36 'status',
37 'parent',
38 'password'
39 ];
40
41 protected $guard = [
42 'id'
43 ];
44
45 protected $private = [
46 'password'
47 ];
48
49 protected $cast = [];
50
51 protected $format = [
52 'name' => 'sanitizeName',
53 'slug' => 'sanitizeSlug',
54 'modified_at' => 'currentDateTime'
55 ];
56
57 /**
58 * Determines what fields should be updated automatically.
59 *
60 * @var array
61 */
62 protected $autoFill = [
63 'modified_at' => ''
64 ];
65
66
67 public function sanitizeName( $value ) {
68 return Sanitize::plaintext( $value );
69 }
70
71 public function sanitizeSlug( $value ) {
72 $value = $value ?? 'document 1';
73 return Sanitize::slug( $value );
74 }
75
76 public function currentDateTime( $value ) {
77 return gmdate('Y-m-d H:i:s', time());
78 }
79
80 /**
81 * Renames the document
82 *
83 * @param string $newName
84 *
85 * @return string|int|null
86 */
87 public function rename( $newName )
88 {
89 return $this->save( ['name' => $newName] );
90 }
91
92 /**
93 * Changes the document slug
94 *
95 * @param string $newSlug
96 *
97 * @return string|int|null
98 */
99 public function changeSlug( $newSlug )
100 {
101 return $this->save( ['slug' => Sanitize::slug( $newSlug )] );
102 }
103
104 /**
105 * Get User ID
106 *
107 * @return string|int|null
108 */
109 public function getUserID()
110 {
111 return $this->properties['author'] ?? null;
112 }
113
114 /**
115 * Get parent ID
116 *
117 * @return string|int|null
118 */
119 public function parent()
120 {
121 return $this->properties['parent'] ?? null;
122 }
123
124 /**
125 * Author ID
126 *
127 * @return int
128 */
129 public function author()
130 {
131 return $this->properties['author'] ?? 0;
132 }
133
134 /**
135 * @param mixed|null $value
136 *
137 * @return bool
138 */
139 public function getIsPublishedProperty($value = null)
140 {
141 return (bool) ( $value ?? $this->status == 'publish' );
142 }
143
144 /**
145 * Draft
146 *
147 * @return $this
148 */
149 public function draft()
150 {
151 return $this->where('status', 'draft');
152 }
153
154 /**
155 * Published
156 *
157 * @return $this
158 */
159 public function published()
160 {
161 return $this->where('status', 'publish');
162 }
163
164 /**
165 * Editor data
166 *
167 * @return $this
168 */
169 public function content()
170 {
171 $properties = $this->getProperties();
172
173 if( isset( $properties['content'] ) ){
174 return $properties['content'];
175 }
176 return null;
177 }
178
179 /**
180 * Status
181 *
182 * @param string $type
183 *
184 * @return $this
185 */
186 public function status($type)
187 {
188 return $this->where('status', $type);
189 }
190
191 /**
192 * Query parent or revision documents
193 *
194 * @param string $revisionOrParent Get revision or parent documents only
195 *
196 * @return $this
197 */
198 public function parents( $revisionOrParent = 0 )
199 {
200 return $this->where('parent', $revisionOrParent );
201 }
202
203 /**
204 * Get public properties for API
205 *
206 * @return array
207 * @throws \Exception
208 */
209 public function getApiProperties()
210 {
211 $properties = $this->getProperties();
212
213 unset( $properties['parent'] );
214 unset( $properties['password'] );
215
216 $properties['publishedAt'] = $this->getLastPublishedAt();
217 $properties['thumbnail'] = '';
218
219 $uploadDir = wp_upload_dir();
220 $thumbnailsBaseDir = $uploadDir['basedir'] . '/depicter/preview-images/';
221 $thumbnailsBaseURL = $uploadDir['baseurl'] . '/depicter/preview-images/';
222
223 $properties['previewImage' ] = is_file( $thumbnailsBaseDir . $this->getID() . '.png' ) ? $thumbnailsBaseURL . $this->getID() . '.png' : '';
224 $properties['sectionThumbnails'] = [];
225 $i = 1;
226 while(1){
227 $fileName = $this->getID() . '-'. $i . '.png';
228 if( ! is_file( $thumbnailsBaseDir . $fileName ) ){
229 break;
230 }
231 $properties['sectionThumbnails'][] = $thumbnailsBaseURL . $fileName;
232 $i++;
233 }
234
235 return $properties;
236 }
237
238 /**
239 * Get latest publish date time for document
240 *
241 * @return mixed
242 * @throws \Exception
243 */
244 public function getLastPublishedAt(){
245 return \Depicter::documentRepository()->getLastPublishedAt( $this->toArray() );
246 }
247
248 /**
249 * Joins meta table and reselects default or passed columns
250 *
251 * @return Document
252 */
253 public function withMeta() {
254 global $wpdb;
255 return $this->join(
256 $wpdb->prefix . 'depicter_meta',
257 $wpdb->prefix . 'depicter_meta.relation_id',
258 $wpdb->prefix . $this->resource . '.' . $this->idColumn
259 )->where($wpdb->prefix . 'depicter_meta.relation', 'document' );
260 }
261
262 /**
263 * Filters meta key and value
264 *
265 * @throws Document
266 */
267 public function whereMeta( $metaKey, $metaValue ){
268 global $wpdb;
269 return $this
270 ->where($wpdb->prefix . 'depicter_meta.meta_key', $metaKey )
271 ->where($wpdb->prefix . 'depicter_meta.meta_value', $metaValue );
272 }
273
274 /**
275 * Retrieves related meta field models
276 *
277 * @return Meta|\TypeRocket\Models\Model|null
278 */
279 public function meta(){
280 return $this->hasMany(Meta::class, 'relation_id', $this->idColumn );
281 }
282 }
283