| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
class Conversation extends Model |
| 6 |
{ |
| 7 |
protected $table = 'fs_conversations'; |
| 8 |
|
| 9 |
/** |
| 10 |
* The attributes that are mass assignable. |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
protected $fillable = [ |
| 15 |
'ticket_id', |
| 16 |
'person_id', |
| 17 |
'message_id', |
| 18 |
'conversation_type', |
| 19 |
'content', |
| 20 |
'source', |
| 21 |
'is_important' |
| 22 |
]; |
| 23 |
|
| 24 |
public static function boot() |
| 25 |
{ |
| 26 |
static::creating(function ($model) { |
| 27 |
if(empty($model->content_hash)) { |
| 28 |
$model->content_hash = md5($model->content); |
| 29 |
} |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* $searchable Columns in table to search |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected $searchable = [ |
| 38 |
'content' |
| 39 |
]; |
| 40 |
|
| 41 |
/** |
| 42 |
* Local scope to filter subscribers by search/query string |
| 43 |
* @param ModelQueryBuilder $query |
| 44 |
* @param string $search |
| 45 |
* @return ModelQueryBuilder |
| 46 |
*/ |
| 47 |
public function scopeSearchBy($query, $search) |
| 48 |
{ |
| 49 |
if ($search) { |
| 50 |
$fields = $this->searchable; |
| 51 |
$query->where(function ($query) use ($fields, $search) { |
| 52 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 53 |
|
| 54 |
foreach ($fields as $field) { |
| 55 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 56 |
} |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
return $query; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Local scope to filter subscribers by search/query string |
| 65 |
* @param ModelQueryBuilder $query |
| 66 |
* @param string $type |
| 67 |
* @return ModelQueryBuilder |
| 68 |
*/ |
| 69 |
public function scopeFilterByType($query, $type) |
| 70 |
{ |
| 71 |
$query->where('conversation_type', $type); |
| 72 |
|
| 73 |
return $query; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* One2Many: Customer has to many Click Tickets |
| 78 |
* @return Model Collection |
| 79 |
*/ |
| 80 |
public function ticket() |
| 81 |
{ |
| 82 |
$class = __NAMESPACE__ . '\Ticket'; |
| 83 |
|
| 84 |
return $this->belongsTo( |
| 85 |
$class, 'ticket_id', 'id' |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* One2Many: Customer has to many Click Tickets |
| 91 |
* @return Model Collection |
| 92 |
*/ |
| 93 |
public function person() |
| 94 |
{ |
| 95 |
$class = __NAMESPACE__ . '\Person'; |
| 96 |
|
| 97 |
return $this->belongsTo( |
| 98 |
$class, 'person_id', 'id' |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* A Conversation belongs to a Customer. |
| 104 |
* |
| 105 |
* @return \FluentSupport\App\Models\Model |
| 106 |
*/ |
| 107 |
public function customer() |
| 108 |
{ |
| 109 |
return $this->person(); |
| 110 |
} |
| 111 |
|
| 112 |
public function attachments() |
| 113 |
{ |
| 114 |
$class = __NAMESPACE__ . '\Attachment'; |
| 115 |
return $this->hasMany($class, 'conversation_id', 'id'); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|