KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00031 function array_merge_overwrite($a1, $a2){ 00032 foreach($a2 as $key2 => $value2){ 00033 if(!is_array($value2)){ 00034 $a1[$key2] = $value2; 00035 } else { 00036 if(!isset($a1[$key2])){ 00037 $a1[$key2] = null; 00038 } 00039 if(!is_array($a1[$key2])){ 00040 $a1[$key2] = $value2; 00041 } else { 00042 $a1[$key2] = array_merge_overwrite($a1[$key2], $a2[$key2]); 00043 } 00044 } 00045 } 00046 return $a1; 00047 } 00048 00058 function array_insert(&$form, $index, $value, $key=null){ 00059 $ret = array(); 00060 $n = 0; 00061 $i = false; 00062 foreach($form as $keys => $val){ 00063 if($n!=$index){ 00064 $ret[$keys] = $val; 00065 } else { 00066 if(!$key){ 00067 $ret[$index] = $value; 00068 $i = true; 00069 } else { 00070 $ret[$key] = $value; 00071 $i = true; 00072 } 00073 $ret[$keys] = $val; 00074 } 00075 $n++; 00076 } 00077 if(!$i){ 00078 if(!$key){ 00079 $ret[$index] = $value; 00080 $i = true; 00081 } else { 00082 $ret[$key] = $value; 00083 $i = true; 00084 } 00085 } 00086 $form = $ret; 00087 } 00088 00095 function array_num_insert(&$array, $index, $value) { 00096 $array2 = array_splice($array, $index); 00097 array_push($array, $value); 00098 $array = array_merge($array, $array2); 00099 } 00100 00108 function value_num($a){ 00109 if($a<=21){ 00110 switch ($a){ 00111 case 1: return 'UNO'; 00112 case 2: return 'DOS'; 00113 case 3: return 'TRES'; 00114 case 4: return 'CUATRO'; 00115 case 5: return 'CINCO'; 00116 case 6: return 'SEIS'; 00117 case 7: return 'SIETE'; 00118 case 8: return 'OCHO'; 00119 case 9: return 'NUEVE'; 00120 case 10: return 'DIEZ'; 00121 case 11: return 'ONCE'; 00122 case 12: return 'DOCE'; 00123 case 13: return 'TRECE'; 00124 case 14: return 'CATORCE'; 00125 case 15: return 'QUINCE'; 00126 case 16: return 'DIECISEIS'; 00127 case 17: return 'DIECISIETE'; 00128 case 18: return 'DIECIOCHO'; 00129 case 19: return 'DIECINUEVE'; 00130 case 20: return 'VEINTE'; 00131 case 21: return 'VEINTIUN'; 00132 } 00133 } else { 00134 if($a<=99){ 00135 if($a>=22&&$a<=29) 00136 return "VENTI".value_num($a % 10); 00137 if($a==30) return "TREINTA"; 00138 if($a>=31&&$a<=39) 00139 return "TREINTA Y ".value_num($a % 10); 00140 if($a==40) $b = "CUARENTA"; 00141 if($a>=41&&$a<=49) 00142 return "CUARENTA Y ".value_num($a % 10); 00143 if($a==50) return "CINCUENTA"; 00144 if($a>=51&&$a<=59) 00145 return "CINCUENTA Y ".value_num($a % 10); 00146 if($a==60) return "SESENTA"; 00147 if($a>=61&&$a<=69) 00148 return "SESENTA Y ".value_num($a % 10); 00149 if($a==70) return "SETENTA"; 00150 if($a>=71&&$a<=79) 00151 return "SETENTA Y ".value_num($a % 10); 00152 if($a==80) return "OCHENTA"; 00153 if($a>=81&&$a<=89) 00154 return "OCHENTA Y ".value_num($a % 10); 00155 if($a==90) return "NOVENTA"; 00156 if($a>=91&&$a<=99) 00157 return "NOVENTA Y ".value_num($a % 10); 00158 } else { 00159 if($a==100) return "CIEN"; 00160 if($a>=101&&$a<=199) 00161 return "CIENTO ".value_num($a % 100); 00162 if($a>=200&&$a<=299) 00163 return "DOSCIENTOS ".value_num($a % 100); 00164 if($a>=300&&$a<=399) 00165 return "TRECIENTOS ".value_num($a % 100); 00166 if($a>=400&&$a<=499) 00167 return "CUATROCIENTOS ".value_num($a % 100); 00168 if($a>=500&&$a<=599) 00169 return "QUINIENTOS ".value_num($a % 100); 00170 if($a>=600&&$a<=699) 00171 return "SEICIENTOS ".value_num($a % 100); 00172 if($a>=700&&$a<=799) 00173 return "SETECIENTOS ".value_num($a % 100); 00174 if($a>=800&&$a<=899) 00175 return "OCHOCIENTOS ".value_num($a % 100); 00176 if($a>=901&&$a<=999) 00177 return "NOVECIENTOS ".value_num($a % 100); 00178 } 00179 } 00180 } 00187 function millones($a){ 00188 $a = $a / 1000000; 00189 if($a==1) 00190 return "UN MILLON "; 00191 else 00192 return value_num($a)." MILLONES "; 00193 } 00194 00201 function miles($a){ 00202 $a = $a / 1000; 00203 if($a==1) 00204 return "MIL"; 00205 else 00206 return value_num($a)."MIL "; 00207 } 00208 00217 function money_letter($valor, $moneda, $centavos){ 00218 $a = $valor; 00219 $p = $moneda; 00220 $c = $centavos; 00221 $val = ""; 00222 $v = $a; 00223 $a = (int) $a; 00224 $d = round($v - $a, 2); 00225 if($a>=1000000){ 00226 $val = millones($a - ($a % 1000000)); 00227 $a = $a % 1000000; 00228 } 00229 if($a>=1000){ 00230 $val.= miles($a - ($a % 1000)); 00231 $a = $a % 1000; 00232 } 00233 $val.= value_num($a)." $p "; 00234 if($d){ 00235 $d*=100; 00236 $val.=" CON ".value_num($d)." $c "; 00237 } 00238 return $val; 00239 } 00240 00247 function to_human($num){ 00248 if($num<1024){ 00249 return $num." bytes"; 00250 } else { 00251 if($num<1024*1024){ 00252 return round($num/1024, 2)." kb"; 00253 } else { 00254 return round($num/1024/1024, 2)." mb"; 00255 } 00256 } 00257 } 00258 00265 function camelize($str) { 00266 $str = strtr($str, '_', ' '); 00267 $str = ucwords($str); 00268 $str = str_replace(' ', '', $str); 00269 00270 return $str; 00271 } 00272 00279 function uncamelize($str) { 00280 00281 return strtolower(preg_replace('/([A-Z])/', "_\\1", $str)); 00282 } 00283 00290 function get_params($params){ 00291 $data = array(); 00292 foreach ($params as $p) { 00293 if(is_string($p) && preg_match('/^(\w+): (.*)/', $p, $match)){ 00294 $data[$match[1]] = $match[2]; 00295 } else { 00296 $data[] = $p; 00297 } 00298 } 00299 return $data; 00300 } 00301 00302 /* 00303 * Recibe una cadena como: item1,item2,item3 y retorna una como: "item1","item2","item3". 00304 * @param string $lista Cadena con Items separados por comas (,). 00305 * @return string $listaEncomillada Cadena con Items encerrados en doblecomillas y separados por comas (,). 00306 */ 00307 function encomillar_lista($lista){ 00308 $arrItems = explode(',', $lista); 00309 $n = count($arrItems); 00310 $listaEncomillada = ''; 00311 for ($i=0; $i<$n-1; $i++) { 00312 $listaEncomillada.= "\"".$arrItems[$i]."\","; 00313 } 00314 $listaEncomillada.= "\"".$arrItems[$n-1]."\""; 00315 return $listaEncomillada; 00316 } 00317 00325 function comillas($word){ 00326 return "'$word'"; 00327 } 00328 00336 function highlight($sentence, $what){ 00337 return str_replace($what, '<strong class="highlight">'.$what.'</strong>', $sentence); 00338 } 00339 00346 function money($number){ 00347 $number = my_round($number); 00348 return "$ ".number_format($number, 2, ",", "."); 00349 } 00350 00358 function roundnumber($n, $d = 0) { 00359 $n = $n - 0; 00360 if ($d === NULL) $d = 2; 00361 00362 $f = pow(10, $d); 00363 $n += pow(10, - ($d + 1)); 00364 $n = round($n * $f) / $f; 00365 $n += pow(10, - ($d + 1)); 00366 $n += ''; 00367 00368 if ( $d == 0 ): 00369 return substr($n, 0, strpos($n, '.')); 00370 else: 00371 return substr($n, 0, strpos($n, '.') + $d + 1); 00372 endif; 00373 } 00374 00383 function my_round($number, $n=2){ 00384 $number = (float) $number; 00385 $n = (int) $number; 00386 return ActiveRecord::static_select_one("round($number, $n)"); 00387 } 00388 00395 function copy_dir($source, $target) { 00396 if (is_dir($source)) { 00397 00398 if (!is_dir($target)){ 00399 @mkdir($target); 00400 } 00401 00402 $d = dir($source); 00403 00404 while (false !== ($entry = $d->read())) { 00405 if ($entry == '.' || $entry == '..') { 00406 continue; 00407 } 00408 00409 $Entry = $source.'/'.$entry; 00410 if (is_dir($Entry)) { 00411 copy_dir($Entry, $target.'/'.$entry); 00412 continue; 00413 } 00414 copy($Entry, $target.'/'. $entry); 00415 } 00416 00417 $d->close(); 00418 }else { 00419 copy($source, $target); 00420 } 00421 } 00422 00428 function join_path($dirs){ 00429 if(!is_array($dirs)) { 00430 $dirs = func_get_args(); 00431 } 00432 $n = count($dirs); 00433 00434 $path= ''; 00435 for($i=0; $i<$n; $i++) { 00436 $dir = $dirs[$i]; 00437 if(!empty($dir)) { 00438 $path.=$dir; 00439 if($i<($n-1) && $dir[strlen($dir)-1]!='/') $path.='/'; 00440 } 00441 } 00442 00443 return $path; 00444 } 00445 00452 function mkpath($path){ 00453 $path = join_path(func_get_args()); 00454 if(@mkdir($path) or file_exists($path)) return true; 00455 return (mkpath(dirname($path)) and mkdir($path)); 00456 } 00457 00472 function age(){ 00473 $params = get_params(func_get_args()); 00474 $error = false; 00475 00476 $active_app = Router::get_application(); 00477 00478 if(!isset($params['order'])){ 00479 if($kumbia_config = Config::read('config')){ 00480 if(preg_match('/^DD[^DMY]MM[^DMY]YYYY$/', $kumbia_config->$active_app->dbdate)){ 00481 $params['order'] = 'd-m-Y'; 00482 } elseif(preg_match('/^DD[^DMY]YYYY[^DMY]MM$/', $kumbia_config->$active_app->dbdate)){ 00483 $params['order'] = 'd-Y-m'; 00484 } elseif(preg_match('/^MM[^DMY]DD[^DMY]YYYY$/', $kumbia_config->$active_app->dbdate)) { 00485 $params['order'] = 'm-d-Y'; 00486 } elseif(preg_match('/^MM[^DMY]YYYY[^DMY]DD$/', $kumbia_config->$active_app->dbdate)) { 00487 $params['order'] = 'm-Y-d'; 00488 } elseif(preg_match('/^YYYY[^DMY]DD[^DMY]MM$/', $kumbia_config->$active_app->dbdate)) { 00489 $params['order'] = 'Y-d-m'; 00490 } else { 00491 $params['order'] = 'Y-m-d'; 00492 } 00493 } 00494 } 00495 00496 if(isset($params['month'], $params['day'], $params['year'])){ 00497 $time_nac = mktime(0, 0, 0, $params['month'], $params['day'], $params['year']); 00498 } elseif(isset($params['birthdate'])) { 00499 if (preg_match( '/^([0-9]+)[^0-9]([0-9]+)[^0-9]([0-9]+)$/', $params['birthdate'], $date)) { 00500 if($params['order'] == 'd-m-Y'){ 00501 if(checkdate($date[2], $date[1], $date[3])) { 00502 $time_nac = mktime(0, 0, 0, $date[2], $date[1], $date[3]); 00503 } else { 00504 $error = true; 00505 } 00506 } elseif($params['order'] == 'd-Y-m'){ 00507 if(checkdate($date[3], $date[1], $date[2])) { 00508 $time_nac = mktime(0, 0, 0, $date[3], $date[1], $date[2]); 00509 } else { 00510 $error = true; 00511 } 00512 } elseif($params['order'] == 'm-d-Y') { 00513 if(checkdate($date[1], $date[2], $date[3])) { 00514 $time_nac = mktime(0, 0, 0, $date[1], $date[2], $date[3]); 00515 } else { 00516 $error = true; 00517 } 00518 } elseif($params['order'] == 'm-Y-d') { 00519 if(checkdate($date[1], $date[3], $date[2])) { 00520 $time_nac = mktime(0, 0, 0, $date[1], $date[3], $date[2]); 00521 } else { 00522 $error = true; 00523 } 00524 } elseif($params['order'] == 'Y-d-m') { 00525 if(checkdate($date[3], $date[2], $date[1])) { 00526 $time_nac = mktime(0, 0, 0, $date[3], $date[2], $date[1]); 00527 } else { 00528 $error = true; 00529 } 00530 } else { 00531 if(checkdate($date[2], $date[3], $date[1])) { 00532 $time_nac = mktime(0, 0, 0, $date[2], $date[3], $date[1]); 00533 } else { 00534 $error = true; 00535 } 00536 } 00537 } else { 00538 $error = true; 00539 } 00540 } else { 00541 $time_nac = time(); 00542 } 00543 00544 if(isset($params['today_month'], $params['today_day'], $params['today_year'])){ 00545 $time = mktime(0, 0, 0, $params['today_month'], $params['today_day'], $params['today_year']); 00546 } elseif(isset($params['today'])) { 00547 if (preg_match( '/^([0-9]+)[^0-9]([0-9]+)[^0-9]([0-9]+)$/', $params['today'], $date)) { 00548 if($params['order'] == 'd-m-Y'){ 00549 if(checkdate($date[2], $date[1], $date[3])) { 00550 $time = mktime(0, 0, 0, $date[2], $date[1], $date[3]); 00551 } else { 00552 $error = true; 00553 } 00554 } elseif($params['order'] == 'd-Y-m'){ 00555 if(checkdate($date[3], $date[1], $date[2])) { 00556 $time = mktime(0, 0, 0, $date[3], $date[1], $date[2]); 00557 } else { 00558 $error = true; 00559 } 00560 } elseif($params['order'] == 'm-d-Y') { 00561 if(checkdate($date[1], $date[2], $date[3])) { 00562 $time = mktime(0, 0, 0, $date[1], $date[2], $date[3]); 00563 } else { 00564 $error = true; 00565 } 00566 } elseif($params['order'] == 'm-Y-d') { 00567 if(checkdate($date[1], $date[3], $date[2])) { 00568 $time = mktime(0, 0, 0, $date[1], $date[3], $date[2]); 00569 } else { 00570 $error = true; 00571 } 00572 } elseif($params['order'] == 'Y-d-m') { 00573 if(checkdate($date[3], $date[2], $date[1])) { 00574 $time = mktime(0, 0, 0, $date[3], $date[2], $date[1]); 00575 } else { 00576 $error = true; 00577 } 00578 } else { 00579 if(checkdate($date[2], $date[3], $date[1])) { 00580 $time = mktime(0, 0, 0, $date[2], $date[3], $date[1]); 00581 } else { 00582 $error = true; 00583 } 00584 } 00585 } else { 00586 $error = true; 00587 } 00588 } else { 00589 $time = time(); 00590 } 00591 00592 if(!$error){ 00593 $edad = idate('Y' ,$time) - idate('Y' ,$time_nac); 00594 } else { 00595 $edad = 0; 00596 } 00597 00598 if($edad>0){ 00599 if(idate('m' ,$time) < idate('m' ,$time_nac)){ 00600 $edad--; 00601 } else if(idate('m' ,$time) == idate('m' ,$time_nac)){ 00602 if(idate('d' ,$time) < idate('d' ,$time_nac)){ 00603 $edad--; 00604 } 00605 } 00606 } elseif($edad<0) { 00607 $edad = 0; 00608 } 00609 00610 return $edad; 00611 } 00612 00619 function remove_dir($dir){ 00623 $dir = join_path(func_get_args()); 00624 00628 if($files = array_merge(glob(join_path($dir,'*')), glob(join_path($dir,'.*')))) { 00632 foreach($files as $file) { 00636 if(!preg_match("/^.*\/?[\.]{1,2}$/",$file)) { 00637 if(is_dir($file)) { 00638 remove_dir($file); 00639 } else { 00640 unlink($file); 00641 } 00642 } 00643 } 00644 } 00645 return rmdir($dir); 00646 } 00647 00653 function get_arguments($params) { 00654 $args = array(); 00655 foreach($params as $k=>$v) { 00656 if(is_numeric($k)) { 00657 array_push($args, $v); 00658 } else { 00659 array_push($args, "$k: $v"); 00660 } 00661 } 00662 return $args; 00663 } 00664 00670 function lcfirst($s) { 00671 return strtolower(substr($s, 0, 1)) . substr($s, 1); 00672 } 00673 00678 function object_from_params($s='') { 00679 $params = is_array($s) ? $s : get_params(func_get_args()); 00680 $obj = (object) $params; 00681 return $obj; 00682 } 00683 00690 function mirror_range($start, $end, $step=1) { 00691 $args = func_get_args(); 00692 $arr = call_user_func_array('range', $args); 00693 $mirror = array(); 00694 foreach($arr as $v) { 00695 $mirror[$v] = $v; 00696 } 00697 return $mirror; 00698 } 00699 00705 function file_extension($filename) { 00706 $ext = strchr($filename,"."); 00707 return $ext; 00708 } 00709 00715 function get_server_url($route) { 00716 $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http'; 00717 return "$protocol://{$_SERVER['SERVER_NAME']}".get_kumbia_url($route); 00718 } 00719 00720 00725 function use_helper($helper) { 00726 call_user_func_array(array('view' , 'helpers'), func_get_args()); 00727 } 00728 00729 00734 function content(){ 00735 View::content(); 00736 } 00737 00746 function truncate($text, $word, $number=0){ 00747 if($number){ 00748 $word = substr($word, 0, $number); 00749 } else { 00750 $word = rtrim($word); 00751 } 00752 00753 return $word.$text; 00754 } 00755 00763 function render_partial($partial, $time=false, $params=array()) { 00764 View::partial($partial, $time, $params); 00765 } 00766 00773 function redirect_to($action, $seconds = 0.01){ 00774 $seconds*=1000; 00775 return xhtml_tag('script', 'type: text/javascript', "content: setTimeout('window.location=\"?/$action\"', $seconds)"); 00776 }