Axis--StandardLibrary.php

Go to the documentation of this file.
00001 <?PHP
00002 
00003 #
00004 #   Axis--StandardLibrary.php
00005 #   A Collection of Utility Routines
00006 #
00007 #   Copyright 1999-2002 Axis Data
00008 #   This code is free software that can be used or redistributed under the
00009 #   terms of Version 2 of the GNU General Public License, as published by the
00010 #   Free Software Foundation (http://www.fsf.org).
00011 #
00012 #   Author:  Edward Almasy (almasy@axisdata.com)
00013 #
00014 #   Part of the AxisPHP library v1.2.4
00015 #   For more information see http://www.axisdata.com/AxisPHP/
00016 #
00017 
00018 
00019 # (accepts a date string in the form YYYY-MM-DD and adds or subtracts days)
00020 function CalcDate($DateString, $DaysToAdd)
00021 {
00022     # parse date string
00023     $Pieces = explode("-", $DateString);
00024 
00025     # convert to value of date in seconds (a la Unix timestamp)
00026     $DateInSeconds = mktime(1, 1, 1, $Pieces[1], $Pieces[2], $Pieces[0]);
00027 
00028     # perform date arithmetic
00029     $DateInSeconds = $DateInSeconds + ($DaysToAdd * (60 * 60 * 24));
00030 
00031     # return YYYY-MM-DD date string to caller
00032     return date("Y-m-d", $DateInSeconds);
00033 }
00034 
00035 function GetHtmlEscapedString($String)
00036 {
00037     return htmlentities(stripslashes($String));
00038 }
00039 
00040 function GetUnHtmlEscapedString($String)
00041 {
00042     $TranslationTable = get_html_translation_table(HTML_ENTITIES);
00043     $TranslationTable = array_flip($TranslationTable);
00044     return strtr($String, $TranslationTable);
00045 }
00046 
00047 function HtmlSafePrint($String)
00048 {
00049     print(GetHtmlEscapedString($String));
00050 }
00051 
00052 function PrintAutoRefreshPage($Title, $NewUrl)
00053 {
00054     </script>
00055 
00056         <html>
00057         <head>
00058             <title><?php  printf("%s", $Title);  ?></title>
00059             <meta http-equiv="refresh" content="0; URL=<?php  print($NewUrl);  ?>">
00060         </head>
00061         <body bgcolor="white">
00062         </body>
00063         </html>
00064 
00065     <script language="php">
00066 }
00067 
00068 function GetOrdinalSuffix($Number)
00069 {
00070     if (($Number > 10) && ($Number < 20))
00071     {
00072         $Suffix = "th";
00073     }
00074     else
00075     {
00076         $Digit = $Number % 10;
00077         if ($Digit == 1)
00078         {
00079             $Suffix = "st";
00080         }
00081         elseif ($Digit == 2)
00082         {
00083             $Suffix = "nd";
00084         }
00085         elseif ($Digit == 3)
00086         {
00087             $Suffix = "rd";
00088         }
00089         else
00090         {
00091             $Suffix = "th";
00092         }
00093     }
00094 
00095     return $Suffix;
00096 }
00097 
00098 function GetOrdinalNumber($Number)
00099 {
00100     return $Number.GetOrdinalSuffix($Number);
00101 }
00102 
00103 function GetMonthName($MonthNumber)
00104 {
00105     $MonthNames = array(
00106             1 => "January",
00107             2 => "February",
00108             3 => "March",
00109             4 => "April",
00110             5 => "May",
00111             6 => "June",
00112             7 => "July",
00113             8 => "August",
00114             9 => "September",
00115             10 => "October",
00116             11 => "November",
00117             12 => "December");
00118 
00119     return $MonthNames[$MonthNumber];
00120 }
00121 
00122 function PrintOptionList($ResultVar, $Items,
00123                          $SelectedValue = NULL,
00124                          $SubmitOnChange = "",
00125                          $Size = 1,
00126                          $PrintEvenIfEmpty = 1,
00127                          $MultipleAllowed = false,
00128                          $OnChangeAction = NULL,
00129                          $Width = NULL)
00130 {
00131     if ((count($Items) > 0) || $PrintEvenIfEmpty)
00132     {
00133         # determine forced display width for option list (if needed)
00134         if ($Width)
00135         {
00136             # width given in px
00137             if (is_numeric($Width))
00138             {
00139                 $ForcedWidthAttrib = " style=\"width: ".$Width."px;\"";
00140             }
00141 
00142             # width given with type specifier (%, px, etc)
00143             else
00144             {
00145                 $ForcedWidthAttrib = " style=\"width: ".$Width.";\"";
00146             }
00147         }
00148         else
00149         {
00150             $Labels = $Items;
00151             sort($Labels);
00152             $MatchingCharThreshold = 11;
00153             $MaxMatchingChars = $MatchingCharThreshold;
00154             foreach ($Labels as $Label)
00155             {
00156                 if (isset($PreviousLabel))
00157                 {
00158                     $Len = ($MaxMatchingChars + 1);
00159                     while (substr($Label, 0, $Len) ==
00160                            substr($PreviousLabel, 0, $Len)
00161                            && ($Len < strlen($Label)))
00162                     {
00163                         $MaxMatchingChars = $Len;
00164                         $Len++;
00165                     }
00166                 }
00167                 $PreviousLabel = $Label;
00168             }
00169             if ($MaxMatchingChars > $MatchingCharThreshold)
00170             {
00171                 $ExtraCharsToDisplayBeyondMatch = 6;
00172                 $ForcedWidth = $MaxMatchingChars + $ExtraCharsToDisplayBeyondMatch;;
00173                 $ForcedWidthAttrib = " style=\"width: ".$ForcedWidth."ex;\"";
00174             }
00175             else
00176             {
00177                 $ForcedWidthAttrib = " style=\"width: auto;\"";
00178             }
00179         }
00180 
00181         # print option list begin
00182         print("<select name=\"".$ResultVar."\""
00183               ." size=\"".$Size."\""
00184               ." id=\"".$ResultVar."\""
00185               .($SubmitOnChange ? " onChange=\"submit()\"" : "")
00186               .($OnChangeAction ? " onChange=\"".$OnChangeAction."\"" : "")
00187               .($MultipleAllowed ? " multiple" : "")
00188               .$ForcedWidthAttrib
00189               .">\n");
00190 
00191         # for each element in list
00192         reset($Items);
00193         while (list($Value, $Label) = each($Items))
00194         {
00195             # print option list item
00196             printf("    <option value=\"%s\"", htmlspecialchars($Value));
00197             if ((is_array($SelectedValue) && in_array($Value, $SelectedValue))
00198                     || ($Value == $SelectedValue)) {  printf(" selected");  }
00199             printf(">%s</option>\n", GetHtmlEscapedString($Label));
00200         }
00201 
00202         # print option list end
00203         printf("</select>\n");
00204     }
00205 }
00206 
00207 function PrintOptionListsOfDateComponents($FieldPrefix, $AllowNullDate = 0, $Year = -1, $Month = -1, $Day = -1, $SubmitOnChange = "")
00208 {
00209     # if no date passed in
00210     if (($Year == -1) && ($AllowNullDate))
00211     {
00212         # if null date allowed
00213         if ($AllowNullDate)
00214         {
00215             # use null date
00216             $Year = 0;
00217             $Month = 0;
00218             $Day = 0;
00219         }
00220         else
00221         {
00222             # use current date
00223             $Year = date("Y");
00224             $Month = date("n");
00225             $Day = date("j");
00226         }
00227     }
00228 
00229     # if date string passed in
00230     if ((strlen($Year) > 4) && ($Month == -1))
00231     {
00232         # split into component parts
00233         list($Year, $Month, $Day) = split("[-/]", $Year);
00234     }
00235 
00236     # print option list for months if month value supplied
00237     if ($Month != -1)
00238     {
00239         $Index = 1;
00240         print("\n    <select name=\"".$FieldPrefix."Month\""
00241               ." id=\"".$FieldPrefix."Month\""
00242               .($SubmitOnChange ? " onChange='submit()'" : "")
00243               .">\n");
00244         if ($AllowNullDate)
00245         {
00246             print("<option value='0'>--</option>\n");
00247         }
00248         while ($Index <= 12)
00249         {
00250             if ($Index == $Month)
00251             {
00252                 printf("        <option value='%s' selected>%s</option>\n", $Index, GetMonthName($Index));
00253             }
00254             else
00255             {
00256                 printf("        <option value='%s'>%s</option>\n", $Index, GetMonthName($Index));
00257             }
00258             $Index++;
00259         }
00260         printf("    </select>\n");
00261     }
00262 
00263     # print option list for days if day value supplied
00264     if ($Day != -1)
00265     {
00266         $Index = 1;
00267         print("\n    <select name=\"".$FieldPrefix."Day\""
00268               ." id=\"".$FieldPrefix."Day\""
00269               .($SubmitOnChange ? " onChange='submit()'" : "")
00270               .">\n");
00271         if ($AllowNullDate)
00272         {
00273             print("<option value='0'>--</option>\n");
00274         }
00275         while ($Index <= 31)
00276         {
00277             if ($Index == $Day)
00278             {
00279                 printf("        <option value='%s' selected>%s</option>\n", $Index, GetOrdinalNumber($Index));
00280             }
00281             else
00282             {
00283                 printf("        <option value='%s'>%s</option>\n", $Index, GetOrdinalNumber($Index));
00284             }
00285             $Index++;
00286         }
00287         printf("    </select>\n");
00288     }
00289 
00290     # print option list for years
00291     $Index = date("Y") - 45;
00292     $EndIndex = $Index + 45;
00293     print("\n    <select name=\"".$FieldPrefix."Year\""
00294           ." id=\"".$FieldPrefix."Year\""
00295           .($SubmitOnChange ? " onChange='submit()'" : "")
00296           .">\n");
00297     if ($AllowNullDate)
00298     {
00299         print("<option value='0'>--</option>\n");
00300     }
00301     while ($Index <= $EndIndex)
00302     {
00303         if ($Index == $Year)
00304         {
00305             printf("        <option value=\"%s\" selected>%s</option>\n", $Index, $Index);
00306         }
00307         else
00308         {
00309             printf("        <option value=\"%s\">%s</option>\n", $Index, $Index);
00310         }
00311         $Index++;
00312     }
00313     printf("    </select>\n");
00314 }
00315 
00316 function PrintOptionListsOfTimeComponents($FieldPrefix, $Hour = -1, $Minute = -1)
00317 {
00318     # use current date if no date passed in
00319     if ($Hour == -1)
00320     {
00321         $Hour = date("G");
00322         $Minute = date("i");
00323     }
00324 
00325     # print option list for hours if hour value supplied
00326     $Index = 0;
00327     print("\n    <select name=\"".$FieldPrefix."Hour\" id=\"".$FieldPrefix."Hour\">\n");
00328     while ($Index < 24)
00329     {
00330         if ($Index == $Hour)
00331         {
00332             printf("        <option value='%s' selected>%d</option>\n", $Index, $Index);
00333         }
00334         else
00335         {
00336             printf("        <option value='%s'>%d</option>\n", $Index, $Index);
00337         }
00338         $Index++;
00339     }
00340     printf("    </select>\n");
00341 
00342     # print option list for minutes if minute value supplied
00343     if ($Minute != -1)
00344     {
00345         $Index = 0;
00346         print("\n    <select name=\"".$FieldPrefix."Minute\" id=\"".$FieldPrefix."Minute\">\n");
00347         while ($Index < 60)
00348         {
00349             if ($Index == $Minute)
00350             {
00351                 printf("        <option value='%s' selected>%02d</option>\n", $Index, $Index);
00352             }
00353             else
00354             {
00355                 printf("        <option value='%s'>%02d</option>\n", $Index, $Index);
00356             }
00357             $Index++;
00358         }
00359         printf("    </select>\n");
00360     }
00361 }
00362 
00363 function LongestStringLineLength($String)
00364 {
00365     # split string on newlines
00366     $Pieces = explode("\n", $String);
00367 
00368     # for each line in string
00369     $MaxLen = 0;
00370     for ($Index = 0;  $Index < count($Pieces);  $Index++)
00371     {
00372         # save length if longer than current max
00373         if (strlen($Pieces[$Index]) > $MaxLen)
00374         {
00375             $MaxLen = strlen($Pieces[$Index]);
00376         }
00377     }
00378 
00379     # return length of longest segment to caller
00380     return $MaxLen;
00381 }
00382 
00383 function PrintOptionListFromDB($DB, $Table, $Condition, $SortBy, $ResultVar, $ValueQuery, $LabelQuery, $SelectedValue, $Size = 1, $SubmitOnChange = "", $PrintEvenIfEmpty = 0)
00384 {
00385     # set up condition and sorting parameters
00386     if ($Condition != "") {  $Condition = "WHERE ".$Condition;  }
00387     if ($SortBy != "") {  $SortBy = "ORDER BY ".$SortBy;  }
00388 
00389     # grab records to be listed from database
00390     $QueryString = sprintf("SELECT * FROM %s %s %s",
00391             $Table, $Condition, $SortBy);
00392     $DB->Query($QueryString);
00393 
00394     # if records were found
00395     if ($DB->NumRowsSelected() > 0)
00396     {
00397         # build array of items
00398         while ($Row = $DB->FetchRow())
00399         {
00400             $Items[$Row[$ValueQuery]] = $Row[$LabelQuery];
00401         }
00402 
00403         # sort array if not already sorted
00404         if ($SortBy == "") {  asort($Items);  }
00405     }
00406 
00407     # print option list
00408     PrintOptionList($ResultVar, $Items, $SelectedValue, $SubmitOnChange, $Size, $PrintEvenIfEmpty);
00409 }
00410 
00411 
00412 ?>