2018-04-08 22:07:13 -04:00
< ? php
namespace BusinessLogic\Tickets ;
2018-04-10 13:11:48 -04:00
use BusinessLogic\Emails\Addressees ;
2018-04-08 22:07:13 -04:00
use BusinessLogic\Emails\EmailSenderHelper ;
2018-04-09 13:01:49 -04:00
use BusinessLogic\Emails\EmailTemplateRetriever ;
2018-04-08 22:07:13 -04:00
use BusinessLogic\Exceptions\ApiFriendlyException ;
2018-04-09 13:01:49 -04:00
use BusinessLogic\Exceptions\ValidationException ;
2018-04-08 22:07:13 -04:00
use BusinessLogic\Helpers ;
use BusinessLogic\Security\UserContext ;
2018-04-09 13:01:49 -04:00
use BusinessLogic\Statuses\Closable ;
use BusinessLogic\Statuses\DefaultStatusForAction ;
2018-04-08 22:07:13 -04:00
use BusinessLogic\ValidationModel ;
use DataAccess\AuditTrail\AuditTrailGateway ;
2018-04-09 13:01:49 -04:00
use DataAccess\Security\LoginGateway ;
2018-04-08 22:07:13 -04:00
use DataAccess\Security\UserGateway ;
use DataAccess\Statuses\StatusGateway ;
2018-04-09 13:01:49 -04:00
use DataAccess\Tickets\ReplyGateway ;
2018-04-08 22:07:13 -04:00
use DataAccess\Tickets\TicketGateway ;
2018-04-10 13:11:48 -04:00
class ReplyCreator extends \BaseClass {
2018-04-08 22:07:13 -04:00
private $statusGateway ;
private $ticketGateway ;
private $emailSenderHelper ;
private $userGateway ;
private $auditTrailGateway ;
2018-04-09 13:01:49 -04:00
private $loginGateway ;
private $replyGateway ;
2018-04-08 22:07:13 -04:00
public function __construct ( StatusGateway $statusGateway ,
TicketGateway $ticketGateway ,
EmailSenderHelper $emailSenderHelper ,
UserGateway $userGateway ,
2018-04-09 13:01:49 -04:00
AuditTrailGateway $auditTrailGateway ,
LoginGateway $loginGateway ,
ReplyGateway $replyGateway ) {
2018-04-08 22:07:13 -04:00
$this -> statusGateway = $statusGateway ;
$this -> ticketGateway = $ticketGateway ;
$this -> emailSenderHelper = $emailSenderHelper ;
$this -> userGateway = $userGateway ;
$this -> auditTrailGateway = $auditTrailGateway ;
2018-04-09 13:01:49 -04:00
$this -> loginGateway = $loginGateway ;
$this -> replyGateway = $replyGateway ;
2018-04-08 22:07:13 -04:00
}
/**
* @ param $replyRequest CreateReplyRequest
* @ param $heskSettings array
* @ param $modsForHeskSettings array
* @ throws ApiFriendlyException
2018-04-09 13:01:49 -04:00
* @ throws \Exception
2018-04-08 22:07:13 -04:00
*/
2018-04-10 13:11:48 -04:00
function createReplyByCustomer ( $replyRequest , $heskSettings , $modsForHeskSettings ) {
2018-04-08 22:07:13 -04:00
$ticket = $this -> ticketGateway -> getTicketByTrackingId ( $replyRequest -> trackingId , $heskSettings );
if ( $ticket === null ) {
throw new ApiFriendlyException ( " Ticket with tracking ID { $replyRequest -> trackingId } not found. " ,
" Ticket not found " , 404 );
}
$validationModel = new ValidationModel ();
2018-05-19 22:30:34 -04:00
if ( $ticket -> id !== $replyRequest -> ticketId ) {
$validationModel -> errorKeys [] = 'TICKET_ID_TRACKING_NUMBER_MISMATCH' ;
}
2018-04-10 13:11:48 -04:00
if ( $replyRequest -> replyMessage === null || trim ( $replyRequest -> replyMessage ) === '' ) {
2018-04-08 22:07:13 -04:00
$validationModel -> errorKeys [] = 'MESSAGE_REQUIRED' ;
2018-04-10 13:11:48 -04:00
}
2018-05-19 22:30:34 -04:00
if ( $replyRequest -> ipAddress === null || trim ( $replyRequest -> ipAddress ) === '' ) {
$validationModel -> errorKeys [] = 'IP_REQUIRED' ;
}
if ( $replyRequest -> hasHtml === null ) {
$validationModel -> errorKeys [] = 'HAS_HTML_REQUIRED' ;
}
2018-04-10 13:11:48 -04:00
if ( $heskSettings [ 'email_view_ticket' ]) {
if ( $replyRequest -> emailAddress === null || trim ( $replyRequest -> emailAddress ) === '' ) {
$validationModel -> errorKeys [] = 'EMAIL_REQUIRED' ;
} elseif ( ! in_array ( $replyRequest -> emailAddress , $ticket -> email )) {
$validationModel -> errorKeys [] = 'EMAIL_NOT_FOUND_ON_TICKET' ;
}
}
2018-04-09 13:01:49 -04:00
2018-04-10 13:11:48 -04:00
if ( count ( $validationModel -> errorKeys ) > 0 ) {
2018-04-09 13:01:49 -04:00
throw new ValidationException ( $validationModel );
2018-04-08 22:07:13 -04:00
}
2018-05-19 22:30:34 -04:00
if ( $replyRequest -> hasHtml ) {
2018-04-08 22:07:13 -04:00
$replyRequest -> replyMessage = Helpers :: heskMakeUrl ( $replyRequest -> replyMessage );
$replyRequest -> replyMessage = nl2br ( $replyRequest -> replyMessage );
}
2018-04-09 13:01:49 -04:00
if ( $this -> loginGateway -> isIpLockedOut ( $replyRequest -> ipAddress , $heskSettings )) {
throw new ApiFriendlyException ( " The IP address entered has been locked out of the system for { $heskSettings [ 'attempt_banmin' ] } minutes because of too many login failures " ,
" Locked Out " ,
403 );
}
if ( $this -> ticketGateway -> areRepliesBeingFlooded ( $replyRequest -> ticketId , $replyRequest -> ipAddress , $heskSettings )) {
throw new ApiFriendlyException ( " You have been locked out of the system for { $heskSettings [ 'attempt_banmin' ] } minutes because of too many replies to a ticket. " ,
" Locked Out " ,
403 );
}
// If staff hasn't replied yet, don't change the status; otherwise set it to the status for customer replies
$currentStatus = $this -> statusGateway -> getStatusById ( $ticket -> statusId , $heskSettings );
if ( $currentStatus -> closable === Closable :: YES || $currentStatus -> closable === Closable :: CUSTOMERS_ONLY ) {
$customerReplyStatus = $this -> statusGateway -> getStatusForDefaultAction ( DefaultStatusForAction :: CUSTOMER_REPLY , $heskSettings );
$defaultNewTicketStatus = $this -> statusGateway -> getStatusForDefaultAction ( DefaultStatusForAction :: NEW_TICKET , $heskSettings );
$ticket -> statusId = $ticket -> statusId === $defaultNewTicketStatus -> id ?
$defaultNewTicketStatus -> id :
$customerReplyStatus -> id ;
}
$this -> ticketGateway -> updateMetadataForReply ( $ticket -> id , $ticket -> statusId , $heskSettings );
2018-04-10 13:11:48 -04:00
$createdReply = $this -> replyGateway -> insertReply ( $ticket -> id , $ticket -> name , $replyRequest -> replyMessage , $replyRequest -> hasHtml , $heskSettings );
2018-04-09 13:01:49 -04:00
//-- Changing the ticket message to the reply's
$ticket -> message = $replyRequest -> replyMessage ;
2018-04-10 13:11:48 -04:00
$addressees = new Addressees ();
if ( $ticket -> ownerId !== null && $ticket -> ownerId !== 0 ) {
$owner = $this -> userGateway -> getUserById ( $ticket -> ownerId , $heskSettings );
if ( $owner -> notificationSettings -> replyToMe ) {
$addressees -> to [] = $owner -> email ;
$language = $owner -> language === null ? $heskSettings [ 'language' ] : $owner -> language ;
$this -> emailSenderHelper -> sendEmailForTicket ( EmailTemplateRetriever :: NEW_REPLY_BY_CUSTOMER ,
$language ,
$addressees ,
$ticket ,
$heskSettings ,
$modsForHeskSettings );
}
} else {
$users = $this -> userGateway -> getUsersForUnassignedReplyNotification ( $heskSettings );
foreach ( $users as $user ) {
$addressees -> to [] = $user -> email ;
$language = $user -> language === null ? $heskSettings [ 'language' ] : $user -> language ;
$this -> emailSenderHelper -> sendEmailForTicket ( EmailTemplateRetriever :: NEW_REPLY_BY_CUSTOMER ,
$language ,
$addressees ,
$ticket ,
$heskSettings ,
$modsForHeskSettings );
}
}
return $createdReply ;
2018-04-08 22:07:13 -04:00
}
}