/application/helpers/formatter_helper.php
PHP | 353 lines | 283 code | 61 blank | 9 comment | 67 complexity | d700639cfe1ceb16ca10e0db1447a333 MD5 | raw file
- <?
- if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- if (!function_exists('pluralise'))
- {
- function pluralise($string, $count)
- {
- if ($count < 2) return $string;
- // special cases
- switch (strtolower($string))
- {
- case 'is':
- return 'are';
- case 'day':
- return 'days';
- }
- $idx = strlen ($string) - 1;
- if ($string[$idx] == 'y') return substr($string, 0, $idx) . 'ies';
- else return $string . 's';
- }
- }
- if (!function_exists('dropdown'))
- {
- function dropdown($dataset, $value = 'id', $text = 'name')
- {
- $ret = array();
- foreach ($dataset as $ds)
- {
- if (isset ($ds->$value) && isset ($ds->$text))
- $ret[$ds->$value] = $ds->$text;
- }
- return $ret;
- }
- }
- if (!function_exists('roundDown'))
- {
- function roundDown($amount, $decimals = 4)
- {
- return floor ($amount * pow(10, $decimals)) / pow(10, $decimals);
- }
- }
- if (!function_exists('roundUp'))
- {
- function roundUp($amount, $decimals = 4)
- {
- return ceil ($amount * pow(10, $decimals)) / pow(10, $decimals);
- }
- }
- if (!function_exists('validateLRAccount'))
- {
- function validateLRAccount($account)
- {
- return preg_match ('/^[U|X][0-9]{1,}$/is', $account);
- }
- }
- if (!function_exists('money'))
- {
- function money($amount, $currency = '$', $forceSign = false, $maxDecimals = 5)
- {
- $decimals = 2;
- $amount = number_format($amount, 7, '.', ''); // that should be the max decimals ever
- $decimalValue = substr(strrchr((string)$amount, "."), 1);
- if ($decimalValue)
- {
- while ($decimalValue && $decimalValue{strlen($decimalValue) - 1} == '0')
- $decimalValue = substr($decimalValue, 0, -1);
- // Max of 5 decimals, minimum of 2
- $decimals = max(min($maxDecimals, strlen($decimalValue)), 2);
- }
- $formattedAmount = number_format(sprintf("%01.{$decimals}f", abs($amount)), $decimals);
- switch ($currency)
- {
- case 'USD':
- case '$':
- $formattedAmount = "$" . $formattedAmount;
- break;
- case 'EUR':
- $formattedAmount = "€" . $formattedAmount;
- break;
- }
- if ($amount < 0)
- return ($forceSign ? '<span class="red">' : '') . '-' . $formattedAmount . ($forceSign ? '</span>' : '');
- return ($forceSign ? '<span class="green">+' : '') . $formattedAmount . ($forceSign ? '</span>' : '');
- }
- }
- if (!function_exists('percent'))
- {
- function percent($amount)
- {
- return money($amount, '', true, 2);
- }
- }
- if (!function_exists('generatePagination'))
- {
- function generatePagination($url, $count, $page, $perpage, $showPages = false)
- {
- if (strpos($url, "%d") === false)
- $url .= '/%d/%d';
- $pages = ceil ($count / $perpage);
- if ($pages > 15) // Showing style 1, 2, 3, 4, ... , 8, 9, <10>, 11, 12, ... , 57, 58, 59
- {
- // Beginning Initial pagination, from 1 to 4
- $paginate = array();
- $end_begin = min(4, $pages);
- for($i=1; $i<=$end_begin; $i++)
- {
- $pageUrl = sprintf($url, $i, $perpage);
- $paginate[] = ($i == $page) ? '<strong>' . $i . '</strong>' :
- '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
- }
- // Middle
- $init_middle = max ($end_begin+1, $page-2);
- $end_middle = min($page+2, $pages);
- if($page > $end_begin + 2)
- $paginate[] = ' ... '; //To include '...' between numbers
- for ($i = $init_middle; $i <= $end_middle; $i++)
- {
- $pageUrl = sprintf($url, $i, $perpage);
- $paginate[] = ($i == $page) ? '<strong>' . $i . '</strong>' :
- '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
- }
- //Ending
- $init_ending = max ($end_middle + 1, $pages-2);
- if ($init_ending != $end_middle+1)
- $paginate[] = ' ... '; //To include '...' between numbers
- for ($i = $init_ending; $i <= $pages; $i++)
- {
- $pageUrl = sprintf($url, $i, $perpage);
- $paginate[] = '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
- }
- $paging = implode(', ',$paginate);
- }
- else //Showing style 1, 2, 3, 4, <5>, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
- {
- $paging = '';
- for ($i = 0; $i < $pages; $i++)
- {
- $start = $i * $perpage + 1;
- $end = min ($start + $perpage - 1, $count);
- $index = $i + 1;
- $pageUrl = sprintf($url, $index, $perpage);
- if ($showPages)
- $paging .= ($paging ? ', ' : '') . (($i + 1) == $page ? "<strong>$page</strong>" : '<a href="' . $pageUrl . '" class="pagination">' . ($i + 1) . '</a>');
- else $paging .= ($paging ? ', ' : '') . (($i + 1) == $page ? "<strong>$start-$end</strong>" : '<a href="' . $pageUrl . '" class="pagination">' . $start . '-' . $end . '</a>');
- }
- }
- return $paging;
- }
- }
- if (!function_exists('dateToDays'))
- {
- function dateToDays($date)
- {
- return ceil((now() - $date) / 86400);
- }
- }
- if (!function_exists('ellipsis'))
- {
- function ellipsis($string, $length, $stopanywhere = false)
- {
- if (strlen($string) > $length)
- {
- $string = substr($string, 0, ($length - 1));
- if ($stopanywhere)
- {
- $string .= '…';
- }
- else
- {
- $string = substr($string, 0, strrpos($string,' ')) . '…';
- }
- }
- return $string;
- }
- }
- //This function is used to format decimal numbers to the right
- if (!function_exists('deleteZeros'))
- {
- function deleteZeros($number)
- {
- $number = number_format($number, 5);
- //format of number
- $num = explode(".",$number);
- $long = strlen($num[1]);
- $dec = 5;
- for ($i=($long-1); $i>=0 ;$i--)
- {
- if(($num[1]{$i}) == 0)
- $dec--;
- else
- break;
- }
- $num = implode(".",$num);
- $dec = ($dec<=2) ? 2 : $dec;
- return number_format($num,$dec,'.',',');
- }
- }
- function elapsedTime($origin, $stopTime = null)
- {
- $fromNow = $stopTime == null;
- if (!$stopTime)
- $stopTime = now();
- $offset = $stopTime - $origin;
- if ($offset < 30) // Less than 30 seconds
- {
- if ($fromNow)
- return 'just now';
- else
- {
- return $offset > 0 ? $offset . ' ' . pluralise('sec', $offset) : 'instantly';
- }
- }
- if ($offset < 3600) // Less than 1 hour
- {
- $count = ceil($offset / 60);
- return $count . ' ' . pluralise('min', $count);
- }
- if ($offset < 86400) // Less than 1 day
- {
- $count = ceil($offset / 3600);
- return $count . ' ' . pluralise('hour', $count);
- }
- // in days
- $count = ceil($offset / 86400);
- return $count . ' ' . pluralise('day', $count);
- }
- function eta($deadline)
- {
- $offset = $deadline - now();
- if ($offset < 30) // Less than 60 seconds
- return $offset . ' ' . pluralise('sec', $offset);
- if ($offset < 3600) // Less than 1 hour
- {
- $count = ceil($offset / 60);
- return $count . ' ' . pluralise('min', $count);
- }
- if ($offset < 86400) // Less than 1 day
- {
- $count = ceil($offset / 3600);
- return $count . ' ' . pluralise('hour', $count);
- }
- // in days
- $count = ceil($offset / 86400);
- return $count . ' ' . pluralise('day', $count);
- }
- function displayCountDown($offset, $stringified = false)
- {
- $hours = floor($offset / 3600);
- $offset -= $hours * 3600;
- $minutes = floor($offset / 60);
- $offset -= $minutes * 60;
- $seconds = $offset;
- $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
- $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
- $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
- if ($stringified)
- return $hours . pluralise (' hour', $hours) . ', ' . $minutes . pluralise (' minute', $minutes) . ', ' . $seconds . pluralise (' second', $seconds);
- return $hours . ':' . $minutes . ':' . $seconds;
- }
- function prettify($data)
- {
- $result = '';
- foreach ($data as $k=>$v)
- {
- $k = str_replace ('_', ' ', $k);
- if ($v != '')
- $result .= ($result ? '<br/>' : '') . '<u>' . ucwords($k) . '</u>: ' . $v;
- }
- return $result;
- }
- function renderErrors($errorArray)
- {
- $result = '';
- foreach ($errorArray as $error)
- $result .= "<li>$error</li>";
- return "The following errors have been found:<ul>$result</ul>";
- }
- function stringifyBill($data, $operation, $type)
- {
- if (isset($data[$operation]) && isset($data[$operation][$type]))
- {
- $billData = $data[$operation][$type];
- $percent = (isset($billData->percent) && $billData->percent > 0) ? $billData->percent . '%' : null;
- $fixed = (isset($billData->fixed) && $billData->fixed > 0) ? money($billData->fixed) : null;
- $max = (isset($billData->max) && $billData->max > 0) ? 'MAX: ' . money($billData->max) : null;
- $string = null;
- if ($percent) $string .= $percent;
- if ($fixed) $string .= ($string ? ' + ' : '') . $fixed;
- if ($max) $string .= ($string ? ' ' : '') . "($max)";
- if ($string) return $string;
- }
- return '<span class="inactive">none</span>';
- }