Coding with SPIP 3.0
SPIP3.0 Technical Documentation

> > > > Calculating the day-of-the-year

Calculating the day-of-the-year

This short example makes it possible to calculate and display the day of the year for a date entered on a form.

This form will be named "calculate_doy", and can then be called from within a SPIP template file with #FORMULAIRE_CALCULATE_DOY or within the text of an article by using <formulaire|calculate_doy>.

Implementation

The two files necessary will be created as follows:

  • formulaires/calculate_doy.html for the HTML section
  • formulaires/calculate_doy.php for the PHP analysis and processing the CVT functions.

The HTML template file

The formulaires/calculate_doy.html file contains the following code, respecting the recommended HTML structure and CSS classes:

  1. <div class="formulaire_spip formulaire_#FORM">
  2.  
  3. [<p class="reponse_formulaire reponse_formulaire_ok">(#ENV*{message_ok})</p>]
  4. [<p class="reponse_formulaire reponse_formulaire_erreur">(#ENV*{message_erreur})</p>]
  5.  
  6. [(#ENV{editable}|oui)
  7. <form name="formulaire_#FORM" action="#ENV{action}" method="post"><div>
  8.  
  9. #ACTION_FORMULAIRE{#ENV{action}}
  10. <ul>
  11. <li class="editer_date_jour obligatoire[ (#ENV**{erreurs}|table_valeur{message}|oui)erreur]">
  12. <label for="champ_date_jour">Date (dd/mm/yyyy) :</label>
  13. [<span class='erreur_message'>(#ENV**{erreurs}|table_valeur{message})</span>]
  14. <input type="text" id="champ_date_jour" name="date_jour" value="[(#ENV{date_jour})]" />
  15. </li>
  16. </ul>
  17. <p class="boutons">
  18. <input type="submit" name="ok" value="Calculate" />
  19. </p>
  20. </div></form>
  21. ]
  22. </div>

Download

Note that the "Saisies" plugin can be used to write the form’s fields using a #SAISIE tag, and specifying the type and name of the variable used, followed by whichever optional parameters are useful. Doing so would produce (the code section between <ul> and </ul>):

  1. <ul>
  2. [(#SAISIE{input, date_jour, obligatoire=oui, label="Date (dd/mm/yyyy) :"})]
  3. </ul>

Download

Loading, verifying and processing

The formulaires/calculate_doy.php file contains the three following functions:

The "loading" file lists the variables which will be passed into the template environment and initialises their default values. There is no default date here, but it would be possible to specify one if you wanted.

  1. function formulaires_calculate_doy_charger_dist (){
  2. $valeurs = array(
  3. 'date_jour' => ''
  4. );
  5. return $valeurs;
  6. }

Download

The "verify" function checks to make sure the compulsory fields are entered and that the date format appears to be correct:

  1. function formulaires_calculate_doy_verifier_dist (){
  2. $erreurs = array();
  3. // compulsory fields
  4. foreach(array ('date_jour') as $obligatoire) {
  5. if (!_request($obligatoire)) $erreurs[$obligatoire] = 'This field is compulsory';
  6. }
  7. // correct date format
  8. if (!isset($erreurs['date_jour'])) {
  9. list($jour, $mois,U $annee) = explode('/', _request('date_jour'));
  10. if (!intval($jour) or !intval($mois) or !intval($annee)) {
  11. $erreurs['date_jour'] = "Unknown date format.";
  12. }
  13. }
  14. if (count($erreurs)) {
  15. $erreurs['message_erreur'] = 'Your data contains errors!';
  16. }
  17. return $erreurs;
  18. }

Download

If the verifications are correct (no errors found), then the "process" function is executed. The form is declared as re-editable, which means that a new date value can be entered again immediately after the validation.

  1. function formulaires_calculate_doy_traiter_dist (){
  2. $date_jour = _request('date_jour');
  3. $retour = array('editable' => true);
  4. if ($doy = calculate_doy($date_jour)) {
  5. $retour['message_ok'] = "The day of the year for $date_jour is $doy";
  6. } else {
  7. $retour['message_erreur'] = "DOY calculation error!";
  8. }
  9. return $retour;
  10. }

Download

Of course, this still omits the function used to calculate the day-of-the-year, but a few simple lines of PHP will fix that. This function can be implemented in the same file as the three previous functions:

  1. function calculate_doy($date_jour) {
  2. list($jour, $mois, $annee) = explode('/', $date_jour);
  3. if ($time = mktime( 0, 0, 0, $mois, $jour, $annee)) {
  4. return date('z', $time);
  5. }
  6. return false;
  7. }

Download

  • Author :
  • Published :
  • Updated : 02/06/10
  • Translations : English, français