/**
 * Main plugin call.
 *
 * @param string $id A form ID.
 *
 * @return string (X)HTML.
 *
 * @global array  The configuration of the plugins.
 * @global array  The localization of the plugins.
 * @global string The script name.
 * @global string The (X)HTML fragment containing error messages.
 * @global array  The paths of system files and folders.
 */
function Advancedform_main($id)
{
    global $plugin_cf, $plugin_tx, $sn, $e, $pth;
    $pcf = $plugin_cf['advancedform'];
    $ptx = $plugin_tx['advancedform'];
    $fn = $pth['folder']['plugins'] . $pcf['captcha_plugin'] . '/captcha.php';
    if (file_exists($fn)) {
        include_once $fn;
    } else {
        e('cntopen', 'file', $fn);
    }
    $hooks = Advancedform_dataFolder() . $id . '.inc' . ($pcf['php_extension'] ? '.php' : '');
    if (file_exists($hooks)) {
        include $hooks;
    }
    $forms = Advancedform_db();
    if (!isset($forms[$id])) {
        $e .= '<li>' . sprintf($ptx['error_form_missing'], $id) . '</li>' . PHP_EOL;
        return '';
    }
    $form = $forms[$id];
    if (isset($_POST['advfrm']) && $_POST['advfrm'] == $id) {
        if (($res = Advancedform_check($id)) === true) {
            if ($form['store']) {
                Advancedform_appendCsv($id);
            }
            if (!Advancedform_mail($id, false)) {
                return Advancedform_formView($id);
            }
            if (function_exists('advfrm_custom_thanks_page')) {
                Advancedform_fields($fields);
                $thanks = advfrm_custom_thanks_page($id, $fields);
            }
            if (empty($thanks)) {
                $thanks = $form['thanks_page'];
            }
            if (!empty($thanks)) {
                if (!Advancedform_mail($id, true)) {
                    return Advancedform_formView($id);
                }
                header('Location: ' . $sn . '?' . $thanks);
                // FIXME: exit()?
            } else {
                return Advancedform_mailInfo($id, false, true);
            }
        } else {
            return $res . Advancedform_formView($id);
        }
    }
    return Advancedform_formView($id);
}
/**
 * Creates a basic template of the form. Returns the the mail form administration.
 *
 * @param string $id A form ID.
 *
 * @return string (X)HTML.
 *
 * @global array  The configuration of the plugins.
 * @global object The CSRF protector.
 */
function Advancedform_createFormTemplate($id)
{
    global $plugin_cf, $_XH_csrfProtection;
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        return Advancedform_formsAdministration();
    }
    if (isset($_XH_csrfProtection)) {
        $_XH_csrfProtection->check();
    }
    $forms = Advancedform_db();
    if (isset($forms[$id])) {
        $form = $forms[$id];
        $tpl = '<div id="advfrm-' . $id . '">' . PHP_EOL;
        $css = '#advfrm-' . $id . ' {}' . PHP_EOL . PHP_EOL . '#advfrm-' . $id . ' div.break {clear: both}' . PHP_EOL . PHP_EOL . '#advfrm-' . $id . ' div.float {float: left; margin-right: 1em}' . PHP_EOL . PHP_EOL . '#advfrm-' . $id . ' div.label' . ' {/* float: left; width: 12em; margin-bottom: 0.5em; */}' . PHP_EOL . '#advfrm-' . $id . ' div.field ' . ' { margin-bottom: 0.5em; /* float: left;*/}' . PHP_EOL . PHP_EOL . '/* the individual fields */' . PHP_EOL . PHP_EOL;
        $first = true;
        foreach ($form['fields'] as $field) {
            if ($first) {
                $tpl .= '  <?php Advancedform_focusField(\'' . $id . '\', \'advfrm-' . $field['field'] . '\')' . ' // focus the first field?>' . PHP_EOL;
                $first = false;
            }
            $labelled = !in_array($field['type'], array('checkbox', 'radio', 'hidden'));
            if (in_array($field['type'], array('hidden'))) {
                $label = '';
            } elseif (!$field['required']) {
                $label = $field['label'];
            } else {
                $label = sprintf($plugin_cf['advancedform']['required_field_mark'], $field['label']);
            }
            if ($labelled) {
                $label = '<label for="advfrm-' . $id . '-' . $field['field'] . '">' . $label . '</label>';
            }
            $tpl .= '  <div class="break">' . PHP_EOL . '    <div class="label">' . $label . '</div>' . PHP_EOL . '    <div class="field"><?field ' . $field['field'] . '?></div>' . PHP_EOL . '  </div>' . PHP_EOL;
            $css .= '#advfrm-' . $id . '-' . $field['field'] . ' {}' . PHP_EOL;
        }
        $tpl .= '  <div class="break"></div>' . PHP_EOL . '</div>' . PHP_EOL;
        $fn = Advancedform_dataFolder() . $id . '.tpl';
        if (!($fh = fopen($fn, 'w')) || fwrite($fh, $tpl) === false) {
            e('cntsave', 'file', $fn);
        }
        if ($fh) {
            fclose($fh);
        }
        $fn = Advancedform_dataFolder() . 'css/' . $id . '.css';
        if (!($fh = fopen($fn, 'w')) || fwrite($fh, $css) === false) {
            e('cntsave', 'file', $fn);
        }
        if ($fh) {
            fclose($fh);
        }
    } else {
        $e .= '<li><b>' . sprintf($ptx['error_form_missing'], $id) . '</b></li>';
    }
    return Advancedform_formsAdministration();
}