-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Methods that admin models should have | ||
*/ | ||
|
||
namespace Javaabu\Helpers\AdminModel; | ||
|
||
interface AdminModel | ||
{ | ||
/** | ||
* Get the admin url | ||
* @return string | ||
*/ | ||
public function getAdminUrlAttribute(); | ||
|
||
/** | ||
* Get the admin link | ||
* @return string | ||
*/ | ||
public function getAdminLinkAttribute(); | ||
|
||
/** | ||
* Get the name to be displayed on the admin link | ||
* @return string | ||
*/ | ||
public function getAdminLinkNameAttribute(); | ||
|
||
/** | ||
* Get can view admin link | ||
* | ||
* @return boolean | ||
*/ | ||
public function canViewAdminLink(); | ||
|
||
/** | ||
* Get the name for log | ||
* @return string | ||
*/ | ||
public function getLoggingNameAttribute(); | ||
|
||
/** | ||
* Get the log url | ||
* @return string | ||
*/ | ||
public function getLogUrlAttribute(); | ||
|
||
/** | ||
* Get the causer log url | ||
* @return string | ||
*/ | ||
public function getCauserLogUrlAttribute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Methods that admin models should have | ||
*/ | ||
|
||
namespace Javaabu\Helpers\AdminModel; | ||
|
||
trait IsAdminModel | ||
{ | ||
/** | ||
* Get admin user attribute | ||
* | ||
* @return string | ||
*/ | ||
public function getAdminLinkAttribute() | ||
{ | ||
if ($this->canViewAdminLink()) { | ||
$admin_url = $this->admin_url; | ||
} else { | ||
$admin_url = ''; | ||
} | ||
|
||
$before = $admin_url ? '<a href="' . e($admin_url) . '">' : ''; | ||
$after = $admin_url ? '</a>' : ''; | ||
|
||
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Simple trait for models with excerpts | ||
*/ | ||
|
||
namespace Javaabu\Helpers\Traits; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait HasExcerpt | ||
{ | ||
/** | ||
* Get the excerpt attribute | ||
* @return string | ||
*/ | ||
public function getExcerptAttribute() | ||
{ | ||
return $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Simple trait for formatted ids | ||
*/ | ||
|
||
namespace Javaabu\Helpers\Traits; | ||
|
||
trait HasFormattedId | ||
{ | ||
/** | ||
* Get the id prefix | ||
* @return string | ||
*/ | ||
public function getIdPrefixAttribute() | ||
{ | ||
return 'ID'; | ||
} | ||
|
||
/** | ||
* Get the formatted id | ||
* @return string | ||
*/ | ||
public function getFormattedIdAttribute() | ||
{ | ||
return $this->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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/** | ||
* Simple trait to set slug field | ||
*/ | ||
|
||
namespace Javaabu\Helpers\Traits; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
trait HasSlug | ||
{ | ||
/** | ||
* Boot function from laravel. | ||
*/ | ||
public static function bootHasSlug() | ||
{ | ||
static::saving(function ($model) { | ||
$field = $model->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)); | ||
} | ||
} |
Oops, something went wrong.