Classes

File util/MiniCalendar.class.php

File util/MiniCalendar.class.php

  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:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  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:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 
<?php
/**
 * This class enables you to retrieve easily a date entered by a user.
 * If the user isn't in the same timezone as the server, the hour will be automatically recomputed.
 * @package     Util
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 11 19
 * @since       PHPBoost 2.0 - 2008 06 03
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
 * @contributor Sebastien LARTIGUE <babsolune@phpboost.com>
*/

class MiniCalendar
{
    /**
     * @var int The number of calendars created in that page (used to know if we have to load the javascript code)
     */
    private $num_instance = 0;
    /**
     * @var string The CSS properties of the calendar
     */
    private $style = '';
    /**
     * @var string The calendar id
     */
    private $html_id = '';

    /**
     * @var Date The date it displays
     */
    private $date;

    private static $num_instances = 0;

    private static $js_inclusion_already_done = false;

    /**
     * Builds a calendar which will be displayable.
     * @param string $form_name Name of the mini calendar in the HTML code (you will retrieve the data in that field).
     * This name must be a HTML identificator.
     */
    public function __construct($html_id, Date $date = null)
    {
        $this->html_id = $html_id;
        $this->num_instance = ++self::$num_instances;

        $this->set_date($date);
    }

    /**
     * Sets the date at which will be initialized the calendar.
     * @param Date $date Date
     */
    public function set_date($date)
    {
        $this->date = $date;
    }

    /**
     * Sets the CSS properties of the element.
     * You can use it if you want to customize the mini calendar, but the best solution is to redefine the template in your module.
     * The template used is framework/mini_calendar.tpl.
     * @param string $style The CSS properties
     */
    public function set_style($style)
    {
        $this->style = $style;
    }

    /**
     * Returns the date
     * @return Date the date
     */
    public function get_date()
    {
        return $this->date;
    }

    /**
     * Displays the mini calendar. You must call the display method in the same order as the calendars are displayed, because it requires a javascript code loading.
     * @return string The code to write in the HTML page.
     */
    public function display()
    {
        //On crée le code selon le template
        $template = new FileTemplate('framework/util/mini_calendar.tpl');
        $template->add_lang(LangLoader::get('common'));

        $template->put_all(array(
            'DEFAULT_DATE' => !empty($this->date) ? $this->date->format(Date::FORMAT_ISO_DAY_MONTH_YEAR) : '',
            'CALENDAR_ID' => $this->html_id,
            'CALENDAR_NUMBER' => (string)$this->num_instance,
            'DAY' => !empty($this->date) ? $this->date->get_day() : '',
            'MONTH' => !empty($this->date) ? $this->date->get_month() : '',
            'YEAR' => !empty($this->date) ? $this->date->get_year() : '',
            'CALENDAR_STYLE' => $this->style,
            'C_INCLUDE_JS' => !self::$js_inclusion_already_done
        ));

        self::$js_inclusion_already_done = true;

        return $template->render();
    }

    /**
     * Retrieves a date entered in a mini calendar.
     * @param string $calendar_name Name of the calendar (HTML identifier).
     * @return Date The date of the calendar.
     */
    public static function retrieve_date($calendar_name)
    {
        $value = retrieve(REQUEST, $calendar_name, '', TSTRING_UNCHANGE);
        return preg_match('`^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$`', $value) > 0 ? new Date($value) : null;
    }
}
?>