Jump to content

Add 2 New Field in contact form


Recommended Posts

contact-form.tpl

 
{capture name=path}{l s='Contact'}{/capture}
 
 
<h1>{l s='Customer Service'} - {if isset($customerThread) && $customerThread}{l s='Your reply'}{else}{l s='Contact us'}{/if}</h1>
 
{if isset($confirmation)}
<p>{l s='Your message has been successfully sent to our team.'}</p>
<ul class="footer_links">
<li><a href="{$base_dir}"><img class="icon" alt="" src="{$img_dir}icon/home.gif"/></a><a href="{$base_dir}">{l s='Home'}</a></li>
</ul>
{elseif isset($alreadySent)}
<p>{l s='Your message has already been sent.'}</p>
<ul class="footer_links">
<li><a href="{$base_dir}"><img class="icon" alt="" src="{$img_dir}icon/home.gif"/></a><a href="{$base_dir}">{l s='Home'}</a></li>
</ul>
{else}
<p class="bold">{l s='For questions about an order or for more information about our products'}.</p>
{include file="$tpl_dir./errors.tpl"}
<form action="{$request_uri|escape:'htmlall':'UTF-8'}" method="post" class="std" enctype="multipart/form-data">
<fieldset>
        
<h3>{l s='Send a message'}</h3>
            <div class="content_form">
            <div style="float:left; width: 580px;">
<p class="select">
<label for="id_contact">{l s='Subject Heading'}</label>
{if isset($customerThread.id_contact)}
{foreach from=$contacts item=contact}
{if $contact.id_contact == $customerThread.id_contact}
<input type="text" id="contact_name" name="contact_name" value="{$contact.name|escape:'htmlall':'UTF-8'}" readonly="readonly" />
<input type="hidden" name="id_contact" value="{$contact.id_contact}" />
{/if}
{/foreach}
</p>
{else}
<select id="id_contact" name="id_contact" onchange="showElemFromSelect('id_contact', 'desc_contact')">
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$contacts item=contact}
<option value="{$contact.id_contact|intval}" {if isset($smarty.post.id_contact) && $smarty.post.id_contact == $contact.id_contact}selected="selected"{/if}>{$contact.name|escape:'htmlall':'UTF-8'}</option>
{/foreach}
</select>
</p>
<p id="desc_contact0" class="desc_contact"> </p>
{foreach from=$contacts item=contact}
<p id="desc_contact{$contact.id_contact|intval}" class="desc_contact" style="display:none;">
{$contact.description|escape:'htmlall':'UTF-8'}
</p>
{/foreach}
{/if}
<p class="text">
    <label for="contact_nm">{l s='Name'}</label>
    {if isset($customerThread.contact_nm)}
        <input type="text" id="contact_nm" name="contact_nm" value="{$customerThread.contact_nm|escape:'htmlall':'UTF-8'}" readonly="readonly" />
    {else}
        <input type="text" id="contact_nm" name="contact_nm" value="" />
    {/if}
</p>  
<p class="text">
<label for="email">{l s='E-mail address'}</label>
{if isset($customerThread.email)}
<input type="text" id="email" name="from" value="{$customerThread.email|escape:'htmlall':'UTF-8'}" readonly="readonly" />
{else}
<input type="text" id="email" name="from" value="{$email|escape:'htmlall':'UTF-8'}" />
{/if}
</p>
 
{if !$PS_CATALOG_MODE}
{if (!isset($customerThread.id_order) || $customerThread.id_order > 0)}
<!--<p class="text select">
<label for="id_order">{l s='Order ID(Optional)'}</label>
{if !isset($customerThread.id_order) && isset($isLogged) && $isLogged == 1}
<select name="id_order" >
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$orderList item=order}
                            <option value="{$order.value|intval}" {if $order.selected|intval}selected="selected"{/if}>{$order.label|escape:'htmlall':'UTF-8'}</option>
 
{/foreach}
</select>
 
{elseif !isset($customerThread.id_order) && !isset($isLogged)}
<input type="text" name="id_order" id="id_order" value="{if isset($customerThread.id_order) && $customerThread.id_order > 0}{$customerThread.id_order|intval}{else}{if isset($smarty.post.id_order)}{$smarty.post.id_order|intval}{/if}{/if}" />
{elseif $customerThread.id_order > 0}
<input type="text" name="id_order" id="id_order" value="{$customerThread.id_order|intval}" readonly="readonly" />
{/if}
 
</p>-->
{/if}
{if isset($isLogged) && $isLogged}
<p class="text select">
<label for="id_product">{l s='Product'}</label>
{if !isset($customerThread.id_product)}
{foreach from=$orderedProductList key=id_order item=products name=products}
<select name="id_product" id="{$id_order}_order_products" class="product_select" style="{if !$smarty.foreach.products.first} display:none; {/if}" {if !$smarty.foreach.products.first}disabled="disabled" {/if}>
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$products item=product}
<option value="{$product.value|intval}">{$product.label|escape:'htmlall':'UTF-8'}</option>
{/foreach}
</select>
{/foreach}
{elseif $customerThread.id_product > 0}
<input type="text" name="id_product" id="id_product" value="{$customerThread.id_product|intval}" readonly="readonly" />
{/if}
</p>
{/if}
{/if}
<!-- {if $fileupload == 1}
<p class="text">
<label for="fileUpload">{l s='Attach File'}</label>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="fileUpload" id="fileUpload" />
</p>
{/if}-->
<p class="text">
    <label for="contact_no">{l s='Contact No'}</label>
    {if isset($customerThread.contact_no)}
        <input type="text" id="contact_no" name="contact_no" value="{$customerThread.contact_no|escape:'htmlall':'UTF-8'}" readonly="readonly" />
    {else}
        <input type="text" id="contact_no" name="contact_no" value="" />
    {/if}
</p>  
<p class="textarea">
<label for="message">{l s='Message'}</label>
<textarea id="message" name="message" rows="15" cols="10">{if isset($message)}{$message|escape:'htmlall':'UTF-8'|stripslashes}{/if}</textarea>
</p>
  
<p class="submit">
<input type="submit" name="submitMessage" id="submitMessage" value="{l s='Send'}" class="button_large" onclick="$(this).hide();" />
</p>
        </div>
        <div style="float:left;">
       
<H2 style="color: #4B4B4B;">Our Location</H2>
<b><p>KVM Switches India</b><br>
Pune,Kolkata,Mumbai, 
<br>Delhi, Bangalore.</p>
<br>
<H2 style="color: #4B4B4B;">Call Us</H2>
       <p>([spam-filter])-8376807021</p>
<H2 style="color: #4B4B4B;">Email Us</H2>
      </div>
        </div>
</fieldset>
</form>
{/if}
 
 
Override ContactController.php
<?php
 
class ContactController extends ContactControllerCore
{
    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage'))
        {
            $fileAttachment = null;
            if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !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::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.
            if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from))
                $this->errors[] = Tools::displayError('Invalid email address.');
            else if (!$message)
                $this->errors[] = Tools::displayError('The message cannot be blank.');
            else if (!Validate::isCleanHtml($message))
                $this->errors[] = Tools::displayError('Invalid message');
            else if (!($id_contact = (int)(Tools::getValue('id_contact'))) || !(Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id))))
                $this->errors[] = Tools::displayError('Please select a subject from the list provided. ');
            else if (!empty($_FILES['fileUpload']['name']) && $_FILES['fileUpload']['error'] != 0)
                $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
            else if (!empty($_FILES['fileUpload']['name']) && !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) && !in_array(substr($_FILES['fileUpload']['name'], -5), $extension))
                $this->errors[] = Tools::displayError('Bad file extension');
            else
            {
                $customer = $this->context->customer;
                if (!$customer->id)
                    $customer->getByEmail($from);
 
                $contact = new Contact($id_contact, $this->context->language->id);
 
                if (!((
                        ($id_customer_thread = (int)Tools::getValue('id_customer_thread'))
                        && (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 cm.id_shop = '.(int)$this->context->shop->id.' AND token = \''.pSQL(Tools::getValue('token')).'\'')
                    ) || (
                        $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, (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 cm.id_shop = '.(int)$this->context->shop->id.' 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'] && $row['id_customer'] != $customer->id && $row['email'] != $from)
                            continue;
                        if ($row['id_order'] != 0 && 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 && $row['id_product'] == Tools::getValue('id_product'))
                            $tmp += 2;
                        if ($tmp >= 5 && $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
                    LEFT JOIN '._DB_PREFIX_.'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
                    WHERE cc.id_customer_thread = '.(int)($id_customer_thread).' AND cc.id_shop = '.(int)$this->context->shop->id.'
                    ORDER BY cm.date_add DESC');
                if ($old_message == $message)
                {
                    $this->context->smarty->assign('alreadySent', 1);
                    $contact->email = '';
                    $contact->customer_service = 0;
                }
 
                if ($contact->customer_service)
                {
                   if ((int)$id_customer_thread)
{
    $ct = new CustomerThread($id_customer_thread);
    $ct->status = 'open';
    $ct->id_lang = (int)$this->context->language->id;
    $ct->id_contact = (int)($id_contact);
    if ($id_order = (int)Tools::getValue('id_order'))
        $ct->id_order = $id_order;
    if ($id_product = (int)Tools::getValue('id_product'))
        $ct->id_product = $id_product;
    $ct->contact_nm=Tools::getValue('contact_nm');    
    $ct->contact_no=Tools::getValue('contact_no');
   
    $ct->update();
}
else
{
    $ct = new CustomerThread();
    if (isset($customer->id))
        $ct->id_customer = (int)($customer->id);
    $ct->id_shop = (int)$this->context->shop->id;
    if ($id_order = (int)Tools::getValue('id_order'))
        $ct->id_order = $id_order;
    if ($id_product = (int)Tools::getValue('id_product'))
        $ct->id_product = $id_product;
    $ct->id_contact = (int)($id_contact);
    $ct->id_lang = (int)$this->context->language->id;
    $ct->email = $from;
    $ct->status = 'open';
    $ct->token = Tools::passwdGen(12);
    $ct->contact_no=Tools::getValue('contact_no');
    $ct->contact_nm=Tools::getValue('contact_nm');
     
    $ct->add();
}
                    if ($ct->id)
                    {
                        $cm = new CustomerMessage();
                        $cm->id_customer_thread = $ct->id;
                        $cm->message = Tools::htmlentitiesUTF8($message);
                        if (isset($filename) && 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())
                            $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                    else
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                }
 
                if (!count($this->errors))
                {
                    $var_list = array(
                                    '{order_name}' => '-',
                                    '{attached_file}' => '-',
                                    '{message}' => Tools::nl2br(stripslashes($message)),
                                    '{email}' =>  $from,
                                    '{contact_no}' =>  (isset($ct) && $ct->contact_no) ? $ct->contact_no: '',
                                    '{contact_nm}' =>  (isset($ct) && $ct->contact_nm) ? $ct->contact_nm: '',
                                    
                                );
 
                    if (isset($filename))
                        $var_list['{attached_file}'] = $_FILES['fileUpload']['name'];
 
                    $id_order = (int)Tools::getValue('id_order');
                     
                    if (isset($ct) && Validate::isLoadedObject($ct))
                    {
                        if ($ct->id_order)
                            $id_order = $ct->id_order;
                        $subject = sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token);
                    }
                    else
                        $subject = Mail::l('Your message has been correctly sent');
 
                    if ($id_order)
                    {
                        $order = new Order((int)$id_order);
                        $var_list['{order_name}'] = $order->getUniqReference();
                        $var_list['{id_order}'] = $id_order;
                    }
                     
                    if (empty($contact->email))
                        Mail::Send($this->context->language->id, 'contact_form', $subject, $var_list, $from, null, null, null, $fileAttachment);
                    else
                    {                   
                        if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form'),
                            $var_list, $contact->email, $contact->name, $from, ($customer->id ? $customer->firstname.' '.$customer->lastname : ''),
                                    $fileAttachment) ||
                                !Mail::Send($this->context->language->id, 'contact_form', $subject, $var_list, $from, null, $contact->email, $contact->name, $fileAttachment))
                                    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                }
                 
                if (count($this->errors) > 1)
                    array_unique($this->errors);
                else
                    $this->context->smarty->assign('confirmation', 1);
            }
        }
    }
 
}
 
 override CustomerThread.php
<?php
 
class CustomerThread extends CustomerThreadCore
{
    public $contact_no;
    public $contact_nm;
 
    public static $definition = array(
        'table' => 'customer_thread',
        'primary' => 'id_customer_thread',
        'fields' => array(
            'id_lang' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_shop' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_order' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'email' =>       array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254),
            'token' =>       array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),
            'status' =>  array('type' => self::TYPE_STRING),
            'date_add' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'date_upd' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'contact_no' =>  array('type' => self::TYPE_INT, 'validate' => 'isGenericName'),
            'contact_nm' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
            
        ),
    );
 
 
 
}
Edited by ajay21 (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...