I recently needed to generate on the fly a text document from the field values of a form inside of a SPIP article. Based on the code I found by F.Quointeau to invoke a php script from a SPIP article, I wrote a filter that enabled me to solve my issue.
This code will allows us to place a tag
#FORM (filename)
within the body of an article and have it replaced by the content of an html template named „filename“ and placed in a directory named „formulaires“ at the root of our SPIP hierarchy.
The content of our template could look like :
<form enctype="multipart/form-data" method="post" name="postref">
<fieldset>
<legend style="caption-side: left;">Vos informations personelles</legend>
<table width="100%" border="0">
<tr>
<td width="200">
<label for="PRENOM">N:</label>
</td><td>
<input maxlength="80" size="40" name="PRENOM" value="<!--%PRENOMf%-->">
</td>
</tr><tr>
<td width="200">
<label for"NOM">Nom :</label>
</td><td>
<input maxlength="80" size="40" name="NOM" value="<!--%NOMf%-->">
</td>
</tr>
</table>
<input name="submit" value="Valider" type="submit"><br>
</form>
<h2> Ceci est le texte qui sera remplacé </h2>
<p> Bonjour, je m'appelle <!--%PRENOM%--> <!--%NOM%--></p>
In order for out filter to be called, we need to modify the squelette that will use it so that the filter is added as a parameter of the #TEXTE tage. Ex :
(#TEXTE|form_nb)
<?php
/*****************************************************************************/
/** Cette fonction permet de placer un formulaire de génération de texte
/**
/** La syntaxe est la suivante :
/** #FORM ( nom_fichier )
/**
/** Auteur : N.Barcet
/** Version : 0.0.1
/** Date : 13 Avril 2007
/*****************************************************************************/
function form_nb ( $texte ) {
// Dossier où sont stockés les formulaires
$dossier_inclus = 'formulaires/';
// Les fichiers inclus autorises sont : *.htm, *.html, *.tpl
$chaine_recherche = '/#FORM( *)\(( *)([^\)]*\.(tpl|htm|html))( *)\)/i';
// Recherche la chaine #FORM(form)
while( preg_match( $chaine_recherche, $texte, $resultats)) {
// Verifie que le fichier existe
if( file_exists ( $dossier_inclus . $resultats[3])) {
// Vide le buffer de sortie
$affichage_php = '';
//Liste des champs que nous devons traiter dans le formulaire
$fields = array("PRENOM","NOM");
$file = $dossier_inclus . $resultats[3];
//On récupère le contenu de la template du formulaire
$fd = fopen($file, "r");
$doc = fread($fd, filesize ($file));
fclose($fd);
//Remplace les champs par leur valeur
$size = count($fields);
for ($i=1 ; $i <= $size; $i++) {
$doc = str_replace("<!--%".$fields[$i]."%-->", htmlentities(utf8_decode($_POST["$fields[$i]"])), $doc);
$doc = str_replace("<!--%".$fields[$i]."f%-->", $_POST[$fields[$i]], $doc);
}
//retourne le formulaire complet
$affichage_php = $doc;
} else {
$affichage_php = "<b>#INCLURE: Le fichier de formulaire n'existe pas !</b>";
}
// Attention, n'effectue qu'un seul remplacement à la fois !
$texte = preg_replace( $chaine_recherche, $affichage_php, $texte, 1);
}
return( $texte);
}
?>