Пример #1
0
 function addTicketReply($params, $files = array())
 {
     $db = JFactory::getDBO();
     // get the ticket
     $ticket_id = (int) $params['ticket_id'];
     $ticket =& JTable::getInstance('RSTicketsPro_Tickets', 'Table');
     $ticket->load($params['ticket_id']);
     // get the department
     $department_id = $ticket->department_id;
     $department =& JTable::getInstance('RSTicketsPro_Departments', 'Table');
     $department->load($department_id);
     // date and time
     if (!empty($params['date'])) {
         $date = JFactory::getDate($params['date']);
     } else {
         $date = JFactory::getDate();
     }
     $date = $date->toUnix();
     $user_id = (int) $params['user_id'];
     $message = $params['message'];
     if (!RSTicketsProHelper::getConfig('allow_rich_editor')) {
         $message = RSTicketsProHelper::htmlEscape($message);
     }
     // append the signature ?
     if ($ticket->customer_id != $user_id) {
         $is_staff = RSTicketsProHelper::isStaff($user_id);
         if ($is_staff) {
             $signature = RSTicketsProHelper::getSignature();
             if (!empty($params['use_signature']) && $signature) {
                 $message .= '<div class="rst_signature">' . $signature . '</div>';
             }
         }
     } else {
         $is_staff = false;
     }
     // save the ticket message
     $ticket_message =& JTable::getInstance('RSTicketsPro_Ticket_Messages', 'Table');
     $ticket_message->ticket_id = $ticket_id;
     $ticket_message->user_id = $user_id;
     $ticket_message->message = $message;
     $ticket_message->date = $date;
     $ticket_message->store();
     if (!RSTicketsProHelper::getConfig('allow_rich_editor')) {
         $message = nl2br($message);
     }
     $ticket_message_id = $ticket_message->id;
     jimport('joomla.filesystem.file');
     // save the files in the uploads folder & database
     $attachments = RSTicketsProHelper::addAttachments($ticket_id, $ticket_message_id, $department, $files);
     if (count($attachments)) {
         $db->setQuery("UPDATE #__rsticketspro_tickets SET has_files='1' WHERE id='" . $ticket_id . "'");
         $db->query();
     }
     // if a customer replied, we don't need to autoclose anymore
     if (!$is_staff || $ticket->customer_id == $user_id) {
         $db->setQuery("UPDATE #__rsticketspro_tickets SET autoclose_sent='0' WHERE id='" . $ticket_id . "' LIMIT 1");
         $db->query();
     }
     // assign the ticket if the department's assignment type is static and the ticket isn't already assigned
     if ($is_staff && $department->assignment_type == 0 && $ticket->staff_id == 0) {
         $ticket->staff_id = $user_id;
         $db->setQuery("UPDATE #__rsticketspro_tickets SET staff_id='" . $user_id . "' WHERE id='" . $ticket_id . "' LIMIT 1");
         $db->query();
     }
     // update the status if not closed
     if ($ticket->status_id != 2) {
         if (!$is_staff || $ticket->customer_id == $user_id) {
             $ticket->status_id = 1;
         } elseif ($is_staff) {
             $ticket->status_id = 3;
         }
         // set to on-hold if a staff member replied
         $db->setQuery("UPDATE #__rsticketspro_tickets SET status_id='" . $ticket->status_id . "' WHERE id='" . $ticket_id . "' LIMIT 1");
         $db->query();
     }
     $last_reply_customer = $is_staff ? 0 : 1;
     $db->setQuery("UPDATE #__rsticketspro_tickets SET last_reply='" . $date . "', replies = replies + 1, last_reply_customer='" . $last_reply_customer . "' WHERE id='" . $ticket_id . "' LIMIT 1");
     $db->query();
     // get email sending settings
     $from = RSTicketsProHelper::getConfig('email_address');
     $fromname = RSTicketsProHelper::getConfig('email_address_fullname');
     // are we using global ?
     if (RSTicketsProHelper::getConfig('email_use_global')) {
         $config = new JConfig();
         $from = $config->mailfrom;
         $fromname = $config->fromname;
     }
     if (!$department->get('email_use_global')) {
         $from = $department->email_address;
         $fromname = $department->email_address_fullname;
     }
     // send email to the staff member with the customer's reply
     if (empty($params['dont_send_emails']) && !$is_staff && $department->staff_send_email && $ticket->staff_id) {
         $email = RSTicketsProHelper::getEmail('add_ticket_reply_staff');
         $customer = JFactory::getUser($user_id);
         $staff = JFactory::getUser($ticket->staff_id);
         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{department_id}', '{department_name}');
         $with = array(JURI::root(), RSTicketsProHelper::mailRoute('index.php?option=com_rsticketspro&view=ticket&cid=' . $ticket_id . ':' . JFilterOutput::stringURLSafe($ticket->subject), true, RSTicketsProHelper::getConfig('staff_itemid')), $customer->get('name'), $customer->get('username'), $customer->get('email'), $staff->get('name'), $staff->get('username'), $staff->get('email'), $ticket->code, $ticket->subject, $message, $department->get('id'), JText::_($department->get('name')));
         $email_subject = '[' . $ticket->code . '] ' . $ticket->subject;
         $email_message = str_replace($replace, $with, $email->message);
         $email_message = RSTicketsProHelper::getReplyAbove() . $email_message;
         RSTicketsProHelper::sendMail($from, $fromname, $staff->get('email'), $email_subject, $email_message, 1, $department->staff_attach_email ? $attachments : null, $department->get('cc'), $department->get('bcc'));
     }
     // send email to the customer with the staff member's reply
     if (empty($params['dont_send_emails']) && $is_staff && $department->customer_send_email) {
         $email = RSTicketsProHelper::getEmail('add_ticket_reply_customer');
         $customer = JFactory::getUser($ticket->customer_id);
         $staff = JFactory::getUser($user_id);
         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{department_id}', '{department_name}');
         $with = array(JURI::root(), RSTicketsProHelper::mailRoute('index.php?option=com_rsticketspro&view=ticket&cid=' . $ticket_id . ':' . JFilterOutput::stringURLSafe($ticket->subject), true, RSTicketsProHelper::getConfig('customer_itemid')), $customer->get('name'), $customer->get('username'), $customer->get('email'), $staff->get('name'), $staff->get('username'), $staff->get('email'), $ticket->code, $ticket->subject, $message, $department->get('id'), JText::_($department->get('name')));
         $email_subject = '[' . $ticket->code . '] ' . $ticket->subject;
         $email_message = str_replace($replace, $with, $email->message);
         $email_message = RSTicketsProHelper::getReplyAbove() . $email_message;
         RSTicketsProHelper::sendMail($from, $fromname, $customer->get('email'), $email_subject, $email_message, 1, $department->customer_attach_email ? $attachments : null, $department->get('cc'), $department->get('bcc'));
     }
     if (!$is_staff) {
         //check if notification email address is not empty
         $to = RSTicketsProHelper::getConfig('notice_email_address');
         $to = explode(',', $to);
         $staff = JFactory::getUser($ticket->staff_id);
         if ($staff->get('email')) {
             $to[] = $staff->get('email');
         }
         //check if number of max replies is reached
         if (!empty($to)) {
             $max_replies_nr = (int) RSTicketsProHelper::getConfig('notice_max_replies_nr');
             $current_replies = RSTicketsProHelper::getConsecutiveReplies($ticket->id);
             if ($max_replies_nr > 0 && $current_replies == $max_replies_nr && !$ticket->staff_id) {
                 $email = RSTicketsProHelper::getEmail('notification_max_replies_nr');
                 $customer = JFactory::getUser($user_id);
                 $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{code}', '{subject}', '{message}', '{replies}', '{department_id}', '{department_name}');
                 $with = array(JURI::root(), RSTicketsProHelper::mailRoute('index.php?option=com_rsticketspro&view=ticket&cid=' . $ticket_id . ':' . JFilterOutput::stringURLSafe($ticket->subject), true, RSTicketsProHelper::getConfig('staff_itemid')), $customer->get('name'), $customer->get('username'), $customer->get('email'), $ticket->code, $ticket->subject, $message, $current_replies, $department->get('id'), JText::_($department->get('name')));
                 $email_subject = str_replace($replace, $with, $email->subject);
                 $email_message = str_replace($replace, $with, $email->message);
                 $email_message = RSTicketsProHelper::getReplyAbove() . $email_message;
                 RSTicketsProHelper::sendMail($from, $fromname, $to, $email_subject, $email_message, 1, null, $department->get('cc'), $department->get('bcc'));
             }
             //check if number of max replies with no staff response is reached
             $max_replies_nr = (int) RSTicketsProHelper::getConfig('notice_replies_with_no_response_nr');
             if ($max_replies_nr > 0 && $current_replies == $max_replies_nr && $ticket->staff_id) {
                 $email = RSTicketsProHelper::getEmail('notification_replies_with_no_response_nr');
                 $customer = JFactory::getUser($user_id);
                 $staff = JFactory::getUser($ticket->staff_id);
                 $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{replies}', '{department_id}', '{department_name}');
                 $with = array(JURI::root(), RSTicketsProHelper::mailRoute('index.php?option=com_rsticketspro&view=ticket&cid=' . $ticket_id . ':' . JFilterOutput::stringURLSafe($ticket->subject), true, RSTicketsProHelper::getConfig('staff_itemid')), $customer->get('name'), $customer->get('username'), $customer->get('email'), $staff->get('name'), $staff->get('username'), $staff->get('email'), $ticket->code, $ticket->subject, $message, $current_replies, $department->get('id'), JText::_($department->get('name')));
                 $email_subject = str_replace($replace, $with, $email->subject);
                 $email_message = str_replace($replace, $with, $email->message);
                 $email_message = RSTicketsProHelper::getReplyAbove() . $email_message;
                 RSTicketsProHelper::sendMail($from, $fromname, $to, $email_subject, $email_message, 1, null, $department->get('cc'), $department->get('bcc'));
             }
             //check if it has restricted words
             $keywords = explode(',', RSTicketsProHelper::getConfig('notice_not_allowed_keywords'));
             if (!empty($keywords)) {
                 foreach ($keywords as $word) {
                     $word = trim($word);
                     if (empty($word)) {
                         continue;
                     }
                     $pattern = '/\\b(\\w*' . preg_quote($word) . '\\w*)\\b/';
                     if (preg_match($pattern, $message)) {
                         $email = RSTicketsProHelper::getEmail('notification_not_allowed_keywords');
                         $customer = JFactory::getUser($user_id);
                         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{department_id}', '{department_name}');
                         $with = array(JURI::root(), RSTicketsProHelper::mailRoute('index.php?option=com_rsticketspro&view=ticket&cid=' . $ticket_id . ':' . JFilterOutput::stringURLSafe($ticket->subject), true, RSTicketsProHelper::getConfig('staff_itemid')), $customer->get('name'), $customer->get('username'), $customer->get('email'), $staff->get('name'), $staff->get('username'), $staff->get('email'), $ticket->code, $ticket->subject, $message, $department->get('id'), JText::_($department->get('name')));
                         $email_subject = str_replace($replace, $with, $email->subject);
                         $email_message = str_replace($replace, $with, $email->message);
                         $email_message = RSTicketsProHelper::getReplyAbove() . $email_message;
                         RSTicketsProHelper::sendMail($from, $fromname, $to, $email_subject, $email_message, 1, null, $department->get('cc'), $department->get('bcc'));
                         break;
                     }
                 }
             }
         }
     }
     if (!empty($params['return_array'])) {
         return array($ticket_message_id, $attachments);
     }
     return $ticket_message_id;
 }