git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@492 97e9348a-65ac-dc4b-aefc-98561f571b83
312 lines
8.6 KiB
PHP
312 lines
8.6 KiB
PHP
<?php
|
|
|
|
class FormatHelper extends AppHelper {
|
|
|
|
static $time;
|
|
static $number;
|
|
|
|
function __construct() {
|
|
App::import('Helper', array('Time', 'Number'));
|
|
self::$time = new TimeHelper;
|
|
self::$number = new NumberHelper;
|
|
}
|
|
|
|
function _n($amount, $singular, $plural = null) {
|
|
return ($amount . ' ' .
|
|
__n($singular,
|
|
$plural ? $plural : $singular.'s',
|
|
$amount,
|
|
true));
|
|
}
|
|
|
|
function currency($amount, $spans = false, $dollar_sign = null) {
|
|
if (!isset($amount))
|
|
return '-';
|
|
//return null;
|
|
|
|
$currency = self::$number->currency($amount,
|
|
isset($dollar_sign) ? $dollar_sign : 'USD',
|
|
$spans ? array('before'=>'', 'after'=>'') : array());
|
|
|
|
if ($spans)
|
|
return ('<SPAN CLASS="dollar-sign">$</SPAN>' .
|
|
'<SPAN CLASS="dollar-amount">' . $currency . '</SPAN>');
|
|
|
|
return $currency;
|
|
}
|
|
|
|
function date($date, $age = false) {
|
|
if (!$date) return null;
|
|
|
|
$date_fmt = 'm/d/Y';
|
|
return (self::$time->format($date_fmt, $date) .
|
|
($age
|
|
? ' (' . self::age($date, 60*60*24) . ')'
|
|
: ''));
|
|
}
|
|
|
|
function datetime($datetime, $age = false) {
|
|
if (!$datetime) return null;
|
|
return (self::$time->nice($datetime) .
|
|
($age
|
|
? ' (' . self::age($datetime) . ')'
|
|
: ''));
|
|
}
|
|
|
|
function phone($phone, $ext = null) {
|
|
if (!isset($phone))
|
|
return null;
|
|
|
|
$phone = preg_replace("/\D/", "", $phone);
|
|
if(strlen($phone) == 7)
|
|
$phone = preg_replace("/(\d{3})(\d{4})/", "$1-$2", $phone);
|
|
elseif(strlen($phone) == 10)
|
|
$phone = preg_replace("/(\d{3})(\d{3})(\d{4})/", "$1-$2-$3", $phone);
|
|
|
|
if ($ext)
|
|
$phone .= ' x' . $ext;
|
|
|
|
return $phone;
|
|
}
|
|
|
|
function comment($comment) {
|
|
if (isset($comment) && is_array($comment)) {
|
|
foreach (array_keys($comment) AS $k) {
|
|
if (!$comment[$k])
|
|
unset($comment[$k]);
|
|
}
|
|
return implode('; ', $comment);
|
|
}
|
|
|
|
return $comment;
|
|
}
|
|
|
|
function age($datetime, $min_span = 0) {
|
|
if (!isset($datetime))
|
|
return null;
|
|
|
|
$now = time();
|
|
$seconds = self::$time->fromString($datetime);
|
|
$backwards = ($seconds > $now);
|
|
|
|
$timefrom = $backwards ? $now : $seconds;
|
|
$timeto = $backwards ? $seconds : $now;
|
|
$span = $timeto - $timefrom;
|
|
|
|
//pr(compact('now', 'seconds', 'backwards', 'timefrom', 'timeto', 'span', 'min_span'));
|
|
|
|
// If now, just return so
|
|
if ($span === 0)
|
|
return __('now', true);
|
|
|
|
// Display seconds if under 45 seconds
|
|
if ($span < 45 && $span >= $min_span) {
|
|
$approx = round($span);
|
|
$unit = 'second';
|
|
}
|
|
|
|
// Display minutes if under 45 minutes
|
|
if (!isset($approx)) {
|
|
$unit = 'minute';
|
|
$span /= 60; $min_span /= 60;
|
|
if ($span < 45 && ($span >= $min_span || $min_span <= 1))
|
|
$approx = round($span);
|
|
}
|
|
|
|
// Display hours if under 18 hours
|
|
if (!isset($approx)) {
|
|
$unit = 'hour';
|
|
$span /= 60; $min_span /= 60;
|
|
if ($span < 18 && ($span >= $min_span || $min_span <= 1))
|
|
$approx = round($span);
|
|
}
|
|
|
|
// Display days if under 6.5 days
|
|
if (!isset($approx)) {
|
|
$unit = 'day';
|
|
$span /= 24; $min_span /= 24;
|
|
if ($span < 6.5 && ($span >= $min_span || $min_span <= 1))
|
|
$approx = round($span);
|
|
}
|
|
|
|
// Display weeks if less than 8 weeks
|
|
if (!isset($approx)) {
|
|
$unit = 'week';
|
|
$span /= 7; $min_span /= 7;
|
|
if ($span < 8 && ($span >= $min_span || $min_span <= 1))
|
|
$approx = round($span);
|
|
}
|
|
|
|
// Display months if less than 20 months
|
|
if (!isset($approx)) {
|
|
$unit = 'month';
|
|
$span /= 365.2425 / (7*12); $min_span /= 365.2425 / (7*12);
|
|
if ($span < 20 && ($span >= $min_span || $min_span <= 1)) {
|
|
$approx = round($span);
|
|
// Months are from 28-31 days. If it's too
|
|
// close to being an exact month, just fudge
|
|
// by saying the result is 'about' N months
|
|
// instead of 'almost' or 'over' N months,
|
|
// since we can't be accurate on this without
|
|
// taking into account the day of the week.
|
|
if ((abs($span - $approx) * (365.2425 / 12)) < 3)
|
|
$relative = 'about';
|
|
}
|
|
}
|
|
|
|
// Otherwise, just display years
|
|
if (!isset($approx)) {
|
|
$unit = 'year';
|
|
$span /= 12; $min_span /= 12;
|
|
$approx = round($span);
|
|
}
|
|
|
|
//pr(compact('span', 'min_span', 'approx', 'unit'));
|
|
|
|
if ($approx == 0) {
|
|
if ($unit == 'day')
|
|
return __('today', true);
|
|
|
|
return __('this ' . $unit, true);
|
|
}
|
|
|
|
return (__(isset($relative)
|
|
? $relative
|
|
: ($approx == $span
|
|
? ''
|
|
: ($approx > $span ? 'almost' : 'over')), true)
|
|
. ' '
|
|
. self::_n($approx, $unit)
|
|
. ($backwards ? '' : __(' ago', true)));
|
|
}
|
|
|
|
/*****************************
|
|
** Test code
|
|
**/
|
|
/* function basevals($max) { */
|
|
/* return incrvals($max); */
|
|
/* if ($max % 2) { */
|
|
/* return array(0, 1, ($max+1)/2-1, $max/2, ($max+1)/2, $max-1); */
|
|
/* } */
|
|
/* else { */
|
|
/* return array(0, 1, $max/2-1, $max/2, $max/2+1, $max-1); */
|
|
/* } */
|
|
/* } */
|
|
|
|
/* function incrvals($max, $suppress = false) { */
|
|
/* if ($suppress) */
|
|
/* //return array(0); */
|
|
/* return array(0, 4, $max-1); */
|
|
|
|
/* //return array(0, 1, $max/3, (int)(($max+1)/2)-1, 2*$max/3, 3*$max/4, 4*$max/5, $max-1); */
|
|
/* return array(0, 1, 3, $max/2+2, (int)(($max+1)/2)+4, 2*$max/3, 3*$max/4, 4*$max/5, $max-1); */
|
|
/* } */
|
|
|
|
/* echo('<TABLE>' . "\n"); */
|
|
/* foreach (incrvals(10) AS $y) { */
|
|
/* foreach (incrvals(12, $y) AS $w) { */
|
|
/* foreach (incrvals(30, $y||$w) AS $d) { */
|
|
/* foreach (incrvals(24, $y||$w||$d) AS $h) { */
|
|
/* foreach (incrvals(60, $y||$w||$d||$h) AS $m) { */
|
|
/* foreach ($y||$w||$d||$h||$m ? array(0,1,59) : basevals(60) AS $s) { */
|
|
/* $seconds = 60*(60*(24*(30*(12*$y +$w) + $d) + $h) + $m) + $s; */
|
|
/* //$seconds = 60*(60*(24*(7*(52*$y +$w) + $d) + $h) + $m) + $s; */
|
|
/* $age = time() - $seconds; */
|
|
/* echo ('<TR>' */
|
|
/* . ' <TD>' . "y:$y; M:$w; d:$d; h:$h; m:$m; s:$s" . '</TD>' */
|
|
/* . ' <TD>' . FormatHelper::datetime($age) . '</TD>' */
|
|
/* . ' <TD>' . FormatHelper::age($age) . '</TD>' */
|
|
/* . ' <TD>' . $seconds . '; days='.($seconds/60/60/24).'</TD>' */
|
|
/* .'</TR>' . "\n"); */
|
|
/* } */
|
|
/* } */
|
|
/* } */
|
|
/* } */
|
|
/* } */
|
|
/* } */
|
|
/* echo('</TABLE>' . "\n"); */
|
|
|
|
|
|
// Helper function to convert PHP vars to javascript
|
|
function phpVarToJavascript($var, $name = '', $depth='', $special = false) {
|
|
|
|
// Establish a prefix to use before printing $var
|
|
$prefix = $depth;
|
|
|
|
// If given a name, set it up JS style
|
|
if ($name)
|
|
$prefix .= $name . ": ";
|
|
|
|
if (!isset($var))
|
|
return $prefix . 'null';
|
|
|
|
if (is_bool($var))
|
|
return $prefix . ($var ? "true" : "false");
|
|
|
|
if (is_numeric($var))
|
|
return $prefix . $var;
|
|
|
|
if (is_string($var)) {
|
|
// Check to see if this is a special
|
|
if ($special)
|
|
return $prefix . $var;
|
|
|
|
// OK, must just be a string after all
|
|
$str = '';
|
|
$first = true;
|
|
$lines = explode("\n", $var);
|
|
//pr(compact('var', 'lines'));
|
|
foreach ($lines AS $line) {
|
|
if (!$first)
|
|
$str .= ' + "\n" +' . "\n";
|
|
$str .= $prefix . "'" . preg_replace("/'/", '\\\'', $line) . "'";
|
|
$first = false;
|
|
}
|
|
return $str;
|
|
}
|
|
|
|
// The only thing left that we know how to dump
|
|
// is an array. Die if we have anything else.
|
|
if (!is_array($var)) {
|
|
die;
|
|
return null;
|
|
}
|
|
|
|
// Keep a lookout for special cases, flagged by '--special'
|
|
|
|
// eg: array('name' => array('--special' => array('a' => 'one', 'b' => 'two')))
|
|
// GIVES: name: [ 'one', 'two' ]
|
|
// NOT: name: { --special: { a: 'one', b: 'two' } }
|
|
|
|
// eg: array('name' => array('--special' => 'varname'))
|
|
// GIVES: name: varname
|
|
// NOT: name: { --special: 'varname' }
|
|
|
|
if (isset($var['--special']) && count($var) == 1)
|
|
return self::phpVarToJavascript($var['--special'], $name, $depth, true);
|
|
|
|
// PHP array indices can be a mix of integer and string based.
|
|
// Just guess here, unless flagged as a special case.
|
|
if (isset($var[0]) || $special)
|
|
return ($prefix . "[\n"
|
|
. implode(",\n",
|
|
array_map(array('FormatHelper', 'phpVarToJavascript'),
|
|
array_values($var),
|
|
array(),
|
|
array_fill(0, count($var), $depth.' ')
|
|
))
|
|
. "\n$depth]");
|
|
|
|
return ($prefix . "{\n"
|
|
. implode(",\n",
|
|
array_map(array('FormatHelper', 'phpVarToJavascript'),
|
|
array_values($var), array_keys($var),
|
|
array_fill(0, count($var), $depth.' ')
|
|
))
|
|
. "\n$depth}");
|
|
}
|
|
|
|
}
|
|
|
|
|