Search:

CWIS Developers Documentation

  • Main Page
  • Classes
  • Files
  • File List
  • File Members

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                          $DisabledOptions = NULL)
00131 {
00132     if ((count($Items) > 0) || $PrintEvenIfEmpty)
00133     {
00134         # determine forced display width for option list (if needed)
00135         if ($Width)
00136         {
00137             # width given in px
00138             if (is_numeric($Width))
00139             {
00140                 $ForcedWidthAttrib = " style=\"width: ".$Width."px;\"";
00141             }
00142 
00143             # width given with type specifier (%, px, etc)
00144             else
00145             {
00146                 $ForcedWidthAttrib = " style=\"width: ".$Width.";\"";
00147             }
00148         }
00149         else
00150         {
00151             $Labels = $Items;
00152             sort($Labels);
00153             $MatchingCharThreshold = 11;
00154             $MaxMatchingChars = $MatchingCharThreshold;
00155             foreach ($Labels as $Label)
00156             {
00157                 if (isset($PreviousLabel))
00158                 {
00159                     $Len = ($MaxMatchingChars + 1);
00160                     while (substr($Label, 0, $Len) ==
00161                            substr($PreviousLabel, 0, $Len)
00162                            && ($Len < strlen($Label)))
00163                     {
00164                         $MaxMatchingChars = $Len;
00165                         $Len++;
00166                     }
00167                 }
00168                 $PreviousLabel = $Label;
00169             }
00170             if ($MaxMatchingChars > $MatchingCharThreshold)
00171             {
00172                 $ExtraCharsToDisplayBeyondMatch = 6;
00173                 $ForcedWidth = $MaxMatchingChars + $ExtraCharsToDisplayBeyondMatch;;
00174                 $ForcedWidthAttrib = " style=\"width: ".$ForcedWidth."ex;\"";
00175             }
00176             else
00177             {
00178                 $ForcedWidthAttrib = " style=\"width: auto;\"";
00179             }
00180         }
00181 
00182         # print option list begin
00183         print("<select name=\"".$ResultVar."\""
00184               ." size=\"".$Size."\""
00185               ." id=\"".$ResultVar."\""
00186               .($SubmitOnChange ? " onChange=\"submit()\"" : "")
00187               .($OnChangeAction ? " onChange=\"".$OnChangeAction."\"" : "")
00188               .($MultipleAllowed ? " multiple" : "")
00189               .$ForcedWidthAttrib
00190               .">\n");
00191 
00192         # for each element in list
00193         reset($Items);
00194         while (list($Value, $Label) = each($Items))
00195         {
00196             # print option list item
00197             printf("    <option value=\"%s\"", htmlspecialchars($Value));
00198             if ((is_array($SelectedValue) && in_array($Value, $SelectedValue))
00199                     || ($Value == $SelectedValue)) {  printf(" selected");  }
00200             if ( !is_null($DisabledOptions)
00201                  && isset($DisabledOptions[$Label]))
00202             {
00203                 printf(" disabled");
00204             }
00205             printf(">%s</option>\n", GetHtmlEscapedString($Label));
00206         }
00207 
00208         # print option list end
00209         printf("</select>\n");
00210     }
00211 }
00212 
00213 function PrintOptionListsOfDateComponents($FieldPrefix, $AllowNullDate = 0, $Year = -1, $Month = -1, $Day = -1, $SubmitOnChange = "")
00214 {
00215     # if no date passed in
00216     if (($Year == -1) && ($AllowNullDate))
00217     {
00218         # if null date allowed
00219         if ($AllowNullDate)
00220         {
00221             # use null date
00222             $Year = 0;
00223             $Month = 0;
00224             $Day = 0;
00225         }
00226         else
00227         {
00228             # use current date
00229             $Year = date("Y");
00230             $Month = date("n");
00231             $Day = date("j");
00232         }
00233     }
00234 
00235     # if date string passed in
00236     if ((strlen($Year) > 4) && ($Month == -1))
00237     {
00238         # split into component parts
00239         list($Year, $Month, $Day) = split("[-/]", $Year);
00240     }
00241 
00242     # print option list for months if month value supplied
00243     if ($Month != -1)
00244     {
00245         $Index = 1;
00246         print("\n    <select name=\"".$FieldPrefix."Month\""
00247               ." id=\"".$FieldPrefix."Month\""
00248               .($SubmitOnChange ? " onChange='submit()'" : "")
00249               .">\n");
00250         if ($AllowNullDate)
00251         {
00252             print("<option value='0'>--</option>\n");
00253         }
00254         while ($Index <= 12)
00255         {
00256             if ($Index == $Month)
00257             {
00258                 printf("        <option value='%s' selected>%s</option>\n", $Index, GetMonthName($Index));
00259             }
00260             else
00261             {
00262                 printf("        <option value='%s'>%s</option>\n", $Index, GetMonthName($Index));
00263             }
00264             $Index++;
00265         }
00266         printf("    </select>\n");
00267     }
00268 
00269     # print option list for days if day value supplied
00270     if ($Day != -1)
00271     {
00272         $Index = 1;
00273         print("\n    <select name=\"".$FieldPrefix."Day\""
00274               ." id=\"".$FieldPrefix."Day\""
00275               .($SubmitOnChange ? " onChange='submit()'" : "")
00276               .">\n");
00277         if ($AllowNullDate)
00278         {
00279             print("<option value='0'>--</option>\n");
00280         }
00281         while ($Index <= 31)
00282         {
00283             if ($Index == $Day)
00284             {
00285                 printf("        <option value='%s' selected>%s</option>\n", $Index, GetOrdinalNumber($Index));
00286             }
00287             else
00288             {
00289                 printf("        <option value='%s'>%s</option>\n", $Index, GetOrdinalNumber($Index));
00290             }
00291             $Index++;
00292         }
00293         printf("    </select>\n");
00294     }
00295 
00296     # print option list for years
00297     $Index = date("Y") - 45;
00298     $EndIndex = $Index + 45;
00299     print("\n    <select name=\"".$FieldPrefix."Year\""
00300           ." id=\"".$FieldPrefix."Year\""
00301           .($SubmitOnChange ? " onChange='submit()'" : "")
00302           .">\n");
00303     if ($AllowNullDate)
00304     {
00305         print("<option value='0'>--</option>\n");
00306     }
00307     while ($Index <= $EndIndex)
00308     {
00309         if ($Index == $Year)
00310         {
00311             printf("        <option value=\"%s\" selected>%s</option>\n", $Index, $Index);
00312         }
00313         else
00314         {
00315             printf("        <option value=\"%s\">%s</option>\n", $Index, $Index);
00316         }
00317         $Index++;
00318     }
00319     printf("    </select>\n");
00320 }
00321 
00322 function PrintOptionListsOfTimeComponents($FieldPrefix, $Hour = -1, $Minute = -1)
00323 {
00324     # use current date if no date passed in
00325     if ($Hour == -1)
00326     {
00327         $Hour = date("G");
00328         $Minute = date("i");
00329     }
00330 
00331     # print option list for hours if hour value supplied
00332     $Index = 0;
00333     print("\n    <select name=\"".$FieldPrefix."Hour\" id=\"".$FieldPrefix."Hour\">\n");
00334     while ($Index < 24)
00335     {
00336         if ($Index == $Hour)
00337         {
00338             printf("        <option value='%s' selected>%d</option>\n", $Index, $Index);
00339         }
00340         else
00341         {
00342             printf("        <option value='%s'>%d</option>\n", $Index, $Index);
00343         }
00344         $Index++;
00345     }
00346     printf("    </select>\n");
00347 
00348     # print option list for minutes if minute value supplied
00349     if ($Minute != -1)
00350     {
00351         $Index = 0;
00352         print("\n    <select name=\"".$FieldPrefix."Minute\" id=\"".$FieldPrefix."Minute\">\n");
00353         while ($Index < 60)
00354         {
00355             if ($Index == $Minute)
00356             {
00357                 printf("        <option value='%s' selected>%02d</option>\n", $Index, $Index);
00358             }
00359             else
00360             {
00361                 printf("        <option value='%s'>%02d</option>\n", $Index, $Index);
00362             }
00363             $Index++;
00364         }
00365         printf("    </select>\n");
00366     }
00367 }
00368 
00369 function LongestStringLineLength($String)
00370 {
00371     # split string on newlines
00372     $Pieces = explode("\n", $String);
00373 
00374     # for each line in string
00375     $MaxLen = 0;
00376     for ($Index = 0;  $Index < count($Pieces);  $Index++)
00377     {
00378         # save length if longer than current max
00379         if (strlen($Pieces[$Index]) > $MaxLen)
00380         {
00381             $MaxLen = strlen($Pieces[$Index]);
00382         }
00383     }
00384 
00385     # return length of longest segment to caller
00386     return $MaxLen;
00387 }
00388 
00389 function PrintOptionListFromDB($DB, $Table, $Condition, $SortBy, $ResultVar, $ValueQuery, $LabelQuery, $SelectedValue, $Size = 1, $SubmitOnChange = "", $PrintEvenIfEmpty = 0)
00390 {
00391     # set up condition and sorting parameters
00392     if ($Condition != "") {  $Condition = "WHERE ".$Condition;  }
00393     if ($SortBy != "") {  $SortBy = "ORDER BY ".$SortBy;  }
00394 
00395     # grab records to be listed from database
00396     $QueryString = sprintf("SELECT * FROM %s %s %s",
00397             $Table, $Condition, $SortBy);
00398     $DB->Query($QueryString);
00399 
00400     # if records were found
00401     if ($DB->NumRowsSelected() > 0)
00402     {
00403         # build array of items
00404         while ($Row = $DB->FetchRow())
00405         {
00406             $Items[$Row[$ValueQuery]] = $Row[$LabelQuery];
00407         }
00408 
00409         # sort array if not already sorted
00410         if ($SortBy == "") {  asort($Items);  }
00411     }
00412 
00413     # print option list
00414     PrintOptionList($ResultVar, $Items, $SelectedValue, $SubmitOnChange, $Size, $PrintEvenIfEmpty);
00415 }
00416 
00417 
00418 ?>

CWIS logo doxygen
Copyright 2010 Internet Scout