Skip to content

Instantly share code, notes, and snippets.

@0test
Created March 16, 2023 17:31
Show Gist options
  • Save 0test/2d5f335dc9d400629cb07d07169c2ea1 to your computer and use it in GitHub Desktop.
Save 0test/2d5f335dc9d400629cb07d07169c2ea1 to your computer and use it in GitHub Desktop.
<?php
namespace EvolutionCMS\Main\Controllers;
class ContactsFormController {
public function send()
{
$form = evo()->runSnippet('FormLister',[
'formid' => 'myform',
'errorClass' => 'is-invalid',
'requiredClass' => 'is-invalid',
'rules' => [
'name' => [
'required' => 'Заполните имя'
],
'email'=> [
'required' => 'Заполните почту',
'email' => 'Почта неверная'
],
'message'=> [
'required' => 'Заполните сообщение'
],
],
'attachments' => 'file',
'fileRules' => [
'file' =>
[
"required" => "Приложите документ",
"allowed" => [
"params" => [ ["jpg","jpeg","png"] ],
"message" => "Разрешены только картинки"
],
"maxSize" => [
"params" => 10000,
"message" => "Размер файла не должен превышать 10000 кб"
]]
],
'filterer' => '\FormLister\Filters',
'filters' => [
"name" => ["trim"],
"email" => ["trim"]
],
'to' => 'xxx@xxx.ru',
'subject' => 'Contacts form ',
'reportTpl' => '@CODE:
<p>Имя: [+name.value+]</p>
<p>Email: <a href="mailto:[+email.value+]">[+email.value+]</a></p>
<p>Сообщение: [+message:strip_tags:nl2br+]</p>
<p>Доп.:[+file.value+]
<p>Вложения: [+attachments.value+]</p>
',
'api' => 2,
'successTpl' => '@CODE: Спасибо!',
'submitLimit' => 0,
'protectSubmit' => 0,
'prepareAfterProcess' => [
'EvolutionCMS\Main\Controllers\ContactsFormController::prepareTelegramData'
],
]);
return $form;
}
public static function prepareTelegramData($modx,$data,$FL,$name){
$file = $FL->getFormData('files');
$data['img'] = new \CURLFile($file['file']['tmp_name']);
$result = self::sendToTelegram($data);
if($result->ok == true){
$tpl = "@CODE: Отправил, сообщение №" . $result->result->message_id;
}else{
$tpl = "@CODE: Что-то пошло не так";
/**
* А то именно - делаем dd($result) и читаем
*/
}
$FL->config->setConfig([
'successTpl' => $tpl
]);
return $data;
}
public static function sendToTelegram($data){
$bot_id = env('TG_BOT_ID');
$chat_id = env('TG_CHAT_ID');
$bot_url = "https://api.telegram.org/bot" . $bot_id . "/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id;
$params = [
'chat_id' => $chat_id,
'photo' => $data['img'],
'caption' => 'Это сообщение с сайта: ' . $data['name'] . ' ' . $data['email'] . ' ' . $data['message'],
'disable_web_page_preview' => true,
'parse_mode' => 'HTML',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$output = curl_exec($ch);
return json_decode($output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment