We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Well, kind of a report generator. It works best if you have a naming convention for your fields, such as field_name_in_lowercase_with_underscore, but if you do, you might never have to make an email report chunk again smiley.

    Note: it skips all array values and any values listed in the &figrExcludedFields parameters. If you have array values in your form, you need to call the fiProcessArrays hook (see below) before the fiGenerateReport hook.

    Here’s an example FormIt call:
    [[!FormIt? &hooks=`recaptcha,spam,fiGenerateReport,email,redirect` &emailTpl=`ContactFormReport` &emailTo=`[[++emailsender]]` &emailSubject=`New message from [[++site_name]] [[*pagetitle]] page.` &redirectTo=`6` &submitVar=`submitForm` &figrTpl=`formReportRow`]]
    


    [table]
    [tr][td]Parameter[/td][td]Description[/td][td]Default[/td][/tr]
    [tr][td]&figrTpl[/td][td]the row template[/td][td]formReportRow[/td][/tr]
    [tr][td]&figrAllValuesLabel[/td][td]the name of the placeholder generated for the complete report[/td][td]figr_values[/td][/tr]
    [tr][td]&figrExcludedFields[/td][td]a list of all fields to be excluded from the report[/td][td]nospam,blank,recaptcha_challenge_field,recaptcha_response_field[/td][/tr]
    [/table]

    Here is a possible ContactFormReport chunk, which works for any form:
    <p>A <strong>[[++site_name]]</strong> contact form submission was sent from the <strong>[[*pagetitle]]</strong> page:</p>
    [[+figr_values]]
    


    Here is the formReportRow chunk, which you will need as a template for each report row (note the replacements, which depend on the naming convention above):
    <p><strong>[[+field:replace=`_== `:ucwords]]:</strong> [[+value:nl2br]]</p>


    And here is the actual fiGenerateReport snippet code:
    <?php
    /* 
     * fiGenerateReport formit hook. Processes all values stored as arrays and implodes them, and creates a placeholder with every field except those excluded. 
     * Copyright 2011 Oleg Pryadko (websitezen.com)
     * License GPL 2 or later 
    */ 
    $fieldTpl = $modx->getOption('figrTpl',$scriptProperties,'formReportRow');
    $allValuesLabel = $modx->getOption('figrAllValuesLabel',$scriptProperties,'figr_values');
    $excludedFields = explode(',',$modx->getOption('figrExcludedFields',$scriptProperties,'nospam,blank,recaptcha_challenge_field,recaptcha_response_field'));
    $excludedFields[] = $modx->getOption('submitVar',$scriptProperties,'submit');
    
    // implode fields
    $imploded = '';
    $allFormFields = $hook->getValues();
    foreach ($allFormFields as $fieldName => $fieldValue) {
      if ((!in_array($fieldName,$excludedFields)) && (!is_array($fieldValue))) {
        $imploded .= $modx->getChunk($fieldTpl,array(
          'field' => $fieldName,
          'value' => $fieldValue
        ));
      }
    }
      
    //  generate placeholder
    $hook->setValue($allValuesLabel,$imploded);
    return true;


    Finally, here is the fiProcessArrays snippet code. Call it before the fiGenerateReport like so: &hooks=`recaptcha,spam,fiProcessArrays,fiGenerateReport,email,redirect`
    <?php
    /* 
     * fiProcessArrays formit hook. Processes all values stored as arrays and implodes them.
     * Copyright Oleg Pryadko (websitezen.com) 2011
     * License GPL 2 or later 
    */ 
    $fieldSuffix = $modx->getOption('fipaFieldSuffix',$scriptProperties,'_values');
    $separator = $modx->getOption('fipaValueSeparator',$scriptProperties,', ');
    $allFormFields = $hook->getValues();
    foreach ($allFormFields as $fieldName => $fieldValue) {
      if (is_array($fieldValue)) {
        $imploded = '';
        $count=0;
        foreach ($fieldValue as $value) {
          if (!empty($value)) {
            if ($count) {$imploded .= $separator;}
            $imploded .= $value;
            $count++;
          }
        }
        $hook->setValue($fieldName.$fieldSuffix,$imploded);
      }
    }
    return true;
      WebsiteZen.com - MODX and E-Commerce web development in the San Francisco Bay Area