コード例 #1
0
ファイル: ticket.php プロジェクト: atikahmed/joomla-probid
 function _saveDepartment()
 {
     $department_id = (int) $this->_data['department_id'];
     if (!$this->_permissions->move_ticket || $department_id == $this->_ticket->department_id) {
         return;
     }
     $this->updates[] = "`department_id`='" . $department_id . "'";
     $department =& JTable::getInstance('RSTicketsPro_Departments', 'Table');
     $department->load($department_id);
     // random
     if ($department->get('generation_rule') == 1) {
         // add the department prefix
         $code = $department->get('prefix') . '-' . strtoupper(RSTicketsProHelper::generateNumber(10));
         $this->_db->setQuery("SELECT id FROM #__rsticketspro_tickets WHERE code='" . $code . "'");
         while ($this->_db->loadResult()) {
             // add the department prefix
             $code = $department->get('prefix') . '-' . strtoupper(RSTicketsProHelper::generateNumber(10));
             $this->_db->setQuery("SELECT id FROM #__rsticketspro_tickets WHERE code='" . $code . "'");
         }
     } else {
         $code = $department->get('next_number');
         $code = str_pad($code, 10, 0, STR_PAD_LEFT);
         // add the department prefix
         $code = $department->get('prefix') . '-' . $code;
         $this->_db->setQuery("UPDATE #__rsticketspro_departments SET `next_number` = `next_number` + 1 WHERE id='" . $department_id . "' LIMIT 1");
         $this->_db->query();
     }
     $this->updates[] = "`code`='" . $code . "'";
     $this->_db->setQuery("SELECT v.custom_field_id, v.value, cf.type, cf.name FROM #__rsticketspro_custom_fields_values v LEFT JOIN #__rsticketspro_custom_fields cf ON (cf.id = v.custom_field_id) WHERE v.ticket_id='" . (int) $this->_ticket->id . "' AND cf.published='1'");
     $current_fields = $this->_db->loadObjectList();
     foreach ($current_fields as $field) {
         // check if there's a field that matches
         $this->_db->setQuery("SELECT id FROM #__rsticketspro_custom_fields WHERE department_id='" . $department_id . "' AND name LIKE '" . $this->_db->getEscaped($field->name) . "'");
         $found = $this->_db->loadObject();
         if ($found) {
             $this->_db->setQuery("SELECT id FROM #__rsticketspro_custom_fields_values WHERE custom_field_id='" . $found->id . "' AND ticket_id='" . $this->_ticket->id . "' LIMIT 1");
             $duplicate = $this->_db->loadResult();
             if ($duplicate) {
                 continue;
             }
             $new_field =& JTable::getInstance('RSTicketsPro_Custom_Fields_Values', 'Table');
             $new_field->custom_field_id = $found->id;
             $new_field->ticket_id = $this->_ticket->id;
             $new_field->value = $field->value;
             $this->_db->setQuery("SELECT id FROM #__rsticketspro_custom_field_values WHERE custom_field_id='" . $found->id . "' AND ticket_id='" . $this->_ticket->id . "'");
             if (!$this->_db->loadResult()) {
                 $new_field->store();
             }
         }
     }
 }
コード例 #2
0
 function addTicket($params, $custom_fields = array(), $files = array())
 {
     $db = JFactory::getDBO();
     $department_id = (int) $params['department_id'];
     if (!$department_id) {
         return false;
     }
     // check user
     $logged = 1;
     $customer_id = (int) @$params['customer_id'];
     // create a new user if no user specified
     if (!$customer_id) {
         $logged = 0;
         $data = array();
         $data['name'] = @$params['name'];
         $customer_id = RSTicketsProHelper::createUser(@$params['email'], $data);
     }
     // get the department
     $department =& JTable::getInstance('RSTicketsPro_Departments', 'Table');
     $department->load($department_id);
     // priority
     $priority_id = (int) @$params['priority_id'];
     if (!$priority_id) {
         $department->get('priority_id');
     }
     // unassigned
     $staff_id = 0;
     // auto-assign to staff member with the least assigned tickets
     if ($department->get('assignment_type') == 1) {
         // select staff members that belong to this department
         $db->setQuery("SELECT user_id FROM #__rsticketspro_staff_to_department WHERE department_id='" . $department->get('id') . "'");
         $staff_ids = $db->loadResultArray();
         if (!empty($staff_ids)) {
             $staff_ids = implode(',', $staff_ids);
             // select groups that can answer tickets
             $db->setQuery("SELECT id FROM #__rsticketspro_groups WHERE answer_ticket='1'");
             $group_ids = $db->loadResultArray();
             if (!empty($group_ids)) {
                 $group_ids = implode(',', $group_ids);
                 $db->setQuery("SELECT user_id FROM #__rsticketspro_staff WHERE group_id IN (" . $group_ids . ") AND user_id IN (" . $staff_ids . ") AND priority_id IN (0" . ($priority_id ? "," . $priority_id : "") . ")");
                 $staff_ids = $db->loadResultArray();
                 if (!empty($staff_ids)) {
                     $db->setQuery("SELECT staff_id, COUNT(id) AS tickets FROM #__rsticketspro_tickets WHERE status_id != 2 AND staff_id IN (" . implode(',', $staff_ids) . ") GROUP BY staff_id ORDER BY tickets ASC");
                     $results = $db->loadObjectList('staff_id');
                     // must make sure we cover all staff members, even those who don't have tickets yet
                     foreach ($staff_ids as $staff) {
                         if (!isset($results[$staff])) {
                             // found a staff member who has 0 tickets - assign
                             $staff_id = $staff;
                             break;
                         }
                     }
                     // no staff member assigned so far - must grab from query the first result
                     if (empty($staff_id)) {
                         if ($tmp = reset($results)) {
                             $staff_id = $tmp->staff_id;
                         }
                     }
                     // get a random staff id from all the members
                     if (empty($staff_id)) {
                         $staff_ids = explode(',', $staff_ids);
                         $staff_id = $staff_ids[mt_rand(0, count($staff_ids) - 1)];
                     }
                 }
             }
         }
         if (empty($staff_id)) {
             $staff_id = 0;
         }
     }
     // random
     if ($department->get('generation_rule') == 1) {
         // add the department prefix
         $code = $department->get('prefix') . '-' . strtoupper(RSTicketsProHelper::generateNumber(10));
         $db->setQuery("SELECT id FROM #__rsticketspro_tickets WHERE code='" . $code . "'");
         while ($db->loadResult()) {
             // add the department prefix
             $code = $department->get('prefix') . '-' . strtoupper(RSTicketsProHelper::generateNumber(10));
             $db->setQuery("SELECT id FROM #__rsticketspro_tickets WHERE code='" . $code . "'");
         }
     } else {
         $code = $department->get('next_number');
         $code = str_pad($code, 10, 0, STR_PAD_LEFT);
         // add the department prefix
         $code = $department->get('prefix') . '-' . $code;
         $db->setQuery("UPDATE #__rsticketspro_departments SET `next_number` = `next_number` + 1 WHERE id='" . $department_id . "' LIMIT 1");
         $db->query();
     }
     // subject
     $subject = $params['subject'];
     // message
     $message = $params['message'];
     // date and time
     if (!empty($params['date'])) {
         $date = JFactory::getDate($params['date']);
     } else {
         $date = JFactory::getDate();
     }
     $date = $date->toUnix();
     // save the ticket details in the database
     $ticket =& JTable::getInstance('RSTicketsPro_Tickets', 'Table');
     $ticket->department_id = $department_id;
     $ticket->staff_id = $staff_id;
     $ticket->customer_id = $customer_id;
     $ticket->code = $code;
     $ticket->subject = $subject;
     $ticket->status_id = 1;
     $ticket->priority_id = $priority_id;
     $ticket->date = $date;
     $ticket->last_reply = $date;
     $ticket->replies = 0;
     $ticket->autoclose_sent = 0;
     $ticket->agent = $params['agent'];
     $ticket->referer = $params['referer'];
     $ticket->ip = $params['ip'];
     $ticket->logged = $logged;
     $ticket->feedback = 0;
     $ticket->store();
     $ticket_id = $ticket->id;
     $message_params = array('ticket_id' => $ticket_id, 'user_id' => $customer_id, 'date' => $date, 'message' => $params['message'], 'dont_send_emails' => 1, 'return_array' => 1);
     $custom_fields_email = '';
     // save the custom fields in the database
     foreach ($custom_fields as $field_id => $value) {
         $new_field =& JTable::getInstance('RSTicketsPro_Custom_Fields_Values', 'Table');
         $new_field->custom_field_id = $field_id;
         $new_field->ticket_id = $ticket_id;
         $new_field->value = is_array($value) ? implode("\n", $value) : $value;
         $new_field->store();
         $custom_field =& JTable::getInstance('RSTicketsPro_Custom_Fields', 'Table');
         $custom_field->load($field_id);
         $custom_fields_email .= '<p>' . JText::_($custom_field->label) . ': ' . (is_array($value) ? implode(', ', $value) : $new_field->value) . '</p>';
     }
     list($ticket_message_id, $attachments) = RSTicketsProHelper::addTicketReply($message_params, $files);
     // 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 customer with a copy of his own ticket
     if ($department->customer_send_copy_email) {
         $email = RSTicketsProHelper::getEmail('add_ticket_customer');
         $customer = JFactory::getUser($customer_id);
         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{code}', '{subject}', '{message}', '{custom_fields}', '{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'), $ticket->code, $ticket->subject, $message, $custom_fields_email, $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'));
     }
     // send email to the staff member that gets assigned this ticket
     if ($department->staff_send_email && $staff_id) {
         $email = RSTicketsProHelper::getEmail('add_ticket_staff');
         $customer = JFactory::getUser($customer_id);
         $staff = JFactory::getUser($staff_id);
         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{custom_fields}', '{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, $custom_fields_email, $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'));
     }
     // notify the email addresses configured in the department
     if ($department->notify_new_tickets_to) {
         $email = RSTicketsProHelper::getEmail('add_ticket_notify');
         $notify_new_tickets_to = $department->notify_new_tickets_to;
         $notify_new_tickets_to = str_replace("\r\n", "\n", $notify_new_tickets_to);
         $notify_new_tickets_to = explode("\n", $notify_new_tickets_to);
         $customer = JFactory::getUser($customer_id);
         $staff = JFactory::getUser($staff_id);
         $replace = array('{live_site}', '{ticket}', '{customer_name}', '{customer_username}', '{customer_email}', '{staff_name}', '{staff_username}', '{staff_email}', '{code}', '{subject}', '{message}', '{custom_fields}', '{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, $custom_fields_email, $department->get('id'), JText::_($department->get('name')));
         $email_subject = '[' . $ticket->code . '] ' . $ticket->subject;
         $email_message = str_replace($replace, $with, $email->message);
         foreach ($notify_new_tickets_to as $notify_email) {
             $notify_email = trim($notify_email);
             RSTicketsProHelper::sendMail($from, $fromname, $notify_email, $email_subject, $email_message, 1, $department->staff_attach_email ? $attachments : null, $department->get('cc'), $department->get('bcc'));
         }
     }
     return $ticket_id;
 }