You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.1 KiB
129 lines
4.1 KiB
<?php
|
|
|
|
abstract class FormHandler {
|
|
protected $nextHandler;
|
|
|
|
public function __construct($nextHandler = null) {
|
|
$this->nextHandler = $nextHandler;
|
|
}
|
|
|
|
public function handle($data) {
|
|
if ($this->nextHandler) {
|
|
return $this->nextHandler->handle($data);
|
|
}
|
|
return ['success' => true, 'message' => 'Форма обработана'];
|
|
}
|
|
}
|
|
|
|
class b24Handler extends FormHandler {
|
|
public function handle($data) {
|
|
// Логика отправки в Mailchimp
|
|
error_log("Отправка в b24: " . json_encode($data));
|
|
|
|
// Вызываем следующий обработчик в цепочке
|
|
return parent::handle($data);
|
|
}
|
|
}
|
|
|
|
class zohoHandler extends FormHandler {
|
|
public function handle($data) {
|
|
// Логика отправки в HubSpot
|
|
error_log("Отправка в Zoho: " . json_encode($data));
|
|
|
|
return parent::handle($data);
|
|
}
|
|
}
|
|
|
|
class mindboxHandler extends FormHandler {
|
|
public function handle($data) {
|
|
// Отправка в стандартный обработчик (например, email)
|
|
error_log("Отправка в mindBox: " . json_encode($data));
|
|
// if (is_string($data)) {
|
|
// parse_str($data, $parsedData); // Преобразуем строку в массив
|
|
// $data = $parsedData;
|
|
// }
|
|
|
|
// $url = 'https://api.mindbox.ru/v3/operations/async?endpointId=cosmopet.Website&operation=DobavleniePolzovatelyaSSajta';
|
|
|
|
// $data = array(
|
|
// "email" => $data['email'],
|
|
// "subscriptions" => array(
|
|
// array(
|
|
// "pointOfContact"=> "Email"
|
|
// ),
|
|
// ),
|
|
// );
|
|
|
|
// $data_string = json_encode(array("customer" =>$data));
|
|
|
|
// $ch = curl_init($url);
|
|
|
|
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
|
|
|
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
|
|
|
|
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
// $result = curl_exec($ch);
|
|
|
|
// curl_close($ch);
|
|
return parent::handle($data);
|
|
}
|
|
}
|
|
|
|
class emailHandler extends FormHandler {
|
|
public function handle($data) {
|
|
|
|
$to = 'pro@cosmopet.shop'; // Укажите email, на который нужно отправить данные
|
|
$subject = 'Форма обработана'; // Тема письма
|
|
$message = $this->formatData($data); // Форматируем данные
|
|
|
|
$headers = ['Content-Type: text/plain; charset=UTF-8']; // Заголовки письма
|
|
|
|
// Отправляем письмо
|
|
wp_mail($to, $subject, $message, $headers);
|
|
|
|
// Вызываем следующий обработчик в цепочке
|
|
return parent::handle($data);
|
|
}
|
|
|
|
private function formatData($data) {
|
|
if (is_string($data)) {
|
|
parse_str($data, $parsedData); // Преобразуем строку в массив
|
|
$data = $parsedData;
|
|
}
|
|
$formattedMessage = "Данные формы:\n\n";
|
|
|
|
// Проходим по всем полям данных и добавляем их в сообщение
|
|
foreach ($data as $key => $value) {
|
|
$formattedMessage .= sprintf("%s: %s\n", $key, $value);
|
|
}
|
|
|
|
return $formattedMessage;
|
|
}
|
|
}
|
|
|
|
class FormHandlerFactory {
|
|
public static function getHandler(array $enabledHandlers) {
|
|
$handler = null;
|
|
if (in_array('email', $enabledHandlers)) {
|
|
$handler = new emailHandler($handler);
|
|
}
|
|
if (in_array('zoho', $enabledHandlers)) {
|
|
$handler = new zohoHandler($handler);
|
|
}
|
|
if (in_array('mindbox', $enabledHandlers)) {
|
|
$handler = new mindboxHandler($handler);
|
|
}
|
|
if (in_array('b24', $enabledHandlers)) {
|
|
$handler = new b24Handler($handler);
|
|
}
|
|
|
|
|
|
return $handler ?: new DefaultHandler();
|
|
}
|
|
}
|
|
|
|
|
|
?>
|