Skip to content

Instantly share code, notes, and snippets.

@andronex
Created December 8, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andronex/9c151453ad3c7934f3aa39d5d7e2ee3e to your computer and use it in GitHub Desktop.
Save andronex/9c151453ad3c7934f3aa39d5d7e2ee3e to your computer and use it in GitHub Desktop.
Веб-хук для Гугл форм с записью данных в таблицу БД и отправкой email уведомления. Интеграция Гугл форм с MODX.
function onFormSubmit(e) {
if( e ) {
values = e.range.getValues();
theNewRow = values[0];
var response = UrlFetchApp.fetch('https://test.ru/assets/components/newsletters/googleform_hook.php?id=1&form=TestForm&data='+theNewRow, {'muteHttpExceptions': true});
Logger.log(response);
}
}
{
"timeZone": "Europe/Moscow",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/forms",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/forms.currentonly",
"https://www.googleapis.com/auth/script.external_request"
]
}
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json; charset=utf-8');
if (!isset($modx)) {
define('MODX_API_MODE', true);
while (!isset($modx) && ($i = isset($i) ? --$i : 10)) {
if (($file = dirname(!empty($file) ? dirname($file) : __FILE__) . '/index.php') AND !file_exists($file)) {
continue;
}
require_once $file;
}
if (!is_object($modx)) {
exit('{"success":false,"message":"Access denied"}');
}
$modx->getService('error', 'error.modError');
$modx->getRequest();
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->lexicon->load('default');
}
$ctx = !empty($_REQUEST['ctx']) ? $_REQUEST['ctx'] : $modx->context->get('key');
if ($ctx != $modx->context->get('key')) {
$modx->switchContext($ctx);
}
$data = $_REQUEST;
$modx->addPackage('newsletters', MODX_CORE_PATH . 'components/newsletters/model/');
$table = $modx->newObject('newslettersItem');
$date = date('Y-m-d H:i:s');
$form_name = $data['form']?:'Внешняя гугл форма';
$allFormFields['createdon'] = $date;
$allFormFields['form_name'] = $form_name;
$mail_to = $modx->getOption('site_email_request');
if($data['data']){
$form_id = $data['id'];
$data = explode(',', $data['data']);
if($form_id == 1 && $data[1]){
$allFormFields['email'] = $data[1];
$table->fromArray($allFormFields);
if($table->save()){
$pdoTools = $modx->getService('pdoTools');
$msg = $pdoTools->getChunk('@INLINE {if $data}{foreach $data as $item}{$item}<br>{/foreach}{/if}', array('data' => array_merge(['form' => $form_name], $data)));
$from_sent = 'Название сайта';
$subj = 'Заполнена форма';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $msg);
$modx->mail->set(modMail::MAIL_FROM, 'noreply@' . MODX_HTTP_HOST);
$modx->mail->set(modMail::MAIL_FROM_NAME, $from_sent);
$modx->mail->set(modMail::MAIL_SENDER, 'noreply@' . MODX_HTTP_HOST);
$modx->mail->set(modMail::MAIL_SUBJECT, $subj);
$mail_to_sent = array_map('trim', explode(',', $mail_to));
if(is_array($mail_to_sent)){
foreach($mail_to_sent as $m_to){
$modx->mail->address('to', $m_to);
}
}
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
}
}
if($form_id == 2 && $data[1]){
$allFormFields['email'] = $data[1];
$table->fromArray($allFormFields);
if($table->save()){
$pdoTools = $modx->getService('pdoTools');
$msg = $pdoTools->getChunk('@INLINE {if $data}{foreach $data as $item}{$item}<br>{/foreach}{/if}', array('data' => array_merge(['form' => $form_name], $data)));
$from_sent = 'Название сайта';
$subj = 'Заполнена форма';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $msg);
$modx->mail->set(modMail::MAIL_FROM, 'noreply@' . MODX_HTTP_HOST);
$modx->mail->set(modMail::MAIL_FROM_NAME, $from_sent);
$modx->mail->set(modMail::MAIL_SENDER, 'noreply@' . MODX_HTTP_HOST);
$modx->mail->set(modMail::MAIL_SUBJECT, $subj);
$mail_to_sent = array_map('trim', explode(',', $mail_to));
if(is_array($mail_to_sent)){
foreach($mail_to_sent as $m_to){
$modx->mail->address('to', $m_to);
}
}
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
}
}
}
die(json_encode(array('success' => true)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment