├── FormTemplateProcessor.module └── README /FormTemplateProcessor.module: -------------------------------------------------------------------------------- 1 | get('FormTemplateProcessor'); 16 | * $form->template = $templates->get('my_contact_form_template'); // required 17 | * $form->requiredFields = array('fullname', 'email'); 18 | * $form->email = 'your@email.com'; // optional, sends form as email 19 | * $form->parent = $page; // optional, saves form as page 20 | * echo $form->render(); // draw form or process submitted form 21 | * 22 | * 5. Use CSS to style the fields. See the bottom of this file for suggestions. 23 | * 24 | */ 25 | class FormTemplateProcessor extends WireData implements Module { 26 | 27 | /** 28 | * Return an array of module information 29 | * 30 | * @return array 31 | * 32 | */ 33 | public static function getModuleInfo() { 34 | return array( 35 | 'title' => 'Form Template Processor', 36 | 'version' => 101, 37 | 'summary' => 'Lets you use template fields as web contact forms. Can send forms as emails or save as pages.', 38 | 'singular' => false, 39 | 'autoload' => false, 40 | ); 41 | } 42 | 43 | /** 44 | * Instance of InputfieldForm that we use to hold our form fields. 45 | * 46 | */ 47 | protected $form; 48 | 49 | /** 50 | * Instance of Page, that we use to hold our submitted contact info. 51 | * 52 | */ 53 | protected $contact; 54 | 55 | /** 56 | * Initialize a new web contact form 57 | * 58 | * @param Template $template Template object holding the form's fields. 59 | * 60 | */ 61 | public function init() { 62 | 63 | // create a page for holding our form fields 64 | $this->contact = new Page(); 65 | 66 | // Template to use for the contact form fields (required). 67 | // must be a Template object, it will be assigned to $this->contact. 68 | $this->set('template', null); 69 | 70 | // Optional E-Mail address that form will get submitted to 71 | $this->set('email', ''); 72 | 73 | // Array of field names that are required to complete the submission 74 | // if not specified, it will use the admin field settings. 75 | // I recommend that you make at least one field required, since this 76 | // isn't an admin setting yet. 77 | $this->set('requiredFields', array()); 78 | 79 | // Array of field names that should be skipped when drawing the form 80 | $this->set('skipFields', array('title')); 81 | 82 | // Subject of the email that gets sent 83 | $this->set('emailSubject', 'Web Contact Form Submission'); 84 | 85 | // Optional parent page for the contact. 86 | // If ommited, the page will not be saved to the DB. 87 | $this->set('parent', null); 88 | 89 | 90 | // message output upon successful completion of the form 91 | $this->set('successMessage', '
$value
"; 167 | 168 | // use the first found FieldtypeEmail as the 'From' email 169 | if(!$fromEmail && $field->type instanceof FieldtypeEmail) { 170 | $fromEmail = $value; 171 | } 172 | } 173 | 174 | $message = "$message"; 175 | $headers = "Content-Type: text/html;"; 176 | 177 | if($fromEmail) $headers = "From: $fromEmail\n$headers"; 178 | 179 | // send the email 180 | mail($this->email, $this->emailSubject, $message, $headers); 181 | } 182 | 183 | /** 184 | * Create a new page with the results of the processed form 185 | * 186 | */ 187 | protected function ___savePage($form) { 188 | 189 | if(!$this->contact->parent) return; 190 | 191 | $this->contact->name = date('y-m-d-H-i-s-u'); 192 | 193 | if(in_array('title', $this->skipFields)) { 194 | $this->contact->title = date($this->dateFormat); 195 | } 196 | 197 | if(ProcessWire::versionMajor == 2 && ProcessWire::versionMinor == 0) { 198 | $this->contact->status = Page::statusHidden; 199 | $this->contact->removeRole('guest'); 200 | } else { 201 | // PW 2.1 and above 202 | $this->contact->status = Page::statusUnpublished; 203 | } 204 | 205 | $this->contact->save(); 206 | } 207 | 208 | 209 | /** 210 | * Render a form or process it's input 211 | * 212 | * @return string Output of the form or success message upon completion. 213 | * 214 | */ 215 | public function ___render() { 216 | 217 | if(!$this->contact->template) throw new WireException("You must specify a Template"); 218 | 219 | $form = $this->buildForm(); 220 | 221 | // if the form hasn't been submitted, then just return the rendered form. 222 | if(!$this->input->post->submit) return $form->render(); 223 | 224 | // variable to hold our output, which we will return 225 | $out = ''; 226 | 227 | // now we assume the form has been submitted. 228 | // tell the form to process input frmo the post vars. 229 | $form->processInput($this->input->post); 230 | 231 | // see if any errors occurred 232 | if(count($form->getErrors())) { 233 | // re-render the form, it will include the error messages 234 | $out .= $form->render(); 235 | 236 | } else { 237 | // successful form submission, so populate the new page with the new values. 238 | foreach($form as $field) { 239 | $this->contact->set($field->name, $field->value); 240 | } 241 | 242 | if($this->email) $this->sendEmail($form); 243 | if($this->parent) $this->savePage($form); 244 | 245 | $out .= $this->successMessage; 246 | 247 | } 248 | 249 | return $out; 250 | } 251 | } 252 | 253 | 254 | /** 255 | * Suggested styles to get started for styling the fields created by this module: 256 | * 257 | * 258 | 259 | .Inputfields, 260 | .Inputfields li { 261 | list-style: none; 262 | margin: 1em 0; 263 | padding: 0; 264 | } 265 | 266 | .Inputfields li label { 267 | font-weight: bold; 268 | } 269 | 270 | .Inputfields li p { 271 | margin: 0; 272 | } 273 | 274 | .Inputfields li p.description { 275 | font-style: italic; 276 | } 277 | 278 | .Inputfields textarea, 279 | .Inputfields .InputfieldMaxWidth { 280 | width: 100%; 281 | } 282 | 283 | .Inputfields .InputfieldSubmit label { 284 | display: none; 285 | } 286 | 287 | .ui-state-error-text { 288 | color: red; 289 | } 290 | 291 | */ 292 | 293 | 294 | 295 | 296 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ProcessWire FormTemplateProcessor module 2 | 3 | Overview: 4 | ========= 5 | 6 | Module to let you use templates as web contact forms. 7 | Can send you email and/or save the submission to a page in your site. 8 | Intended to be a starting point and proof-of-concept, so you may 9 | want to take this further. 10 | 11 | For more information: 12 | http://processwire.com/talk/index.php/topic,75.0.html 13 | 14 | Designed for use with ProcessWire 2.0 or 2.1 15 | http://processwire.com 16 | 17 | Installation: 18 | ============= 19 | 20 | 1. Place FormTemplateProcessor.module in your site/modules/ directory. 21 | 2. Login to ProcessWire admin and click to Modules. 22 | 3. Click "Check for new modules". 23 | 4. Click "install" next to the new FormTemplateProcessor module. 24 | 25 | Usage: 26 | ====== 27 | 28 | 1. In admin, create the fields you want to be part of the form. 29 | 2. Create a new template and assign your fields to this template. 30 | 3. Create another template for your contact form page (if you don't already have one). 31 | 4. Use the example below as a starting point for this contact form page: 32 | 33 | $form = $modules->get('FormTemplateProcessor'); 34 | $form->template = $templates->get('my_contact_form_template'); // required 35 | $form->requiredFields = array('fullname', 'email'); 36 | $form->email = 'your@email.com'; // optional, sends form as email 37 | $form->parent = $page; // optional, saves form as page 38 | echo $form->render(); // draw form or process submitted form 39 | 40 | 5. Use CSS to style the fields. See below for a suggested starting point: 41 | 42 | .Inputfields, 43 | .Inputfields li { 44 | list-style: none; 45 | margin: 1em 0; 46 | padding: 0; 47 | } 48 | 49 | .Inputfields li label { 50 | font-weight: bold; 51 | } 52 | 53 | .Inputfields li p { 54 | margin: 0; 55 | } 56 | 57 | .Inputfields li p.description { 58 | font-style: italic; 59 | } 60 | 61 | .Inputfields textarea, 62 | .Inputfields .InputfieldMaxWidth { 63 | width: 100%; 64 | } 65 | 66 | .Inputfields .InputfieldSubmit label { 67 | display: none; 68 | } 69 | 70 | .ui-state-error-text { 71 | color: red; 72 | } 73 | 74 | 75 | 76 | --------------------------------------------------------------------------------