diff --git a/src/AdminModel/AdminModel.php b/src/AdminModel/AdminModel.php new file mode 100644 index 0000000..2472cac --- /dev/null +++ b/src/AdminModel/AdminModel.php @@ -0,0 +1,52 @@ +canViewAdminLink()) { + $admin_url = $this->admin_url; + } else { + $admin_url = ''; + } + + $before = $admin_url ? '' : ''; + $after = $admin_url ? '' : ''; + + return $before . e($this->admin_link_name) . $after; + } + + /** + * Get can view admin link + * + * @return boolean + */ + public function canViewAdminLink() + { + return auth()->check() && auth()->user()->can('view', $this); + } + + /** + * Get the name for the admin link + * + * @return string + */ + public function getAdminLinkNameAttribute() + { + return $this->name; + } + + /** + * Get the name for log + * + * @return string + */ + public function getLoggingNameAttribute() + { + return $this->admin_link_name; + } + + /** + * Get the log url + * + * @return string + */ + public function getLogUrlAttribute() + { + return add_query_arg([ + 'subject_type' => $this->getMorphClass(), + 'subject_id' => $this->id, + ], route('admin.logs.index')); + } + + /** + * Get the causer log url + * + * @return string + */ + public function getCauserLogUrlAttribute() + { + return add_query_arg([ + 'causer_type' => $this->getMorphClass(), + 'causer_id' => $this->id, + ], route('admin.logs.index')); + } +} diff --git a/src/Traits/HasExcerpt.php b/src/Traits/HasExcerpt.php new file mode 100644 index 0000000..2e65918 --- /dev/null +++ b/src/Traits/HasExcerpt.php @@ -0,0 +1,54 @@ +getExcerpt( + $this->getExcerptLength() + ); + } + + /** + * Get the excerpt attribute + * + * @param int $length + * @return string + */ + public function getExcerpt($length = 200) + { + return Str::limit( + strip_tags($this->{$this->getExcerptField()}), + $length + ); + } + + /** + * Get the excerpt field + * @return string + */ + public function getExcerptField() + { + return 'description'; + } + + /** + * Get the default excerpt length + * @return int + */ + public function getExcerptLength() + { + return 200; + } +} diff --git a/src/Traits/HasFormattedId.php b/src/Traits/HasFormattedId.php new file mode 100644 index 0000000..e6e901d --- /dev/null +++ b/src/Traits/HasFormattedId.php @@ -0,0 +1,48 @@ +id_prefix.str_pad($this->id, 6, '0', STR_PAD_LEFT); + } + + /** + * Extract id from formatted id + * @param $formatted_id + * @return string + */ + public function extractId($formatted_id) + { + return intval(preg_replace('/[^0-9]/', '', $formatted_id)); + } + + /** + * Find by formatted id + * @param $query + * @param $formatted_id + * @return mixed + */ + public function scopeByFormattedId($query, $formatted_id) + { + return $query->whereId($this->extractId($formatted_id)); + } +} diff --git a/src/Traits/HasSlug.php b/src/Traits/HasSlug.php new file mode 100644 index 0000000..c6f9f25 --- /dev/null +++ b/src/Traits/HasSlug.php @@ -0,0 +1,135 @@ +getSlugKey(); + $model->{$field} = $model->getUniqueSlug($model->{$field}); + }); + } + + /** + * Get the slug field name + * @return string + */ + public function getSlugKey() + { + return 'slug'; + } + + /** + * Get the name field name + * @return string + */ + public function getSlugNameKey() + { + return 'name'; + } + + /** + * Get the slug separator + * @return string + */ + public function getSlugSeparator() + { + return '-'; + } + + /** + * Slugify the value + * @param $value + * @return string + */ + public function slugify($value) + { + //convert to slug + return Str::slug($value, $this->getSlugSeparator()); + } + + /** + * Get the slug for the value + * @param $value + * @return string + */ + public function getSlug($value) + { + // default to name, if the slug + // is not provided + if (empty($value)) { + $value = $this->{$this->getSlugNameKey()}; + } + + return $this->slugify($value); + } + + /** + * Find a unique slug + * + * @param string $value + * @return string + */ + public function getUniqueSlug($value) + { + + //convert to slug + $value = $this->getSlug($value); + $separator = $this->getSlugSeparator(); + $suffix = ''; + $count = 0; + + //find a unique slug + while (! $this->isUniqueSlug($value . $suffix)) { + $count++; + $suffix = $separator . $count; + } + + return $value . $suffix; + } + + /** + * Check if slug is unique + * + * @param string $value + * @return bool + */ + public function isUniqueSlug($value) + { + + //check if slug exists + $id_key = $this->getKeyName(); + $count = static::where($this->getSlugKey(), $value); + + if ($this->hasSoftDelete()) { + $count->withTrashed(); + } + + if ($id = $this->{$id_key}) { + $count->where($id_key, '!=', $id); + } + + return ! $count->exists(); + } + + /** + * Check if is a soft deleting model + * @return boolean + */ + public function hasSoftDelete() + { + // ... check if 'this' model uses the soft deletes trait + return in_array(SoftDeletes::class, class_uses_recursive($this)); + } +} diff --git a/src/Traits/HasStatus.php b/src/Traits/HasStatus.php new file mode 100644 index 0000000..0a12ca0 --- /dev/null +++ b/src/Traits/HasStatus.php @@ -0,0 +1,62 @@ +getStatusLabel($this->status); + } + + /** + * Get the status slug + * @return string + */ + public function getStatusSlug($status) + { + $status_class = static::$status_class; + return $status_class::getSlug($status); + } + + /** + * Get status slug attribute + * @return string + */ + public function getStatusSlugAttribute() + { + return $this->getStatusSlug($this->status); + } + + /** + * Update the status + * + * @param $status | desired status + * @param bool $save | whether to save + */ + public function updateStatus($status, $save = false) + { + $this->status = $status; + + if ($save) { + $this->save(); + } + } +} diff --git a/src/Traits/Publishable.php b/src/Traits/Publishable.php new file mode 100644 index 0000000..93c66c4 --- /dev/null +++ b/src/Traits/Publishable.php @@ -0,0 +1,206 @@ +status == $this->getPublishedKey(); + } + + /** + * Checks if pending + * @return bool + */ + public function getIsPendingAttribute() + { + return $this->status == $this->getPendingKey(); + } + + /** + * Checks if draft + * @return bool + */ + public function getIsDraftAttribute() + { + return $this->status == $this->getDraftKey(); + } + + /** + * Checks if rejected + * @return bool + */ + public function getIsRejectedAttribute() + { + return $this->status == $this->getRejectedKey(); + } + + /** + * A published scope + * @param $query + * @return mixed + */ + public function scopePublished($query) + { + return $query->where($this->getTable().'.status', '=', $this->getPublishedKey()); + } + + /** + * A draft scope + * @param $query + * @return mixed + */ + public function scopeDraft($query) + { + return $query->where($this->getTable().'.status', '=', $this->getDraftKey()); + } + + /** + * A pending scope + * @param $query + * @return mixed + */ + public function scopePending($query) + { + return $query->where($this->getTable().'.status', '=', $this->getPendingKey()); + } + + /** + * A rejected scope + * @param $query + * @return mixed + */ + public function scopeRejected($query) + { + return $query->where($this->getTable().'.status', '=', $this->getRejectedKey()); + } + + /** + * Return the published key + * @return int + */ + public function getPublishedKey() + { + $status_class = static::$status_class; + return $status_class::PUBLISHED; + } + + /** + * Return the pending key + * @return int + */ + public function getPendingKey() + { + $status_class = static::$status_class; + return $status_class::PENDING; + } + + /** + * Return the draft key + * @return int + */ + public function getDraftKey() + { + $status_class = static::$status_class; + return $status_class::DRAFT; + } + + /** + * Return the rejected key + * @return int + */ + public function getRejectedKey() + { + $status_class = static::$status_class; + return $status_class::REJECTED; + } + + /** + * Update the status + * + * @param $status | desired status + * @param bool $publish | send for approving + * @return void + */ + public function updateStatus($status, $publish = false) + { + //first check if requesting for publishing + if ($publish || $status == $this->getPublishedKey()) { + $this->publish(); + } elseif ($status == $this->getRejectedKey()) { + $this->reject(); + } elseif ($status && auth()->check() && auth()->user()->can('publish', static::class)) { + $this->status = $status; + } else { + $this->draft(); + } + } + + /** + * Publish the post + * @return void + */ + public function publish() + { + if ($user = auth()->user()) { + $this->status = $user->can('publish', static::class) ? $this->getPublishedKey() + : $this->getPendingKey(); + } + } + + /** + * Reject the post + * @return void + */ + public function reject() + { + if ($user = auth()->user()) { + $this->status = $user->can('publish', static::class) ? $this->getRejectedKey() + : $this->getDraftKey(); + } + } + + /** + * Reject the post + * @return void + */ + public function draft() + { + $this->status = $this->getDraftKey(); + } + + /** + * User visible + * + * @param $query + * @return mixed + */ + public function scopeUserVisible($query) + { + $admin = auth()->user() instanceof User ? + auth()->user() : + auth()->guard('web_admin')->user(); + + if ($admin) { + if ($admin->can('create', static::class)) { + //can view all + return $query; + } + } + + // everyone can view published + return $query->published(); + } +}