2018-07-14 03:23:37 +02:00
< ? php
namespace App ;
use Illuminate\Database\Eloquent\Model ;
class Thread extends Model
{
/**
2018-07-25 19:30:43 +02:00
* By whom action performed ( source_via ) .
2018-07-14 03:23:37 +02:00
*/
const PERSON_CUSTOMER = 1 ;
const PERSON_USER = 2 ;
2018-07-25 19:30:43 +02:00
public static $persons = [
2018-07-14 03:23:37 +02:00
self :: PERSON_CUSTOMER => 'customer' ,
2018-07-25 19:30:43 +02:00
self :: PERSON_USER => 'user' ,
];
2018-07-14 03:23:37 +02:00
/**
2018-07-25 19:30:43 +02:00
* Thread types .
2018-07-14 03:23:37 +02:00
*/
// Email from customer
const TYPE_CUSTOMER = 1 ;
// Thead created by user
const TYPE_MESSAGE = 2 ;
const TYPE_NOTE = 3 ;
// Thread status change
const TYPE_LINEITEM = 4 ;
const TYPE_PHONE = 5 ;
// Forwarded threads
const TYPE_FORWARDPARENT = 6 ;
const TYPE_FORWARDCHILD = 7 ;
const TYPE_CHAT = 8 ;
public static $types = [
2018-07-20 08:19:25 +02:00
// Thread by customer
2018-07-25 19:30:43 +02:00
self :: TYPE_CUSTOMER => 'customer' ,
2018-07-20 08:19:25 +02:00
// Thread by user
2018-07-25 19:30:43 +02:00
self :: TYPE_MESSAGE => 'message' ,
self :: TYPE_NOTE => 'note' ,
2018-07-20 08:19:25 +02:00
// lineitem represents a change of state on the conversation. This could include, but not limited to, the conversation was assigned, the status changed, the conversation was moved from one mailbox to another, etc. A line item won’ t have a body, to/cc/bcc lists, or attachments.
2018-07-25 19:30:43 +02:00
self :: TYPE_LINEITEM => 'lineitem' ,
self :: TYPE_PHONE => 'phone' ,
2018-07-20 08:19:25 +02:00
// When a conversation is forwarded, a new conversation is created to represent the forwarded conversation.
// forwardparent is the type set on the thread of the original conversation that initiated the forward event.
2018-07-25 19:30:43 +02:00
self :: TYPE_FORWARDPARENT => 'forwardparent' ,
2018-07-20 08:19:25 +02:00
// forwardchild is the type set on the first thread of the new forwarded conversation.
2018-07-25 19:30:43 +02:00
self :: TYPE_FORWARDCHILD => 'forwardchild' ,
self :: TYPE_CHAT => 'chat' ,
2018-07-14 03:23:37 +02:00
];
/**
2018-07-27 15:19:25 +02:00
* Statuses ( code must be equal to conversations statuses ) .
2018-07-14 03:23:37 +02:00
*/
const STATUS_ACTIVE = 1 ;
2018-07-27 15:19:25 +02:00
const STATUS_PENDING = 2 ;
const STATUS_CLOSED = 3 ;
const STATUS_SPAM = 4 ;
const STATUS_NOCHANGE = 6 ;
2018-07-14 03:23:37 +02:00
2018-07-25 19:30:43 +02:00
public static $statuses = [
self :: STATUS_ACTIVE => 'active' ,
self :: STATUS_CLOSED => 'closed' ,
self :: STATUS_NOCHANGE => 'nochange' ,
self :: STATUS_PENDING => 'pending' ,
self :: STATUS_SPAM => 'spam' ,
];
2018-07-14 03:23:37 +02:00
/**
2018-07-25 19:30:43 +02:00
* States .
2018-07-14 03:23:37 +02:00
*/
const STATE_DRAFT = 1 ;
const STATE_PUBLISHED = 2 ;
const STATE_HIDDEN = 3 ;
2018-08-02 18:17:13 +02:00
// A state of review means the thread has been stopped by Traffic Cop and is waiting
// to be confirmed (or discarded) by the person that created the thread.
2018-07-14 03:23:37 +02:00
const STATE_REVIEW = 4 ;
2018-07-25 19:30:43 +02:00
public static $states = [
self :: STATE_DRAFT => 'draft' ,
self :: STATE_PUBLISHED => 'published' ,
self :: STATE_HIDDEN => 'hidden' ,
self :: STATE_REVIEW => 'review' ,
];
2018-07-14 03:23:37 +02:00
/**
2018-07-25 19:30:43 +02:00
* Action associated with the line item .
2018-07-14 03:23:37 +02:00
*/
2018-07-25 19:25:00 +02:00
// Conversation's status changed
const ACTION_TYPE_STATUS_CHANGED = 1 ;
// Conversation's assignee changed
const ACTION_TYPE_USER_CHANGED = 2 ;
2018-07-14 03:23:37 +02:00
// The conversation was moved from another mailbox
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_MOVED_FROM_MAILBOX = 3 ;
2018-07-14 03:23:37 +02:00
// Another conversation was merged with this conversation
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_MERGED = 4 ;
2018-07-14 03:23:37 +02:00
// The conversation was imported (no email notifications were sent)
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_IMPORTED = 5 ;
2018-07-14 03:23:37 +02:00
// A workflow was run on this conversation (either automatic or manual)
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_WORKFLOW_MANUAL = 6 ;
const ACTION_TYPE_WORKFLOW_AUTO = 7 ;
2018-07-14 03:23:37 +02:00
// The ticket was imported from an external Service
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_IMPORTED_EXTERNAL = 8 ;
2018-07-14 03:23:37 +02:00
// The customer associated with the ticket was changed
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_CHANGED_TICKET_CUSTOMER = 9 ;
2018-07-14 03:23:37 +02:00
// The ticket was deleted
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_DELETED_TICKET = 10 ;
2018-07-14 03:23:37 +02:00
// The ticket was restored
2018-07-25 19:25:00 +02:00
const ACTION_TYPE_RESTORE_TICKET = 11 ;
2018-07-14 03:23:37 +02:00
// Describes an optional action associated with the line item
// todo: values need to be checked via HelpScout API
public static $action_types = [
2018-07-25 19:30:43 +02:00
self :: ACTION_TYPE_STATUS_CHANGED => 'changed-ticket-status' ,
self :: ACTION_TYPE_USER_CHANGED => 'changed-ticket-assignee' ,
self :: ACTION_TYPE_MOVED_FROM_MAILBOX => 'moved-from-mailbox' ,
self :: ACTION_TYPE_MERGED => 'merged' ,
self :: ACTION_TYPE_IMPORTED => 'imported' ,
self :: ACTION_TYPE_WORKFLOW_MANUAL => 'manual-workflow' ,
self :: ACTION_TYPE_WORKFLOW_AUTO => 'automatic-workflow' ,
self :: ACTION_TYPE_IMPORTED_EXTERNAL => 'imported-external' ,
self :: ACTION_TYPE_CHANGED_TICKET_CUSTOMER => 'changed-ticket-customer' ,
self :: ACTION_TYPE_DELETED_TICKET => 'deleted-ticket' ,
self :: ACTION_TYPE_RESTORE_TICKET => 'restore-ticket' ,
2018-07-14 03:23:37 +02:00
];
2018-07-25 19:30:43 +02:00
/**
* 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-25 19:30:43 +02:00
2018-07-14 03:23:37 +02:00
public static $source_types = [
2018-07-25 19:30:43 +02:00
self :: SOURCE_TYPE_EMAIL => 'email' ,
self :: SOURCE_TYPE_WEB => 'web' ,
self :: SOURCE_TYPE_API => 'api' ,
2018-07-14 03:23:37 +02:00
];
2018-07-25 19:30:43 +02:00
/**
* Status of the email sent to the customer or user , to whom the thread is assigned .
2018-07-14 03:23:37 +02:00
*/
const SEND_STATUS_TOSEND = 1 ;
2018-08-02 18:17:13 +02:00
const SEND_STATUS_SEND_SUCCESS = 2 ;
const SEND_STATUS_SEND_ERROR = 3 ;
const SEND_STATUS_DELIVERY_SUCCESS = 4 ;
const SEND_STATUS_DELIVERY_ERROR = 5 ;
2018-07-14 03:23:37 +02:00
/**
2018-07-25 19:30:43 +02:00
* The user assigned to this thread ( assignedTo ) .
2018-07-14 03:23:37 +02:00
*/
public function user ()
{
return $this -> belongsTo ( 'App\User' );
}
/**
2018-07-25 19:30:43 +02:00
* Get the thread customer .
2018-07-14 03:23:37 +02:00
*/
public function customer ()
{
return $this -> belongsTo ( 'App\Customer' );
}
/**
2018-07-25 19:30:43 +02:00
* Get conversation .
2018-07-14 03:23:37 +02:00
*/
public function conversation ()
{
return $this -> belongsTo ( 'App\Conversation' );
}
2018-07-20 08:19:25 +02:00
2018-08-04 12:50:55 +02:00
/**
* Get thread attachmets .
*/
public function attachments ()
{
return $this -> hasMany ( 'App\Attachment' );
}
2018-07-20 08:19:25 +02:00
/**
2018-07-25 19:25:00 +02:00
* Get user who created the thread .
*/
public function created_by_user ()
{
return $this -> belongsTo ( 'App\User' );
}
/**
* Get customer who created the thread .
*/
public function created_by_customer ()
{
return $this -> belongsTo ( 'App\Customer' );
}
/**
2018-07-25 19:30:43 +02:00
* Get sanitized body HTML .
*
2018-07-20 08:19:25 +02:00
* @ return string
*/
public function getCleanBody ()
{
return \Purifier :: clean ( $this -> body );
}
/**
* Get thread recipients .
2018-07-25 19:30:43 +02:00
*
2018-07-20 08:19:25 +02:00
* @ return array
*/
2018-08-02 18:17:13 +02:00
public function getToArray ()
2018-07-20 08:19:25 +02:00
{
if ( $this -> to ) {
return json_decode ( $this -> to );
} else {
return [];
}
}
/**
* Get thread CC recipients .
2018-07-25 19:30:43 +02:00
*
2018-07-20 08:19:25 +02:00
* @ return array
*/
2018-08-02 18:17:13 +02:00
public function getCcArray ()
2018-07-20 08:19:25 +02:00
{
if ( $this -> cc ) {
return json_decode ( $this -> cc );
} else {
return [];
}
}
/**
* Get thread BCC recipients .
2018-07-25 19:30:43 +02:00
*
2018-07-20 08:19:25 +02:00
* @ return array
*/
2018-08-02 18:17:13 +02:00
public function getBccArray ()
2018-07-20 08:19:25 +02:00
{
if ( $this -> bcc ) {
return json_decode ( $this -> bcc );
} else {
return [];
}
}
2018-07-30 17:11:35 +02:00
/**
* Set to as JSON .
*/
public function setTo ( $emails )
{
$emails_array = Conversation :: sanitizeEmails ( $emails );
if ( $emails_array ) {
$this -> to = json_encode ( $emails_array );
} else {
$this -> to = null ;
}
}
public function setCc ( $emails )
{
$emails_array = Conversation :: sanitizeEmails ( $emails );
if ( $emails_array ) {
$this -> cc = json_encode ( $emails_array );
} else {
$this -> cc = null ;
}
}
public function setBcc ( $emails )
{
$emails_array = Conversation :: sanitizeEmails ( $emails );
if ( $emails_array ) {
$this -> bcc = json_encode ( $emails_array );
} else {
$this -> bcc = null ;
}
}
2018-07-20 08:19:25 +02:00
/**
2018-07-25 19:25:00 +02:00
* Get thread ' s status name .
2018-07-25 19:30:43 +02:00
*
2018-07-24 08:34:28 +02:00
* @ return string
2018-07-20 08:19:25 +02:00
*/
2018-07-25 19:25:00 +02:00
public function getStatusName ()
{
return self :: statusCodeToName ( $this -> status );
}
/**
* Get status name . Made as a function to allow status names translation .
2018-07-25 19:30:43 +02:00
*
* @ param int $status
*
* @ return string
2018-07-25 19:25:00 +02:00
*/
public static function statusCodeToName ( $status )
2018-07-20 08:19:25 +02:00
{
switch ( $status ) {
case self :: STATUS_ACTIVE :
2018-07-25 19:30:43 +02:00
return __ ( 'Active' );
2018-07-20 08:19:25 +02:00
break ;
case self :: STATUS_PENDING :
2018-07-25 19:30:43 +02:00
return __ ( 'Pending' );
2018-07-20 08:19:25 +02:00
break ;
case self :: STATUS_CLOSED :
2018-07-25 19:30:43 +02:00
return __ ( 'Closed' );
2018-07-20 08:19:25 +02:00
break ;
case self :: STATUS_SPAM :
2018-07-25 19:30:43 +02:00
return __ ( 'Spam' );
2018-07-20 08:19:25 +02:00
break ;
case self :: STATUS_NOCHANGE :
2018-07-25 19:30:43 +02:00
return __ ( 'Not changed' );
2018-07-20 08:19:25 +02:00
break ;
default :
return '' ;
break ;
}
}
2018-07-27 08:01:34 +02:00
/**
* Get text for the assignee .
2018-07-27 15:30:50 +02:00
*
2018-07-27 08:01:34 +02:00
* @ return string
*/
public function getAssignedName ()
{
if ( ! $this -> user_id ) {
2018-07-27 15:30:50 +02:00
return __ ( 'anyone' );
2018-07-27 08:01:34 +02:00
} elseif ( $this -> user_id == auth () -> user () -> id ) {
2018-07-27 15:30:50 +02:00
return __ ( 'yourself' );
2018-07-27 08:01:34 +02:00
} else {
return $this -> user -> getFullName ();
}
}
2018-08-02 18:17:13 +02:00
/**
* Get user or customer who created the thead .
*/
public function getCreatedBy ()
{
if ( ! empty ( $this -> created_by_user_id )) {
return $this -> created_by_user ;
} else {
return $this -> created_by_customer ;
}
}
2018-07-14 03:23:37 +02:00
}