| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
class TicketTag extends Tag |
| 6 |
{ |
| 7 |
protected static $type = 'ticket_tag'; |
| 8 |
|
| 9 |
public static function boot() |
| 10 |
{ |
| 11 |
parent::boot(); |
| 12 |
|
| 13 |
static::creating(function ($model) { |
| 14 |
$model->tag_type = static::$type; |
| 15 |
if(empty($model->created_by) && $userId = get_current_user_id()) { |
| 16 |
$model->created_by = $userId; |
| 17 |
} |
| 18 |
|
| 19 |
$model->slug = static::slugify($model->title); |
| 20 |
}); |
| 21 |
|
| 22 |
static::addGlobalScope(function ($builder) { |
| 23 |
$builder->where('tag_type', static::$type); |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
public static function slugify($title) |
| 29 |
{ |
| 30 |
$slug = sanitize_title($title, 'ticket-tag', 'display'); |
| 31 |
if (TicketTag::where('slug', $slug)->first()) { |
| 32 |
$slug .= '-' . time(); |
| 33 |
} |
| 34 |
return $slug; |
| 35 |
} |
| 36 |
|
| 37 |
public function tickets() |
| 38 |
{ |
| 39 |
return $this->belongsToMany( |
| 40 |
__NAMESPACE__.'\Ticket', 'fs_tag_pivot', 'tag_id', 'source_id' |
| 41 |
)->where('source_type', 'ticket_tag'); |
| 42 |
} |
| 43 |
|
| 44 |
} |
| 45 |
|