Jump to content

Custom contact on cms page


Niller
 Share

Recommended Posts

Hey Thanks for your reply. 
 
My cms controller is like this:

<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2014 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
 
 
class CmsControllerCore extends FrontController
{
public $php_self = 'cms';
public $assignCase;
public $cms;
public $cms_category;
public $ssl = false;
 
public function canonicalRedirection($canonicalURL = '')
{
if (Tools::getValue('live_edit'))
return ;
if (Validate::isLoadedObject($this->cms) && ($canonicalURL = $this->context->link->getCMSLink($this->cms, $this->cms->link_rewrite, $this->ssl)))
parent::canonicalRedirection($canonicalURL);
else if (Validate::isLoadedObject($this->cms_category) && ($canonicalURL = $this->context->link->getCMSCategoryLink($this->cms_category)))
parent::canonicalRedirection($canonicalURL);
}
 
/**
* Initialize cms controller
* @see FrontController::init()
*/
public function init()
{
if ($id_cms = (int)Tools::getValue('id_cms'))
$this->cms = new CMS($id_cms, $this->context->language->id);
else if ($id_cms_category = (int)Tools::getValue('id_cms_category'))
$this->cms_category = new CMSCategory($id_cms_category, $this->context->language->id);
 
if (Configuration::get('PS_SSL_ENABLED') && Tools::getValue('content_only') && Tools::getValue('id_cms') == (int)Configuration::get('PS_CONDITIONS_CMS_ID') && Validate::isLoadedObject($this->cms))
$this->ssl = true;
 
parent::init();
 
$this->canonicalRedirection();
 
// assignCase (1 = CMS page, 2 = CMS category)
if (Validate::isLoadedObject($this->cms))
{
$adtoken = Tools::getAdminToken('AdminCmsContent'.(int)Tab::getIdFromClassName('AdminCmsContent').(int)Tools::getValue('id_employee'));
if (!$this->cms->isAssociatedToShop() || !$this->cms->active && Tools::getValue('adtoken') != $adtoken)
{
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
}
else
$this->assignCase = 1;
}
else if (Validate::isLoadedObject($this->cms_category))
$this->assignCase = 2;
else
{
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
}
}
 
public function setMedia()
{
parent::setMedia();
 
if ($this->assignCase == 1)
$this->addJS(_THEME_JS_DIR_.'cms.js');
 
$this->addCSS(_THEME_CSS_DIR_.'cms.css');
}
 
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
 
$parent_cat = new CMSCategory(1, $this->context->language->id);
$this->context->smarty->assign('id_current_lang', $this->context->language->id);
$this->context->smarty->assign('home_title', $parent_cat->name);
$this->context->smarty->assign('cgv_id', Configuration::get('PS_CONDITIONS_CMS_ID'));
if (isset($this->cms->id_cms_category) && $this->cms->id_cms_category)
$path = Tools::getFullPath($this->cms->id_cms_category, $this->cms->meta_title, 'CMS');
else if (isset($this->cms_category->meta_title))
$path = Tools::getFullPath(1, $this->cms_category->meta_title, 'CMS');
if ($this->assignCase == 1)
{
$this->context->smarty->assign(array(
'cms' => $this->cms,
'content_only' => (int)(Tools::getValue('content_only')),
'path' => $path,
'body_classes' => array($this->php_self.'-'.$this->cms->id, $this->php_self.'-'.$this->cms->link_rewrite)
));
 
if ($this->cms->indexation == 0)
$this->context->smarty->assign('nobots', true);
}
else if ($this->assignCase == 2)
{
$this->context->smarty->assign(array(
'category' => $this->cms_category, //for backward compatibility
'cms_category' => $this->cms_category,
'sub_category' => $this->cms_category->getSubCategories($this->context->language->id),
'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id), true, (int)$this->context->shop->id),
'path' => ($this->cms_category->id !== 1) ? Tools::getPath($this->cms_category->id, $this->cms_category->name, false, 'CMS') : '',
'body_classes' => array($this->php_self.'-'.$this->cms_category->id, $this->php_self.'-'.$this->cms_category->link_rewrite)
));
}
 
$this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
}
}
 
 
- Every time i place postprocess from contactcontroller.php in cmscontroller.php i get white screens on cms pages. Can you guide me further :)

Share this post


Link to post
Share on other sites

If you want a custom page, you shoud create your own php file, controller and  template in the same model of contact-form.php, ContactController.php, contact-form.tpl...

 

Copy them, rename them for example custom-contact-form.php, CustomContact.php and custom-contact-form.tpl, and do all the modfication you want inside .

 

then you ll be able to acces to your new custom page by {$link->getPageLink('custom-contact-form', true)} in you menu template

Share this post


Link to post
Share on other sites

Do i have to copy the whole contactcontroller.php (post process) to cmscontroller.php

If i do, i get this error: Warning: tempnam(): open_basedir restriction in effect. File(/customers/6/3/b/mobil-reservedele.dk/httpd.wwwcache) is not within the allowed path(s): (/customers/6/3/b/mobil-reservedele.dk/httpd.www:/customers/6/3/b/mobil-reservedele.dk/httpd.private:/customers/6/3/b/mobil-reservedele.dk/tmp:/customers/mobil-reservedele.dk/mobil-reservedele.dk:/var/www/diagnostics:/usr/share/php) in /customers/6/3/b/mobil-reservedele.dk/httpd.www/classes/PrestaShopAutoload.php on line 151 Parse error: syntax error, unexpected T_PUBLIC in /customers/6/3/b/mobil-reservedele.dk/httpd.www/controllers/front/CmsController.php on line 267

 

I appreciate your help!! :)

Share this post


Link to post
Share on other sites

My cms controller looks like this, after the postprocess is added:

 

<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2014 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
 
public function postProcess()
{
if (Tools::isSubmit('submitMessage'))
{
$extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
$fileAttachment = Tools::fileAttachment('fileUpload');
$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($fileAttachment['name']) && $fileAttachment['error'] != 0)
$this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
else if (!empty($fileAttachment['name']) && !in_array( Tools::strtolower(substr($fileAttachment['name'], -4)), $extension) && !in_array( Tools::strtolower(substr($fileAttachment['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);
 
$id_order = (int)$this->getOrder();
 
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, $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)$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 && $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);
$ct->id_order = (int)$id_order;
if ($id_product = (int)Tools::getValue('id_product'))
$ct->id_product = $id_product;
$ct->update();
}
else
{
$ct = new CustomerThread();
if (isset($customer->id))
$ct->id_customer = (int)($customer->id);
$ct->id_shop = (int)$this->context->shop->id;
$ct->id_order = (int)$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->add();
}
 
if ($ct->id)
{
$cm = new CustomerMessage();
$cm->id_customer_thread = $ct->id;
$cm->message = $message;
if (isset($fileAttachment['rename']) && !empty($fileAttachment['rename']) && rename($fileAttachment['tmp_name'], _PS_UPLOAD_DIR_.basename($fileAttachment['rename'])))
{
$cm->file_name = $fileAttachment['rename'];
@chmod(_PS_UPLOAD_DIR_.basename($fileAttachment['rename']), 0664);
}
$cm->ip_address = ip2long(Tools::getRemoteAddr());
$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,
'{product_name}' => '',
);
 
if (isset($fileAttachment['name']))
$var_list['{attached_file}'] = $fileAttachment['name'];
 
$id_product = (int)Tools::getValue('id_product');
 
if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order)
{
$order = new Order((int)$ct->id_order);
$var_list['{order_name}'] = $order->getUniqReference();
$var_list['{id_order}'] = (int)$order->id;
}
 
if ($id_product)
{
$product = new Product((int)$id_product);
if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id]))
$var_list['{product_name}'] = $product->name[Context::getContext()->language->id];
}
 
if (empty($contact->email))
Mail::Send($this->context->language->id, 'contact_form', ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')), $var_list, $from, null, null, null, $fileAttachment);
else
{
if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form').' [no_sync]',
$var_list, $contact->email, $contact->name, $from, ($customer->id ? $customer->firstname.' '.$customer->lastname : ''),
$fileAttachment) ||
!Mail::Send($this->context->language->id, 'contact_form', ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')), $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);
}
}
}
 
public function setMedia()
{
parent::setMedia();
$this->addCSS(_THEME_CSS_DIR_.'contact-form.css');
$this->addJS(_THEME_JS_DIR_.'contact-form.js');
$this->addJS(_PS_JS_DIR_.'validate.js');
}
 
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
 
$this->assignOrderList();
 
$email = Tools::safeOutput(Tools::getValue('from',
((isset($this->context->cookie) && isset($this->context->cookie->email) && Validate::isEmail($this->context->cookie->email)) ? $this->context->cookie->email : '')));
$this->context->smarty->assign(array(
'errors' => $this->errors,
'email' => $email,
'fileupload' => Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD')
));
 
 
if (($id_customer_thread = (int)Tools::getValue('id_customer_thread')) && $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 cm.id_shop = '.(int)$this->context->shop->id.' 
AND token = \''.pSQL($token).'\'
');
$this->context->smarty->assign('customerThread', $customerThread);
}
 
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
 
$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
}
 
/**
* Assign template vars related to order list and product list ordered by the customer
*/
protected function assignOrderList()
{
if ($this->context->customer->isLogged())
{
$this->context->smarty->assign('isLogged', 1);
 
$products = array();
$result = Db::getInstance()->executeS('
SELECT id_order
FROM '._DB_PREFIX_.'orders
WHERE id_customer = '.(int)$this->context->customer->id.' ORDER BY date_add');
$orders = array();
 
foreach ($result as $row)
{
$order = new Order($row['id_order']);
$date = explode(' ', $order->date_add);
$tmp = $order->getProducts();
foreach ($tmp as $key => $val)
$products[$row['id_order']][$val['product_id']] = array('value' => $val['product_id'], 'label' => $val['product_name']);
 
$orders[] = array('value' => $order->id, 'label' => $order->getUniqReference().' - '.Tools::displayDate($date[0], null) , 'selected' => (int)$this->getOrder() == $order->id);
}
 
$this->context->smarty->assign('orderList', $orders);
$this->context->smarty->assign('orderedProductList', $products);
}
}
 
protected function getOrder()
{
$id_order = false;
if (!is_numeric($reference = Tools::getValue('id_order')))
{
$orders = Order::getByReference($reference);
if ($orders)
foreach ($orders as $order)
{
$id_order = $order->id;
break;
}
}
else
$id_order = Tools::getValue('id_order');
return (int)$id_order;
}
}
class CmsControllerCore extends FrontController
{
public $php_self = 'cms';
public $assignCase;
public $cms;
public $cms_category;
public $ssl = false;
 
public function canonicalRedirection($canonicalURL = '')
{
if (Tools::getValue('live_edit'))
return ;
if (Validate::isLoadedObject($this->cms) && ($canonicalURL = $this->context->link->getCMSLink($this->cms, $this->cms->link_rewrite, $this->ssl)))
parent::canonicalRedirection($canonicalURL);
else if (Validate::isLoadedObject($this->cms_category) && ($canonicalURL = $this->context->link->getCMSCategoryLink($this->cms_category)))
parent::canonicalRedirection($canonicalURL);
}
 
/**
* Initialize cms controller
* @see FrontController::init()
*/
public function init()
{
if ($id_cms = (int)Tools::getValue('id_cms'))
$this->cms = new CMS($id_cms, $this->context->language->id);
else if ($id_cms_category = (int)Tools::getValue('id_cms_category'))
$this->cms_category = new CMSCategory($id_cms_category, $this->context->language->id);
 
if (Configuration::get('PS_SSL_ENABLED') && Tools::getValue('content_only') && Tools::getValue('id_cms') == (int)Configuration::get('PS_CONDITIONS_CMS_ID') && Validate::isLoadedObject($this->cms))
$this->ssl = true;
 
parent::init();
 
$this->canonicalRedirection();
 
// assignCase (1 = CMS page, 2 = CMS category)
if (Validate::isLoadedObject($this->cms))
{
$adtoken = Tools::getAdminToken('AdminCmsContent'.(int)Tab::getIdFromClassName('AdminCmsContent').(int)Tools::getValue('id_employee'));
if (!$this->cms->isAssociatedToShop() || !$this->cms->active && Tools::getValue('adtoken') != $adtoken)
{
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
}
else
$this->assignCase = 1;
}
else if (Validate::isLoadedObject($this->cms_category))
$this->assignCase = 2;
else
{
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
}
}
 
public function setMedia()
{
parent::setMedia();
 
if ($this->assignCase == 1)
$this->addJS(_THEME_JS_DIR_.'cms.js');
 
$this->addCSS(_THEME_CSS_DIR_.'cms.css');
}
 
/**
* Assign template vars related to page content
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
 
$parent_cat = new CMSCategory(1, $this->context->language->id);
$this->context->smarty->assign('id_current_lang', $this->context->language->id);
$this->context->smarty->assign('home_title', $parent_cat->name);
$this->context->smarty->assign('cgv_id', Configuration::get('PS_CONDITIONS_CMS_ID'));
if (isset($this->cms->id_cms_category) && $this->cms->id_cms_category)
$path = Tools::getFullPath($this->cms->id_cms_category, $this->cms->meta_title, 'CMS');
else if (isset($this->cms_category->meta_title))
$path = Tools::getFullPath(1, $this->cms_category->meta_title, 'CMS');
if ($this->assignCase == 1)
{
$this->context->smarty->assign(array(
'cms' => $this->cms,
'content_only' => (int)(Tools::getValue('content_only')),
'path' => $path,
'body_classes' => array($this->php_self.'-'.$this->cms->id, $this->php_self.'-'.$this->cms->link_rewrite)
));
 
if ($this->cms->indexation == 0)
$this->context->smarty->assign('nobots', true);
}
else if ($this->assignCase == 2)
{
$this->context->smarty->assign(array(
'category' => $this->cms_category, //for backward compatibility
'cms_category' => $this->cms_category,
'sub_category' => $this->cms_category->getSubCategories($this->context->language->id),
'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id), true, (int)$this->context->shop->id),
'path' => ($this->cms_category->id !== 1) ? Tools::getPath($this->cms_category->id, $this->cms_category->name, false, 'CMS') : '',
'body_classes' => array($this->php_self.'-'.$this->cms_category->id, $this->php_self.'-'.$this->cms_category->link_rewrite)
));
}
 
$this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
}
}
 
I havent worked with controllers before, so excuse me :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More