2016-02-19 15:30:29 +01:00
< ? php
require_once ( dirname ( __FILE__ ) . '/../../config/config.inc.php' );
require_once ( dirname ( __FILE__ ) . '/../../init.php' );
require_once ( dirname ( __FILE__ ) . '/Reason.php' );
$controller -> preProcess ();
$langs = Language :: getLanguages ();
$lang = ( int ) $cookie -> id_lang ;
$isolang = Language :: getIsoById ( $lang );
$errors = array ();
if ( $cookie -> isLogged ()) {
$smarty -> assign ( 'isLogged' , 1 );
$customer = new Customer (( int )( $cookie -> id_customer ));
if ( ! Validate :: isLoadedObject ( $customer )) {
die ( Tools :: displayError ( 'Customer not found' ));
}
$products = array ();
$orders = array ();
$getOrders = Db :: getInstance () -> ExecuteS ( '
SELECT id_order
FROM '._DB_PREFIX_.' orders
WHERE id_customer = '.(int)$customer->id.' ORDER BY date_add
' );
foreach ( $getOrders as $row ) {
$order = new Order ( $row [ 'id_order' ]);
$date = explode ( ' ' , $order -> date_add );
$orders [ $row [ 'id_order' ]] = Tools :: displayDate ( $date [ 0 ], $cookie -> id_lang );
}
$orderList = '' ;
foreach ( $orders as $key => $val ) {
2016-10-24 18:33:51 +02:00
$orderList .= '<option value="' . $key . '" ' . (( int )( Tools :: getValue ( 'id_order' )) == $key ? 'selected' : '' ) . ' >#' . $key . ' - ' . $val . '</option>' ;
2016-02-19 15:30:29 +01:00
}
$smarty -> assign ( 'orderList' , $orderList );
}
if ( Tools :: isSubmit ( 'submitMessage' )) {
$fileAttachment = NULL ;
if ( isset ( $_FILES [ 'fileUpload' ][ 'name' ]) AND ! empty ( $_FILES [ 'fileUpload' ][ 'name' ]) AND ! empty ( $_FILES [ 'fileUpload' ][ 'tmp_name' ]))
{
$extension = array ( '.txt' , '.rtf' , '.doc' , '.docx' , '.pdf' , '.zip' , '.png' , '.jpeg' , '.gif' , '.jpg' );
$filename = uniqid () . substr ( $_FILES [ 'fileUpload' ][ 'name' ], - 5 );
$fileAttachment [ 'content' ] = file_get_contents ( $_FILES [ 'fileUpload' ][ 'tmp_name' ]);
$fileAttachment [ 'name' ] = $_FILES [ 'fileUpload' ][ 'name' ];
$fileAttachment [ 'mime' ] = $_FILES [ 'fileUpload' ][ 'type' ];
}
$message = Tools :: htmlentitiesUTF8 ( Tools :: getValue ( 'message' ));
if ( ! ( $from = trim ( Tools :: getValue ( 'from' ))) OR ! Validate :: isEmail ( $from ))
$errors [] = Tools :: displayError ( 'Invalid e-mail address' );
elseif ( ! ( $message = nl2br2 ( $message )))
$errors [] = Tools :: displayError ( 'Message cannot be blank' );
elseif ( ! Validate :: isCleanHtml ( $message ))
$errors [] = Tools :: displayError ( 'Invalid message' );
elseif ( ! ( $id_contact = ( int )( Tools :: getValue ( 'id_contact' ))) OR ! ( Validate :: isLoadedObject ( $contact = new Contact (( int )( $id_contact ), ( int )( $cookie -> id_lang )))))
$errors [] = Tools :: displayError ( 'Please select a subject on the list.' );
elseif (( $id_contact = ( int )( Tools :: getValue ( 'id_contact' ))) && $id_contact == 9 && ! Tools :: getValue ( 'id_reason' ))
$errors [] = Tools :: displayError ( 'Please select a reason on the list.' );
2017-02-07 16:34:03 +01:00
elseif (( $id_contact = ( int )( Tools :: getValue ( 'id_contact' ))) && ( $id_contact == 9 || $id_contact == 1 || $id_contact == 2 ) && ! Tools :: getValue ( 'id_order' ))
2016-02-19 15:30:29 +01:00
$errors [] = Tools :: displayError ( 'Please enter/choose an order ID' );
2016-10-24 18:33:51 +02:00
elseif (( $id_contact = ( int )( Tools :: getValue ( 'id_contact' ))) && $id_contact == 1 && ! Tools :: getValue ( 'id_products' ))
2016-02-19 15:30:29 +01:00
$errors [] = Tools :: displayError ( 'Please choose a product' );
elseif ( ! empty ( $_FILES [ 'fileUpload' ][ 'name' ]) AND $_FILES [ 'fileUpload' ][ 'error' ] != 0 )
$errors [] = Tools :: displayError ( 'An error occurred during the file upload' );
elseif ( ! empty ( $_FILES [ 'fileUpload' ][ 'name' ]) AND ! in_array ( substr ( $_FILES [ 'fileUpload' ][ 'name' ], - 4 ), $extension ) AND ! in_array ( substr ( $_FILES [ 'fileUpload' ][ 'name' ], - 5 ), $extension ))
$errors [] = Tools :: displayError ( 'Bad file extension' );
else
{
if (( int )( $cookie -> id_customer ))
$customer = new Customer (( int )( $cookie -> id_customer ));
else
{
$customer = new Customer ();
$customer -> getByEmail ( $from );
}
$contact = new Contact ( $id_contact , $cookie -> id_lang );
if ( ! ((
$id_customer_thread = ( int ) Tools :: getValue ( 'id_customer_thread' )
AND ( int ) Db :: getInstance () -> getValue ( '
SELECT cm . id_customer_thread FROM '._DB_PREFIX_.' customer_thread cm
WHERE cm . id_customer_thread = '.(int)$id_customer_thread.' AND token = \ '' . pSQL ( Tools :: getValue ( 'token' )) . '\'' )
) OR (
$id_customer_thread = ( int ) Db :: getInstance () -> getValue ( '
SELECT cm . id_customer_thread FROM '._DB_PREFIX_.' customer_thread cm
WHERE cm . email = \ '' . pSQL ( $from ) . '\' AND cm.id_order = ' . ( int )( Tools :: getValue ( 'id_order' )) . '' )
)))
{
$fields = Db :: getInstance () -> ExecuteS ( '
SELECT cm . id_customer_thread , cm . id_contact , cm . id_customer , cm . id_order , cm . id_product , cm . email
FROM '._DB_PREFIX_.' customer_thread cm
WHERE email = \ '' . pSQL ( $from ) . '\' AND (' .
( $customer -> id ? 'id_customer = ' . ( int )( $customer -> id ) . ' OR ' : '' ) . '
id_order = '.(int)(Tools::getValue(' id_order ')).' ) ' );
$score = 0 ;
foreach ( $fields as $key => $row )
{
$tmp = 0 ;
if (( int ) $row [ 'id_customer' ] AND $row [ 'id_customer' ] != $customer -> id AND $row [ 'email' ] != $from )
continue ;
if ( $row [ 'id_order' ] != 0 AND Tools :: getValue ( 'id_order' ) != $row [ 'id_order' ])
continue ;
if ( $row [ 'email' ] == $from )
$tmp += 4 ;
if ( $row [ 'id_contact' ] == $id_contact )
$tmp ++ ;
if ( Tools :: getValue ( 'id_product' ) != 0 AND $row [ 'id_product' ] == Tools :: getValue ( 'id_product' ))
$tmp += 2 ;
if ( $tmp >= 5 AND $tmp >= $score )
{
$score = $tmp ;
$id_customer_thread = $row [ 'id_customer_thread' ];
}
}
}
$old_message = Db :: getInstance () -> getValue ( '
SELECT cm . message FROM '._DB_PREFIX_.' customer_message cm
WHERE cm . id_customer_thread = '.(int)($id_customer_thread).'
ORDER BY date_add DESC ' );
if ( $old_message == htmlentities ( $message , ENT_COMPAT , 'UTF-8' ))
{
$smarty -> assign ( 'alreadySent' , 1 );
$contact -> email = '' ;
$contact -> customer_service = 0 ;
}
if ( ! empty ( $contact -> email ))
{
2016-11-02 12:29:20 +01:00
if ( Mail :: Send (( int )( $cookie -> id_lang ), 'contact_form' , Mail :: l ( 'Your message has been correctly sent' ), array ( '{message}' => stripslashes ( $message )), $from )) {
2016-02-19 15:30:29 +01:00
$smarty -> assign ( 'confirmation' , 1 );
2016-11-02 12:29:20 +01:00
} else {
$errors [] = Tools :: displayError ( 'An error occurred while sending message.' );
2016-02-19 15:30:29 +01:00
}
// local
2016-11-02 12:29:20 +01:00
// $smarty->assign('confirmation', 1);
2016-02-19 15:30:29 +01:00
}
if ( $contact -> customer_service )
{
if (( int ) $id_customer_thread )
{
$ct = new CustomerThread ( $id_customer_thread );
$ct -> status = 'open' ;
$ct -> id_lang = ( int ) $cookie -> id_lang ;
$ct -> id_contact = ( int )( $id_contact );
2016-10-24 18:33:51 +02:00
if ( $id_order = ( int ) Tools :: getValue ( 'id_order' )) {
2016-02-19 15:30:29 +01:00
$ct -> id_order = $id_order ;
2016-10-24 18:33:51 +02:00
}
if ( $id_products = Tools :: getValue ( 'id_products' )) {
$first_product = explode ( " _ " , $id_products [ 0 ]);
$ct -> id_product = ( int ) $first_product [ 0 ];
}
2016-02-19 15:30:29 +01:00
$ct -> update ();
}
else
{
$ct = new CustomerThread ();
if ( isset ( $customer -> id ))
$ct -> id_customer = ( int )( $customer -> id );
2016-10-24 18:33:51 +02:00
if ( $id_order = ( int ) Tools :: getValue ( 'id_order' )){
2016-02-19 15:30:29 +01:00
$ct -> id_order = $id_order ;
2016-10-24 18:33:51 +02:00
}
if ( $id_products = Tools :: getValue ( 'id_products' )) {
$first_product = explode ( " _ " , $id_products [ 0 ]);
$ct -> id_product = ( int ) $first_product [ 0 ];
}
2016-02-19 15:30:29 +01:00
$ct -> id_contact = ( int )( $id_contact );
$ct -> id_lang = ( int ) $cookie -> id_lang ;
$ct -> email = $from ;
$ct -> status = 'open' ;
$ct -> token = Tools :: passwdGen ( 12 );
$ct -> add ();
}
if ( $ct -> id )
{
// adding (antadis) - linking reason and customer_thread
if ( Tools :: getValue ( 'id_reason' )) {
$id_reason = ( int ) Tools :: getValue ( 'id_reason' );
$reason = new Reason ( $id_reason );
if ( $reason -> id !== null ) {
$reason -> insertReasonCustomerThread (( int ) $ct -> id );
}
}
2016-10-24 18:33:51 +02:00
// adding (antadis) - linking productS and customer_thread
if ( $id_products = Tools :: getValue ( 'id_products' )) {
foreach ( $id_products as $key => $val ) {
$p = explode ( " _ " , $val );
DB :: getInstance () -> execute ( '
INSERT IGNORE INTO `'._DB_PREFIX_.'support_product_customerthread` ( `id_product` , `id_product_attribute` , `id_customer_thread` )
VALUE (
'.(int)$p[0].' ,
'.(int)$p[1].' ,
'.(int)$ct->id.'
)
' );
}
}
2016-02-19 15:30:29 +01:00
$cm = new CustomerMessage ();
$cm -> id_customer_thread = $ct -> id ;
$cm -> message = htmlentities ( $message , ENT_COMPAT , 'UTF-8' );
if ( isset ( $filename ) AND rename ( $_FILES [ 'fileUpload' ][ 'tmp_name' ], _PS_MODULE_DIR_ . '../upload/' . $filename ))
$cm -> file_name = $filename ;
$cm -> ip_address = ip2long ( $_SERVER [ 'REMOTE_ADDR' ]);
$cm -> user_agent = $_SERVER [ 'HTTP_USER_AGENT' ];
if ( $cm -> add ())
{
2016-11-02 12:29:20 +01:00
if ( empty ( $contact -> email )) {
2016-02-19 15:30:29 +01:00
Mail :: Send (( int )( $cookie -> id_lang ), 'contact_form' , Mail :: l ( 'Your message has been correctly sent' ), array ( '{message}' => stripslashes ( $message )), $from );
2016-11-02 12:29:20 +01:00
}
2016-02-19 15:30:29 +01:00
$smarty -> assign ( 'confirmation' , 1 );
}
else
$errors [] = Tools :: displayError ( 'An error occurred while sending message.' );
}
else
$errors [] = Tools :: displayError ( 'An error occurred while sending message.' );
}
2016-11-14 14:19:28 +01:00
// Module::hookExec('ant_alert', array()); // @ToDo - uncomment it
2016-02-19 15:30:29 +01:00
if ( count ( $errors ) > 1 )
array_unique ( $errors );
}
}
$email = Tools :: safeOutput ( Tools :: getValue ( 'from' , (( isset ( $cookie ) AND isset ( $cookie -> email ) AND Validate :: isEmail ( $cookie -> email )) ? $cookie -> email : '' )));
$smarty -> assign ( array (
'errors' => $errors ,
'message' => html_entity_decode ( Tools :: getValue ( 'message' )),
'contacts' => Contact :: getContacts (( int )( $cookie -> id_lang )),
'email' => $email ,
'fileupload' => Configuration :: get ( 'PS_CUSTOMER_SERVICE_FILE_UPLOAD' )
));
if ( $id_customer_thread = ( int ) Tools :: getValue ( 'id_customer_thread' ) AND $token = Tools :: getValue ( 'token' )) {
$customerThread = Db :: getInstance () -> getRow ( '
SELECT cm .* FROM '._DB_PREFIX_.' customer_thread cm
WHERE cm . id_customer_thread = '.(int)$id_customer_thread.' AND token = \ '' . pSQL ( $token ) . '\'' );
$smarty -> assign ( 'customerThread' , $customerThread );
}
if ( isset ( $_GET [ 'id_contact' ]) && is_numeric ( $_GET [ 'id_contact' ])) {
$id_contact = $_GET [ 'id_contact' ];
$contact = new Contact (( int ) $id_contact , $lang );
if ( Validate :: isLoadedObject ( $contact )) {
// A changer selon id_contact
switch ( $id_contact ) {
case '1' :
$allow_product = true ;
$allow_order = true ;
2017-02-07 16:34:03 +01:00
$allow_file = false ;
break ;
case '2' :
$allow_order = true ;
$allow_product = false ;
$allow_file = true ;
2016-02-19 15:30:29 +01:00
break ;
case '9' :
$allow_order = true ;
$allow_product = false ;
2017-02-07 16:34:03 +01:00
$allow_file = false ;
2016-02-19 15:30:29 +01:00
break ;
default :
$allow_product = false ;
$allow_order = false ;
2017-02-07 16:34:03 +01:00
$allow_file = false ;
2016-02-19 15:30:29 +01:00
break ;
}
$smarty -> assign ( array (
2017-02-07 16:34:03 +01:00
'id_contact' => $id_contact ,
2016-02-19 15:30:29 +01:00
'allow_product' => $allow_product ,
2017-02-07 16:34:03 +01:00
'allow_order' => $allow_order ,
'allow_file' => $allow_file ,
'subject' => $contact -> name
2016-02-19 15:30:29 +01:00
));
if ( isset ( $_GET [ 'id_reason' ]) && is_numeric ( $_GET [ 'id_reason' ])) {
$smarty -> assign ( 'id_reason' , $_GET [ 'id_reason' ]);
} else {
$reasons = Reason :: getReasons ( array ( 'id_contact = ' . ( int ) $id_contact ));
foreach ( $reasons as $key => & $reason ) {
if ( ! $reason -> visible ) {
unset ( $reasons [ $key ]);
}
}
$smarty -> assign ( 'reasons' , $reasons );
}
}
} else {
/* $reasons = Reason :: getReasons ();
foreach ( $reasons as $key => & $reason ) {
if ( ! $reason -> visible ) {
unset ( $reasons [ $key ]);
}
}
$smarty -> assign ( 'reasons' , $reasons ); */
}
$_POST = array_merge ( $_POST , $_GET );
require_once ( dirname ( __FILE__ ) . '/../../header.php' );
echo Module :: display ( dirname ( __FILE__ ), 'support.tpl' );
require_once ( dirname ( __FILE__ ) . '/../../footer.php' );