| 1 |
<?php |
| 2 |
namespace Depicter\Database\Entity; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\Sanitize; |
| 5 |
|
| 6 |
class Document extends Model |
| 7 |
{ |
| 8 |
protected $idColumn = 'id'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Resource name. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $resource = 'depicter_documents'; |
| 16 |
|
| 17 |
protected $routeResource = 'depicter-documents'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Determines what fields can be saved without be explicitly. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
protected $builtin = [ |
| 25 |
'id', |
| 26 |
'name', |
| 27 |
'slug', |
| 28 |
'type', |
| 29 |
'author', |
| 30 |
'sections_count', |
| 31 |
'created_at', |
| 32 |
'modified_at', |
| 33 |
'thumbnail', |
| 34 |
'content', |
| 35 |
'status', |
| 36 |
'parent', |
| 37 |
'password' |
| 38 |
]; |
| 39 |
|
| 40 |
protected $guard = [ |
| 41 |
'id' |
| 42 |
]; |
| 43 |
|
| 44 |
protected $private = [ |
| 45 |
'password' |
| 46 |
]; |
| 47 |
|
| 48 |
protected $cast = []; |
| 49 |
|
| 50 |
protected $format = [ |
| 51 |
'name' => 'static::sanitizeName', |
| 52 |
'slug' => 'static::sanitizeSlug', |
| 53 |
'modified_at' => 'static::currentDateTime' |
| 54 |
]; |
| 55 |
|
| 56 |
/** |
| 57 |
* Determines what fields should be updated automatically. |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
protected $autoFill = [ |
| 62 |
'modified_at' => '' |
| 63 |
]; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function sanitizeName( $value ) { |
| 68 |
return Sanitize::plaintext( $value ); |
| 69 |
} |
| 70 |
|
| 71 |
public static 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 |
* Get public properties for API |
| 193 |
* |
| 194 |
* @return array |
| 195 |
* @throws \Exception |
| 196 |
*/ |
| 197 |
public function getApiProperties() |
| 198 |
{ |
| 199 |
$properties = $this->getProperties(); |
| 200 |
|
| 201 |
unset( $properties['parent'] ); |
| 202 |
unset( $properties['password'] ); |
| 203 |
|
| 204 |
$properties['publishedAt'] = $this->getLastPublishedAt(); |
| 205 |
$properties['thumbnail'] = ''; |
| 206 |
|
| 207 |
$uploadDir = wp_upload_dir(); |
| 208 |
$thumbnailsBaseDir = $uploadDir['basedir'] . '/depicter/preview-images/'; |
| 209 |
$thumbnailsBaseURL = $uploadDir['baseurl'] . '/depicter/preview-images/'; |
| 210 |
|
| 211 |
$properties['previewImage' ] = is_file( $thumbnailsBaseDir . $this->getID() . '.png' ) ? $thumbnailsBaseURL . $this->getID() . '.png' : ''; |
| 212 |
$properties['sectionThumbnails'] = []; |
| 213 |
$i = 1; |
| 214 |
while(1){ |
| 215 |
$fileName = $this->getID() . '-'. $i . '.png'; |
| 216 |
if( ! is_file( $thumbnailsBaseDir . $fileName ) ){ |
| 217 |
break; |
| 218 |
} |
| 219 |
$properties['sectionThumbnails'][] = $thumbnailsBaseURL . $fileName; |
| 220 |
$i++; |
| 221 |
} |
| 222 |
|
| 223 |
return $properties; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get latest publish date time for document |
| 228 |
* |
| 229 |
* @return mixed |
| 230 |
* @throws \Exception |
| 231 |
*/ |
| 232 |
public function getLastPublishedAt(){ |
| 233 |
return \Depicter::documentRepository()->getLastPublishedAt( $this->toArray() ); |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|