1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-24 11:22:42 +01:00
freescout/app/Conversation.php

2332 lines
71 KiB
PHP
Raw Normal View History

2018-07-14 03:23:37 +02:00
<?php
namespace App;
use App\Attachment;
2020-01-09 15:04:12 +01:00
use App\Customer;
use App\Mailbox;
use App\Folder;
use App\Follower;
2019-07-26 08:01:03 +02:00
use App\Thread;
use App\User;
2020-01-09 15:04:12 +01:00
use App\Events\UserAddedNote;
use App\Events\UserReplied;
2020-01-03 08:22:39 +01:00
use App\Events\ConversationStatusChanged;
use App\Events\ConversationUserChanged;
2018-08-29 08:45:15 +02:00
use App\Events\ConversationCustomerChanged;
2018-07-14 03:23:37 +02:00
use Illuminate\Database\Eloquent\Model;
2018-08-10 16:28:35 +02:00
use Illuminate\Support\Facades\Input;
2019-06-24 07:39:30 +02:00
use Watson\Rememberable\Rememberable;
2018-07-14 03:23:37 +02:00
class Conversation extends Model
{
2019-06-24 07:39:30 +02:00
use Rememberable;
// This is obligatory.
public $rememberCacheDriver = 'array';
2018-09-16 12:52:52 +02:00
/**
* Max length of the preview.
*/
const PREVIEW_MAXLENGTH = 255;
/**
* Default subject length.
*/
const SUBJECT_LENGTH = 80;
2018-09-16 12:52:52 +02:00
/**
* Conversation reply undo timeout in seconds.
* Value has to be larger than close_after in fsFloatingAlertsInit.
*/
const UNDO_TIMOUT = 15;
2018-07-24 08:34:28 +02:00
/**
* By whom action performed (used in fields: source_via, last_reply_from).
*/
2018-07-14 03:23:37 +02:00
const PERSON_CUSTOMER = 1;
const PERSON_USER = 2;
2018-07-24 08:34:28 +02:00
public static $persons = [
2018-07-14 03:23:37 +02:00
self::PERSON_CUSTOMER => 'customer',
2018-07-24 08:34:28 +02:00
self::PERSON_USER => 'user',
];
2018-07-14 03:23:37 +02:00
2018-07-24 08:34:28 +02:00
/**
* Conversation types.
*/
2018-07-14 03:23:37 +02:00
const TYPE_EMAIL = 1;
const TYPE_PHONE = 2;
const TYPE_CHAT = 3;
2018-07-14 03:23:37 +02:00
2018-07-24 08:34:28 +02:00
public static $types = [
self::TYPE_EMAIL => 'email',
self::TYPE_PHONE => 'phone',
self::TYPE_CHAT => 'chat',
];
2018-07-14 03:23:37 +02:00
/**
2018-07-27 15:19:25 +02:00
* Conversation statuses (code must be equal to thread statuses).
2018-07-14 03:23:37 +02:00
*/
const STATUS_ACTIVE = 1;
2018-07-17 13:27:19 +02:00
const STATUS_PENDING = 2;
const STATUS_CLOSED = 3;
const STATUS_SPAM = 4;
// Not used
//const STATUS_OPEN = 5;
2018-07-14 03:23:37 +02:00
2018-07-24 08:34:28 +02:00
public static $statuses = [
self::STATUS_ACTIVE => 'active',
2018-07-17 13:27:19 +02:00
self::STATUS_PENDING => 'pending',
2018-07-24 08:34:28 +02:00
self::STATUS_CLOSED => 'closed',
self::STATUS_SPAM => 'spam',
2018-07-17 13:27:19 +02:00
//self::STATUS_OPEN => 'open',
2018-07-24 08:34:28 +02:00
];
2018-07-17 13:27:19 +02:00
/**
2018-07-24 08:34:28 +02:00
* https://glyphicons.bootstrapcheatsheets.com/.
2018-07-17 13:27:19 +02:00
*/
2018-07-24 08:34:28 +02:00
public static $status_icons = [
2018-09-15 15:37:29 +02:00
self::STATUS_ACTIVE => 'flag',
2018-12-21 09:05:45 +01:00
self::STATUS_PENDING => 'ok',
2018-07-24 08:34:28 +02:00
self::STATUS_CLOSED => 'lock',
self::STATUS_SPAM => 'ban-circle',
2018-07-17 13:27:19 +02:00
//self::STATUS_OPEN => 'folder-open',
2018-07-24 08:34:28 +02:00
];
2018-07-18 01:43:59 +02:00
2018-08-09 10:14:48 +02:00
public static $status_classes = [
2018-07-24 08:34:28 +02:00
self::STATUS_ACTIVE => 'success',
2018-09-27 07:10:38 +02:00
self::STATUS_PENDING => 'lightgrey',
2018-07-24 08:34:28 +02:00
self::STATUS_CLOSED => 'grey',
self::STATUS_SPAM => 'danger',
2018-07-18 01:43:59 +02:00
//self::STATUS_OPEN => 'folder-open',
2018-07-24 08:34:28 +02:00
];
2018-07-14 03:23:37 +02:00
2018-08-09 10:14:48 +02:00
public static $status_colors = [
2018-12-02 13:49:02 +01:00
self::STATUS_ACTIVE => '#6ac27b',
2018-11-15 14:17:39 +01:00
self::STATUS_PENDING => '#8b98a6',
2018-10-24 09:50:54 +02:00
self::STATUS_CLOSED => '#6b6b6b',
2018-08-09 10:14:48 +02:00
self::STATUS_SPAM => '#de6864',
];
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Conversation states.
2018-07-14 03:23:37 +02:00
*/
const STATE_DRAFT = 1;
const STATE_PUBLISHED = 2;
const STATE_DELETED = 3;
2018-07-24 08:34:28 +02:00
public static $states = [
self::STATE_DRAFT => 'draft',
self::STATE_PUBLISHED => 'published',
self::STATE_DELETED => 'deleted',
];
/**
* Source types (equal to thread source types).
2018-07-14 03:23:37 +02:00
*/
const SOURCE_TYPE_EMAIL = 1;
const SOURCE_TYPE_WEB = 2;
const SOURCE_TYPE_API = 3;
2018-07-24 08:34:28 +02:00
public static $source_types = [
self::SOURCE_TYPE_EMAIL => 'email',
self::SOURCE_TYPE_WEB => 'web',
self::SOURCE_TYPE_API => 'api',
];
2018-07-14 03:23:37 +02:00
/**
* Email history options.
*/
// const EMAIL_HISTORY_GLOBAL = 0;
// const EMAIL_HISTORY_NONE = 1;
// const EMAIL_HISTORY_LAST = 2;
// const EMAIL_HISTORY_FULL = 3;
public static $email_history_codes = [
'global',
'none',
'last',
'full',
];
2019-12-31 13:19:58 +01:00
/**
* Assignee.
*/
const USER_UNASSIGNED = -1;
2018-08-23 10:46:08 +02:00
/**
2019-07-12 11:52:11 +02:00
* Search filters.
2018-08-23 10:46:08 +02:00
*/
2019-07-12 11:52:11 +02:00
public static $search_filters = [
2018-08-23 10:46:08 +02:00
'assigned',
'customer',
'mailbox',
'status',
'state',
2018-08-23 10:46:08 +02:00
'subject',
2019-07-12 11:52:11 +02:00
'attachments',
2018-08-23 10:46:08 +02:00
'type',
'body',
'number',
'following',
2018-08-23 10:46:08 +02:00
'id',
'after',
'before',
2019-07-12 11:52:11 +02:00
//'between',
//'on',
2018-08-23 10:46:08 +02:00
];
2019-07-12 11:52:11 +02:00
/**
* Search mode.
*/
const SEARCH_MODE_CONV = 'conversations';
const SEARCH_MODE_CUSTOMERS = 'customers';
2018-09-04 14:50:15 +02:00
/**
* Default size of the conversations table.
*/
const DEFAULT_LIST_SIZE = 50;
2018-09-14 16:52:16 +02:00
/**
* Cache of the conversations starred by user.
2018-11-12 10:12:57 +01:00
*
2018-09-14 16:52:16 +02:00
* @var array
*/
public static $starred_conversation_ids = [];
2018-09-14 16:52:16 +02:00
/**
* Cache of the app.custom_number option.
*/
public static $custom_number_cache = null;
2018-07-15 12:30:49 +02:00
/**
* Automatically converted into Carbon dates.
*/
2018-09-18 08:25:15 +02:00
protected $dates = ['created_at', 'updated_at', 'last_reply_at', 'closed_at', 'user_updated_at'];
2018-07-15 12:30:49 +02:00
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Attributes which are not fillable using fill() method.
2018-07-14 03:23:37 +02:00
*/
protected $guarded = ['id', 'folder_id'];
2022-12-18 07:20:43 +01:00
/**
* Convert to array.
*/
protected $casts = [
'meta' => 'array',
];
/**
* Default values.
*/
protected $attributes = [
'preview' => '',
];
2018-07-14 03:23:37 +02:00
protected static function boot()
{
parent::boot();
2018-07-24 08:34:28 +02:00
self::creating(function (Conversation $model) {
2018-11-12 10:12:57 +01:00
$next_ticket = (int) Option::get('next_ticket');
$current_number = Conversation::max('number');
if ($next_ticket) {
Option::remove('next_ticket');
}
2018-11-12 10:12:57 +01:00
if ($next_ticket && $next_ticket >= ($current_number + 1) && !Conversation::where('number', $next_ticket)->exists()) {
$model->number = $next_ticket;
} else {
2018-11-12 10:12:57 +01:00
$model->number = $current_number + 1;
}
2018-07-14 03:23:37 +02:00
});
}
/**
2018-07-24 08:34:28 +02:00
* Who the conversation is assigned to (assignee).
2018-07-14 03:23:37 +02:00
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
2018-09-14 10:24:04 +02:00
* Get the folder to which conversation belongs via folder field.
2018-07-14 03:23:37 +02:00
*/
public function folder()
{
return $this->belongsTo('App\Folder');
}
2018-09-14 10:24:04 +02:00
/**
* Get the folder to which conversation belongs via conversation_folder table.
*/
public function folders()
{
return $this->belongsToMany('App\Folder');
}
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Get the mailbox to which conversation belongs.
2018-07-14 03:23:37 +02:00
*/
public function mailbox()
{
return $this->belongsTo('App\Mailbox');
}
2020-05-16 04:10:14 +02:00
/**
2023-03-09 13:03:46 +01:00
* Cached mailbox.
2020-05-16 04:10:14 +02:00
* @return [type] [description]
*/
public function mailbox_cached()
{
return $this->mailbox()->rememberForever();
}
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Get the customer associated with this conversation (primaryCustomer).
2018-07-14 03:23:37 +02:00
*/
public function customer()
{
return $this->belongsTo('App\Customer');
}
/**
* Cached customer.
*/
public function customer_cached()
{
return $this->customer()->rememberForever();
}
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Get conversation threads.
2018-07-14 03:23:37 +02:00
*/
public function threads()
{
return $this->hasMany('App\Thread');
}
/**
2018-07-24 08:34:28 +02:00
* Folders containing starred conversations.
2018-07-14 03:23:37 +02:00
*/
2018-07-14 14:17:11 +02:00
public function extraFolders()
2018-07-14 03:23:37 +02:00
{
2018-07-14 14:17:11 +02:00
return $this->belongsTo('App\Customer');
2018-07-14 03:23:37 +02:00
}
2018-07-25 19:25:00 +02:00
/**
* Get user who created the conversations.
*/
public function created_by_user()
{
return $this->belongsTo('App\User');
}
/**
* Get customer who created the conversations.
*/
public function created_by_customer()
{
return $this->belongsTo('App\Customer');
}
/**
* Get user who closed the conversations.
*/
public function closed_by_user()
{
return $this->belongsTo('App\User');
}
2018-07-25 19:30:43 +02:00
2019-12-25 14:17:17 +01:00
/**
* Get conversations followers.
*/
public function followers()
{
return $this->hasMany('App\Follower');
}
/**
* Check if user is following this conversation.
*/
public function isUserFollowing($user_id)
{
// We intentionally select all records from followers table,
// as it is more efficient than querying a particular user record.
foreach ($this->followers as $follower) {
if ($follower->user_id == $user_id) {
return true;
}
}
return false;
}
2018-08-02 18:17:13 +02:00
/**
* Get only reply threads from conversations.
2018-08-02 18:18:32 +02:00
*
2018-08-02 18:17:13 +02:00
* @return Collection
*/
public function getReplies()
{
return $this->threads()
->whereIn('type', [Thread::TYPE_CUSTOMER, Thread::TYPE_MESSAGE])
->where('state', Thread::STATE_PUBLISHED)
->orderBy('created_at', 'desc')
->get();
}
2018-08-08 09:52:53 +02:00
/**
* Get all published conversation threads in desc order.
2018-08-08 09:56:03 +02:00
*
2018-08-08 09:52:53 +02:00
* @return Collection
*/
2020-01-08 15:13:52 +01:00
public function getThreads($skip = null, $take = null, $types = [])
2018-08-08 09:52:53 +02:00
{
$query = $this->threads()
->where('state', Thread::STATE_PUBLISHED)
->orderBy('created_at', 'desc');
if (!is_null($skip)) {
$query->skip($skip);
}
if (!is_null($take)) {
$query->take($take);
}
2020-01-08 15:13:52 +01:00
if ($types) {
$query->whereIn('type', $types);
}
2018-08-08 09:52:53 +02:00
return $query->get();
}
2019-06-03 12:06:42 +02:00
/**
* Get first thread of the conversation.
*/
public function getFirstThread()
{
return $this->threads()
->orderBy('created_at', 'asc')
->first();
}
2018-12-02 13:49:02 +01:00
/**
* Get last reply by customer or support agent.
2018-12-05 08:06:54 +01:00
*
* @param bool $last [description]
*
* @return [type] [description]
2018-12-02 13:49:02 +01:00
*/
public function getLastReply($include_phone_replies = false)
2018-12-02 13:49:02 +01:00
{
$types = [Thread::TYPE_CUSTOMER, Thread::TYPE_MESSAGE];
if ($include_phone_replies && $this->isPhone()) {
$types[] = Thread::TYPE_NOTE;
}
2018-12-02 13:49:02 +01:00
return $this->threads()
->whereIn('type', $types)
2018-12-02 13:49:02 +01:00
->where('state', Thread::STATE_PUBLISHED)
->orderBy('created_at', 'desc')
->first();
}
2019-06-27 09:17:50 +02:00
/**
* Get last thread by type.
*/
public function getLastThread($types = [])
{
$query = $this->threads()
->where('state', Thread::STATE_PUBLISHED)
->orderBy('created_at', 'desc');
if ($types) {
2020-01-15 08:54:37 +01:00
if (count($types) == 1 && $types[0]) {
$query->where('type', $types[0]);
} else {
$query->whereIn('type', $types);
}
2019-06-27 09:17:50 +02:00
}
return $query->first();
}
2018-07-14 03:23:37 +02:00
/**
2018-07-24 08:34:28 +02:00
* Set preview text.
*
2018-07-14 03:23:37 +02:00
* @param string $text
*/
public function setPreview($text = '')
{
$this->preview = '';
2018-07-30 17:11:35 +02:00
if (!$text) {
2018-07-14 03:23:37 +02:00
$first_thread = $this->threads()->first();
if ($first_thread) {
2018-07-30 17:11:35 +02:00
$text = $first_thread->body;
2018-07-14 03:23:37 +02:00
}
}
2018-07-24 08:34:28 +02:00
$this->preview = \Helper::textPreview($text, self::PREVIEW_MAXLENGTH);
2018-07-30 17:11:35 +02:00
2018-07-14 03:23:37 +02:00
return $this->preview;
}
2018-07-15 12:30:49 +02:00
/**
* Get conversation timestamp title.
2018-07-24 08:34:28 +02:00
*
2018-07-15 12:30:49 +02:00
* @return string
*/
public function getDateTitle()
{
2018-07-19 13:29:40 +02:00
if ($this->threads_count == 1) {
2019-12-06 10:28:22 +01:00
$title = __('Created by :person', ['person' => __(ucfirst(self::$persons[$this->source_via]))]);
$title .= '<br/>'.User::dateFormat($this->created_at, 'M j, Y H:i');
2018-07-15 12:30:49 +02:00
} else {
2019-07-12 11:52:11 +02:00
$person = '';
if (!empty(self::$persons[$this->last_reply_from])) {
2019-12-06 10:28:22 +01:00
$person = __(ucfirst(self::$persons[$this->last_reply_from]));
2019-07-12 11:52:11 +02:00
}
2019-12-06 10:28:22 +01:00
$title = __('Last reply by :person', ['person' => $person]);
$last_reply_at = $this->created_at;
if ($this->last_reply_at) {
$last_reply_at = $this->last_reply_at;
}
$title .= '<br/>'.User::dateFormat($last_reply_at, 'M j, Y H:i');
2018-07-15 12:30:49 +02:00
}
2018-07-24 08:34:28 +02:00
2018-07-15 12:30:49 +02:00
return $title;
}
public function isActive()
{
return $this->status == self::STATUS_ACTIVE;
}
2018-07-17 13:27:19 +02:00
public function isPending()
{
return $this->status == self::STATUS_PENDING;
}
public function isSpam()
{
return $this->status == self::STATUS_SPAM;
}
2022-12-18 07:20:43 +01:00
public function isClosed()
{
return $this->status == self::STATUS_CLOSED;
}
public function isPublished()
{
return $this->state == self::STATE_PUBLISHED;
}
public function isDraft()
{
return $this->state == self::STATE_DRAFT;
}
2018-07-17 13:27:19 +02:00
/**
* Get status name.
2018-08-08 09:56:03 +02:00
*
2018-08-08 09:52:53 +02:00
* @return string
*/
public function getStatusName()
{
return self::statusCodeToName($this->status);
}
/**
* Convert status code to name.
2018-07-24 08:34:28 +02:00
*
* @param int $status
*
* @return string
2018-07-17 13:27:19 +02:00
*/
2018-08-08 09:52:53 +02:00
public static function statusCodeToName($status)
2018-07-17 13:27:19 +02:00
{
switch ($status) {
case self::STATUS_ACTIVE:
2018-07-24 08:34:28 +02:00
return __('Active');
2018-07-17 13:27:19 +02:00
break;
case self::STATUS_PENDING:
2018-07-24 08:34:28 +02:00
return __('Pending');
2018-07-17 13:27:19 +02:00
break;
case self::STATUS_CLOSED:
2018-07-24 08:34:28 +02:00
return __('Closed');
2018-07-17 13:27:19 +02:00
break;
case self::STATUS_SPAM:
2018-07-24 08:34:28 +02:00
return __('Spam');
2018-07-17 13:27:19 +02:00
break;
// case self::STATUS_OPEN:
// return __('Open');
// break;
2018-07-17 13:27:19 +02:00
default:
return '';
break;
}
}
2018-07-25 16:09:20 +02:00
/**
* Convert state code to name.
*
* @param int $status
*
* @return string
*/
public static function stateCodeToName($status)
{
switch ($status) {
case self::STATE_DRAFT:
return __('Draft');
break;
case self::STATE_PUBLISHED:
return __('Published');
break;
case self::STATE_DELETED:
return __('Deleted');
break;
default:
return '';
break;
}
}
public function getStatus()
{
if (array_key_exists($this->status, self::$statuses)) {
return $this->status;
} else {
return self::STATUS_ACTIVE;
}
}
2018-07-25 19:25:00 +02:00
/**
2023-03-09 13:03:46 +01:00
* Set conversation status and all related fields.
2018-07-25 19:30:43 +02:00
*
* @param int $status
2018-07-25 19:25:00 +02:00
*/
public function setStatus($status, $user = null)
{
$now = date('Y-m-d H:i:s');
$this->status = $status;
$this->updateFolder();
$this->user_updated_at = $now;
if ($user && $status == self::STATUS_CLOSED) {
$this->closed_by_user_id = $user->id;
$this->closed_at = $now;
}
}
2018-07-27 08:01:34 +02:00
/**
* Set conversation user and all related fields.
2018-07-27 08:03:12 +02:00
*
2018-07-27 08:01:34 +02:00
* @param int $user_id
*/
public function setUser($user_id)
{
$now = date('Y-m-d H:i:s');
2018-07-27 10:10:51 +02:00
if ($user_id == -1) {
$user_id = null;
}
2018-07-27 08:01:34 +02:00
$this->user_id = $user_id;
$this->updateFolder();
$this->user_updated_at = $now;
// If user was previously following the conversation then unfollow
if (!is_null($user_id)) {
$follower = Follower::where('conversation_id', $this->id)
->where('user_id', $user_id)
->first();
if ($follower) {
$follower->delete();
}
}
2018-07-27 08:01:34 +02:00
}
2018-07-25 16:09:20 +02:00
/**
* Get next active conversation.
2018-07-25 16:15:35 +02:00
*
* @param string $mode next|prev|closest
*
2018-07-25 16:09:20 +02:00
* @return Conversation
*/
public function getNearby($mode = 'closest', $folder_id = null)
2018-07-25 16:09:20 +02:00
{
$conversation = null;
if ($folder_id) {
$folder = Folder::find($folder_id);
} else {
$folder = $this->folder;
}
//$query = self::where('folder_id', $folder->id)->where('id', '<>', $this->id);
$query = self::getQueryByFolder($folder, \Auth::id())
2018-07-25 16:09:20 +02:00
->where('id', '<>', $this->id);
$query = \Eventy::filter('conversation.get_nearby_query', $query, $this, $mode, $folder);
2018-07-25 16:09:20 +02:00
$order_bys = $folder->getOrderByArray();
2018-07-25 16:15:35 +02:00
// Next.
2018-07-25 16:09:20 +02:00
if ($mode != 'prev') {
// Try to get next conversation
$query_next = $query;
foreach ($order_bys as $order_by) {
foreach ($order_by as $field => $sort_order) {
2018-09-18 08:25:15 +02:00
if (!$this->$field) {
continue;
}
2018-07-25 16:09:20 +02:00
if ($sort_order == 'asc') {
$query_next->where($field, '>=', $this->$field);
} else {
$query_next->where($field, '<=', $this->$field);
}
$query_next->orderBy($field, $sort_order);
}
}
$conversation = $query_next->first();
2018-07-25 16:09:20 +02:00
}
if ($conversation || $mode == 'next') {
return $conversation;
}
// Prev.
2018-07-25 16:09:20 +02:00
$query_prev = $query;
foreach ($order_bys as $order_by) {
foreach ($order_by as $field => $sort_order) {
2018-09-18 08:25:15 +02:00
if (!$this->$field) {
continue;
}
2018-07-25 16:09:20 +02:00
if ($sort_order == 'asc') {
$query_prev->where($field, '<=', $this->$field);
} else {
$query_prev->where($field, '>=', $this->$field);
2018-07-25 16:09:20 +02:00
}
$query_prev->orderBy($field, $sort_order == 'asc' ? 'desc' : 'asc');
2018-07-25 16:09:20 +02:00
}
}
2018-07-25 16:15:35 +02:00
2018-07-25 16:09:20 +02:00
return $query_prev->first();
}
2018-07-25 19:25:00 +02:00
/**
* Get URL of the next conversation.
*/
public function urlNext($folder_id = null)
{
$next_conversation = $this->getNearby('next', $folder_id);
if ($next_conversation) {
$url = $next_conversation->url();
} else {
// Show folder
2019-05-06 12:30:14 +02:00
$url = route('mailboxes.view.folder', ['id' => $this->mailbox_id, 'folder_id' => $this->getCurrentFolder($this->folder_id)]);
}
2018-11-12 10:12:57 +01:00
return $url;
}
/**
* Get URL of the previous conversation.
*/
public function urlPrev($folder_id = null)
{
$prev_conversation = $this->getNearby('prev', $folder_id);
if ($prev_conversation) {
$url = $prev_conversation->url();
} else {
// Show folder
2019-05-06 12:30:14 +02:00
$url = route('mailboxes.view.folder', ['id' => $this->mailbox_id, 'folder_id' => $this->getCurrentFolder($this->folder_id)]);
}
2018-11-12 10:12:57 +01:00
return $url;
}
2018-07-25 19:25:00 +02:00
/**
2018-07-27 08:01:34 +02:00
* Set folder according to the status, state and user of the conversation.
2018-07-25 19:25:00 +02:00
*/
2019-07-26 08:01:03 +02:00
public function updateFolder($mailbox = null)
2018-07-25 19:25:00 +02:00
{
if ($this->state == self::STATE_DRAFT) {
$folder_type = Folder::TYPE_DRAFTS;
} elseif ($this->state == self::STATE_DELETED) {
$folder_type = Folder::TYPE_DELETED;
} elseif ($this->status == self::STATUS_SPAM) {
$folder_type = Folder::TYPE_SPAM;
} elseif ($this->status == self::STATUS_CLOSED) {
$folder_type = Folder::TYPE_CLOSED;
} elseif ($this->user_id) {
$folder_type = Folder::TYPE_ASSIGNED;
} else {
$folder_type = Folder::TYPE_UNASSIGNED;
}
2019-07-26 08:01:03 +02:00
if (!$mailbox) {
$mailbox = $this->mailbox;
}
2018-07-25 19:25:00 +02:00
// Find folder
2019-07-26 08:01:03 +02:00
$folder = $mailbox->folders()
2018-07-25 19:25:00 +02:00
->where('type', $folder_type)
->first();
if ($folder) {
$this->folder_id = $folder->id;
}
}
2018-07-30 17:11:35 +02:00
/**
* Set CC as JSON.
*/
public function setCc($emails)
{
$emails_array = self::sanitizeEmails($emails);
if ($emails_array) {
2018-08-04 17:33:45 +02:00
$emails_array = array_unique($emails_array);
2019-07-27 15:07:04 +02:00
$this->cc = \Helper::jsonEncodeUtf8($emails_array);
2018-07-30 17:11:35 +02:00
} else {
$this->cc = null;
}
}
/**
* Set BCC as JSON.
*/
2018-07-30 17:13:33 +02:00
public function setBcc($emails)
2018-07-30 17:11:35 +02:00
{
$emails_array = self::sanitizeEmails($emails);
if ($emails_array) {
2018-08-04 17:33:45 +02:00
$emails_array = array_unique($emails_array);
2019-07-27 15:07:04 +02:00
$this->bcc = \Helper::jsonEncodeUtf8($emails_array);
2018-07-30 17:11:35 +02:00
} else {
$this->bcc = null;
}
}
2018-08-02 18:17:13 +02:00
/**
* Get CC recipients.
*
* @return array
*/
2018-08-27 10:08:20 +02:00
public function getCcArray($exclude_array = [])
2018-08-02 18:17:13 +02:00
{
2018-09-20 09:08:02 +02:00
return \App\Misc\Helper::jsonToArray($this->cc, $exclude_array);
2018-08-02 18:17:13 +02:00
}
/**
* Get BCC recipients.
*
* @return array
*/
2018-08-27 10:08:20 +02:00
public function getBccArray($exclude_array = [])
2018-08-02 18:17:13 +02:00
{
2018-09-20 09:08:02 +02:00
return \App\Misc\Helper::jsonToArray($this->bcc, $exclude_array);
2018-08-02 18:17:13 +02:00
}
2018-07-30 17:11:35 +02:00
/**
* Convert list of email to array.
2018-07-30 17:13:33 +02:00
*
* @return
2018-07-30 17:11:35 +02:00
*/
public static function sanitizeEmails($emails)
{
// Create customers if needed: Test <test1@example.com>
if (is_array($emails)) {
foreach ($emails as $i => $email) {
2022-02-04 08:30:48 +01:00
preg_match("/^(.+)\s+([^\s]+)$/", $email ?? '', $m);
if (count($m) == 3) {
$customer_name = trim($m[1]);
$email_address = trim($m[2]);
if ($customer_name) {
preg_match("/^([^\s]+)\s+([^\s]+)$/", $customer_name, $m_customer);
$customer_data = [];
if (count($m_customer) == 3) {
$customer_data['first_name'] = $m_customer[1];
$customer_data['last_name'] = $m_customer[2];
} else {
$customer_data['first_name'] = $customer_name;
}
Customer::create($email_address, $customer_data);
}
$emails[$i] = $email_address;
}
}
}
return \MailHelper::sanitizeEmails($emails);
2018-07-30 17:11:35 +02:00
}
2018-08-08 09:52:53 +02:00
/**
* Get conversation URL.
*
* @return string
*/
2018-09-08 14:40:21 +02:00
public function url($folder_id = null, $thread_id = null, $params = [])
2018-08-08 09:52:53 +02:00
{
2018-08-10 16:28:35 +02:00
if (!$folder_id) {
2019-05-06 12:30:14 +02:00
$folder_id = $this->getCurrentFolder();
2018-08-10 16:28:35 +02:00
}
2019-05-06 12:30:14 +02:00
return self::conversationUrl($this->id, $folder_id, $thread_id, $params);
}
/**
* Static function for retrieving URL.
*
2019-05-06 12:30:14 +02:00
* @param [type] $id [description]
* @param [type] $folder_id [description]
* @param [type] $thread_id [description]
* @param array $params [description]
* @return [type] [description]
*/
public static function conversationUrl($id, $folder_id = null, $thread_id = null, $params = [])
{
$params = array_merge($params, ['id' => $id]);
2018-08-10 16:28:35 +02:00
$params['folder_id'] = $folder_id;
2018-09-07 15:08:34 +02:00
$url = route('conversations.view', $params);
if ($thread_id) {
$url .= '#thread-'.$thread_id;
}
return $url;
2018-08-08 09:52:53 +02:00
}
2018-08-09 10:14:48 +02:00
/**
* Get CSS color of the status.
2018-08-09 10:16:53 +02:00
*
2018-08-09 10:14:48 +02:00
* @return string
*/
public function getStatusColor()
{
return self::$status_colors[$this->status];
}
2018-08-10 16:28:35 +02:00
/**
* Get folder ID from request or use the default one.
*/
public function getCurrentFolder($default_folder_id = null)
{
$folder_id = self::getFolderParam();
if ($folder_id) {
return $folder_id;
}
if ($this->folder_id) {
return $this->folder_id;
} else {
return $default_folder_id;
}
}
public static function getFolderParam()
{
if (!empty(request()->folder_id)) {
return request()->folder_id;
} elseif (!empty(Input::get('folder_id'))) {
return Input::get('folder_id');
}
2018-08-10 16:34:10 +02:00
2018-08-10 16:28:35 +02:00
return '';
}
/**
* Check if conversation can be in the folder.
*/
public function isInFolderAllowed($folder)
{
if (in_array($folder->type, Folder::$public_types)) {
2018-08-10 16:34:10 +02:00
return $folder->id == $this->folder_id;
2018-08-10 16:28:35 +02:00
} elseif ($folder->type == Folder::TYPE_MINE) {
$user = auth()->user();
if ($user && $user->id == $folder->user_id && $this->user_id == $user->id) {
return true;
} else {
return false;
}
} else {
// todo: check ConversationFolder here
return \Eventy::filter('conversation.is_in_folder_allowed', false, $folder, $this);
2018-08-10 16:28:35 +02:00
}
2018-08-10 16:34:10 +02:00
2018-08-10 16:28:35 +02:00
return false;
}
2018-08-15 15:03:53 +02:00
2018-09-14 16:52:16 +02:00
/**
* Check if conversation is starred.
* For each user starred conversations are cached.
*/
public function isStarredByUser($user_id = null)
2018-11-12 10:12:57 +01:00
{
2018-09-14 16:52:16 +02:00
if (!$user_id) {
$user = auth()->user();
if ($user) {
$user_id = $user->id;
} else {
return false;
}
}
$mailbox_id = $this->mailbox_id;
2018-09-14 16:52:16 +02:00
// Get ids of all the conversations starred by user and cache them
if (!isset(self::$starred_conversation_ids[$mailbox_id])) {
self::$starred_conversation_ids[$mailbox_id] = self::getUserStarredConversationIds($mailbox_id, $user_id);
2018-09-14 16:52:16 +02:00
}
if (self::$starred_conversation_ids[$mailbox_id]) {
return in_array($this->id, self::$starred_conversation_ids[$mailbox_id]);
2018-09-14 16:52:16 +02:00
} else {
return false;
}
}
2019-12-23 14:54:36 +01:00
public static function clearStarredByUserCache($user_id, $mailbox_id)
2018-09-14 16:52:16 +02:00
{
if (!$user_id) {
$user = auth()->user();
if ($user) {
$user_id = $user->id;
} else {
return false;
}
}
2019-12-23 14:54:36 +01:00
\Cache::forget('user_starred_conversations_'.$user_id.'_'.$mailbox_id);
2018-09-14 16:52:16 +02:00
}
/**
* Get IDs of the conversations starred by user.
*/
public static function getUserStarredConversationIds($mailbox_id, $user_id = null)
{
2019-12-10 07:51:49 +01:00
return \Cache::rememberForever('user_starred_conversations_'.$user_id.'_'.$mailbox_id, function () use ($mailbox_id, $user_id) {
2018-09-14 16:52:16 +02:00
// Get user's folder
$folder = Folder::select('id')
->where('mailbox_id', $mailbox_id)
->where('user_id', $user_id)
->where('type', Folder::TYPE_STARRED)
->first();
if ($folder) {
return ConversationFolder::where('folder_id', $folder->id)
->pluck('conversation_id')
->toArray();
} else {
activity()
->withProperties([
'error' => "Folder not found (mailbox_id: $mailbox_id, user_id: $user_id)",
])
->useLog(\App\ActivityLog::NAME_SYSTEM)
->log(\App\ActivityLog::DESCRIPTION_SYSTEM_ERROR);
2018-11-12 10:12:57 +01:00
2018-09-14 16:52:16 +02:00
return [];
}
});
}
2018-08-15 15:03:53 +02:00
/**
* Get text for the assignee.
*
* @return string
*/
public function getAssigneeName($ucfirst = false, $user = null)
{
if (!$this->user_id) {
if ($ucfirst) {
return __('Anyone');
} else {
return __('anyone');
}
} elseif (($user && $this->user_id == $user->id) || (!$user && $this->user_id == auth()->user()->id)) {
if ($ucfirst) {
return __('Me');
} else {
return __('me');
}
} else {
return $this->user->getFullName();
}
}
2018-08-22 20:34:44 +02:00
/**
* Get query to fetch conversations by folder.
*/
public static function getQueryByFolder($folder, $user_id)
{
if ($folder->type == Folder::TYPE_MINE) {
// Get conversations from personal folder
2018-11-12 10:12:57 +01:00
$query_conversations = self::where('user_id', $user_id)
->where('mailbox_id', $folder->mailbox_id)
2018-11-12 10:12:57 +01:00
->whereIn('status', [self::STATUS_ACTIVE, self::STATUS_PENDING])
->where('state', self::STATE_PUBLISHED);
2018-08-22 20:34:44 +02:00
} elseif ($folder->type == Folder::TYPE_ASSIGNED) {
2018-08-22 20:34:44 +02:00
// Assigned - do not show my conversations
$query_conversations = $folder->conversations()
2018-09-20 16:34:34 +02:00
// This condition also removes from result records with user_id = null
->where('user_id', '<>', $user_id)
2018-11-12 10:12:57 +01:00
->where('state', self::STATE_PUBLISHED);
2018-09-14 16:52:16 +02:00
} elseif ($folder->type == Folder::TYPE_STARRED) {
2018-11-12 10:12:57 +01:00
$starred_conversation_ids = self::getUserStarredConversationIds($folder->mailbox_id, $user_id);
$query_conversations = self::whereIn('id', $starred_conversation_ids);
2018-09-14 10:24:04 +02:00
} elseif ($folder->isIndirect()) {
2018-09-14 10:24:04 +02:00
// Conversations are connected to folder via conversation_folder table.
2018-11-12 10:12:57 +01:00
$query_conversations = self::select('conversations.*')
2018-09-20 16:34:34 +02:00
//->where('conversations.mailbox_id', $folder->mailbox_id)
2018-09-14 10:24:04 +02:00
->join('conversation_folder', 'conversations.id', '=', 'conversation_folder.conversation_id')
->where('conversation_folder.folder_id', $folder->id);
if ($folder->type != Folder::TYPE_DRAFTS) {
2018-11-12 10:12:57 +01:00
$query_conversations->where('state', self::STATE_PUBLISHED);
2018-09-14 10:24:04 +02:00
}
2018-09-18 08:25:15 +02:00
} elseif ($folder->type == Folder::TYPE_DELETED) {
2018-11-12 10:12:57 +01:00
$query_conversations = $folder->conversations()->where('state', self::STATE_DELETED);
2018-08-22 20:34:44 +02:00
} else {
2018-11-12 10:12:57 +01:00
$query_conversations = $folder->conversations()->where('state', self::STATE_PUBLISHED);
2018-08-22 20:34:44 +02:00
}
2019-12-20 10:41:06 +01:00
return \Eventy::filter('folder.conversations_query', $query_conversations, $folder, $user_id);
2018-08-22 20:34:44 +02:00
}
2018-08-25 11:52:53 +02:00
/**
* Replace vars in signature.
2018-12-05 08:05:27 +01:00
* `data` contains extra info which can be used to build signature.
2018-08-25 11:52:53 +02:00
*/
2020-01-23 08:09:39 +01:00
public function getSignatureProcessed($data = [], $escape = false)
2018-08-25 11:52:53 +02:00
{
2022-02-20 09:29:06 +01:00
$replaced_text = $this->replaceTextVars( $this->mailbox->signature, $data, $escape );
2022-02-20 09:29:06 +01:00
return \Eventy::filter( 'conversation.signature_processed', $replaced_text, $this, $data, $escape );
2020-01-09 15:04:12 +01:00
}
/**
* Replace vars in the text.
*/
2020-01-23 08:09:39 +01:00
public function replaceTextVars($text, $data = [], $escape = false)
2020-01-09 15:04:12 +01:00
{
if (!\MailHelper::hasVars($text)) {
2020-01-09 15:04:12 +01:00
return $text;
2018-08-25 11:52:53 +02:00
}
2018-12-05 08:05:27 +01:00
2020-01-09 15:04:12 +01:00
if (empty($data['user'])) {
// `user` should contain a user who replies to the conversation.
$user = auth()->user();
if (!$user && !empty($data['thread'])) {
$user = $data['thread']->created_by_user;
}
} else {
$user = $data['user'];
2018-12-05 08:05:27 +01:00
}
2018-08-25 11:52:53 +02:00
$data = [
'mailbox' => $this->mailbox,
'conversation' => $this,
'customer' => $this->customer_cached,
2018-12-05 08:05:27 +01:00
'user' => $user,
2018-08-25 11:52:53 +02:00
];
// Set variables
2020-01-23 08:09:39 +01:00
return \MailHelper::replaceMailVars($text, $data, $escape);
2018-08-25 11:52:53 +02:00
}
2018-08-29 08:45:15 +02:00
/**
* Change conversation customer.
* Customer is changed using customer email, as each conversation has customer email.
* Method also creates line item thread if customer changed by user.
* Both by_user and by_customer can be null.
*/
public function changeCustomer($customer_email, $customer = null, $by_user = null, $by_customer = null)
{
if (!$customer) {
$email = Email::where('email', $customer_email)->first();
if ($email) {
$customer = $email->customer;
} else {
return false;
}
}
if (!$customer_email) {
$customer_email = $customer->getMainEmail();
}
2018-08-29 08:45:15 +02:00
$prev_customer_id = $this->customer_id;
$prev_customer_email = $this->customer_email;
$this->customer_email = $customer_email;
$this->customer_id = $customer->id;
$this->save();
// Create line item thread
if ($by_user) {
$thread = new Thread();
$thread->conversation_id = $this->id;
$thread->user_id = $this->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_CUSTOMER_CHANGED;
$thread->action_data = $this->customer_email;
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $by_user->id;
$thread->save();
}
event(new ConversationCustomerChanged($this, $prev_customer_id, $prev_customer_email, $by_user, $by_customer));
return true;
}
2018-09-05 10:05:00 +02:00
2019-07-26 08:01:03 +02:00
/**
* Move conversation to another mailbox.
*/
public function moveToMailbox($mailbox, $user)
{
$prev_mailbox = $this->mailbox;
foreach ($this->folders as $folder) {
// Process indirect folders.
if (!in_array($folder->type, Folder::$indirect_types)) {
continue;
}
// Remove conversation from the folder.
$this->removeFromFolder($folder->type, $folder->user_id);
if ($folder->type == Folder::TYPE_STARRED) {
self::clearStarredByUserCache($folder->user_id, $this->mailbox_id);
}
}
2019-07-26 08:01:03 +02:00
// We don't know how to replace $this->mailbox object.
$this->mailbox_id = $mailbox->id;
// Check assignee.
if ($this->user_id && !in_array($this->user_id, $mailbox->userIdsHavingAccess())) {
// Assign conversation to the user who moved it.
$this->user_id = $user->id;
2019-07-26 08:01:03 +02:00
}
$this->updateFolder($mailbox);
$this->save();
foreach ($this->folders as $folder) {
// Process indirect folders.
if (!in_array($folder->type, Folder::$indirect_types)) {
continue;
}
// If user has access to the new mailbox,
// move conversation to the same folder in the new mailbox.
if ($folder->user_id) {
if ($folder->user->hasAccessToMailbox($mailbox->id)) {
foreach ($mailbox->folders as $mailbox_folder) {
if ($mailbox_folder->type == $folder->type) {
$this->addToFolder($folder->type, $folder->user_id);
if ($folder->type == Folder::TYPE_STARRED) {
self::clearStarredByUserCache($folder->user_id, $mailbox->id);
}
break;
}
}
}
} else {
foreach ($mailbox->folders as $mailbox_folder) {
if ($mailbox_folder->type == $folder->type) {
$this->addToFolder($folder->type, $folder->user_id);
break;
}
}
}
}
2019-07-26 08:01:03 +02:00
// Add record to the conversation history.
Thread::create($this, Thread::TYPE_LINEITEM, '', [
'created_by_user_id' => $user->id,
'user_id' => $this->user_id,
'state' => Thread::STATE_PUBLISHED,
'action_type' => Thread::ACTION_TYPE_MOVED_FROM_MAILBOX,
'source_via' => Thread::PERSON_USER,
'source_type' => Thread::SOURCE_TYPE_WEB,
'customer_id' => $this->customer_id,
]);
// Update counters.
$prev_mailbox->updateFoldersCounters();
$mailbox->updateFoldersCounters();
2020-09-15 16:48:13 +02:00
\Eventy::action('conversation.moved', $this, $user, $prev_mailbox);
2019-07-26 08:01:03 +02:00
return true;
}
2020-09-22 09:59:23 +02:00
/**
* Merge conversations
*/
public function mergeConversations($merge_conversation, $user)
{
// Move all threads from old to new conversation.
foreach ($merge_conversation->threads as $thread) {
$thread->conversation_id = $this->id;
$thread->setMeta(Thread::META_PREV_CONVERSATION, $merge_conversation->id);
$thread->save();
}
// Add record to the new conversation.
Thread::create($this, Thread::TYPE_LINEITEM, '', [
'created_by_user_id' => $user->id,
'user_id' => $this->user_id,
'state' => Thread::STATE_PUBLISHED,
'action_type' => Thread::ACTION_TYPE_MERGED,
'source_via' => Thread::PERSON_USER,
'source_type' => Thread::SOURCE_TYPE_WEB,
'customer_id' => $this->customer_id,
'meta' => [Thread::META_MERGED_WITH_CONV => $merge_conversation->id],
]);
// Add record to the old conversation.
Thread::create($merge_conversation, Thread::TYPE_LINEITEM, '', [
'created_by_user_id' => $user->id,
'user_id' => $merge_conversation->user_id,
'state' => Thread::STATE_PUBLISHED,
'action_type' => Thread::ACTION_TYPE_MERGED,
'source_via' => Thread::PERSON_USER,
'source_type' => Thread::SOURCE_TYPE_WEB,
'customer_id' => $merge_conversation->customer_id,
'meta' => [Thread::META_MERGED_INTO_CONV => $this->id],
]);
if ($merge_conversation->has_attachments && !$this->has_attachments) {
$this->has_attachments = true;
$this->save();
}
2020-09-22 09:59:23 +02:00
// Delete old conversation.
$merge_conversation->deleteToFolder($user);
// Update counters.
$this->mailbox->updateFoldersCounters();
if ($this->mailbox_id != $merge_conversation->mailbox_id) {
$merge_conversation->mailbox->updateFoldersCounters();
}
\Eventy::action('conversation.merged', $this, $merge_conversation, $user);
return true;
}
2019-07-26 08:01:03 +02:00
2018-09-05 10:05:00 +02:00
/**
* Get all users for conversations in one query.
*/
public static function loadUsers($conversations)
{
$user_ids = $conversations->pluck('user_id')->unique()->toArray();
if (!$user_ids || (count($user_ids) == 1 && empty($user_ids[0]))) {
2018-09-05 10:05:00 +02:00
return;
}
$users = User::whereIn('id', $user_ids)->get();
if (!$users) {
return;
}
foreach ($conversations as $conversation) {
if (empty($conversation->user_id)) {
continue;
}
foreach ($users as $user) {
if ($user->id == $conversation->user_id) {
$conversation->user = $user;
continue 2;
}
}
}
}
/**
* Get all customers for conversations in one query.
*/
public static function loadCustomers($conversations)
{
$ids = $conversations->pluck('customer_id')->unique()->toArray();
if (!$ids) {
return;
}
$customers = Customer::whereIn('id', $ids)->get();
if (!$customers) {
return;
}
foreach ($conversations as $conversation) {
if (empty($conversation->customer_id)) {
continue;
}
foreach ($customers as $customer) {
if ($customer->id == $conversation->customer_id) {
$conversation->customer = $customer;
continue 2;
}
}
}
}
2018-09-14 10:24:04 +02:00
/**
* Load mailboxes.
*/
public static function loadMailboxes($conversations)
{
$ids = $conversations->pluck('mailbox_id')->unique()->toArray();
if (!$ids) {
return;
}
$mailboxes = Mailbox::whereIn('id', $ids)->get();
if (!$mailboxes) {
return;
}
foreach ($conversations as $conversation) {
if (empty($conversation->mailbox_id)) {
continue;
}
foreach ($mailboxes as $mailbox) {
if ($mailbox->id == $conversation->mailbox_id) {
$conversation->mailbox = $mailbox;
continue 2;
}
}
}
}
2018-09-14 10:24:04 +02:00
public function getSubject()
{
if ($this->subject) {
return $this->subject;
} else {
return __('(no subject)');
}
}
/**
* Add conversation to folder via conversation_folder table.
*/
public function addToFolder($folder_type, $user_id = null)
2018-09-14 10:24:04 +02:00
{
// Find folder.
$folder_query = Folder::where('mailbox_id', $this->mailbox_id)
->where('type', $folder_type);
if ($user_id) {
$folder_query->where('user_id', $user_id);
}
$folder = $folder_query->first();
2018-09-14 10:24:04 +02:00
if (!$folder) {
return false;
}
$values = [
2018-11-12 10:12:57 +01:00
'folder_id' => $folder->id,
2018-09-14 10:24:04 +02:00
'conversation_id' => $this->id,
];
$folder_exists = ConversationFolder::select('id')->where($values)->first();
if (!$folder_exists) {
// This throws an exception if record exists
$this->folders()->attach($folder->id);
}
2018-09-23 19:29:36 +02:00
$folder->updateCounters();
2018-11-12 10:12:57 +01:00
2018-09-14 10:24:04 +02:00
// updateOrCreate does not create properly with ManyToMany
// $values = [
// 'folder_id' => $folder->id,
// 'conversation_id' => $this->id,
// ];
// ConversationFolder::updateOrCreate($values, $values);
return true;
2018-09-14 10:24:04 +02:00
}
/**
* When removing from Starred folder, don't forget to clear cache using clearStarredByUserCache()
*/
public function removeFromFolder($folder_type, $user_id = null)
2018-09-14 10:24:04 +02:00
{
// Find folder
$folder_query = Folder::where('mailbox_id', $this->mailbox_id)
->where('type', $folder_type);
if ($user_id) {
$folder_query->where('user_id', $user_id);
}
$folder = $folder_query->first();
2018-09-14 10:24:04 +02:00
if (!$folder) {
return false;
}
$this->folders()->detach($folder->id);
2018-09-23 19:29:36 +02:00
$folder->updateCounters();
return true;
2018-09-14 10:24:04 +02:00
}
/**
* Remove conversation from drafts folder if there are no draft threads in conversation.
*/
public function maybeRemoveFromDrafts()
{
$has_drafts = Thread::where('conversation_id', $this->id)
->where('state', Thread::STATE_DRAFT)
->select('id')
->first();
if (!$has_drafts) {
$this->removeFromFolder(Folder::TYPE_DRAFTS);
2018-11-12 10:12:57 +01:00
2018-09-14 10:24:04 +02:00
return true;
}
2018-11-12 10:12:57 +01:00
2018-09-14 10:24:04 +02:00
return false;
}
/**
* Delete threads and everything connected to threads.
*/
public function deleteThreads()
{
$this->threads->each(function ($thread, $i) {
$thread->deleteThread();
});
}
2018-12-05 13:33:05 +01:00
/**
* Get waiting since time for the conversation.
2018-12-13 10:11:15 +01:00
*
* @param [type] $folder [description]
*
* @return [type] [description]
2018-12-05 13:33:05 +01:00
*/
public function getWaitingSince($folder = null)
2018-12-05 13:33:05 +01:00
{
if (!$folder) {
$folder = $this->folder;
}
2018-12-05 13:33:05 +01:00
$waiting_since_field = $folder->getWaitingSinceField();
if ($waiting_since_field) {
// For phone conversations.
if (empty($this->$waiting_since_field)) {
$waiting_since_field = 'updated_at';
}
2018-12-05 13:33:05 +01:00
return \App\User::dateDiffForHumans($this->$waiting_since_field);
} else {
return '';
}
}
2019-06-24 07:39:30 +02:00
2019-07-12 11:52:11 +02:00
/**
* Get type name.
*/
public function getTypeName()
{
return self::typeToName($this->type);
}
/**
* Get type name .
*/
public static function typeToName($type)
{
$name = '';
switch ($type) {
case self::TYPE_EMAIL:
$name = __('Email');
break;
case self::TYPE_PHONE:
$name = __('Phone');
break;
case self::TYPE_CHAT:
$name = __('Chat');
break;
default:
$name = \Eventy::filter('conversation.type_name', $type);
break;
}
return $name;
}
2019-07-16 14:52:09 +02:00
/**
* Get emails which are excluded from CC and BCC.
*/
public function getExcludeArray($mailbox = null)
{
if (!$mailbox) {
$mailbox = $this->mailbox;
}
$customer_emails = [$this->customer_email];
if (strstr($this->customer_email ?? '', ',')) {
2019-07-16 14:52:09 +02:00
// customer_email contains mutiple addresses (when new conversation for multiple recipients created)
$customer_emails = explode(',', $this->customer_email);
}
return array_merge($mailbox->getEmails(), $customer_emails);
}
/**
* Is it an email conversation.
*/
public function isEmail()
{
return ($this->type == self::TYPE_EMAIL);
}
2019-07-22 08:39:23 +02:00
/**
* Is it as phone conversation.
*/
public function isPhone()
{
return ($this->type == self::TYPE_PHONE);
}
2021-03-02 07:50:19 +01:00
/**
* Is it as chat conversation.
*/
public function isChat()
{
return ($this->type == self::TYPE_CHAT);
}
/**
* Get information on viewers for conversation table.
*/
public static function getViewersInfo($conversations, $fields = ['id', 'first_name', 'last_name'], $exclude_user_ids = [])
{
$viewers_cache = \Cache::get('conv_view');
$viewers = [];
$first_user_id = null;
$user_ids = [];
foreach ($conversations as $conversation) {
if (!empty($viewers_cache[$conversation->id])) {
// Get replying viewers
foreach ($viewers_cache[$conversation->id] as $user_id => $viewer) {
if (!$first_user_id) {
$first_user_id = $user_id;
}
if (!empty($viewer['r']) && !in_array($user_id, $exclude_user_ids)) {
$viewers[$conversation->id] = [
'user' => null,
'user_id' => $user_id,
'replying' => true
];
$user_ids[] = $user_id;
break;
}
}
// Get first non-replying viewer
if (empty($viewers[$conversation->id]) && !in_array($user_id, $exclude_user_ids)) {
$viewers[$conversation->id] = [
'user' => null,
'user_id' => $first_user_id,
'replying' => false
];
$user_ids[] = $first_user_id;
}
}
}
// Get all viewing users in one query
if ($user_ids) {
$user_ids = array_unique($user_ids);
$users = User::select($fields)->whereIn('id', $user_ids)->get();
foreach ($viewers as $i => $viewer) {
foreach ($users as $user) {
if ($user->id == $viewer['user_id']) {
$viewers[$i]['user'] = $user;
}
}
}
}
return $viewers;
}
2022-09-17 11:39:41 +02:00
public function changeState($new_state, $user = null)
{
if (!array_key_exists($new_state, self::$states)) {
return;
}
$prev_state = $this->state;
$this->state = $new_state;
$this->save();
\Eventy::action('conversation.state_changed', $this, $user, $prev_state);
}
2020-01-03 08:22:39 +01:00
public function changeStatus($new_status, $user, $create_thread = true)
{
if (!array_key_exists($new_status, self::$statuses)) {
return;
}
2020-01-03 08:22:39 +01:00
$prev_status = $this->status;
$this->setStatus($new_status, $user);
$this->save();
// Create lineitem thread
if ($create_thread) {
$thread = new Thread();
$thread->conversation_id = $this->id;
$thread->user_id = $this->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = $this->status;
$thread->action_type = Thread::ACTION_TYPE_STATUS_CHANGED;
$thread->source_via = Thread::PERSON_USER;
// todo: this need to be changed for API
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $user->id;
$thread->save();
}
event(new ConversationStatusChanged($this));
\Eventy::action('conversation.status_changed', $this, $user, $changed_on_reply = false, $prev_status);
}
2020-01-04 10:00:43 +01:00
public function changeUser($new_user_id, $user, $create_thread = true)
{
$prev_user_id = $this->user_id;
2020-01-04 10:00:43 +01:00
$this->setUser($new_user_id);
$this->save();
if ($create_thread) {
// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $this->id;
$thread->user_id = $this->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_USER_CHANGED;
$thread->source_via = Thread::PERSON_USER;
// todo: this need to be changed for API
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $user->id;
$thread->save();
}
2020-01-04 10:00:43 +01:00
event(new ConversationUserChanged($this, $user));
\Eventy::action('conversation.user_changed', $this, $user, $prev_user_id);
}
public function deleteToFolder($user)
{
$folder_id = $this->getCurrentFolder();
2022-09-17 11:39:41 +02:00
$prev_state = $this->state;
$this->state = Conversation::STATE_DELETED;
$this->user_updated_at = date('Y-m-d H:i:s');
$this->updateFolder();
$this->save();
// Create lineitem thread
$thread = new Thread();
$thread->conversation_id = $this->id;
$thread->user_id = $this->user_id;
$thread->type = Thread::TYPE_LINEITEM;
$thread->state = Thread::STATE_PUBLISHED;
$thread->status = Thread::STATUS_NOCHANGE;
$thread->action_type = Thread::ACTION_TYPE_DELETED_TICKET;
$thread->source_via = Thread::PERSON_USER;
// todo: this need to be changed for API
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $user->id;
$thread->save();
// Remove conversation from drafts folder.
$this->removeFromFolder(Folder::TYPE_DRAFTS);
// Recalculate only old and new folders
$this->mailbox->updateFoldersCounters();
2020-09-15 14:07:35 +02:00
\Eventy::action('conversation.deleted', $this, $user);
2022-09-17 11:39:41 +02:00
\Eventy::action('conversation.state_changed', $this, $user, $prev_state);
}
2020-01-03 10:03:36 +01:00
public function deleteForever()
{
self::deleteConversationsForever([$this->id]);
}
public static function deleteConversationsForever($conversation_ids)
{
\Eventy::action('conversations.before_delete_forever', $conversation_ids);
//$conversation_ids = $conversations->pluck('id')->toArray();
for ($i=0; $i < ceil(count($conversation_ids) / \Helper::IN_LIMIT); $i++) {
2020-01-03 10:03:36 +01:00
$ids = array_slice($conversation_ids, $i*\Helper::IN_LIMIT, \Helper::IN_LIMIT);
2020-01-03 10:03:36 +01:00
// Delete attachments.
$thread_ids = Thread::whereIn('conversation_id', $ids)->pluck('id')->toArray();
Attachment::deleteByThreadIds($thread_ids);
2022-12-07 07:39:27 +01:00
// Observers do not react on this kind of deleting.
// Delete threads.
Thread::whereIn('conversation_id', $ids)->delete();
// Delete followers.
Follower::whereIn('conversation_id', $ids)->delete();
// Delete conversations.
Conversation::whereIn('id', $ids)->delete();
ConversationFolder::whereIn('conversation_id', $ids)->delete();
}
2020-01-03 10:03:36 +01:00
}
2020-01-09 15:04:12 +01:00
/**
* Create note or reply.
*/
public function createUserThread($user, $body, $data = [])
{
// Create thread
$thread = Thread::create($this, $data['type'] ?? Thread::TYPE_MESSAGE, $body, $data, false);
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->user_id = $this->user_id;
$thread->status = $this->status;
$thread->state = Thread::STATE_PUBLISHED;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $user->id;
$thread->edited_by_user_id = null;
$thread->edited_at = null;
$thread->body = $body;
$thread->setTo($this->customer_email);
$thread->save();
// Update folders counters
$this->mailbox->updateFoldersCounters();
if ($thread->type == Thread::TYPE_NOTE) {
event(new UserAddedNote($this, $thread));
\Eventy::action('conversation.note_added', $this, $thread);
} else {
event(new UserReplied($this, $thread));
\Eventy::action('conversation.user_replied', $this, $thread);
}
}
public function forward($user, $body, $to = '', $data = [], $include_attachments = false)
2020-01-09 15:04:12 +01:00
{
// Create thread
$thread = Thread::create($this, $data['type'] ?? Thread::TYPE_NOTE, $body, $data, false);
$thread->source_via = Thread::PERSON_USER;
$thread->source_type = Thread::SOURCE_TYPE_WEB;
$thread->user_id = $this->user_id;
$thread->status = $this->status;
$thread->state = Thread::STATE_PUBLISHED;
$thread->customer_id = $this->customer_id;
$thread->created_by_user_id = $user->id;
$thread->edited_by_user_id = null;
$thread->edited_at = null;
$thread->body = $body;
$thread->setTo($to);
// Create forwarded conversation.
$now = date('Y-m-d H:i:s');
$forwarded_conversation = $this->replicate();
$forwarded_conversation->type = Conversation::TYPE_EMAIL;
$forwarded_conversation->setPreview($thread->body);
$forwarded_conversation->created_by_user_id = $user->id;
$forwarded_conversation->source_via = Conversation::PERSON_USER;
$forwarded_conversation->source_type = Conversation::SOURCE_TYPE_WEB;
$forwarded_conversation->threads_count = 0; // Counter will be incremented in ThreadObserver.
$forwarded_customer = Customer::create($to);
$forwarded_conversation->customer_id = $forwarded_customer->id;
$forwarded_conversation->customer_email = $to;
$forwarded_conversation->subject = 'Fwd: '.$forwarded_conversation->subject;
$forwarded_conversation->setCc(array_merge(Conversation::sanitizeEmails($data['cc'] ?? []), [$to]));
$forwarded_conversation->setBcc($data['bcc'] ?? []);
$forwarded_conversation->last_reply_at = $now;
$forwarded_conversation->last_reply_from = Conversation::PERSON_USER;
$forwarded_conversation->user_updated_at = $now;
$forwarded_conversation->updateFolder();
$forwarded_conversation->save();
$forwarded_thread = $thread->replicate();
// Set forwarding meta data.
$thread->subtype = Thread::SUBTYPE_FORWARD;
2022-09-25 08:37:42 +02:00
$thread->setMeta(Thread::META_FORWARD_CHILD_CONVERSATION_NUMBER, $forwarded_conversation->number);
$thread->setMeta(Thread::META_FORWARD_CHILD_CONVERSATION_ID, $forwarded_conversation->id);
2020-01-09 15:04:12 +01:00
$thread->save();
// Save forwarded thread.
$forwarded_thread->conversation_id = $forwarded_conversation->id;
$forwarded_thread->type = Thread::TYPE_MESSAGE;
$forwarded_thread->subtype = null;
$forwarded_thread->setTo($to);
// if ($attachments_info['has_attachments']) {
// $forwarded_thread->has_attachments = true;
// }
2022-09-25 08:37:42 +02:00
$forwarded_thread->setMeta(Thread::META_FORWARD_PARENT_CONVERSATION_NUMBER, $this->number);
$forwarded_thread->setMeta(Thread::META_FORWARD_PARENT_CONVERSATION_ID, $this->id);
$forwarded_thread->setMeta(Thread::META_FORWARD_PARENT_THREAD_ID, $thread->id);
2020-01-09 15:04:12 +01:00
$forwarded_thread->save();
// Add attachments if needed.
if ($include_attachments) {
$replies = $this->getReplies();
$has_attachments = false;
foreach ($replies as $reply_thread) {
$thread_has_attachments = false;
foreach ($reply_thread->attachments as $attachment) {
$new_attachment = $attachment->replicate();
$new_attachment->thread_id = $forwarded_thread->id;
// We need to copy attachment file, because conversations
// can be deleted along with attachments.
$new_attachment->save();
try {
$attachment_file = new \Illuminate\Http\UploadedFile(
$attachment->getLocalFilePath(), $attachment->file_name,
null, null, true
);
$file_info = Attachment::saveFileToDisk($new_attachment, $new_attachment->file_name, '', $attachment_file);
if (!empty($file_info['file_dir'])) {
$new_attachment->file_dir = $file_info['file_dir'];
$new_attachment->save();
$has_attachments = true;
$thread_has_attachments = true;
}
} catch (\Exception $e) {
\Helper::logException($e);
}
}
if ($thread_has_attachments) {
$forwarded_thread->has_attachments = true;
$forwarded_thread->save();
}
}
if ($has_attachments) {
$forwarded_conversation->has_attachments = true;
$forwarded_conversation->save();
}
}
2020-01-09 15:04:12 +01:00
// Update folders counters
$this->mailbox->updateFoldersCounters();
// Notifications to users not sent.
event(new UserAddedNote($this, $thread));
// To send email with forwarded conversation.
event(new UserReplied($forwarded_conversation, $forwarded_thread));
\Eventy::action('conversation.user_forwarded', $this, $thread, $forwarded_conversation, $forwarded_thread);
}
// public function getEmailHistoryCode()
// {
// return self::$email_history_codes[(int)$this->email_history] ?? 'global';
// }
public static function getEmailHistoryName($code) {
$label = '';
switch ($code) {
case 'global':
$label = __('Default');
$label .= ' ('.self::getEmailHistoryName(config('app.email_conv_history')).')';
break;
case 'none':
$label = __('Do not include previous messages');
break;
case 'last':
$label = __('Include the last message');
break;
case 'full':
$label = __('Send full conversation history');
break;
}
return $label;
}
/**
* Create conversation.
*
* $threads should go from old to new.
*/
public static function create($data, $threads, $customer)
{
// Detect source_via.
$source_via = $data['source_via'] ?? 0;
if (!$source_via && !empty($threads[0])) {
if (!empty($threads[0]['type']) && $threads[0]['type'] == Thread::TYPE_CUSTOMER) {
$source_via = self::PERSON_CUSTOMER;
} else {
$source_via = self::PERSON_USER;
}
}
$conversation = new Conversation();
$conversation->type = $data['type'];
$conversation->subject = $data['subject'];
$conversation->mailbox_id = $data['mailbox_id'];
$conversation->source_via = $source_via;
$conversation->source_type = $data['source_type'];
$conversation->customer_id = $customer->id;
$conversation->customer_email = $customer->getMainEmail().'';
$conversation->state = $data['state'] ?? Conversation::STATE_PUBLISHED;
$conversation->imported = (int)($data['imported'] ?? false);
$conversation->closed_at = $data['closed_at'] ?? null;
$conversation->channel = $data['channel'] ?? null;
$conversation->preview = '';
// Phone conversation is always pending.
if ($conversation->isPhone()) {
$conversation->status = Conversation::STATUS_PENDING;
}
// Set assignee
$conversation->user_id = null;
if (!empty($data['user_id'])) {
$user_assignee = User::find($data['user_id']);
if ($user_assignee) {
$conversation->user_id = $user_assignee->id;
}
}
$conversation->updateFolder();
$conversation->save();
// Create threads.
$threads = array_reverse($threads);
$thread_created = false;
$last_customer_id = null;
2021-03-13 11:18:38 +01:00
$thread_result = null;
foreach ($threads as $thread) {
$thread['conversation_id'] = $conversation->id;
if ($conversation->imported) {
$thread['imported'] = true;
}
if (!empty($data['status'])) {
$thread['status'] = $data['status'];
}
$thread_result = Thread::createExtended($thread, $conversation, $customer, false);
if ($thread_result) {
$thread_created = true;
}
}
// If no threads created, delete conversation
if (!$thread_created) {
$conversation->delete();
return false;
}
// Restore customer if needed.
// if ($last_customer_id && $last_customer_id != $customer->id) {
// // Otherwise it does not save.
// $conversation = $conversation->fresh();
// $conversation->customer_id = $customer->id;
// $conversation->customer_email = $customer->getMainEmail();
// $conversation->save();
// }
// Update folders counters
$conversation->mailbox->updateFoldersCounters();
2021-03-13 11:18:38 +01:00
return [
'conversation' => $conversation,
'thread' => $thread_result
];
}
2021-03-02 07:50:19 +01:00
public function getChannelName()
{
2022-01-30 14:43:18 +01:00
return self::channelCodeToName($this->channel);
}
public static function channelCodeToName($channel)
{
return \Eventy::filter('channel.name', '', $channel);
2021-03-02 07:50:19 +01:00
}
public static function subjectFromText($text)
{
return \Helper::textPreview($text, self::SUBJECT_LENGTH);
}
public static function refreshConversations($conversation, $thread)
{
\App\Events\RealtimeConvNewThread::dispatchSelf($thread);
\App\Events\RealtimeMailboxNewThread::dispatchSelf($conversation->mailbox_id);
}
2022-01-22 08:53:12 +01:00
public static function getConvTableSorting($request = null)
{
if (!$request) {
$request = request();
}
$result = [
'sort_by' => 'date',
'order' => 'desc',
];
if (
!empty($request->sorting['sort_by']) && !empty($request->sorting['order']) &&
in_array($request->sorting['sort_by'], ['subject', 'number', 'date']) &&
in_array($request->sorting['order'], ['asc', 'desc'])
2022-01-22 08:53:12 +01:00
) {
$result['sort_by'] = $request->sorting['sort_by'];
$result['order'] = $request->sorting['order'];
2022-01-22 08:53:12 +01:00
}
return $result;
}
2022-01-30 14:43:18 +01:00
public static function search($q, $filters, $user = null, $query_conversations = null)
{
$mailbox_ids = [];
// Like is case insensitive.
$like = '%'.mb_strtolower($q).'%';
if (!$query_conversations) {
$query_conversations = Conversation::select('conversations.*');
}
2022-01-30 14:43:18 +01:00
// https://github.com/laravel/framework/issues/21242
// https://github.com/laravel/framework/pull/27675
2022-01-31 09:29:24 +01:00
$query_conversations->groupby('conversations.id');
2022-01-30 14:43:18 +01:00
if (!empty($filters['mailbox'])) {
// Check if the user has access to the mailbox.
if ($user->hasAccessToMailbox($filters['mailbox'])) {
$mailbox_ids[] = $filters['mailbox'];
} else {
unset($filters['mailbox']);
$mailbox_ids = $user->mailboxesIdsCanView();
}
} else {
// Get IDs of mailboxes to which user has access
$mailbox_ids = $user->mailboxesIdsCanView();
}
$query_conversations->whereIn('conversations.mailbox_id', $mailbox_ids);
$like_op = 'like';
if (\Helper::isPgSql()) {
$like_op = 'ilike';
}
2022-01-30 14:43:18 +01:00
if ($q) {
$query_conversations->where(function ($query) use ($like, $filters, $q, $like_op) {
$query->where('conversations.subject', $like_op, $like)
->orWhere('conversations.customer_email', $like_op, $like)
2022-09-09 09:30:48 +02:00
->orWhere('conversations.'.self::numberFieldName(), (int)$q)
2022-01-30 14:43:18 +01:00
->orWhere('conversations.id', (int)$q)
->orWhere('customers.first_name', $like_op, $like)
->orWhere('customers.last_name', $like_op, $like)
->orWhere('threads.body', $like_op, $like)
->orWhere('threads.from', $like_op, $like)
->orWhere('threads.to', $like_op, $like)
->orWhere('threads.cc', $like_op, $like)
->orWhere('threads.bcc', $like_op, $like);
2022-01-30 14:43:18 +01:00
$query = \Eventy::filter('search.conversations.or_where', $query, $filters, $q);
});
}
// Apply search filters.
if (!empty($filters['assigned'])) {
if ($filters['assigned'] == self::USER_UNASSIGNED) {
$filters['assigned'] = null;
}
2022-01-30 14:43:18 +01:00
$query_conversations->where('conversations.user_id', $filters['assigned']);
}
if (!empty($filters['customer'])) {
$customer_id = $filters['customer'];
$query_conversations->where(function ($query) use ($customer_id) {
$query->where('conversations.customer_id', '=', $customer_id)
->orWhere('threads.created_by_customer_id', '=', $customer_id);
});
}
if (!empty($filters['status'])) {
if (count($filters['status']) == 1) {
// = is faster than IN.
$query_conversations->where('conversations.status', '=', $filters['status'][0]);
} else {
$query_conversations->whereIn('conversations.status', $filters['status']);
}
}
if (!empty($filters['state'])) {
if (count($filters['state']) == 1) {
// = is faster than IN.
$query_conversations->where('conversations.state', '=', $filters['state'][0]);
} else {
$query_conversations->whereIn('conversations.state', $filters['state']);
}
}
if (!empty($filters['subject'])) {
$query_conversations->where('conversations.subject', $like_op, '%'.mb_strtolower($filters['subject']).'%');
2022-01-30 14:43:18 +01:00
}
if (!empty($filters['attachments'])) {
$has_attachments = ($filters['attachments'] == 'yes' ? true : false);
$query_conversations->where('conversations.has_attachments', '=', $has_attachments);
}
if (!empty($filters['type'])) {
$query_conversations->where('conversations.type', '=', $filters['type']);
2022-01-30 14:43:18 +01:00
}
if (!empty($filters['body'])) {
$query_conversations->where('threads.body', $like_op, '%'.mb_strtolower($filters['body']).'%');
2022-01-30 14:43:18 +01:00
}
if (!empty($filters['number'])) {
2022-09-09 09:30:48 +02:00
$query_conversations->where('conversations.'.self::numberFieldName(), '=', $filters['number']);
2022-01-30 14:43:18 +01:00
}
if (!empty($filters['following'])) {
if ($filters['following'] == 'yes') {
$query_conversations->join('followers', function ($join) {
$join->on('followers.conversation_id', '=', 'conversations.id');
$join->where('followers.user_id', auth()->user()->id);
});
}
}
if (!empty($filters['id'])) {
$query_conversations->where('conversations.id', '=', $filters['id']);
}
if (!empty($filters['after'])) {
$query_conversations->where('conversations.created_at', '>=', date('Y-m-d 00:00:00', strtotime($filters['after'])));
}
if (!empty($filters['before'])) {
$query_conversations->where('conversations.created_at', '<=', date('Y-m-d 23:59:59', strtotime($filters['before'])));
}
2022-12-07 07:59:19 +01:00
// Join tables if needed
$query_sql = $query_conversations->toSql();
if (!strstr($query_sql, '`threads`.`conversation_id`')) {
2022-01-31 09:29:24 +01:00
$query_conversations->join('threads', function ($join) {
$join->on('conversations.id', '=', 'threads.conversation_id');
});
}
2022-12-07 07:59:19 +01:00
if (!strstr($query_sql, '`customers`.`id`')) {
$query_conversations->leftJoin('customers', 'conversations.customer_id', '=' ,'customers.id');
}
2022-03-04 13:01:15 +01:00
$query_conversations = \Eventy::filter('search.conversations.apply_filters', $query_conversations, $filters, $q);
$sorting = Conversation::getConvTableSorting();
if ($sorting['sort_by'] == 'date') {
$sorting['sort_by'] = 'last_reply_at';
}
$query_conversations->orderBy($sorting['sort_by'], $sorting['order']);
2022-01-30 14:43:18 +01:00
return $query_conversations;
}
public function getNumberAttribute($value)
{
if (self::$custom_number_cache === null) {
self::$custom_number_cache = config('app.custom_number');
}
if (self::$custom_number_cache) {
return $value;
} else {
return $this->id;
}
}
2022-09-09 09:30:48 +02:00
public static function numberFieldName()
{
if (self::$custom_number_cache === null) {
self::$custom_number_cache = config('app.custom_number');
}
if (self::$custom_number_cache) {
return 'number';
} else {
return 'id';
}
}
2022-12-18 07:20:43 +01:00
/**
* Get meta value.
*/
public function getMeta($key, $default = null)
{
if (isset($this->meta[$key])) {
return $this->meta[$key];
} else {
return $default;
}
}
2019-06-24 07:39:30 +02:00
2022-12-18 07:20:43 +01:00
/**
* Set meta value.
*/
public function setMeta($key, $value, $save = false)
{
$meta = $this->meta;
$meta[$key] = $value;
$this->meta = $meta;
2019-06-24 07:39:30 +02:00
2022-12-18 07:20:43 +01:00
if ($save) {
$this->save();
}
}
public static function updatePreview($conversation_id)
{
// Get last suitable thread.
$thread = Thread::where('conversation_id', $conversation_id)
->whereIn('type', [Thread::TYPE_CUSTOMER, Thread::TYPE_MESSAGE, Thread::TYPE_NOTE])
->where('state', Thread::STATE_PUBLISHED)
->where(function ($query) {
$query->where('subtype', null)
->orWhere('subtype', '!=', Thread::SUBTYPE_FORWARD);
})
->orderBy('created_at', 'desc')
->first();
if ($thread) {
$thread->conversation->setPreview($thread->body);
$thread->conversation->save();
}
}
2018-07-14 03:23:37 +02:00
}