| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
|
| 8 |
class Conversation extends Model |
| 9 |
{ |
| 10 |
protected $table = 'fs_conversations'; |
| 11 |
|
| 12 |
/** |
| 13 |
* The attributes that are mass assignable. |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected $fillable = [ |
| 18 |
'ticket_id', |
| 19 |
'person_id', |
| 20 |
'message_id', |
| 21 |
'conversation_type', |
| 22 |
'content', |
| 23 |
'source', |
| 24 |
'is_important' |
| 25 |
]; |
| 26 |
|
| 27 |
public static function boot() |
| 28 |
{ |
| 29 |
parent::boot(); |
| 30 |
|
| 31 |
static::creating(function ($model) { |
| 32 |
if(empty($model->content_hash)) { |
| 33 |
$model->content_hash = md5($model->content); |
| 34 |
} |
| 35 |
$model->created_at = current_time('mysql'); |
| 36 |
$model->updated_at = current_time('mysql'); |
| 37 |
}); |
| 38 |
|
| 39 |
static::deleting(function ($model) { |
| 40 |
//Delete cc info |
| 41 |
Meta::where('object_type', 'response')->where('object_id', $model->id)->delete(); |
| 42 |
// Delete conversation-level attachments — files first, then DB records |
| 43 |
$class = __NAMESPACE__ . '\Attachment'; |
| 44 |
$attachments = $class::where('conversation_id', $model->id)->get(); |
| 45 |
$class::purgeAttachments($attachments); |
| 46 |
$class::where('conversation_id', $model->id)->delete(); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* $searchable Columns in table to search |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
protected $searchable = [ |
| 55 |
'content' |
| 56 |
]; |
| 57 |
|
| 58 |
/** |
| 59 |
* Local scope to filter subscribers by search/query string |
| 60 |
* @param ModelQueryBuilder $query |
| 61 |
* @param string $search |
| 62 |
* @return ModelQueryBuilder |
| 63 |
*/ |
| 64 |
public function scopeSearchBy($query, $search) |
| 65 |
{ |
| 66 |
if ($search) { |
| 67 |
$fields = $this->searchable; |
| 68 |
$query->where(function ($query) use ($fields, $search) { |
| 69 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 70 |
|
| 71 |
foreach ($fields as $field) { |
| 72 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 73 |
} |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
return $query; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Local scope to filter subscribers by search/query string |
| 82 |
* @param ModelQueryBuilder $query |
| 83 |
* @param string $type |
| 84 |
* @return ModelQueryBuilder |
| 85 |
*/ |
| 86 |
public function scopeFilterByType($query, $type) |
| 87 |
{ |
| 88 |
$query->whereIn('conversation_type', $type); |
| 89 |
|
| 90 |
return $query; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* One2Many: Customer has to many Click Tickets |
| 95 |
* @return Model Collection |
| 96 |
*/ |
| 97 |
public function ticket() |
| 98 |
{ |
| 99 |
$class = __NAMESPACE__ . '\Ticket'; |
| 100 |
|
| 101 |
return $this->belongsTo( |
| 102 |
$class, 'ticket_id', 'id' |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* One2Many: Customer has to many Click Tickets |
| 108 |
* @return Model Collection |
| 109 |
*/ |
| 110 |
public function person() |
| 111 |
{ |
| 112 |
$class = __NAMESPACE__ . '\Person'; |
| 113 |
|
| 114 |
return $this->belongsTo( |
| 115 |
$class, 'person_id', 'id' |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* A Conversation belongs to a Customer. |
| 121 |
* |
| 122 |
* @return \FluentSupport\App\Models\Model |
| 123 |
*/ |
| 124 |
public function customer() |
| 125 |
{ |
| 126 |
return $this->person(); |
| 127 |
} |
| 128 |
|
| 129 |
public function attachments() |
| 130 |
{ |
| 131 |
$class = __NAMESPACE__ . '\Attachment'; |
| 132 |
return $this->hasMany($class, 'conversation_id', 'id'); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* One2One: Conversation has cc info |
| 137 |
* @return Model Collection |
| 138 |
*/ |
| 139 |
public function ccinfo() |
| 140 |
{ |
| 141 |
$class = __NAMESPACE__ . '\Meta'; |
| 142 |
|
| 143 |
return $this->hasOne( |
| 144 |
$class, 'object_id', 'id' |
| 145 |
)->where('object_type', 'response')->where('key', 'settings'); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
public function getSettingsValue($valueKey = false, $default = false) |
| 150 |
{ |
| 151 |
$exist = Meta::where('object_type', 'response') |
| 152 |
->where('key', 'settings') |
| 153 |
->where('object_id', $this->id) |
| 154 |
->first(); |
| 155 |
|
| 156 |
if ($exist) { |
| 157 |
$value = Helper::safeUnserialize($exist->value); |
| 158 |
if ($valueKey) { |
| 159 |
if (!is_array($value)) { |
| 160 |
return $default; |
| 161 |
} |
| 162 |
return Arr::get($value, $valueKey, $default); |
| 163 |
} |
| 164 |
return $value; |
| 165 |
} |
| 166 |
|
| 167 |
return $default; |
| 168 |
} |
| 169 |
|
| 170 |
public function updateSettingsValue($valueKey, $value) |
| 171 |
{ |
| 172 |
$exist = Meta::where('object_type', 'response') |
| 173 |
->where('key', 'settings') |
| 174 |
->where('object_id', $this->id) |
| 175 |
->first(); |
| 176 |
|
| 177 |
if ($exist) { |
| 178 |
$existingValue = Helper::safeUnserialize($exist->value); |
| 179 |
|
| 180 |
if (!is_array($existingValue)) { |
| 181 |
$existingValue = []; |
| 182 |
} |
| 183 |
|
| 184 |
$existingValue[$valueKey] = $value; |
| 185 |
|
| 186 |
$exist->value = maybe_serialize($existingValue); |
| 187 |
$exist->save(); |
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
$settings = [ |
| 192 |
'object_type' => 'response', |
| 193 |
'key' => 'settings', |
| 194 |
'object_id' => $this->id, |
| 195 |
'value' => maybe_serialize([ |
| 196 |
$valueKey => $value |
| 197 |
]) |
| 198 |
]; |
| 199 |
|
| 200 |
Meta::create($settings); |
| 201 |
|
| 202 |
return $this; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
public static function deleteAll($ticketId){ |
| 207 |
$conversations = Conversation::where('ticket_id', $ticketId)->get(); |
| 208 |
foreach ($conversations as $conversation) { |
| 209 |
$conversation->delete(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|