CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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