| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Models; |
| 4 | 4 | |
| 5 | +use FluentSupport\App\Services\Helper; | |
| 6 | +use FluentSupport\Framework\Support\Arr; | |
| 7 | + | |
| 5 | 8 | class Conversation extends Model |
| 6 | 9 | { |
| 7 | 10 | protected $table = 'fs_conversations'; |
| 8 | 11 | |
| @@ -22,13 +25,27 @@ | ||
| 22 | 25 | ]; |
| 23 | 26 | |
| 24 | 27 | public static function boot() |
| 25 | 28 | { |
| 29 | + parent::boot(); | |
| 30 | + | |
| 26 | 31 | static::creating(function ($model) { |
| 27 | 32 | if(empty($model->content_hash)) { |
| 28 | 33 | $model->content_hash = md5($model->content); |
| 29 | 34 | } |
| 35 | + $model->created_at = current_time('mysql'); | |
| 36 | + $model->updated_at = current_time('mysql'); | |
| 30 | 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 | + }); | |
| 31 | 48 | } |
| 32 | 49 | |
| 33 | 50 | /** |
| 34 | 51 | * $searchable Columns in table to search |
| @@ -67,9 +84,9 @@ | ||
| 67 | 84 | * @return ModelQueryBuilder |
| 68 | 85 | */ |
| 69 | 86 | public function scopeFilterByType($query, $type) |
| 70 | 87 | { |
| 71 | - $query->where('conversation_type', $type); | |
| 88 | + $query->whereIn('conversation_type', $type); | |
| 72 | 89 | |
| 73 | 90 | return $query; |
| 74 | 91 | } |
| 75 | 92 | |
| @@ -112,7 +129,85 @@ | ||
| 112 | 129 | public function attachments() |
| 113 | 130 | { |
| 114 | 131 | $class = __NAMESPACE__ . '\Attachment'; |
| 115 | 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 | + } | |
| 116 | 211 | } |
| 117 | 212 | |
| 118 | 213 | } |