}
 }
 // Read in the criteria field selection, if any
 $i = 1;
 while (isset($_POST['defcritsel' . $i])) {
     // then there is at least one criteria
     // Build the criteria default string
     $Prefs['CritListings'][$i - 1]['params']['default'] = $_POST['defcritsel' . $i];
     $Prefs['CritListings'][$i - 1]['params']['min_val'] = $_POST['fromvalue' . $i];
     $Prefs['CritListings'][$i - 1]['params']['max_val'] = $_POST['tovalue' . $i];
     $i++;
 }
 // build the pdf pages (this function exits the script if successful; otherwise returns with error)
 if ($Prefs['serialform']) {
     // build sequential form (receipt)
     $output = BuildSeq($ReportID, $Prefs, $delivery_method);
     // build and output form, should not return from this function
 } else {
     // build PDF
     $output = BuildPDF($ReportID, $Prefs, $delivery_method);
     // build and output form, should not return from this function
 }
 // if we are here, there's been an error or delivery method was email
 if ($output['pdf'] && $delivery_method == 'S') {
     // open a temp file
     $temp_file = DIR_FS_MY_FILES . $_SESSION['company'] . '/' . $output['filename'];
     $handle = fopen($temp_file, 'w');
     // put the string into the file
     fwrite($handle, $output['pdf']);
     fclose($handle);
     // generate the email
Example #2
0
function BuildForm($report, $delivery_method = 'D')
{
    // for forms only
    global $db, $messageStack, $FieldValues;
    require_once DIR_FS_MODULES . 'phreeform/classes/form_generator.php';
    $output = array();
    // check for at least one field selected to show
    if (!$report->fieldlist) {
        // No fields are checked to show, that's bad
        return $messageStack->add(PHREEFORM_NOROWS, 'caution');
    }
    // Let's build the sql field list for the general data fields (not totals, blocks or tables)
    $strField = array();
    foreach ($report->fieldlist as $key => $field) {
        // check for a data field and build sql field list
        if (in_array($field->type, array('Data', 'BarCode', 'ImgLink'))) {
            // then it's data field make sure it's not empty
            if ($field->boxfield[0]->fieldname) {
                $strField[] = prefixTables($field->boxfield[0]->fieldname) . ' as d' . $key;
            } else {
                // the field is empty, bad news, error and exit
                return $messageStack->add(PHREEFORM_EMPTYFIELD . $key, 'error');
            }
        }
    }
    $report->sqlField = implode(', ', $strField);
    // fetch the sort order and add to group by string to finish ORDER BY string
    $strSort = array();
    if (is_array($report->sortlist)) {
        foreach ($report->sortlist as $sortline) {
            if ($sortline->default == '1') {
                $strSort[] = prefixTables($sortline->fieldname);
            }
        }
    }
    $sqlSort = implode(', ', $strSort);
    // fetch criteria and date filter info
    $strCrit = array();
    if ($report->datedefault) {
        $dates = gen_build_sql_date($report->datedefault, prefixTables($report->datefield));
        if ($dates['sql']) {
            $strCrit[] = $dates['sql'];
        }
    }
    //echo 'report = '; print_r($report); echo '<br />';
    $criteria = build_criteria($report);
    if ($criteria['sql']) {
        $strCrit[] = $criteria['sql'];
    }
    $report->sqlCrit = implode(' and ', $strCrit);
    // fetch the tables to query
    $report->sqlTable = '';
    foreach ($report->tables as $table) {
        if (isset($table->relationship)) {
            if (!$table->joinopt) {
                $table->joinopt = ' JOIN ';
            }
            $report->sqlTable .= ' ' . $table->joinopt . ' ' . DB_PREFIX . $table->tablename . ' ON ' . prefixTables($table->relationship);
        } else {
            $report->sqlTable .= DB_PREFIX . $table->tablename;
        }
    }
    // We now have the sql, find out how many groups in the query (to determine the number of forms)
    $form_field_list = $report->filenamefield == '' ? prefixTables($report->formbreakfield) : prefixTables($report->formbreakfield) . ', ' . prefixTables($report->filenamefield);
    $sql = 'select ' . $form_field_list . ' from ' . $report->sqlTable;
    if ($report->sqlCrit) {
        $sql .= ' where ' . $report->sqlCrit;
    }
    $sql .= ' group by ' . prefixTables($report->formbreakfield);
    if ($strSort) {
        $sql .= ' order by ' . $sqlSort;
    }
    // execute sql to see if we have data
    //echo 'sql = ' . $sql . '<br />'; exit();
    $result = $db->Execute($sql);
    if (!$result->RecordCount()) {
        return $messageStack->add(PHREEFORM_NOROWS, 'caution');
    }
    // set the filename for download or email
    if ($report->filenameprefix || $report->filenamefield) {
        $report->filename = $report->filenameprefix . $result->fields[strip_tablename($report->filenamefield)] . '.pdf';
    } else {
        $report->filename = ReplaceNonAllowedCharacters($report->title) . '.pdf';
    }
    // create an array for each form
    $report->recordID = array();
    while (!$result->EOF) {
        $report->recordID[] = $result->fields[strip_tablename($report->formbreakfield)];
        $result->MoveNext();
    }
    // retrieve the company information
    for ($i = 0; $i < sizeof($report->fieldlist); $i++) {
        if ($report->fieldlist[$i]->type == 'CDta') {
            $report->fieldlist[$i]->text = constant($report->fieldlist[$i]->boxfield[0]->fieldname);
        } else {
            if ($report->fieldlist[$i]->type == 'CBlk') {
                if (!$report->fieldlist[$i]->boxfield) {
                    return $messageStack->add(PHREEFORM_EMPTYFIELD . $report->fieldlist[$i]->description, 'error');
                }
                $TextField = '';
                foreach ($report->fieldlist[$i]->boxfield as $entry) {
                    $temp = $entry->formatting ? ProcessData(constant($entry->fieldname), $entry->formatting) : constant($entry->fieldname);
                    $TextField .= AddSep($temp, $entry->processing);
                }
                $report->fieldlist[$i]->text = $TextField;
            }
        }
    }
    // patch for special_reports (forms) where the data file is generated externally from the standard class
    if ($report->special_class) {
        if (!($path = find_special_class($report->special_class))) {
            return false;
        }
        load_special_language($path, $report->special_class);
        require_once $path . '/classes/' . $report->special_class . '.php';
    }
    if ($report->serialform) {
        $output = BuildSeq($report, $delivery_method);
        // build sequential form (receipt style)
    } else {
        $output = BuildPDF($report, $delivery_method);
        // build standard PDF form, doesn't return if download
    }
    return $output;
    //if we are here, return the contents for an email attachment
}