/*
* Written by: Adam Crownoble
* Contact: adam@obledesign.com
* Documentation: http://www.obledesign.com/form_processor
* Created: 12/11/2005
* For: MODx cms (modxcms.com)
* Name: FormProcessor
* Version: 2
* Description: Sends form data via email and/or writes data to database
* Properties: &form_chunk=Form Chunk Name;string;
&form_required=Form Required Fields;string;
&data_email_address=Data Email Address;string;
&data_email_subject=Data Email Subject;string;
&data_email_chunk=Data Email Chunk Name;string;
&confirmation_page_chunk=Confirmation Page Chunk Name;string;
&confirmation_email_from=Confirmation Email From Name;string;
&confirmation_email_subject=Confirmation Email Subject;string;
&write_database=Save in Database;int;1
*/
/*
License
FormProcessor - A MODx snippet which sends form data via email and/or
writes data to database
Copyright (C) 2005 Adam Crownoble
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if(!function_exists('check_requirements')) {
// Check that all required fields have been filled
function check_requirements($required, $form) {
global $modx;
// PSEUDO CONSTANTS
$required_class = 'required';
$required_message = 'Missing [NAME]';
$required_messages = '';
$requirements_met = true;
// Requirements are comma separated, turn them into an array
if($required) { $required = explode(',',$required); } else { $required = array(); }
foreach($required as $requirement) {
if( (!isset($form[$requirement]) || !trim($form[$requirement])) && ($form[$requirement]!=='0')) {
$requirement_clean = str_replace('_', ' ', $requirement);
$this_required_message = str_replace('[NAME]', $requirement_clean, $required_message);
// Set placeholders for the required class and message
$modx->setPlaceholder("{$requirement}-required_class", $required_class);
$modx->setPlaceholder("{$requirement}-required_message", $this_required_message);
$required_messages .= "{$this_required_message}\n";
$requirements_met = false;
}
}
if(!$requirements_met) {
$modx->setPlaceholder('required_messages', $required_messages);
}
return $requirements_met;
}
}
if(!function_exists('email_data')) {
// Send an email with the collected data
function email_data($to, $subject, $form, $chunk='') {
global $modx;
$body = '';
$today = date('l, F j');
$server_name = $_SERVER['SERVER_NAME'];
$url = $modx->makeURL($modx->documentIdentifier);
// Set default recipient and subject if not already set
if(!$to) { $to = "webmaster@{$server_name}"; }
$from = "{$modx->config['site_name']}";
if(!$subject) { $subject = "Submission from {$server_name}{$url}"; }
if(isset($form['send_to'])) { unset($form['send_to']); }
if(!$chunk) {
// Set the body of the email message
$body = <<{$subject}