| @@ -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,15 +25,30 @@ | ||
| 22 | 25 | ]; |
| 23 | 26 | |
| 24 | 27 | public static function boot() |
| 25 | 28 | { |
| 29 | + parent::boot(); | |
| 30 | + | |
| 26 | 31 | static::creating(function ($model) { |
| 27 | - $model->content_hash = md5($model->content); | |
| 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'); | |
| 28 | 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 | + }); | |
| 29 | 48 | } |
| 30 | 49 | |
| 31 | 50 | /** |
| 32 | - * $searchable Columns in table to search | |
| 33 | 51 | * @var array |
| 34 | 52 | */ |
| 35 | 53 | protected $searchable = [ |
| 36 | 54 | 'content' |
| @@ -36,9 +54,8 @@ | ||
| 36 | 54 | 'content' |
| 37 | 55 | ]; |
| 38 | 56 | |
| 39 | 57 | /** |
| 40 | - * Local scope to filter subscribers by search/query string | |
| 41 | 58 | * @param ModelQueryBuilder $query |
| 42 | 59 | * @param string $search |
| 43 | 60 | * @return ModelQueryBuilder |
| 44 | 61 | */ |
| @@ -58,22 +75,20 @@ | ||
| 58 | 75 | return $query; |
| 59 | 76 | } |
| 60 | 77 | |
| 61 | 78 | /** |
| 62 | - * Local scope to filter subscribers by search/query string | |
| 63 | 79 | * @param ModelQueryBuilder $query |
| 64 | - * @param string $type | |
| 80 | + * @param array $type | |
| 65 | 81 | * @return ModelQueryBuilder |
| 66 | 82 | */ |
| 67 | 83 | public function scopeFilterByType($query, $type) |
| 68 | 84 | { |
| 69 | - $query->where('conversation_type', $type); | |
| 85 | + $query->whereIn('conversation_type', $type); | |
| 70 | 86 | |
| 71 | 87 | return $query; |
| 72 | 88 | } |
| 73 | 89 | |
| 74 | 90 | /** |
| 75 | - * One2Many: Customer has to many Click Tickets | |
| 76 | 91 | * @return Model Collection |
| 77 | 92 | */ |
| 78 | 93 | public function ticket() |
| 79 | 94 | { |
| @@ -84,9 +99,8 @@ | ||
| 84 | 99 | ); |
| 85 | 100 | } |
| 86 | 101 | |
| 87 | 102 | /** |
| 88 | - * One2Many: Customer has to many Click Tickets | |
| 89 | 103 | * @return Model Collection |
| 90 | 104 | */ |
| 91 | 105 | public function person() |
| 92 | 106 | { |
| @@ -97,9 +111,9 @@ | ||
| 97 | 111 | ); |
| 98 | 112 | } |
| 99 | 113 | |
| 100 | 114 | /** |
| 101 | - * A Conversation belongs to a Customer. | |
| 115 | + * Alias of person(). The author may be an agent, not only a customer. | |
| 102 | 116 | * |
| 103 | 117 | * @return \FluentSupport\App\Models\Model |
| 104 | 118 | */ |
| 105 | 119 | public function customer() |
| @@ -110,7 +124,86 @@ | ||
| 110 | 124 | public function attachments() |
| 111 | 125 | { |
| 112 | 126 | $class = __NAMESPACE__ . '\Attachment'; |
| 113 | 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 | + } | |
| 114 | 207 | } |
| 115 | 208 | |
| 116 | 209 | } |