| 1 |
<?php |
| 2 |
|
| 3 |
// Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com> |
| 4 |
// License http://www.opensource.org/licenses/mit-license.html |
| 5 |
|
| 6 |
require_once 'db.php'; |
| 7 |
require_once 'SixtyLinesTemplate.php'; |
| 8 |
|
| 9 |
function isBlank($str) { |
| 10 |
if (is_string($str)) { |
| 11 |
return strlen(trim($str)) == 0; |
| 12 |
}else if (is_array($str)) { |
| 13 |
foreach ($str as $v) { |
| 14 |
if (!isBlank($v)) { |
| 15 |
return false; |
| 16 |
} |
| 17 |
} |
| 18 |
return true; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
function explodeTrim($str, $delimiter = ',', $limit = 100) { |
| 23 |
$arr = explode($delimiter, $str, $limit); |
| 24 |
foreach ($arr as &$value) { |
| 25 |
$value = trim($value); |
| 26 |
} |
| 27 |
return $arr; |
| 28 |
} |
| 29 |
|
| 30 |
function coalesce() { |
| 31 |
$args = func_get_args(); |
| 32 |
foreach ($args as $arg) { |
| 33 |
if (!is_null($arg)) { |
| 34 |
return $arg; |
| 35 |
} |
| 36 |
} |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
function coalesce2() { |
| 41 |
$args = func_get_args(); |
| 42 |
foreach ($args as $arg) { |
| 43 |
if (!is_null($arg)) { |
| 44 |
if (is_string($arg)) { |
| 45 |
if (strlen(trim($arg))) { |
| 46 |
return $arg; |
| 47 |
} |
| 48 |
}else { |
| 49 |
return $arg; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
function parseFraction($str) { |
| 57 |
$nums = explode('/', $str); |
| 58 |
if (count($nums) != 2) { |
| 59 |
return -1; |
| 60 |
} |
| 61 |
if (!is_numeric($nums[0]) || !is_numeric($nums[1])) { |
| 62 |
return -1; |
| 63 |
}else { |
| 64 |
return (double)$nums[0] / (double)$nums[1]; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
function toNumber($a) { |
| 69 |
$str = trim($a); |
| 70 |
if (is_numeric($str)) { |
| 71 |
return (double) $str; |
| 72 |
}else { |
| 73 |
$parts = explode(' ', $str); |
| 74 |
switch (count($parts)) { |
| 75 |
case 1: |
| 76 |
return parseFraction($parts[0]); |
| 77 |
break; |
| 78 |
case 2: |
| 79 |
if (!is_numeric($parts[0])) { |
| 80 |
return $a; |
| 81 |
} |
| 82 |
$val = parseFraction($parts[1]); |
| 83 |
if ($val == -1) { |
| 84 |
return $a; |
| 85 |
} |
| 86 |
return (double)$parts[0] + $val; |
| 87 |
break; |
| 88 |
default: |
| 89 |
return $a; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
function removeIfLastIsEmpty(&$arr) { |
| 95 |
$cnt = count($arr); |
| 96 |
if ($cnt && !strlen($arr[$cnt-1])) { |
| 97 |
array_pop($arr); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function array_keys_exist($checkKeys, $array) { |
| 102 |
$arrayKeys = array_keys($array); |
| 103 |
foreach ($checkKeys as $checkKey) { |
| 104 |
if (!array_key_exists($checkKey, $array)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
} |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
function buildArray($keyNames, $initValue = '') { |
| 112 |
return array_combine($keyNames, array_fill(0, count($keyNames), $initValue)); |
| 113 |
} |
| 114 |
|
| 115 |
function prepareArray($memberNames, $from, $key) { |
| 116 |
$ret = buildArray($memberNames); |
| 117 |
if (array_key_exists($key, $from)) { |
| 118 |
$ret = array_merge($ret, getMembers($from[$key], $memberNames)); |
| 119 |
} |
| 120 |
return $ret; |
| 121 |
} |
| 122 |
|
| 123 |
// original : http://phpspot.net/php/pg%90%B3%8BK%95%5C%8C%BB%81F%82%B7%82%D7%82%C4%94%BC%8Ap%89p%90%94%82%A9%82%C7%82%A4%82%A9%92%B2%82%D7%82%E9.html |
| 124 |
function is_alnum($text) { |
| 125 |
if (preg_match("/^[a-zA-Z0-9]+$/", $text)) { |
| 126 |
return true; |
| 127 |
} else { |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function initMembers(&$to, $names, $value = '') { |
| 133 |
if (is_array($to)) { |
| 134 |
foreach ($names as $name) { |
| 135 |
$to[$name] = $value; |
| 136 |
} |
| 137 |
}else if (is_object($to)) { |
| 138 |
foreach ($names as $name) { |
| 139 |
$to->$name = $value; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
function copyMembers($from, &$to, $names = NULL) { |
| 145 |
assert(is_array($from)); |
| 146 |
if (is_array($to)) { |
| 147 |
if (is_null($names)) { |
| 148 |
foreach ($from as $key => $value) { |
| 149 |
$to[$key] = $value; |
| 150 |
} |
| 151 |
}else { |
| 152 |
foreach ($names as $name) { |
| 153 |
if (array_key_exists($name, $from)) { |
| 154 |
$to[$name] = $from[$name]; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
}else if (is_object($to)) { |
| 159 |
foreach ($names as $name) { |
| 160 |
$to->$name = $from[$name]; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
function getMembers($from, $names) { |
| 166 |
assert(is_array($from)); |
| 167 |
assert(is_array($names)); |
| 168 |
$ret = array(); |
| 169 |
foreach ($names as $name) { |
| 170 |
if (array_key_exists($name, $from)) { |
| 171 |
$value = $from[$name]; |
| 172 |
$ret[$name] = $value; |
| 173 |
} |
| 174 |
} |
| 175 |
return $ret; |
| 176 |
} |
| 177 |
|
| 178 |
function mergeMembers($first, $second) { |
| 179 |
foreach ($first as $key=>$value) { |
| 180 |
$second[$key] = array_merge($value, $second[$key]); |
| 181 |
} |
| 182 |
return $second; |
| 183 |
} |
| 184 |
|
| 185 |
function array_unset_value($value, &$array) { |
| 186 |
unset($array[ array_search($value, $array) ]); |
| 187 |
} |
| 188 |
|
| 189 |
function array_values_or($arr) { |
| 190 |
$nval = 0; |
| 191 |
if (is_array($arr)) { |
| 192 |
foreach ($arr as $val) { |
| 193 |
$nval |= $val; |
| 194 |
} |
| 195 |
} |
| 196 |
return $nval; |
| 197 |
} |
| 198 |
|
| 199 |
function getColumnValues($recs, $columnName) { |
| 200 |
$ret = array(); |
| 201 |
foreach ($recs as $key=>$rec) { |
| 202 |
$ret[$key] = $rec[$columnName]; |
| 203 |
} |
| 204 |
return $ret; |
| 205 |
} |
| 206 |
|
| 207 |
function recordsToArray($recs, $keyColumnNames, $valueColumnName = null, $sub=false) { |
| 208 |
$ret = array(); |
| 209 |
if (is_null($valueColumnName)) { |
| 210 |
if (is_array($keyColumnNames)) { |
| 211 |
// make array of array |
| 212 |
$keyColumnNameCount = count($keyColumnNames); |
| 213 |
foreach ($recs as $rec) { |
| 214 |
$arr = &$ret; |
| 215 |
for ($i=0; $i<$keyColumnNameCount-1; ++$i) { |
| 216 |
$keyColumnName = $keyColumnNames[$i]; |
| 217 |
$key = $rec[$keyColumnName]; |
| 218 |
if (!array_key_exists($key, $arr)) { |
| 219 |
$arr[$key] = array(); |
| 220 |
} |
| 221 |
$subArray = &$arr[$key]; |
| 222 |
unset($arr); |
| 223 |
$arr = &$subArray; |
| 224 |
} |
| 225 |
$lastKeyColumnName = $keyColumnNames[$keyColumnNameCount-1]; |
| 226 |
$key = $rec[$lastKeyColumnName]; |
| 227 |
$arr[$key] = $rec; |
| 228 |
} |
| 229 |
}else { |
| 230 |
if ($sub) { |
| 231 |
foreach ($recs as $rec) { |
| 232 |
$key = $rec[$keyColumnNames]; |
| 233 |
$ret[$key][] = $rec; |
| 234 |
} |
| 235 |
}else { |
| 236 |
foreach ($recs as $rec) { |
| 237 |
$key = $rec[$keyColumnNames]; |
| 238 |
$ret[$key] = $rec; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
}else { |
| 243 |
if ($sub) { |
| 244 |
foreach ($recs as $rec) { |
| 245 |
$key = $rec[$keyColumnNames]; |
| 246 |
$value = $rec[$valueColumnName]; |
| 247 |
$ret[$key][] = $value; |
| 248 |
} |
| 249 |
}else { |
| 250 |
foreach ($recs as $rec) { |
| 251 |
$key = $rec[$keyColumnNames]; |
| 252 |
$value = $rec[$valueColumnName]; |
| 253 |
$ret[$key] = $value; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
return $ret; |
| 258 |
} |
| 259 |
|
| 260 |
function replaceRecordsValues(&$recs, $columnName, $arr) { |
| 261 |
foreach ($recs as &$rec) { |
| 262 |
$v = $rec[$columnName]; |
| 263 |
if (!isBlank($v)) { |
| 264 |
if (is_int(current($arr))) { |
| 265 |
$v = (int) $v; |
| 266 |
} |
| 267 |
$nv = $arr[$v]; |
| 268 |
$rec[$columnName] = $nv; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
function createSelectOptionTags($arr, $selectedKey = NULL) { |
| 274 |
$str = ''; |
| 275 |
$str .= "<option value=''></option>"; |
| 276 |
foreach ($arr as $key => $value) { |
| 277 |
$selected = ''; |
| 278 |
if ($key == $selectedKey) { |
| 279 |
$selected = 'selected'; |
| 280 |
} |
| 281 |
$key = htmlspecialchars($key); |
| 282 |
$value = htmlspecialchars($value); |
| 283 |
$str .= "<option value='$key' $selected>$value</option>"; |
| 284 |
} |
| 285 |
return $str; |
| 286 |
} |
| 287 |
|
| 288 |
function createCheckboxTags($name, $arr, $selectedKeys = NULL, $separator = "\n") { |
| 289 |
$name = htmlspecialchars($name); |
| 290 |
$str = ''; |
| 291 |
|
| 292 |
foreach ($arr as $key => $value) { |
| 293 |
$checked = ''; |
| 294 |
if (is_array($selectedKeys)) { |
| 295 |
if (array_search($key, $selectedKeys) !== FALSE) { |
| 296 |
$checked = 'checked'; |
| 297 |
} |
| 298 |
} |
| 299 |
$valueStr = htmlspecialchars($key); |
| 300 |
$label = htmlspecialchars($value); |
| 301 |
$str .= "<label><input type='checkbox' name='${name}[]' value='$valueStr' $checked>$label</label>"; |
| 302 |
$str .= $separator; |
| 303 |
} |
| 304 |
return $str; |
| 305 |
} |
| 306 |
|
| 307 |
function convertToHTMLLines($strings) { |
| 308 |
return implode(array_map('htmlspecialchars', $strings), '<BR>'); |
| 309 |
} |
| 310 |
|
| 311 |
// convert struct of arrays into array of structs (not really) |
| 312 |
function SOA2AOS($s, $names) { |
| 313 |
$ret = array(); |
| 314 |
foreach ($names as $name) { |
| 315 |
if (!array_key_exists($name, $s)) { |
| 316 |
continue; |
| 317 |
} |
| 318 |
$arr = $s[$name]; |
| 319 |
if (is_array($arr)) { |
| 320 |
foreach ($arr as $key=>$value) { |
| 321 |
if (!array_key_exists($key, $ret) || !is_array($ret[$key])) { |
| 322 |
$ret[$key] = array(); |
| 323 |
} |
| 324 |
$ret[$key][$name] = $value; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
return $ret; |
| 329 |
} |
| 330 |
|