<?php

use generators\DbConversor;
use lib\Email;
use lib\Config;
// /submitform/:id controller
// Submit form controller
// A form gets submitted and you get redirected where the form says
// TODO: Validate different field types
$app->post('/submitform/:id', function ($id) use($app) {
    $c = array();
    $parameters = array();
    $post_vars = $app->request()->post();
    // We grab the form
    $form = models\Form::find($id);
    // We grab its fields
    $fields = $form->fields;
    // We grab its contacts
    $contacts = $form->contacts;
    // We add a response
    $response = new models\ModelBuilder();
    $response::fromTable('responses_' . $id);
    $response->form_id = $id;
    // This is the array containing the form fields and their values
    // that will be passed to the twig template for them to be sent by e-mail
    $parameters['fields'] = array();
    // We have to iterate through the form fields
    foreach ($post_vars as $key => $var) {
        // We skip everything that is not a field
        // as field lengths for example
        if (substr($key, 0, 6) != 'field_') {
<?php

// GET index route
$app->get('/listing', function () use($app) {
    $c = array();
    $forms = models\Form::all();
    // We add the number of responses depending on entries in the dynamically-created table
    // associated to each form
    foreach ($forms as $f) {
        $f['responses'] = $f->getResponses($f->id);
        $c['forms'][] = $f;
    }
    $app->render('pages/listing.html', $c);
})->name('listing');