CWIS Developer Documentation
Axis--StandardLibrary.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # Axis--StandardLibrary.php
5 # A Collection of Utility Routines
6 #
7 # Copyright 1999-2002 Axis Data
8 # This code is free software that can be used or redistributed under the
9 # terms of Version 2 of the GNU General Public License, as published by the
10 # Free Software Foundation (http://www.fsf.org).
11 #
12 # Author: Edward Almasy (almasy@axisdata.com)
13 #
14 # Part of the AxisPHP library v1.2.4
15 # For more information see http://www.axisdata.com/AxisPHP/
16 #
17 
18 
19 # (accepts a date string in the form YYYY-MM-DD and adds or subtracts days)
20 function CalcDate($DateString, $DaysToAdd)
21 {
22  # parse date string
23  $Pieces = explode("-", $DateString);
24 
25  # convert to value of date in seconds (a la Unix timestamp)
26  $DateInSeconds = mktime(1, 1, 1, $Pieces[1], $Pieces[2], $Pieces[0]);
27 
28  # perform date arithmetic
29  $DateInSeconds = $DateInSeconds + ($DaysToAdd * (60 * 60 * 24));
30 
31  # return YYYY-MM-DD date string to caller
32  return date("Y-m-d", $DateInSeconds);
33 }
34 
35 function GetHtmlEscapedString($String)
36 {
37  return htmlentities($String);
38 }
39 
40 function GetUnHtmlEscapedString($String)
41 {
42  $TranslationTable = get_html_translation_table(HTML_ENTITIES);
43  $TranslationTable = array_flip($TranslationTable);
44  return strtr($String, $TranslationTable);
45 }
46 
47 function HtmlSafePrint($String)
48 {
49  print(GetHtmlEscapedString($String));
50 }
51 
52 function PrintAutoRefreshPage($Title, $NewUrl)
53 {
54  ?>
55  <html>
56  <head>
57  <title><?php printf("%s", $Title); ?></title>
58  <meta http-equiv="refresh" content="0; URL=<?php print($NewUrl); ?>">
59  </head>
60  <body bgcolor="white">
61  </body>
62  </html>
63  <?PHP
64 }
65 
66 function GetOrdinalSuffix($Number)
67 {
68  if (($Number > 10) && ($Number < 20))
69  {
70  $Suffix = "th";
71  }
72  else
73  {
74  $Digit = $Number % 10;
75  if ($Digit == 1)
76  {
77  $Suffix = "st";
78  }
79  elseif ($Digit == 2)
80  {
81  $Suffix = "nd";
82  }
83  elseif ($Digit == 3)
84  {
85  $Suffix = "rd";
86  }
87  else
88  {
89  $Suffix = "th";
90  }
91  }
92 
93  return $Suffix;
94 }
95 
96 function GetOrdinalNumber($Number)
97 {
98  return $Number.GetOrdinalSuffix($Number);
99 }
100 
101 function GetMonthName($MonthNumber)
102 {
103  $MonthNames = array(
104  1 => "January",
105  2 => "February",
106  3 => "March",
107  4 => "April",
108  5 => "May",
109  6 => "June",
110  7 => "July",
111  8 => "August",
112  9 => "September",
113  10 => "October",
114  11 => "November",
115  12 => "December");
116 
117  return $MonthNames[$MonthNumber];
118 }
119 
120 function PrintOptionList($ResultVar, $Items,
121  $SelectedValue = NULL,
122  $SubmitOnChange = "",
123  $Size = 1,
124  $PrintEvenIfEmpty = 1,
125  $MultipleAllowed = false,
126  $OnChangeAction = NULL,
127  $Width = NULL,
128  $DisabledOptions = NULL)
129 {
130  if ((count($Items) > 0) || $PrintEvenIfEmpty)
131  {
132  # determine forced display width for option list (if needed)
133  if ($Width)
134  {
135  # width given in px
136  if (is_numeric($Width))
137  {
138  $ForcedWidthAttrib = " style=\"width: ".$Width."px;\"";
139  }
140 
141  # width given with type specifier (%, px, etc)
142  else
143  {
144  $ForcedWidthAttrib = " style=\"width: ".$Width.";\"";
145  }
146  }
147  else
148  {
149  $Labels = $Items;
150  sort($Labels);
151  $MatchingCharThreshold = 11;
152  $MaxMatchingChars = $MatchingCharThreshold;
153  foreach ($Labels as $Label)
154  {
155  if (isset($PreviousLabel))
156  {
157  $Len = ($MaxMatchingChars + 1);
158  while (substr($Label, 0, $Len) ==
159  substr($PreviousLabel, 0, $Len)
160  && ($Len < strlen($Label)))
161  {
162  $MaxMatchingChars = $Len;
163  $Len++;
164  }
165  }
166  $PreviousLabel = $Label;
167  }
168  if ($MaxMatchingChars > $MatchingCharThreshold)
169  {
170  $ExtraCharsToDisplayBeyondMatch = 6;
171  $ForcedWidth = $MaxMatchingChars + $ExtraCharsToDisplayBeyondMatch;;
172  $ForcedWidthAttrib = " style=\"width: ".$ForcedWidth."ex;\"";
173  }
174  else
175  {
176  $ForcedWidthAttrib = " style=\"width: auto;\"";
177  }
178  }
179 
180  # print option list begin
181  print("<select name=\"".$ResultVar."\""
182  ." size=\"".$Size."\""
183  ." id=\"".$ResultVar."\""
184  .($SubmitOnChange ? " onChange=\"submit()\"" : "")
185  .($OnChangeAction ? " onChange=\"".$OnChangeAction."\"" : "")
186  .($MultipleAllowed ? " multiple" : "")
187  .$ForcedWidthAttrib
188  .">\n");
189 
190  # for each element in list
191  reset($Items);
192  while (list($Value, $Label) = each($Items))
193  {
194  # print option list item
195  printf(" <option value=\"%s\"", htmlspecialchars($Value));
196  if ((is_array($SelectedValue) && in_array($Value, $SelectedValue))
197  || ($Value == $SelectedValue)) { printf(" selected"); }
198  if ( !is_null($DisabledOptions)
199  && isset($DisabledOptions[$Label]))
200  {
201  printf(" disabled");
202  }
203  printf(">%s</option>\n", GetHtmlEscapedString($Label));
204  }
205 
206  # print option list end
207  printf("</select>\n");
208  }
209 }
210 
211 function PrintOptionListsOfDateComponents($FieldPrefix, $AllowNullDate = 0, $Year = -1, $Month = -1, $Day = -1, $SubmitOnChange = "")
212 {
213  # if no date passed in
214  if (($Year == -1) && ($AllowNullDate))
215  {
216  # if null date allowed
217  if ($AllowNullDate)
218  {
219  # use null date
220  $Year = 0;
221  $Month = 0;
222  $Day = 0;
223  }
224  else
225  {
226  # use current date
227  $Year = date("Y");
228  $Month = date("n");
229  $Day = date("j");
230  }
231  }
232 
233  # if date string passed in
234  if ((strlen($Year) > 4) && ($Month == -1))
235  {
236  # split into component parts
237  list($Year, $Month, $Day) = split("[-/]", $Year);
238  }
239 
240  # print option list for months if month value supplied
241  if ($Month != -1)
242  {
243  $Index = 1;
244  print("\n <select name=\"".$FieldPrefix."Month\""
245  ." id=\"".$FieldPrefix."Month\""
246  .($SubmitOnChange ? " onChange='submit()'" : "")
247  .">\n");
248  if ($AllowNullDate)
249  {
250  print("<option value='0'>--</option>\n");
251  }
252  while ($Index <= 12)
253  {
254  if ($Index == $Month)
255  {
256  printf(" <option value='%s' selected>%s</option>\n", $Index, GetMonthName($Index));
257  }
258  else
259  {
260  printf(" <option value='%s'>%s</option>\n", $Index, GetMonthName($Index));
261  }
262  $Index++;
263  }
264  printf(" </select>\n");
265  }
266 
267  # print option list for days if day value supplied
268  if ($Day != -1)
269  {
270  $Index = 1;
271  print("\n <select name=\"".$FieldPrefix."Day\""
272  ." id=\"".$FieldPrefix."Day\""
273  .($SubmitOnChange ? " onChange='submit()'" : "")
274  .">\n");
275  if ($AllowNullDate)
276  {
277  print("<option value='0'>--</option>\n");
278  }
279  while ($Index <= 31)
280  {
281  if ($Index == $Day)
282  {
283  printf(" <option value='%s' selected>%s</option>\n", $Index, GetOrdinalNumber($Index));
284  }
285  else
286  {
287  printf(" <option value='%s'>%s</option>\n", $Index, GetOrdinalNumber($Index));
288  }
289  $Index++;
290  }
291  printf(" </select>\n");
292  }
293 
294  # print option list for years
295  $Index = date("Y") - 45;
296  $EndIndex = $Index + 45;
297  print("\n <select name=\"".$FieldPrefix."Year\""
298  ." id=\"".$FieldPrefix."Year\""
299  .($SubmitOnChange ? " onChange='submit()'" : "")
300  .">\n");
301  if ($AllowNullDate)
302  {
303  print("<option value='0'>--</option>\n");
304  }
305  while ($Index <= $EndIndex)
306  {
307  if ($Index == $Year)
308  {
309  printf(" <option value=\"%s\" selected>%s</option>\n", $Index, $Index);
310  }
311  else
312  {
313  printf(" <option value=\"%s\">%s</option>\n", $Index, $Index);
314  }
315  $Index++;
316  }
317  printf(" </select>\n");
318 }
319 
320 function PrintOptionListsOfTimeComponents($FieldPrefix, $Hour = -1, $Minute = -1)
321 {
322  # use current date if no date passed in
323  if ($Hour == -1)
324  {
325  $Hour = date("G");
326  $Minute = date("i");
327  }
328 
329  # print option list for hours if hour value supplied
330  $Index = 0;
331  print("\n <select name=\"".$FieldPrefix."Hour\" id=\"".$FieldPrefix."Hour\">\n");
332  while ($Index < 24)
333  {
334  if ($Index == $Hour)
335  {
336  printf(" <option value='%s' selected>%d</option>\n", $Index, $Index);
337  }
338  else
339  {
340  printf(" <option value='%s'>%d</option>\n", $Index, $Index);
341  }
342  $Index++;
343  }
344  printf(" </select>\n");
345 
346  # print option list for minutes if minute value supplied
347  if ($Minute != -1)
348  {
349  $Index = 0;
350  print("\n <select name=\"".$FieldPrefix."Minute\" id=\"".$FieldPrefix."Minute\">\n");
351  while ($Index < 60)
352  {
353  if ($Index == $Minute)
354  {
355  printf(" <option value='%s' selected>%02d</option>\n", $Index, $Index);
356  }
357  else
358  {
359  printf(" <option value='%s'>%02d</option>\n", $Index, $Index);
360  }
361  $Index++;
362  }
363  printf(" </select>\n");
364  }
365 }
366 
367 function LongestStringLineLength($String)
368 {
369  # split string on newlines
370  $Pieces = explode("\n", $String);
371 
372  # for each line in string
373  $MaxLen = 0;
374  for ($Index = 0; $Index < count($Pieces); $Index++)
375  {
376  # save length if longer than current max
377  if (strlen($Pieces[$Index]) > $MaxLen)
378  {
379  $MaxLen = strlen($Pieces[$Index]);
380  }
381  }
382 
383  # return length of longest segment to caller
384  return $MaxLen;
385 }
386 
387 function PrintOptionListFromDB($DB, $Table, $Condition, $SortBy, $ResultVar, $ValueQuery, $LabelQuery, $SelectedValue, $Size = 1, $SubmitOnChange = "", $PrintEvenIfEmpty = 0)
388 {
389  # set up condition and sorting parameters
390  if ($Condition != "") { $Condition = "WHERE ".$Condition; }
391  if ($SortBy != "") { $SortBy = "ORDER BY ".$SortBy; }
392 
393  # grab records to be listed from database
394  $QueryString = sprintf("SELECT * FROM %s %s %s",
395  $Table, $Condition, $SortBy);
396  $DB->Query($QueryString);
397 
398  # if records were found
399  if ($DB->NumRowsSelected() > 0)
400  {
401  # build array of items
402  while ($Row = $DB->FetchRow())
403  {
404  $Items[$Row[$ValueQuery]] = $Row[$LabelQuery];
405  }
406 
407  # sort array if not already sorted
408  if ($SortBy == "") { asort($Items); }
409  }
410 
411  # print option list
412  PrintOptionList($ResultVar, $Items, $SelectedValue, $SubmitOnChange, $Size, $PrintEvenIfEmpty);
413 }
414 
415 
416 ?>
LongestStringLineLength($String)
PrintOptionListsOfTimeComponents($FieldPrefix, $Hour=-1, $Minute=-1)
GetMonthName($MonthNumber)
PrintOptionListFromDB($DB, $Table, $Condition, $SortBy, $ResultVar, $ValueQuery, $LabelQuery, $SelectedValue, $Size=1, $SubmitOnChange="", $PrintEvenIfEmpty=0)
GetHtmlEscapedString($String)
GetUnHtmlEscapedString($String)
PHP CalcDate($DateString, $DaysToAdd)
GetOrdinalSuffix($Number)
HtmlSafePrint($String)
GetOrdinalNumber($Number)
PrintOptionListsOfDateComponents($FieldPrefix, $AllowNullDate=0, $Year=-1, $Month=-1, $Day=-1, $SubmitOnChange="")
PrintAutoRefreshPage($Title, $NewUrl)
PrintOptionList($ResultVar, $Items, $SelectedValue=NULL, $SubmitOnChange="", $Size=1, $PrintEvenIfEmpty=1, $MultipleAllowed=false, $OnChangeAction=NULL, $Width=NULL, $DisabledOptions=NULL)