PHP Classes

File: plugins/html_datepicker/plugin.php

Recommend this page to a friend!
  Classes of P. Krijnberg   Expose template engine   plugins/html_datepicker/plugin.php   Download  
File: plugins/html_datepicker/plugin.php
Role: Auxiliary script
Content type: text/plain
Description: Datepicker plugin
Class: Expose template engine
Template compiler engine extensible with plug-ins
Author: By
Last change: role
Date: 18 years ago
Size: 4,171 bytes
 

Contents

Class file image Download
<?php
function expose_plugin_html_datepicker( & $expose, $params )
{
   
$params = exposeMergeParameters( $params, array(
       
'name' => '',
       
'time' => false,
       
'fmt' => '%m %d %y',
       
'start_year' => date('Y'),
       
'end_year' => date('Y'),
       
'year_format' => '%Y',
       
'month_format' => '%B',
       
'day_format' => '%d',
       
'attr' => ''
   
) );
   
    if(
false === $params['time'] ) {
       
$params['time'] = time();
    } elseif( !
is_numeric($params['time'] ) ) {
       
$params['time'] = strtotime( $params['time'] );
    }

// build value arrays of selectboxes
//
   
   
$d = getdate( $params['time'] );
   
   
$years = array();
    for(
$i=$params['start_year']; $i<=$params['end_year']; $i++ ) {
       
$years[$i] = strftime( $params['year_format'],
           
gmmktime( 12, 0, 0, 1, 1, $i ) );
    }
   
   
$months = array();
    for(
$i=1; $i<=12; $i++ ) {
       
$months[$i] = strftime( $params['month_format'],
           
gmmktime( 12, 0, 0, $i, 1, 2000 ) );
    }
   
   
$days = array();
    for(
$i=1; $i<=31; $i++ ) {
       
$days[$i] = strftime( $params['day_format'],
           
gmmktime( 12, 0, 0, 1, $i, 2000 ) );
    }
   
   
$hours = array();
    for(
$i=0; $i<=23; $i++ ) {
       
$hours[$i] = str_pad( $i, 2, 0, STR_PAD_LEFT );
    }
   
   
$minutes = array();
    for(
$i=0; $i<=59; $i++ ) {
       
$minutes[$i] = str_pad( $i, 2, 0, STR_PAD_LEFT );
    }
   
   
$seconds = array();
    for(
$i=0; $i<=59; $i++ ) {
       
$seconds[$i] = str_pad( $i, 2, 0, STR_PAD_LEFT );
    }
   
// write required JavaScript if neccesary
//
   
echo( "<input type=\"hidden\" name=\"{$params['name']}\" id=\"{$params['name']}\" value=\"\">\n" );

   
exposeIncludeJavascript( dirname( __FILE__ ) . '/plugin.js' );
   
// parse format string
//
   
$fmt = $params['fmt'];
   
$state = 0; // 0=literal, 1=parse
   
$buffer = '';
   
   
$fields = array(
   
// special char => array( name, values, selection )
       
'y' => array('y',$years,$d['year']),
       
'm' => array('m',$months,$d['mon']),
       
'd' => array('d',$days,$d['mday']),
       
'h' => array('h',$hours,$d['hours']),
       
'i' => array('i',$minutes,$d['minutes']),
       
's' => array('s',$seconds,$d['seconds']),
    );
   
$leftovers = array_keys($fields);
   
    for(
$i=0,$max=strlen($fmt); $i<$max; $i++ )
    {
        if(
0 == $state && $fmt[$i] == '%' ) {
       
// goto parse mode; special character expected next.
           
$state = 1;
            continue;
           
        } elseif(
0 == $state ) {
       
// literal mode.
           
$buffer .= $fmt[$i];
        } else {
       
// parse mode; expand special character.
           
if( $buffer ) {
                echo(
$buffer );
               
$buffer = '';
            }
           
           
$char = strtolower( $fmt[$i] );
            if( isset(
$fields[$char] ) ) {
               
$def = $fields[$char];
               
$expose->callPlugin( 'html_selectbox',
                    array(
                       
'name' => '',
                       
'options' => $def[1],
                       
'selected' => $def[2],
                       
'attr' => 'id="'.$params['name'].'_'.$def[0].'" '
                                     
. 'onchange="exposeHTMLDatePicker_onChange( \''.$params['name'].'\' )" '
                                     
. $params['attr']
                )) ;
                unset(
$leftovers[array_search($char,$leftovers)] );
            } elseif(
'%' == $char ) {
               
$buffer .= '%';
            }
   
           
$state = 0;
        }
    }
   
    if(
$buffer ) {
        echo(
$buffer );
    }
   
// write leftovers: date/time fields that are not visible
//
   
foreach( $leftovers as $char ) {
       
$def = $fields[$char];
        echo(
"<input type=\"hidden\" name=\"\" id=\"{$params['name']}_{$def[0]}\" value=\"{$def[2]}\">\n" );
    }
}
?>