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