PHP Class Zebra_Form_Date, Zebra_Form

@author Stefan Gabos
Inheritance: extends Zebra_Form_Control
Show file Open project: stefangabos/zebra_form

Public Methods

Method Description
__construct ( string $id, string $default = '', array $attributes = '' ) : void Adds a date control to the form.
_init ( ) : array Initializes the datepicker's data so calculations for disabled dates can be done.
_is_disabled ( integer $year, integer $month = '', integer $day = '' ) : boolean Checks if, according to the restrictions of the calendar and/or the values defined by the "disabled_dates" property, a day, a month or a year needs to be disabled.
_is_format_valid ( $date ) : mixed Checks if the enetered value is a valid date in the right format.
always_visible ( string $element ) : void Should the date picker be always visible?
container ( string $element ) : void Use this method to instruct the library to open a date picker inside a specific element - useful when you want the date picker to open at a specific position.
custom_classes ( array $custom_classes ) : void Dates that should have custom classes applied to them.
default_position ( string $position ) : void The position of the date picker relative to the element it is attached to.
direction ( mixed $direction ) : void Direction of the calendar.
disable_zebra_datepicker ( ) : void By default, Zebra_Form relies on {@link http://stefangabos.ro/jquery/zebra-datepicker/ Zebra_DatePicker} for {@link Zebra_Form_Date Date} controls. If you want to use a different date picker, you have to disable the built-in one by calling this method.
disabled_dates ( array $disabled_dates ) : void Disables selection of specific dates or range of dates in the calendar.
enabled_dates ( array $enabled_dates ) : void Enables selection of specific dates or range of dates in the calendar, after dates have been previously disabled via {@link disabled_dates()}.
first_day_of_week ( integer $day ) : void Week's starting day.
format ( string $format ) : void Sets the format of the returned date.
get_date ( ) : string To be used after the form is submitted!
header_captions ( $captions ) : void Captions in the datepicker's header, for the 3 possible views: days, months, years.
header_navigation ( $navigation ) : void Sets the HTML to be used for the previous month/next month buttons.
icon_position ( string $position ) : void The position of the date picker's inside the element it is attached to.
inside ( boolean $value ) : void Sets whether the icon for opening the datepicker should be inside or outside the element.
offset ( array $value ) : void Sets the offset, in pixels (x, y), to shift the date picker’s position relative to the top-left of the icon that toggles the date picker.
open_icon_only ( array $value ) : void Sets whether the date picker should be shown *only* when clicking the icon.
pair ( string $value ) : void Pairs the date element with another date element from the page, so that the other date element will use the current date element’s value as starting date.
readonly_element ( boolean $value ) : void Sets whether the element the calendar is attached to should be read-only.
select_other_months ( string $value ) : void Should days from previous and/or next month be selectable when visible?
show_clear_date ( string $value ) : void Should the "Clear date" button be visible?
show_icon ( boolean $visible ) : void Should a calendar icon be added to the elements the plugin is attached to?
show_other_months ( string $value = true ) : void Should days from previous and/or next month be visible?
show_select_today ( string $value = 'Today' ) : void Should the "Today" button be visible?
show_week_number ( string $value ) : void Sets whether an extra column should be shown, showing the number of each week.
start_date ( date $value ) : void Sets a default date to start the date picker with.
strict ( boolean $value ) : void Sets whether default values, in the input field the date picker is attached to, be deleted if they are not valid according to {@link direction() direction} and/or {@link disabled_dates() disabled_dates}.
toHTML ( ) : string Generates the control's HTML code.
view ( string $view ) : void Sets how should the date picker start.
weekend_days ( array $days ) : void Sets the days of the week that are to be considered as "weekend days".
zero_pad ( boolean $state ) : void Should day numbers < 10 be padded with zero?

Method Details

__construct() public method

Do not instantiate this class directly! Use the {@link Zebra_Form::add() add()} method instead! The output of this control will be a {@link Zebra_Form_Text textbox} control with an icon to the right of it.
Clicking the icon will open an inline JavaScript date picker.
create a new form $form = new Zebra_Form('my_form'); add a date control to the form $mydate = $form->add('date', 'my_date', date('Y-m-d')); you *have* to set the "date" rule $mydate->set_rule(array( 'date' => array('error', 'Invalid date specified!'), )); set the date's format $mydate->format('M d, Y'); don't forget to always call this method before rendering the form if ($form->validate()) { get the date in YYYY-MM-DD format so you can play with is easily $date = $mydate->get_date(); } output the form using an automatically generated template $form->render();
public __construct ( string $id, string $default = '', array $attributes = '' ) : void
$id string Unique name to identify the control in the form. The control's name attribute will be the same as the id attribute! This is the name to be used when referring to the control's value in the POST/GET superglobals, after the form is submitted. This is also the name of the variable to be used in custom template files, in order to display the control. // in a template file, in order to print the generated HTML // for a control named "my_date", one would use: echo $my_date; @param string $default (Optional) Default date, formatted according to {@link format() format}. @param array $attributes (Optional) An array of attributes valid for {@link http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.4 input} controls (size, readonly, style, etc) Must be specified as an associative array, in the form of attribute => value. // setting the "readonly" attribute $obj = $form->add( 'date', 'my_date', '', array( 'readonly' => 'readonly' ) ); See {@link Zebra_Form_Control::set_attributes() set_attributes()} on how to set attributes, other than through the constructor. The following attributes are automatically set when the control is created and should not be altered manually:
type, id, name, value, class @return void
$default string
$attributes array
return void

_init() public method

Returns an array with two values: the first and the last selectable dates, as UNIX timestamps. If a value does not apply (i.e. no starting or no ending date), the value will be "0".
public _init ( ) : array
return array Returns an array with two values: the first and the last selectable dates, as UNIX timestamps. @access private

_is_disabled() public method

@param integer $year The year to check
public _is_disabled ( integer $year, integer $month = '', integer $day = '' ) : boolean
$year integer
$month integer The month to check @param integer $day The day to check @return boolean Returns TRUE if the given value is not disabled or FALSE otherwise @access private
$day integer
return boolean

_is_format_valid() public method

@return mixed Returns the UNIX timestamp of the checked date, if the date has the correct format, or FALSE otherwise.
public _is_format_valid ( $date ) : mixed
return mixed

always_visible() public method

Setting this property to a jQuery element will result in the date picker being always visible, the indicated element acting as the date picker's container; Note that when this property is set to TRUE, the {@link always_show_clear()} will be automatically set to TRUE. $date = $form->add('date', 'my_date'); an element having the ID "container" will be the date picker's container $date->always_visible('$("#container")');
public always_visible ( string $element ) : void
$element string A jQuery selector pointing to an existing element from the page to be used as the date picker's container. @return void
return void

container() public method

$date = $form->add('date', 'my_date'); the date picker will open inside this element $date->container('$("#container")');
public container ( string $element ) : void
$element string A jQuery selector pointing to an existing element from the page to be used as the date picker's container. By default, all date pickers are placed at the end of the element @since 2.9.8 @return void
return void

custom_classes() public method

$date = $form->add('date', 'my_date'); apply "myclass1" custom class to the first day, of every month, of every year $date->custom_classes(array( 'myclass1' => array('01 * *'), ));
public custom_classes ( array $custom_classes ) : void
$custom_classes array An array in the form of array( 'myclass1': array(dates_to_apply_myclass1_to), 'myclass2': array(dates_to_apply_myclass2_to), ) ...where "dates_to_apply_myclassx_to" is an array of dates in the same format as required for {@link disabled_dates} property. Custom classes will be applied only in the day picker view and not on month/year views! Also note that the class name will have the “_disabled” suffix added if the day the class is applied to is disabled. In order for the styles in your custom classes to be applied, make sure you are using the following syntax: .Zebra_DatePicker .dp_daypicker td.myclass1 { .. } .Zebra_DatePicker .dp_daypicker td.myclass1_disabled { .. } Default is FALSE, no custom classes @since 2.9.8 @return void
return void

default_position() public method

Note that, regardless of this setting, the date picker's position will be automatically adjusted to fit in the view port, if needed. This property will be ignored if {@link always_visible} or {@link container} properties are set $date = $form->add('date', 'my_date'); the date picker will open *below* the element is attached to $date->default_position('below');
public default_position ( string $position ) : void
$position string The position of the date picker relative to the element it is attached to. Possible values are "above" and "below". Default is "above" @since 2.9.8 @return void
return void

direction() public method

$obj = $form->add('date', 'mydate') calendar starts tomorrow and seven days after that are selectable $obj->direction(array(1, 7)); calendar starts today and seven days after that are selectable $obj->direction(array(true, 7)); calendar starts on January 1st 2013 and has no ending date (assuming "format" is YYYY-MM-DD) $obj->direction(array('2013-01-01', false)); calendar ends today and starts on January 1st 2012 assuming "format" is YYYY-MM-DD) $obj->direction(array(false, '2012-01-01'));
public direction ( mixed $direction ) : void
$direction mixed A positive or negative integer: - n (a positive integer) creates a future-only calendar beginning at n days after today; - -n (a negative integer) creates a past-only calendar ending at n days before today; - if n is 0, the calendar has no restrictions. Use boolean TRUE for a future-only calendar starting with today and use boolean FALSE for a past-only calendar ending today. You may also set this property to an array with two elements in the following combinations: - first item is boolean TRUE (calendar starts today), an integer > 0 (calendar starts n days after today), or a valid date given in the format defined by the "format" attribute (calendar starts at the specified date), and the second item is boolean FALSE (the calendar has no ending date), an integer > 0 (calendar ends n days after the starting date), or a valid date given in the format defined by the "format" attribute and which occurs after the starting date (calendar ends at the specified date) - first item is boolean FALSE (calendar ends today), an integer < 0 (calendar ends n days before today), or a valid date given in the format defined by the "format" attribute (calendar ends at the specified date), and the second item is an integer > 0 (calendar ends n days before the ending date), or a valid date given in the format defined by the "format" attribute and which occurs before the starting date (calendar starts at the specified date) Note that {@link disabled_dates()} will still apply! Default is 0 (no restrictions). @return void
return void

disable_zebra_datepicker() public method

Make sure the language used by the custom date picker is the same as the {@link language() language} of the library, or validation of the date will fail! Also, note that {@link format() format}, {@link direction() direction} and {@link disabled_dates() disabled dates} will still apply and will be taken into account when validating the date, but the other properties will be ignored as are specific to Zebra_DatePicker!
Since: 2.8.7 @return void
public disable_zebra_datepicker ( ) : void
return void

disabled_dates() public method

$obj = $form->add('date', 'mydate') disable January 1, 2012 $obj->disabled_dates(array('1 1 2012')); disable all days in January 2012 $obj->disabled_dates(array('* 1 2012')); disable January 1 through 10 in 2012 $obj->disabled_dates(array('1-10 1 2012')); disable January 1 and 10 in 2012 $obj->disabled_dates(array('1,10 1 2012')); disable 1 through 10, and then 20th, 22nd and 24th of January through March for every year $obj->disabled_dates(array('1-10,20,22,24 1-3 *')); disable all Saturdays and Sundays $obj->disabled_dates(array('* * * 0,6')); disable 1st and 2nd of July 2012, and all of August of 2012; $obj->disabled_dates(array('01 07 2012', '02 07 2012', '* 08 2012'));
public disabled_dates ( array $disabled_dates ) : void
$disabled_dates array An array of strings representing disabled dates. Values in the string have to be in the following format: "day month year weekday" where "weekday" is optional and can be 0-6 (Saturday to Sunday); The syntax is similar to cron's syntax: the values are separated by spaces and may contain * (asterisk) - (dash) and , (comma) delimiters. Default is FALSE, no disabled dates. @return void
return void

enabled_dates() public method

@param array $enabled_dates An array of enabled dates in the same format as required for as argument for the {@link disabled_dates()} method. To be used together with {@link disabled_dates()} by first setting "disabled_dates" to something like array('* * * *') (which will disable everything) and then setting "enabled_dates" to, say, array('* * * 0,6') to enable just weekends. Default is FALSE, all dates are enabled (unless, specificaly disabled via {@link disabled_dates()}).
Since: 2.9.3 @return void
public enabled_dates ( array $enabled_dates ) : void
$enabled_dates array
return void

first_day_of_week() public method

@param integer $day Valid values are 0 to 6, Sunday to Saturday. Default is 1, Monday.
public first_day_of_week ( integer $day ) : void
$day integer
return void

format() public method

@param string $format Format of the returned date. Accepts the following characters for date formatting: d, D, j, l, N, w, S, F, m, M, n, Y, y borrowing syntax from ({@link http://www.php.net/manual/en/function.date.php PHP's date function}) Note that when setting a date format without days (‘d’, ‘j’), the users will be able to select only years and months, and when setting a format without months and days (‘F’, ‘m’, ‘M’, ‘n’, ‘t’, ‘d’, ‘j’), the users will be able to select only years. Also note that the value of the "view" property (see below) may be overridden if it is the case: a value of "days" for the "view" property makes no sense if the date format doesn’t allow the selection of days. Default format is Y-m-d
public format ( string $format ) : void
$format string
return void

get_date() public method

Returns submitted date in the YYYY-MM-DD format so that it's directly usable with a database engine or with PHP's {@link http://php.net/manual/en/function.strtotime.php strtotime} function.
public get_date ( ) : string
return string Returns submitted date in the YYYY-MM-DD format, or an empty string if control was submitted with no value (empty).

header_captions() public method

For each of the 3 views the following special characters may be used borrowing from PHP's "date" function's syntax: m, n, F, M, y and Y; any of these will be replaced at runtime with the appropriate date fragment, depending on the currently viewed date. two more special characters are also available Y1 and Y2 (upper case representing years with 4 digits, lowercase representing years with 2 digits) which represent "currently selected year - 7" and "currently selected year + 4" and which only make sense used in the "years" view. Even though any of these special characters may be used in any of the 3 views, you should use m, n, F, M for the "days" view and y, Y, Y1, Y2, y1, y2 for the "months" and "years" view or you may get unexpected results! Text and HTML can also be used, and will be rendered as it is, as in the example below (the library is smart enough to not replace special characters when used in words or HTML tags): header_captions(array( 'days' => 'Departure:
F, Y', 'months' => 'Departure:
Y', 'years' => 'Departure:
Y1 - Y2' ));
Default is header_captions(array( 'days' => 'F, Y', 'months' => 'Y', 'years' => 'Y1 - Y2' ));
public header_captions ( $captions ) : void
$captions An associative array containing captions in the datepicker's header, for the 3 possible views: days, months, years. @return void
return void

header_navigation() public method

@param $navigation An array with 2 elements containing the HTML to be used for the previous month/next month buttons. Default is array('«','»')
public header_navigation ( $navigation ) : void
return void

icon_position() public method

$date = $form->add('date', 'my_date'); position the date picker's icon to the left $date->icon_position('left');
public icon_position ( string $position ) : void
$position string The position of the date picker's inside the element it is attached to. Possible values are "left" and "right". Default is "right" @since 2.9.8 @return void
return void

inside() public method

@param boolean $value If set to FALSE, the icon will be placed to the right of the parent element, while if set to TRUE it will be placed to the right of the parent element, but *inside* the element itself. Default is TRUE.
public inside ( boolean $value ) : void
$value boolean
return void

offset() public method

@param array $value An array indicating the offset, in pixels (x, y), to shift the date picker’s position relative to the top-left of the icon that toggles the date picker. Default is array(5, -5).
public offset ( array $value ) : void
$value array
return void

open_icon_only() public method

@param array $value An array indicating the offset, in pixels (x, y), to shift the date picker’s position relative to the top-left of the icon that toggles the date picker. Default is FALSE.
public open_icon_only ( array $value ) : void
$value array
return void

pair() public method

let's assume this will be the starting date $date1 = $form->add('date', 'starting_date'); dates are selectable in the future, starting with today $date1->direction(true); indicate another date element that will use this element's value as starting date $date1->pair('ending_date'); the other date element $date2 = $form->add('date', 'ending_date'); start one day after the reference date (that is, one day after whaterver is selected in the first element) $date2->direction(1);
public pair ( string $value ) : void
$value string The ID of another "date" element which will use the current date element's value as starting date. Note that the rules set in the "direction" property will still apply, only that the reference date will not be the current system date but the value selected in the current date picker. Default is FALSE (not paired with another date picker) @return void
return void

readonly_element() public method

@param boolean $value The setting's value If set to TRUE, a date can be set only through the date picker and cannot be enetered manually. Default is TRUE.
public readonly_element ( boolean $value ) : void
$value boolean
return void

select_other_months() public method

@param string $value The setting's value Note that if set to TRUE, the value of {@link show_other_months()} will be considered TRUE regardless of the actual value! Default is TRUE.
Since: 2.9.3 @return void
public select_other_months ( string $value ) : void
$value string
return void

show_clear_date() public method

@param string $value The setting's value Accepted values are: - 0 (zero) – the button for clearing a previously selected date is shown only if a previously selected date already exists; this means that if the input the date picker is attached to is empty, and the user selects a date for the first time, this button will not be visible; once the user picked a date and opens the date picker again, this time the button will be visible. - TRUE will make the button visible all the time - FALSE will disable the button Default is "0" (without quotes)
public show_clear_date ( string $value ) : void
$value string
return void

show_icon() public method

$date = $form->add('date', 'my_date'); do not show the icon $date->show_icon(false);
public show_icon ( boolean $visible ) : void
$visible boolean When set to TRUE the plugin will attach a calendar icon to the elements the plugin is attached to. Default is TRUE @since 2.9.8 @return void
return void

show_other_months() public method

@param string $value The setting's value Default is TRUE.
Since: 2.9.3 @return void
public show_other_months ( string $value = true ) : void
$value string
return void

show_select_today() public method

@param string $value The setting's value Setting this property to anything but a boolean FALSE will enable the button and will use the property's value as caption for the button; setting it to FALSE will disable the button. Default is "Today"
Since: 2.9.4 @return void
public show_select_today ( string $value = 'Today' ) : void
$value string
return void

show_week_number() public method

@param string $value Anything other than FALSE will enable this feature, and use the given value as column title. For example, show_week_number: ‘Wk’ would enable this feature and have "Wk" as the column’s title. Default is FALSE.
public show_week_number ( string $value ) : void
$value string
return void

start_date() public method

@param date $value A default date to start the date picker with, Must be specified in the format defined by the "format" property, or it will be ignored! Note that this value is used only if there is no value in the field the date picker is attached to! Default is FALSE.
public start_date ( date $value ) : void
$value date
return void

strict() public method

@param boolean $value If set to TRUE, default values, in the input field the date picker is attached to, will be deleted if they are not valid according to {@link direction() direction} and/or {@link disabled_dates() disabled_dates}. Default is FALSE.
public strict ( boolean $value ) : void
$value boolean
return void

toHTML() public method

This method is automatically called by the {@link Zebra_Form::render() render()} method!
public toHTML ( ) : string
return string The control's HTML code

view() public method

@param string $view How should the date picker start. Valid values are "days", "months" and "years". Note that the date picker is always cycling days-months-years when clicking in the date picker's header, and years-months-days when selecting dates (unless one or more of the views are missing due to the date's format) Also note that the value of the "view" property may be overridden if the date's format requires so! (i.e. "days" for the "view" property makes no sense if the date format doesn't allow the selection of days) Default is "days".
public view ( string $view ) : void
$view string
return void

weekend_days() public method

@param array $days An array of days of the week that are to be considered as "weekend days". Valid values are 0 to 6 (Sunday to Saturday). Default is array(0,6) (Saturday and Sunday).
public weekend_days ( array $days ) : void
$days array
return void

zero_pad() public method

@param boolean $state When set to TRUE, day numbers < 10 will be prefixed with 0. Default is FALSE.
public zero_pad ( boolean $state ) : void
$state boolean
return void