| px | top | add code | search | signup | login | help |
<?php
/* timeselect.inc
Purpose: Class to build <select> lists for time and numbers
Author: Gary E Bickford (garyb@fxt.com, FXT Corporation (http://www.fxt.com)
Copyright: Copyright (C) 2000 by FXT Corporation. Subject to the GNU Public License (GPL) - may be used and modified at will, so long as license and all attribution is retained.
Disclaimer: Like most free software, no warranty is expressed or implied. If it doesn't work, feel free to fix it yourself. If you have an interesting fix or improvement, I'll be glad to include it - your name in lights!
Compatibility:
Tested on:
Solaris 7 (Apache 1.3.9, PHP3.0.12)
OpenBSD 2.6 (Stronghold 2.2/Apache1.2.5, PHP3.0.7)
<YourSystemHere>
Change Log: Created 2000-01-08 by GB (garyb@fxt.com)
2000-01-09 GB Cleaned up comments and added copyright for submission to PHP Code Exchange
Documentation: $selector=new TimeSelector; $selector->show_usage();
ToDo:
Some default values could be taken from the system's Locale.
PHP4 may include an autodocument function, in which case show_usage() may be obsolete.
PHP3 (my version, any way) doesn't seem to allow defines in a class, so I kluged the default system. I hate repetition, almost as much as I hate repeating myself.
Handling the default select in each function is a little bit of a kluge. Shades of FORTRAN.
Add option and rewrite to allow functions to return, rather than echo, the data.
Add option to use "Jan", "Feb", etc as the keys in the month selector. Maybe.
GMT and Locale settings for time displayed.
*/
// It's pretty complete, but it's beta until someone else has used it and sees what I missed.
class TimeSelector {
// Default constants
var $DefYrs = 3; // Default no. of years to show in the list
var $MaxDefYrs = 10; // Default no. of years to show in the list
var $DefMin = 5; // List every 5 minutes by default
var $DefSec = 5; // List every 5 seconds by default
var $DefNum = 5; // List every 5 numbers by default
var $DefYrHead = "-Yr-";
var $DefMonHead = "-Mo-";
var $DefDayHead = "-Dy-";
var $DefHrHead = "-Hr-";
var $DefMinHead = "-Mn-";
var $DefSecHead = "-Sc-";
var $DefNumHead = "-Nm-";
// I hate repetition - internal vars, should be set from default constants. Maybe a constructor...
var $years = 3;
var $minutes = 5;
var $seconds = 5;
var $numbers = 5;
var $yrhead = "-Yr-";
var $monhead = "-Mo-";
var $dayhead = "-Dy-";
var $hrhead = "-Hr-";
var $minhead = "-Mn-";
var $sechead = "-Sc-";
var $numhead = "-Nm-";
// These are too simple to have default constants
var $headers = TRUE; // Put little strings in a null option at the top
var $autoselect = TRUE; // Default behavior = today is selected, else nothing
var $debug = 0;
var $TimeSelectorVersion = "1.0b 2000-01-09";
var $TimeSelectorAuthor = "<a href=\"http://www.fxt.com/\">Gary E Bickford</A>, <a href=\"mailto:garyb@fxt.com\">garyb@fxt.com</a>";
function set_autoselect($state=TRUE) {
$this->autoselect=$state;
}
function set_headers($state=TRUE) {
$this->headers=$state;
}
function set_years($howmany=-1) {
if ($howmany < 0 || $howmany > $this->MaxDefYrs) { $this->years=$this->DefYrs; } else { $this->years=$howmany; }
}
function set_minutes($howmany=-1) {
if ($howmany > 30 || $howmany < 1) { $this->minutes=$this->DefMin;
} else {
$this->minutes=$howmany;
}
}
function set_seconds($howmany=-1) {
if ($howmany > 30 || $howmany < 1) { $this->seconds=$this->DefMin;
} else {
$this->seconds=$howmany;
}
}
function set_numbers($howmany=-1) {
if ($howmany > 30 || $howmany < 1) { $this->numbers=$this->DefMin;
} else {
$this->numbers=$howmany;
}
}
function set_yrhead ($str="") {
if ( ! $str) { $this->yrhead = $this->DefYrHead; } else { $this->yrhead= $str; }
}
function set_monhead ($str="") {
if (! $str) { $this->monhead=$this->DefMonHead; } else { $this->monhead= $str; }
}
function set_dayhead ($str="") {
if (!$str) { $this->dayhead=$this->DefDayHead; } else { $this->dayhead= $str; }
}
function set_hrhead ($str="") {
if (!$str) { $this->hrhead=$this->DefHrHead; } else { $this->hrhead= $str; }
}
function set_minhead ($str="") {
if (!$str) { $this->minhead=$this->DefMinHead; } else { $this->minhead= $str; }
}
function set_sechead ($str="") {
if (!$str) { $this->sechead=$this->DefSecHead; } else { $this->sechead= $str; }
}
function set_numhead ($str="") {
if (!$str) { $this->numhead=$this->DefNumHead; } else { $this->numhead= $str; }
}
function set_debug ($val=0) {
$this->debug=$val;
}
function year ($name="year", $year=-1) {
if ($this->autoselect && $year==-1) { $year=date("Y");}
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->yrhead\n"; }
for ( $y = date("Y"); $y < $this->years + date ("Y"); $y++ ) {
echo "<option ";
if ($year==$y ) { echo " SELECTED "; }
printf ("value=\"%04d\">%04d</option>\n",$y,$y);
}
echo "</select>";
}
function month ($name="month",$mo=-1) {
$month = array (
"1" => "Jan",
"2" => "Feb",
"3" => "Mar",
"4" => "Apr",
"5" => "Jun",
"6" => "Jun",
"7" => "Jul",
"8" => "Aug",
"9" => "Sep",
"10" => "Oct",
"11" => "Nov",
"12" => "Dec"
);
if ($this->autoselect && $mo==-1) { $mo=date("m");}
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->monhead\n"; }
while ( list($key,$value) = each ($month) ) {
echo "<option ";
if ($key == $mo) {
echo "selected ";
}
printf ("value=\"%02d\">%s\n",$key,$value);
}
echo "</select>";
}
function day ($name="day",$day=-1) {
if ($this->autoselect && $day==-1) { $day=date("d");}
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->dayhead\n"; }
for ($n=1; $n <= 31; $n++) {
echo "<option ";
if ($n==$day) { echo "selected "; }
printf ( ">%02d\n",$n);
}
echo "</select>";
}
function hour ($name="hour",$now=-1) {
if ($this->autoselect && $now==-1) { $now= date("H");}
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->hrhead\n"; }
for ($n=0; $n <= 23; $n++) {
echo "<option ";
if ($n==$now) { echo "selected "; }
printf (">%02d\n",$n);
}
echo "</select>";
}
function minute ($name="minute",$now=-1) {
if ($this->autoselect ) {
if ($now==-1) { $now= date("i");}
} elseif ($now==-1) { $now = -$this->minutes; }
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->minhead\n"; }
for ($n=0; $n <= 59; $n+=$this->minutes) {
echo "<option ";
if (($n-$now)>0 && ($n-$now) < $this->minutes ) { echo "selected "; }
printf (">%02d\n",$n);
}
echo "</select>";
if ($this->debug) { echo ("minute=$now");}
}
function second ($name="second",$now=-1) {
if ($this->autoselect ) {
if ($now==-1) { $now= date("s");}
} elseif ($now==-1) { $now = -$this->seconds; }
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->sechead\n"; }
for ($n=0; $n <= 59; $n+=$this->seconds) {
echo "<option ";
if ( ($n-$now)>0 && ($n-$now) < $this->seconds ) { echo "selected "; }
printf (">%02d\n",$n);
}
echo "</select>";
if ($this->debug) { echo ("second=$now");}
}
function number ($name="number",$selected=-1,$basenum=0, $count=10,$numbers=1) {
if ($this->autoselect ) {
if( $selected==-1) { $selected=$basenum;}
} elseif( $selected==-1) { $selected = $basenum - $count; }
$myselect = $selected - $basenum;
echo "<select name=\"$name\">\n";
if ($this->headers) { echo "<option value=\"NULL\">$this->numhead\n"; }
// We always count up from 0 to make life easy
for ($n=0; $n < $count; $n += abs($numbers) ) {
echo "<option ";
if ( ($n-$myselect > 0) && $n-$myselect< $this->numbers) { echo "selected "; }
printf (">%d\n",$n+$basenum);
}
echo "</select>";
if ($this->debug) { echo ("second=$selected");}
}
function version() {
return ($this->TimeSelectorVersion);
}
function author() {
return ($this->TimeSelectorAuthor);
}
function defaults () {
?>
<table><tr><td valign="top"> <table>
<tr><td><code>$headers</code></td><td><code>= <?php echo $this->headers ?> </code></td></tr>
<tr><td><code>$autoselect</code></td><td><code>= <?php echo $this->autoselect ?> </code></td></tr>
<tr><td><code>$DefYrs</code></td><td><code>= <?php echo $this->DefYrs ?> </code></td></tr>
<tr><td><code>$MaxDefYrs</code></td><td><code>= <?php echo $this->MaxDefYrs ?> </code></td></tr>
<tr><td><code>$DefMin</code></td><td><code>= <?php echo $this->DefMin ?> </code></td></tr>
<tr><td><code>$DefSec</code></td><td><code>= <?php echo $this->DefSec ?> </code></td></tr>
<tr><td><code>$DefNum</code></td><td><code>= <?php echo $this->DefNum ?> </code></td></tr>
</table></code></td><td valign="top"><table>
<tr><td><code>$DefYrHead </code></td><td><code>= "<?php echo $this->DefYrHead ?>"</code></td></tr>
<tr><td><code>$DefMonHead</code></td><td><code>= "<?php echo $this->DefMonHead ?>"</code></td></tr>
<tr><td><code>$DefDayHead</code></td><td><code>= "<?php echo $this->DefDayHead ?>"</code></td></tr>
<tr><td><code>$DefHrHead </code></td><td><code>= "<?php echo $this->DefHrHead ?>"</code></td></tr>
<tr><td><code>$DefMinHead</code></td><td><code>= "<?php echo $this->DefMinHead ?>"</code></td></tr>
<tr><td><code>$DefSecHead</code></td><td><code>= "<?php echo $this->DefSecHead ?>"</code></td></tr>
<tr><td><code>$DefNumHead</code></td><td><code>= "<?php echo $this->DefNumHead ?>"</code></td></tr>
</table> </td></tr> </table>
<?
}
function show_usage () {
?>
<hr>
<H1><i>TimeSelector Class Usage (version <?php echo $this->version(); ?>):</i></H1>
TimeSelector is a set of functions for generating <select> lists for date and time, plus one for integer lists as well. There is not one for arrays. The class is as complete as I can think of, including setting functions for all settable internal default values and functions to show you how to use the class [$this->show_usage()].
<H2>Talking to the Class:</h2>
<h3>Initialization:</H3>
<pre><pre><code> $selector=new TimeSelector; // Create an instance of the class (only do once)
</code></pre>
<h3>Making a selector:</h3>
<code> <ul>
<li>$select->year("MyYearVariableName"); // variable name is $MyYearVariableName
<li>$select->year(); // variable name is $year
<li>$select->number("MyNumber",$selected,$start,$count,$gapbetween);
<li>$select->year("date[]"); $select->month("date[]"); $select->day("date[]"); // A date array is returned
</ul>
</code>
<h3>Modifying behavior:</h3>
<pre><code> $select->set_headers(FALSE); // Don't show pretty headers at top of select list
$select->set_autoselect(FALSE); // If no select arg, nothing is selected.
$select->set_yrhead("-Select a Year-"); // start select list with "-Select a Year-"
$select->set_minutes(3); // list only every 3rd minute
</code></pre>
<ul>
<li>All arguments are optional in all functions.
<li>set_foo() functions without arguments will set the variable back to its permanent default.
</ul>
<h3>Default/ internal Values (as of right now):</h3>
<? $this->defaults();?>
<h3>Miscellaneous Facts:</h3>
<ul>
<li>In each select function [e.g., year("Foo")], the first argument is the desired variable name string.
<li>If no arguments are provided, the form variable in the select is the function name ("day", "month", etc.)
<li>The associated PHP variables will be $day, $month, etc.
<li>The second argument is the item to be 'selected' if any.
<li>If the non-date (header) options are selected by the user, the form will return the string "NULL" for that variable.
</ul>
<H2>Function definitions:</H2>
<h3>Useful Functions:</h3>
<pre><code> function year ($name="year", $year=now) // Selector for the year. Default start is now
function month ($name="month",$mo=now)
function day ($name="day",$day=now)
function hour ($name="hour",$now=now)
function minute ($name="minute",$now=now)
function second ($name="second",$now=now)
function number ($name="number",$selected=none,$basenum=0, $count=10,$numbers=1)
</code></pre>
<H3>Parametric functions</H3>
<pre><code> function set_autoselect ($state=TRUE) // If no selected argument, use now for selected
function set_headers ($state=TRUE) // Show cute string at top
function set_years ($howmany=<?php echo $this->years?>) // No. of years in the list
function set_minutes ($howmany=<?php echo $this->minutes?>) // Space between minutes in the list
function set_seconds ($howmany=<?php echo $this->seconds?>) // Space between seconds in the list
function set_numbers ($howmany=<?php echo $this->numbers?>) // Space between numbers in the list
</code></pre>
<h3>String parameter functions:</h3>
<pre><code> function set_yrhead ($str="<?php echo $this->yrhead?>") // Change the cute string for years (no arg => default)
function set_monhead ($str="<?php echo $this->monhead?>") // QED
function set_dayhead ($str="<?php echo $this->dayhead?>")
function set_hrhead ($str="<?php echo $this->hrhead?>")
function set_minhead ($str="<?php echo $this->minhead?>")
function set_sechead ($str="<?php echo $this->sechead?>")
function set_numhead ($str="<?php echo $this->numhead?>")
</code></pre>
<h3>Help Functions:</h3>
<pre><code> function show_usage (); // All the docs there are
function defaults (); // show present defaults
function version(); // QED
function author(); // That would be me :O) <?php echo $this->author();?>
</code></pre>
<hr>
<? } // show_usage()
} // class SelectTime
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.