forms.php 6.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php
/*
 * AECMS : CMS pour les clubs et activités de l'AE UTBM
 *
 * Copyright 2010
 * - Jérémie Laval < jeremie dot laval at gmail dot com >
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

require_once ('include/site.inc.php');
jlaval's avatar
jlaval committed
28
require_once ('include/form.inc.php');
29 30
require_once ($topdir.'include/entities/news.inc.php');

jlaval's avatar
jlaval committed
31
$form = new formulaire ($site->db, $site->dbrw);
32 33 34 35

if (isset($_REQUEST['id_form']))
  $form->load_by_id ($_REQUEST['id_form']);
else
jlaval's avatar
jlaval committed
36
  $form->load_by_asso ($site->asso->id);
37 38 39

$Erreur = false;

jlaval's avatar
jlaval committed
40 41 42 43 44 45
$site->start_page (CMS_PREFIX.'form', 'Formulaire');
$cts = new contents();

if (isset($_REQUEST['action'])) {
  if ($_REQUEST['action'] == 'addentry') {
    $Erreur = $form->validate_and_post ();
46

jlaval's avatar
jlaval committed
47 48 49
    if ($Erreur == false) {
      $cts->add_title (2, 'Merci de votre participation à : '.$form->name);
      $cts->add_paragraph ($form->success_text);
50

jlaval's avatar
jlaval committed
51 52
      $site->add_contents ($cts);
      $site->end_page ();
53

jlaval's avatar
jlaval committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      exit(0);
    }
  }

  if ($_REQUEST['action'] == 'admin' && $form->is_admin ($site->user)) {
    if (!isset($_REQUEST['view']))
      $_REQUEST['view'] = 'panel';

    if (isset($_REQUEST['op'])) {
      $form->id_asso = $site->asso->id;
      $form->name = $_REQUEST['name'];
      $form->prev_text = $_REQUEST['prev_text'];
      $form->next_text = $_REQUEST['next_text'];
      $form->success_text = $_REQUEST['success_text'];
      $form->json = $_REQUEST['json'];

      if ($_REQUEST['op'] == 'createform') {
        $Erreur = $form->create ();
      } else if ($_REQUEST['op'] == 'updateform') {
        $form->id = $_REQUEST['id_form'];
        $Erreur = $form->update ();
      }
    }

    if (isset($_REQUEST['select']) && $_REQUEST['select'] == 'true') {
      $req = new requete ($site->db, 'SELECT * FROM `aecms_forms` WHERE id_asso = '.$site->asso->id);
      $names = array();

      while ($row = $req->get_row ()) {
        $names[$row['id_form']] = $row['name'];
      }

jlaval's avatar
jlaval committed
86
      $sfrm = new form ('admin', 'forms.php?action=admin&view='.$_REQUEST['view'], false, 'POST', 'Sélection du formulaire');
jlaval's avatar
Fix  
jlaval committed
87 88 89 90
      $sfrm->add_select_field ('id_form', 'Nom du formulaire : ', $names);
      $sfrm->add_submit ('submit', 'Valider');

      $cts->add ($sfrm);
jlaval's avatar
jlaval committed
91 92 93 94 95 96 97 98
    }

    if ($_REQUEST['view'] == 'panel') {
      $cts->add_title(2, 'Admin des formulaires');

      $cts->add_paragraph ('<a href="forms.php?action=admin&view=addform">Ajouter un formulaire</a> / <a href="forms.php?action=admin&view=modform&select=true">Modifier un formulaire</a>');
      $cts->add_paragraph ('<a href="forms.php?action=admin&view=answers&select=true">Visualiser les résultats</a>');
    } else if ($_REQUEST['view'] == 'answers' && isset($_REQUEST['id_form'])) {
jlaval's avatar
jlaval committed
99 100
      $cts->add_paragraph ('<a href="forms.php?action=admin">Retour à l\'interface d\'admin</a>');

jlaval's avatar
Typo  
jlaval committed
101
      $req = new requete ($site->db, 'SELECT * FROM `aecms_forms_results` WHERE id_form = '.$_REQUEST['id_form']);
jlaval's avatar
jlaval committed
102 103 104 105

      $cts->add_title(2, 'Résultats');

      $csv = '';
jlaval's avatar
jlaval committed
106
      $tbl = new table ('Résultats', 'inline doku');
jlaval's avatar
jlaval committed
107 108 109 110 111 112 113 114 115 116 117 118
      while ($row = $req->get_row ()) {
        $obj = json_decode ($row['json_answer'], TRUE);
        if ($obj == NULL)
          continue;

        $values = array_values($obj);
        $tbl->add_row ($values);

        foreach ($values as $value) {
          $csv .= $value;
          $csv .= ',';
        }
jlaval's avatar
Foo  
jlaval committed
119
        $csv .= "\n";
jlaval's avatar
jlaval committed
120 121 122 123 124
      }

      $cts->add ($tbl);

      $cts->add_title (3, 'Format CSV');
jlaval's avatar
Foo  
jlaval committed
125
      $cts->add_paragraph('<pre>'.$csv.'</pre>');
jlaval's avatar
jlaval committed
126 127

    } else if ($_REQUEST['view'] == 'addform') {
jlaval's avatar
jlaval committed
128 129
      $cts->add_paragraph ('<a href="forms.php?action=admin">Retour à l\'interface d\'admin</a>');

jlaval's avatar
jlaval committed
130
      $cfrm = new form ('admin', 'forms.php?action=admin&view=addform', false, 'POST', 'Ajout d\'un formulaire');
jlaval's avatar
jlaval committed
131 132 133 134 135 136 137 138 139 140 141 142
      if ($Erreur != false)
        $cfrm->error ($Erreur);
      $cfrm->add_hidden ('op', 'createform');
      $cfrm->add_text_field ('name', 'Nom du formulaire', $_REQUEST['name'], true);
      $cfrm->add_text_area('prev_text', 'Texte avant le formulaire', $_REQUEST['prev_text']);
      $cfrm->add_text_area('next_text', 'Texte après le formulaire', $_REQUEST['next_text']);
      $cfrm->add_text_area('success_text', 'Texte si succès de l\'opération', $_REQUEST['success_text']);
      $cfrm->add_text_area('json', 'Description JSON', $_REQUEST['json'], 60, 20, true);
      $cfrm->add_submit('submit', 'Valider');

      $cts->add ($cfrm);
    } else if ($_REQUEST['view'] == 'modform' && isset($_REQUEST['id_form'])) {
jlaval's avatar
jlaval committed
143 144
      $cts->add_paragraph ('<a href="forms.php?action=admin">Retour à l\'interface d\'admin</a>');

jlaval's avatar
jlaval committed
145 146
      $form->load_by_id ($_REQUEST['id_form']);

jlaval's avatar
jlaval committed
147
      $cfrm = new form ('admin', 'forms.php?action=admin&view=modform', false, 'POST', 'Modification d\'un formulaire');
jlaval's avatar
jlaval committed
148 149 150 151 152 153 154
      if ($Erreur != false)
        $cfrm->error ($Erreur);
      $cfrm->add_hidden ('op', 'updateform');
      $cfrm->add_hidden ('id_form', $_REQUEST['id_form']);
      $cfrm->add_text_field ('name', 'Nom du formulaire', $form->name, true);
      $cfrm->add_text_area('prev_text', 'Texte avant le formulaire', $form->prev_text);
      $cfrm->add_text_area('next_text', 'Texte après le formulaire', $form->next_text);
jlaval's avatar
Typo  
jlaval committed
155
      $cfrm->add_text_area('success_text', 'Texte si succès de l\'opération', $form->success_text);
jlaval's avatar
jlaval committed
156 157 158 159 160
      $cfrm->add_text_area('json', 'Description JSON', $form->json, 60, 20, true);
      $cfrm->add_submit('submit', 'Valider');

      $cts->add ($cfrm);
    }
161 162 163 164 165 166 167

    $site->add_contents ($cts);
    $site->end_page ();

    exit(0);
  }

jlaval's avatar
jlaval committed
168
}
169

jlaval's avatar
jlaval committed
170 171 172 173 174 175 176 177
if (!$form->is_valid($site->asso->id)) {
  $cts->add_title(2, 'Erreur');
  $cts->add_paragraph ('Formulaire non disponible');
} else {
  $frm = $form->get_form ('addentry', 'forms.php', $Erreur);

  if ($frm == false) {
    $cts->add_title(2, 'Erreur');
178
    $cts->add_paragraph ('Il y\'a eu une erreur lors de la génération du formulaire, merci de réessayer plus tard');
jlaval's avatar
jlaval committed
179 180 181 182 183 184 185 186
  } else {
    $cts->add_title(2, $form->name);

    $cts->add_paragraph ($form->prev_text);
    $cts->add ($frm);
    $cts->add_paragraph ($form->next_text);
  }
}
187 188 189 190 191

$site->add_contents ($cts);
$site->end_page ();

?>