* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7540 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class AdminCustomerThreads extends AdminTab { public function __construct() { global $cookie; $this->table = 'customer_thread'; $this->lang = false; $this->className = 'CustomerThread'; $this->edit = false; $this->view = true; $this->delete = true; $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->_group = 'GROUP BY cm.id_customer_thread'; $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)$cookie->id_lang.')'; $contactArray = array(); $contacts = Contact::getContacts($cookie->id_lang); foreach ($contacts AS $contact) $contactArray[$contact['id_contact']] = $contact['name']; $languageArray = array(); $languages = Language::getLanguages(); foreach ($languages AS $language) $languageArray[$language['id_lang']] = $language['name']; $statusArray = array( 'open' => $this->l('Open'), 'closed' => $this->l('Closed'), 'pending1' => $this->l('Pending 1'), 'pending2' => $this->l('Pending 2') ); $imagesArray = array( 'open' => 'status_green.gif', 'closed' => 'status_red.gif', 'pending1' => 'status_orange.gif', 'pending2' => 'status_orange.gif' ); $this->fieldsDisplay = 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', 'select' => $contactArray, 'filter_key' => 'cl!id_contact', 'filter_type' => 'int'), 'language' => array('title' => $this->l('Language'), 'width' => 60, 'type' => 'select', 'select' => $languageArray, 'filter_key' => 'l!id_lang', 'filter_type' => 'int'), 'status' => array('title' => $this->l('Status'), 'width' => 50, 'type' => 'select', 'select' => $statusArray, 'icon' => $imagesArray, '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' => 0), 'date_upd' => array('title' => $this->l('Last message'), 'width' => 90) ); parent::__construct(); } public function postProcess() { global $currentIndex, $cookie, $link; 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')) { $statusArray = array(1 => 'open', 2 => 'closed', 3 => 'pending1', 4 => 'pending2'); Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer_thread SET status = "'.$statusArray[$id_status].'" WHERE id_customer_thread = '.(int)$id_customer_thread.' LIMIT 1'); } if (isset($_POST['id_employee_forward'])) { // Todo: need to avoid doubles $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)$cookie->id_lang.') 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->displayMsg($message, true, (int)Tools::getValue('id_employee_forward')); $cm = new CustomerMessage(); $cm->id_employee = (int)$cookie->id_employee; $cm->id_customer_thread = (int)Tools::getValue('id_customer_thread'); $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']); $currentEmployee = new Employee($cookie->id_employee); if (($id_employee = (int)Tools::getValue('id_employee_forward')) AND ($employee = new Employee($id_employee)) AND Validate::isLoadedObject($employee)) { $params = array( '{messages}' => $output, '{employee}' => $currentEmployee->firstname.' '.$currentEmployee->lastname, '{comment}' => stripslashes($_POST['message_forward'])); if (Mail::Send((int)($cookie->id_lang), 'forward_msg', Mail::l('Fwd: Customer message'), $params, $employee->email, $employee->firstname.' '.$employee->lastname, $currentEmployee->email, $currentEmployee->firstname.' '.$currentEmployee->lastname, NULL, NULL, _PS_MAIL_DIR_, true)) { $cm->message = $this->l('Message forwarded to').' '.$employee->firstname.' '.$employee->lastname."\n".$this->l('Comment:').' '.$_POST['message_forward']; $cm->add(); } } elseif (($email = Tools::getValue('email')) AND Validate::isEmail($email)) { $params = array( '{messages}' => $output, '{employee}' => $currentEmployee->firstname.' '.$currentEmployee->lastname, '{comment}' => stripslashes($_POST['message_forward'])); if (Mail::Send((int)($cookie->id_lang), 'forward_msg', Mail::l('Fwd: Customer message'), $params, $email, NULL, $currentEmployee->email, $currentEmployee->firstname.' '.$currentEmployee->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 echo '
'.$key.' | '.$val.' |
'; parent::displayListHeader($token); } private function openUploadedFile() { $filename = $_GET['filename']; $extensions = array('.txt' => 'text/plain', '.rtf' => 'application/rtf', '.doc' => 'application/msword', '.docx'=> 'application/msword', '.pdf' => 'application/pdf', '.zip' => 'multipart/x-zip', '.png' => 'image/png', '.jpeg' => 'image/jpeg', '.gif' => 'image/gif', '.jpg' => 'image/jpeg'); $extension = ''; foreach ($extensions AS $key => $val) if (substr($filename, -4) == $key OR substr($filename, -5) == $key) { $extension = $val; break; } ob_end_clean(); header('Content-Type: '.$extension); header('Content-Disposition:attachment;filename="'.$filename.'"'); readfile(_PS_UPLOAD_DIR_.$filename); die; } private function displayMsg($message, $email = false, $id_employee = null) { global $cookie, $currentIndex; $customersToken = Tools::getAdminToken('AdminCustomers'.(int)(Tab::getIdFromClassName('AdminCustomers')).(int)($cookie->id_employee)); $contacts = Contact::getContacts($cookie->id_lang); if (!$email) { if (!empty($message['id_product']) AND empty($message['employee_name'])) $id_order_product = Db::getInstance()->getValue(' SELECT o.id_order FROM '._DB_PREFIX_.'orders o LEFT JOIN '._DB_PREFIX_.'order_detail od ON o.id_order = od.id_order WHERE o.id_customer = '.(int)$message['id_customer'].' AND od.product_id = '.(int)$message['id_product'].' ORDER BY o.date_add DESC'); $output = '