* @copyright 2007-2012 PrestaShop SA * @version Release: $Revision: 7471 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class AdminCustomerThreadsControllerCore extends AdminController { public function __construct() { $this->context = Context::getContext(); $this->table = 'customer_thread'; $this->className = 'CustomerThread'; $this->lang = false; $contact_array = array(); $contacts = Contact::getContacts($this->context->language->id); foreach ($contacts as $contact) $contact_array[$contact['id_contact']] = $contact['name']; $language_array = array(); $languages = Language::getLanguages(); foreach ($languages as $language) $language_array[$language['id_lang']] = $language['name']; $images_array = array( 'open' => array('src' => 'status_green.png', 'alt' => $this->l('Open')), 'closed' => array('src' => 'status_red.png', 'alt' => $this->l('Closed')), 'pending1' => array('src' => 'status_orange.png', 'alt' => $this->l('Pending 1')), 'pending2' => array('src' => 'status_orange.png', 'alt' => $this->l('Pending 2')), ); $status_array = array(); foreach ($images_array as $k => $v) $status_array[$k] = $v['alt']; $this->fields_list = array( 'id_customer_thread' => array( 'title' => $this->l('ID'), 'width' => 25, ), 'customer' => array( 'title' => $this->l('Customer'), 'width' => 100, 'filter_key' => 'customer', 'tmpTableFilter' => true, ), 'email' => array( 'title' => $this->l('E-mail'), 'width' => 100, 'filter_key' => 'a!email', ), 'contact' => array( 'title' => $this->l('Type'), 'width' => 75, 'type' => 'select', 'list' => $contact_array, 'filter_key' => 'cl!id_contact', 'filter_type' => 'int', ), 'language' => array( 'title' => $this->l('Language'), 'width' => 60, 'type' => 'select', 'list' => $language_array, 'filter_key' => 'l!id_lang', 'filter_type' => 'int', ), 'status' => array( 'title' => $this->l('Status'), 'width' => 50, 'type' => 'select', 'list' => $status_array, 'icon' => $images_array, 'align' => 'center', 'filter_key' => 'a!status', 'filter_type' => 'string', ), 'employee' => array( 'title' => $this->l('Employee'), 'width' => 100, 'filter_key' => 'employee', 'tmpTableFilter' => true, ), 'messages' => array( 'title' => $this->l('Messages'), 'width' => 50, 'filter_key' => 'messages', 'tmpTableFilter' => true, 'maxlength' => 40, ), 'date_upd' => array( 'title' => $this->l('Last message'), 'width' => 90, 'havingFilter' => true, ), ); $this->bulk_actions = array( 'delete' => array( 'text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?'), ), ); $this->shopLinkType = 'shop'; $this->fields_options = array( 'contact' => array( 'title' => $this->l('Contact options'), 'fields' => array( 'PS_CUSTOMER_SERVICE_FILE_UPLOAD' => array( 'title' => $this->l('Allow file upload'), 'desc' => $this->l('Allow customers to upload file using contact page'), 'type' => 'bool' ), 'PS_CUSTOMER_SERVICE_SIGNATURE' => array( 'title' => $this->l('Default message'), 'desc' => $this->l('Please fill the message that appears by default when you answer a thread on the customer service page'), 'type' => 'textareaLang', 'lang' => true, 'rows' => 10, 'cols' => 40 ) ), ), 'general' => array( 'title' => $this->l('Customer service options'), 'fields' => array( 'PS_SAV_IMAP_URL' => array( 'title' => $this->l('IMAP URL'), 'desc' => $this->l('URL for IMAP server (mail.server.com)'), 'type' => 'text', 'size' => 40, 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_PORT' => array( 'title' => $this->l('IMAP port'), 'desc' => $this->l('Port to use to connect to IMAP server'), 'type' => 'text', 'defaultValue' => 143, 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_USER' => array( 'title' => $this->l('IMAP user'), 'desc' => $this->l('User to use to connect to IMAP server'), 'type' => 'text', 'size' => 40, 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_PWD' => array( 'title' => $this->l('IMAP password'), 'desc' => $this->l('Password to use to connect IMAP server'), 'type' => 'text', 'size' => 40, 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_DELETE_MSG' => array( 'title' => $this->l('Delete messages'), 'desc' => $this->l('Delete messages after sync. If you do not active this option, the sync will be longer'), 'type' => 'bool', 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_NORSH' => array( 'title' => $this->l('IMAP options').' (/norsh)', 'type' => 'bool', 'desc' => $this->l('Do not use RSH or SSH to establish a preauthenticated IMAP session'), 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_SSL' => array( 'title' => $this->l('IMAP options').' (/ssl)', 'type' => 'bool', 'desc' => $this->l('Use the Secure Socket Layer to encrypt the session'), 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_VALIDATE-CERT' => array( 'title' => $this->l('IMAP options').' (/validate-cert)', 'type' => 'bool', 'desc' => $this->l('Validate certificates from TLS/SSL server'), 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_NOVALIDATE-CERT' => array( 'title' => $this->l('IMAP options').' (/novalidate-cert)', 'type' => 'bool', 'desc' => $this->l('Do not validate certificates from TLS/SSL server, needed if server uses self-signed certificates'), 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_TLS' => array( 'title' => $this->l('IMAP options').' (/tls)', 'type' => 'bool', 'desc' => $this->l('Force use of start-TLS to encrypt the session, and reject connection to servers that do not support it'), 'visibility' => Shop::CONTEXT_ALL, ), 'PS_SAV_IMAP_OPT_NOTLS' => array( 'title' => $this->l('IMAP options').' (/notls)', 'type' => 'bool', 'desc' => $this->l('Do not use start-TLS to encrypt the session, even with servers that support it'), 'visibility' => Shop::CONTEXT_ALL, ), ), 'submit' => array('title' => $this->l('Save'), 'class' => 'button'), ), ); parent::__construct(); } public function renderList() { $this->addRowAction('view'); $this->addRowAction('delete'); $this->_select = ' CONCAT(c.`firstname`," ",c.`lastname`) as customer, cl.`name` as contact, l.`name` as language, group_concat(message) as messages, ( SELECT IFNULL(CONCAT(LEFT(e.`firstname`, 1),". ",e.`lastname`), "--") FROM `'._DB_PREFIX_.'customer_message` cm2 INNER JOIN '._DB_PREFIX_.'employee e ON e.`id_employee` = cm2.`id_employee` WHERE cm2.id_employee > 0 AND cm2.`id_customer_thread` = a.`id_customer_thread` ORDER BY cm2.`date_add` DESC LIMIT 1 ) as employee'; $this->_join = ' LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = a.`id_customer` LEFT JOIN `'._DB_PREFIX_.'customer_message` cm ON cm.`id_customer_thread` = a.`id_customer_thread` LEFT JOIN `'._DB_PREFIX_.'lang` l ON l.`id_lang` = a.`id_lang` LEFT JOIN `'._DB_PREFIX_.'contact_lang` cl ON (cl.`id_contact` = a.`id_contact` AND cl.`id_lang` = '.(int)$this->context->language->id.')'; $this->_group = 'GROUP BY cm.id_customer_thread'; $contacts = CustomerThread::getContacts(); $categories = Contact::getCategoriesContacts(); $params = array( $this->l('Total threads') => $all = CustomerThread::getTotalCustomerThreads(), $this->l('Threads pending') => $pending = CustomerThread::getTotalCustomerThreads('status LIKE "%pending%"'), $this->l('Total customer messages') => CustomerMessage::getTotalCustomerMessages('id_employee = 0'), $this->l('Total employee messages') => CustomerMessage::getTotalCustomerMessages('id_employee != 0'), $this->l('Unread threads') => $unread = CustomerThread::getTotalCustomerThreads('status = "open"'), $this->l('Closed threads') => $all - ($unread + $pending) ); $this->tpl_list_vars = array( 'contacts' => $contacts, 'categories' => $categories, 'params' => $params ); return parent::renderList(); } public function initToolbar() { parent::initToolbar(); unset($this->toolbar_btn['new']); } public function postProcess() { if ($id_customer_thread = (int)Tools::getValue('id_customer_thread')) { if (($id_contact = (int)Tools::getValue('id_contact'))) Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'customer_thread SET id_contact = '.(int)$id_contact.' WHERE id_customer_thread = '.(int)$id_customer_thread ); if ($id_status = (int)Tools::getValue('setstatus')) { $status_array = array(1 => 'open', 2 => 'closed', 3 => 'pending1', 4 => 'pending2'); Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'customer_thread SET status = "'.$status_array[$id_status].'" WHERE id_customer_thread = '.(int)$id_customer_thread.' LIMIT 1 '); } if (isset($_POST['id_employee_forward'])) { $messages = Db::getInstance()->executeS(' SELECT ct.*, cm.*, cl.name subject, CONCAT(e.firstname, \' \', e.lastname) employee_name, CONCAT(c.firstname, \' \', c.lastname) customer_name, c.firstname FROM '._DB_PREFIX_.'customer_thread ct LEFT JOIN '._DB_PREFIX_.'customer_message cm ON (ct.id_customer_thread = cm.id_customer_thread) LEFT JOIN '._DB_PREFIX_.'contact_lang cl ON (cl.id_contact = ct.id_contact AND cl.id_lang = '.(int)$this->context->language->id.') LEFT OUTER JOIN '._DB_PREFIX_.'employee e ON e.id_employee = cm.id_employee LEFT OUTER JOIN '._DB_PREFIX_.'customer c ON (c.email = ct.email) WHERE ct.id_customer_thread = '.(int)Tools::getValue('id_customer_thread').' ORDER BY cm.date_add DESC '); $output = ''; foreach ($messages as $message) $output .= $this->displayMessage($message, true, (int)Tools::getValue('id_employee_forward')); $cm = new CustomerMessage(); $cm->id_employee = (int)$this->context->employee->id; $cm->id_customer_thread = (int)Tools::getValue('id_customer_thread'); $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']); $current_employee = $this->context->employee; $id_employee = (int)Tools::getValue('id_employee_forward'); $employee = new Employee($id_employee); $email = Tools::getValue('email'); if ($id_employee && $employee && Validate::isLoadedObject($employee)) { $params = array( '{messages}' => Tools::nl2br(stripslashes($output)), '{employee}' => $current_employee->firstname.' '.$current_employee->lastname, '{comment}' => stripslashes($_POST['message_forward'])); if (Mail::Send( $this->context->language->id, 'forward_msg', Mail::l('Fwd: Customer message', $this->context->language->id), $params, $employee->email, $employee->firstname.' '.$employee->lastname, $current_employee->email, $current_employee->firstname.' '.$current_employee->lastname, null, null, _PS_MAIL_DIR_, true)) { $cm->private = 1; $cm->message = $this->l('Message forwarded to').' '.$employee->firstname.' '.$employee->lastname."\n".$this->l('Comment:').' '.$_POST['message_forward']; $cm->add(); } } else if ($email && Validate::isEmail($email)) { $params = array( '{messages}' => Tools::nl2br(stripslashes($output)), '{employee}' => $current_employee->firstname.' '.$current_employee->lastname, '{comment}' => stripslashes($_POST['message_forward'])); if (Mail::Send( $this->context->language->id, 'forward_msg', Mail::l('Fwd: Customer message', $this->context->language->id), $params, $email, null, $current_employee->email, $current_employee->firstname.' '.$current_employee->lastname, null, null, _PS_MAIL_DIR_, true)) { $cm->message = $this->l('Message forwarded to').' '.$email."\n".$this->l('Comment:').' '.$_POST['message_forward']; $cm->add(); } } else $this->errors[] = '
'.$content.'