PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Models / Conversation.php

Conversation.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Models/Conversation.php

210 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Attachment files first, then their rows
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 * @var array
52 */
53 protected $searchable = [
54 'content'
55 ];
56
57 /**
58 * @param ModelQueryBuilder $query
59 * @param string $search
60 * @return ModelQueryBuilder
61 */
62 public function scopeSearchBy($query, $search)
63 {
64 if ($search) {
65 $fields = $this->searchable;
66 $query->where(function ($query) use ($fields, $search) {
67 $query->where(array_shift($fields), 'LIKE', "%$search%");
68
69 foreach ($fields as $field) {
70 $query->orWhere($field, 'LIKE', "$search%");
71 }
72 });
73 }
74
75 return $query;
76 }
77
78 /**
79 * @param ModelQueryBuilder $query
80 * @param array $type
81 * @return ModelQueryBuilder
82 */
83 public function scopeFilterByType($query, $type)
84 {
85 $query->whereIn('conversation_type', $type);
86
87 return $query;
88 }
89
90 /**
91 * @return Model Collection
92 */
93 public function ticket()
94 {
95 $class = __NAMESPACE__ . '\Ticket';
96
97 return $this->belongsTo(
98 $class, 'ticket_id', 'id'
99 );
100 }
101
102 /**
103 * @return Model Collection
104 */
105 public function person()
106 {
107 $class = __NAMESPACE__ . '\Person';
108
109 return $this->belongsTo(
110 $class, 'person_id', 'id'
111 );
112 }
113
114 /**
115 * Alias of person(). The author may be an agent, not only a customer.
116 *
117 * @return \FluentSupport\App\Models\Model
118 */
119 public function customer()
120 {
121 return $this->person();
122 }
123
124 public function attachments()
125 {
126 $class = __NAMESPACE__ . '\Attachment';
127 return $this->hasMany($class, 'conversation_id', 'id');
128 }
129
130 /**
131 * CC info stored in fs_meta.
132 *
133 * @return Model Collection
134 */
135 public function ccinfo()
136 {
137 $class = __NAMESPACE__ . '\Meta';
138
139 return $this->hasOne(
140 $class, 'object_id', 'id'
141 )->where('object_type', 'response')->where('key', 'settings');
142 }
143
144
145 public function getSettingsValue($valueKey = false, $default = false)
146 {
147 $exist = Meta::where('object_type', 'response')
148 ->where('key', 'settings')
149 ->where('object_id', $this->id)
150 ->first();
151
152 if ($exist) {
153 $value = Helper::safeUnserialize($exist->value);
154 if ($valueKey) {
155 if (!is_array($value)) {
156 return $default;
157 }
158 return Arr::get($value, $valueKey, $default);
159 }
160 return $value;
161 }
162
163 return $default;
164 }
165
166 public function updateSettingsValue($valueKey, $value)
167 {
168 $exist = Meta::where('object_type', 'response')
169 ->where('key', 'settings')
170 ->where('object_id', $this->id)
171 ->first();
172
173 if ($exist) {
174 $existingValue = Helper::safeUnserialize($exist->value);
175
176 if (!is_array($existingValue)) {
177 $existingValue = [];
178 }
179
180 $existingValue[$valueKey] = $value;
181
182 $exist->value = maybe_serialize($existingValue);
183 $exist->save();
184 return $this;
185 }
186
187 $settings = [
188 'object_type' => 'response',
189 'key' => 'settings',
190 'object_id' => $this->id,
191 'value' => maybe_serialize([
192 $valueKey => $value
193 ])
194 ];
195
196 Meta::create($settings);
197
198 return $this;
199
200 }
201
202 public static function deleteAll($ticketId){
203 $conversations = Conversation::where('ticket_id', $ticketId)->get();
204 foreach ($conversations as $conversation) {
205 $conversation->delete();
206 }
207 }
208
209 }
210