bebeboutik/modules/contactform/Process.php

182 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2016-02-23 13:25:19 +01:00
<?php
class Process
{
private $data;
2016-02-23 15:00:13 +01:00
private $mail_dir;
2016-02-25 12:30:06 +01:00
private $to_provider = [];
private $to_press = [];
2016-02-23 15:00:13 +01:00
public function __construct()
{
2016-02-24 10:12:44 +01:00
$this->mail_dir = dirname(__FILE__);
2016-02-24 17:20:15 +01:00
2016-02-25 12:30:06 +01:00
$_to = Db::getInstance()->ExecuteS('SELECT `email`, `type` FROM `'._DB_PREFIX_.'contactform_email`');
2016-02-24 17:20:15 +01:00
foreach ($_to as $k => $email) {
2016-02-25 12:30:06 +01:00
if ($email['type'] == Contactform::TYPE_PROVIDER) {
2016-03-01 17:28:36 +01:00
$this->to_provider[] = $email['email'];
2016-02-25 12:30:06 +01:00
}
if ($email['type'] == Contactform::TYPE_PRESS) {
2016-03-01 17:28:36 +01:00
$this->to_press[] = $email['email'];
2016-02-25 12:30:06 +01:00
}
2016-02-24 17:20:15 +01:00
}
2016-02-23 15:00:13 +01:00
}
2016-02-23 13:25:19 +01:00
public function addProvider($post)
{
2016-03-02 12:36:13 +01:00
global $cookie;
2016-02-23 13:25:19 +01:00
$this->data = $post;
$errors = [];
$isCorrect = $this->validate(Contactform::TYPE_PROVIDER);
if (!$isCorrect) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Tous les champs ne sont pas remplis');
2016-02-24 11:03:00 +01:00
} else {
2016-02-25 10:55:05 +01:00
if (!Validate::isEmail($this->data['email1'])) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Certains champs ne sont pas valide');
2016-02-25 10:42:49 +01:00
} else {
$query = Db::getInstance()->autoExecute(_DB_PREFIX_.'contactform', [
'brand' => pSQL($this->data['brand']),
'compagny' => pSQL($this->data['compagny']),
'lastname' => pSQL($this->data['lastname']),
'firstname' => pSQL($this->data['firstname']),
'function' => pSQL($this->data['function']),
'email1' => pSQL($this->data['email1']),
'email2' => pSQL($this->data['email2']),
'phone1' => pSQL($this->data['phone1']),
'phone2' => pSQL($this->data['phone2']),
'purpose' => pSQL($this->data['purpose']),
'content' => pSQL($this->data['content']),
2016-02-24 10:35:05 +01:00
'type' => Contactform::TYPE_PROVIDER
2016-02-25 10:42:49 +01:00
], 'INSERT');
2016-03-02 12:36:13 +01:00
2016-02-25 10:42:49 +01:00
if (!$query) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Une erreur s\'est produite. Votre message n\'a pas été envoyé');
2016-03-02 12:36:13 +01:00
} else {
$this->data = array();
foreach ($post as $key => $data) {
$this->data['{'.$key.'}'] = $data;
}
2016-03-01 17:28:36 +01:00
$sended = Mail::Send(
2016-03-02 12:14:34 +01:00
(int)$cookie->id_lang,
2016-02-25 10:42:49 +01:00
'provider',
2016-02-25 12:32:31 +01:00
'Contact fournisseur',
2016-02-25 10:42:49 +01:00
$this->data,
2016-03-02 12:14:34 +01:00
$this->to_provider
2016-02-25 10:42:49 +01:00
);
2016-03-01 17:28:36 +01:00
if (!$sended) {
$errors[] = Tools::displayError('Une erreur s\'est produite. Votre message n\'a pas été envoyé');
}
2016-02-25 10:42:49 +01:00
}
2016-02-24 10:35:05 +01:00
}
}
2016-02-23 15:00:13 +01:00
2016-02-23 17:33:08 +01:00
if (!empty($errors)) {
return $errors;
}
2016-02-25 16:18:37 +01:00
$this->emptyForm();
2016-02-23 17:33:08 +01:00
return true;
2016-02-23 13:25:19 +01:00
}
2016-02-23 15:38:51 +01:00
public function addPress($post)
{
2016-03-02 12:36:13 +01:00
global $cookie;
2016-02-23 15:38:51 +01:00
$this->data = $post;
$errors = [];
$isCorrect = $this->validate(Contactform::TYPE_PRESS);
if (!$isCorrect) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Tous les champs ne sont pas remplis');
2016-02-24 11:03:00 +01:00
} else {
2016-02-25 10:55:05 +01:00
if (!Validate::isEmail($this->data['email1'])) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Certains champs ne sont pas valide');
2016-02-24 11:03:00 +01:00
} else {
2016-02-25 10:42:49 +01:00
$query = Db::getInstance()->autoExecute(_DB_PREFIX_.'contactform', [
'compagny' => pSQL($this->data['compagny']),
'lastname' => pSQL($this->data['lastname']),
'firstname' => pSQL($this->data['firstname']),
'function' => pSQL($this->data['function']),
'email1' => pSQL($this->data['email1']),
'email2' => pSQL($this->data['email2']),
'phone1' => pSQL($this->data['phone1']),
'phone2' => pSQL($this->data['phone2']),
'content' => pSQL($this->data['content']),
'type' => Contactform::TYPE_PRESS
], 'INSERT');
if (!$query) {
2016-02-25 16:18:37 +01:00
$errors[] = Tools::displayError('Une erreur s\'est produite. Votre message n\'a pas été envoyé');
2016-02-25 10:42:49 +01:00
} else {
2016-03-02 12:36:13 +01:00
$this->data = array();
foreach ($post as $key => $data) {
$this->data['{'.$key.'}'] = $data;
}
2016-03-01 17:28:36 +01:00
$sended = Mail::Send(
2016-02-25 10:42:49 +01:00
intval($cookie->id_lang),
'press',
2016-02-25 12:32:31 +01:00
'Contact presse',
2016-02-25 10:42:49 +01:00
$this->data,
2016-03-01 18:17:17 +01:00
$this->to_press
2016-02-25 10:42:49 +01:00
);
2016-03-01 17:28:36 +01:00
if (!$sended) {
$errors[] = Tools::displayError('Une erreur s\'est produite. Votre message n\'a pas été envoyé');
}
2016-02-25 10:42:49 +01:00
}
2016-02-24 10:35:05 +01:00
}
}
2016-02-23 15:38:51 +01:00
2016-02-23 17:33:08 +01:00
if (!empty($errors)) {
return $errors;
}
2016-02-25 16:18:37 +01:00
$this->emptyForm();
2016-02-23 17:33:08 +01:00
return true;
2016-02-23 15:38:51 +01:00
}
2016-02-23 13:25:19 +01:00
private function validate($type)
{
switch ($type) {
case Contactform::TYPE_PROVIDER:
return (!empty($this->data['brand'])
&& !empty($this->data['compagny'])
&& !empty($this->data['lastname'])
&& !empty($this->data['firstname'])
&& !empty($this->data['function'])
&& !empty($this->data['email1'])
&& !empty($this->data['phone1'])
&& !empty($this->data['purpose'])
&& !empty($this->data['content']));
break;
case Contactform::TYPE_PRESS:
return (!empty($this->data['compagny'])
&& !empty($this->data['lastname'])
&& !empty($this->data['firstname'])
&& !empty($this->data['function'])
&& !empty($this->data['email1'])
&& !empty($this->data['phone1'])
&& !empty($this->data['content']));
break;
default:
return false;
break;
}
}
2016-02-25 16:18:37 +01:00
private function emptyForm() {
$_POST['brand'] = '';
$_POST['compagny'] = '';
$_POST['function'] = '';
$_POST['lastname'] = '';
$_POST['firstname'] = '';
$_POST['email1'] = '';
$_POST['email2'] = '';
$_POST['phone1'] = '';
$_POST['phone2'] = '';
$_POST['purpose'] = '';
$_POST['content'] = '';
}
2016-02-23 13:25:19 +01:00
}