Commit 90818efe9835e4b5d58c2e5ad8d0b75b1debae02
1 parent
0d7c0b5b
added array utility functions and getCurrentDateTime
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@218 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
58 additions
and
15 deletions
lib/lookup.inc
| @@ -20,20 +20,7 @@ | @@ -20,20 +20,7 @@ | ||
| 20 | * @return the id of the row in the db with $fieldName=$fieldValue | 20 | * @return the id of the row in the db with $fieldName=$fieldValue |
| 21 | */ | 21 | */ |
| 22 | function lookupID($tableName, $fieldName, $fieldValue){ | 22 | function lookupID($tableName, $fieldName, $fieldValue){ |
| 23 | - $sql = new Owl_DB(); | ||
| 24 | - $query = "select id from $tableName where $fieldName = '$fieldValue'"; | ||
| 25 | - //echo "lookup.inc::lookupID: about to execute $query<br>"; | ||
| 26 | - if ($sql->query($query)) { | ||
| 27 | - if ($sql->next_record()) { | ||
| 28 | - return $sql->f("id"); | ||
| 29 | - } else { | ||
| 30 | - $_SESSION["errorMessage"] = "id retrieval failed."; | ||
| 31 | - return false; | ||
| 32 | - } | ||
| 33 | - } else { | ||
| 34 | - $_SESSION["errorMessage"] = "lookup query ($query) failed."; | ||
| 35 | - return false; | ||
| 36 | - } | 23 | + return lookupField($tableName, "id", $fieldName, $fieldValue); |
| 37 | } | 24 | } |
| 38 | 25 | ||
| 39 | /** | 26 | /** |
| @@ -45,6 +32,7 @@ function lookupID($tableName, $fieldName, $fieldValue){ | @@ -45,6 +32,7 @@ function lookupID($tableName, $fieldName, $fieldValue){ | ||
| 45 | function lookupGroupIDs($userID) { | 32 | function lookupGroupIDs($userID) { |
| 46 | global $default; | 33 | global $default; |
| 47 | $groupIDs = array(); | 34 | $groupIDs = array(); |
| 35 | + | ||
| 48 | $sql = new Owl_DB; | 36 | $sql = new Owl_DB; |
| 49 | $sql->query("select group_id from $default->owl_groups_users_table where user_id = '$userID'"); | 37 | $sql->query("select group_id from $default->owl_groups_users_table where user_id = '$userID'"); |
| 50 | while($sql->next_record()) { | 38 | while($sql->next_record()) { |
| @@ -53,7 +41,37 @@ function lookupGroupIDs($userID) { | @@ -53,7 +41,37 @@ function lookupGroupIDs($userID) { | ||
| 53 | return $groupIDs; | 41 | return $groupIDs; |
| 54 | } | 42 | } |
| 55 | 43 | ||
| 56 | - | 44 | +/** |
| 45 | + * Performs a generic one field lookup on a table | ||
| 46 | + * | ||
| 47 | + * @param $tableName the name of the table to perform the lookup on | ||
| 48 | + * @param $selectFieldName the field to return | ||
| 49 | + * @param $whereFieldName the field to discriminate against(?!) | ||
| 50 | + * @param $whereFieldValue the field value to return rows for | ||
| 51 | + * (NOTE: the caller is responsible for quoting this value) | ||
| 52 | + */ | ||
| 53 | +function lookupField($tableName, $selectFieldName, $whereFieldName, $whereFieldValue) { | ||
| 54 | + global $default; | ||
| 55 | + $sql = new Owl_DB(); | ||
| 56 | + $query = "select $selectFieldName from $tableName where $whereFieldName = $whereFieldValue"; | ||
| 57 | + $default->log->debug("lookup.inc::lookupField query=$query"); | ||
| 58 | + if ($sql->query($query)) { | ||
| 59 | + if ($sql->next_record()) { | ||
| 60 | + return $sql->f($selectFieldName); | ||
| 61 | + } else { | ||
| 62 | + $_SESSION["errorMessage"] = "$selectFieldName field lookup retrieval failed ($query)."; | ||
| 63 | + return false; | ||
| 64 | + } | ||
| 65 | + } else { | ||
| 66 | + $_SESSION["errorMessage"] = "lookup query failed ($query)."; | ||
| 67 | + return false; | ||
| 68 | + } | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | + | ||
| 72 | +/** | ||
| 73 | + * Converts an array to a string | ||
| 74 | + */ | ||
| 57 | function arrayToString($array) { | 75 | function arrayToString($array) { |
| 58 | ob_start(); | 76 | ob_start(); |
| 59 | print_r($array); | 77 | print_r($array); |
| @@ -61,4 +79,29 @@ function arrayToString($array) { | @@ -61,4 +79,29 @@ function arrayToString($array) { | ||
| 61 | ob_end_clean(); | 79 | ob_end_clean(); |
| 62 | return $arrToStr; | 80 | return $arrToStr; |
| 63 | } | 81 | } |
| 82 | + | ||
| 83 | +/** | ||
| 84 | + * Converts an array to a comma separated string | ||
| 85 | + * | ||
| 86 | + * @param $array the array to convert | ||
| 87 | + * @return a comma separated string of the array values | ||
| 88 | + */ | ||
| 89 | +function arrayToCss($array) { | ||
| 90 | + $css = ""; | ||
| 91 | + foreach ($array as $key=>$value) { | ||
| 92 | + $css = $css . $value . ","; | ||
| 93 | + } | ||
| 94 | + // trim the last comma | ||
| 95 | + $css = substr("$css", 0, -1); | ||
| 96 | + return $css; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +/** | ||
| 100 | + * Returns the current date time | ||
| 101 | + * | ||
| 102 | + * @return String the current date time (Y-m-d H:i:s) | ||
| 103 | + */ | ||
| 104 | +function getCurrentDateTime() { | ||
| 105 | + return date("Y-m-d H:i:s", time()); | ||
| 106 | +} | ||
| 64 | ?> | 107 | ?> |