PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/formatter_helper.php

https://bitbucket.org/justin_anastos/coin_flip_game
PHP | 353 lines | 283 code | 61 blank | 9 comment | 67 complexity | d700639cfe1ceb16ca10e0db1447a333 MD5 | raw file
  1. <?
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. if (!function_exists('pluralise'))
  4. {
  5. function pluralise($string, $count)
  6. {
  7. if ($count < 2) return $string;
  8. // special cases
  9. switch (strtolower($string))
  10. {
  11. case 'is':
  12. return 'are';
  13. case 'day':
  14. return 'days';
  15. }
  16. $idx = strlen ($string) - 1;
  17. if ($string[$idx] == 'y') return substr($string, 0, $idx) . 'ies';
  18. else return $string . 's';
  19. }
  20. }
  21. if (!function_exists('dropdown'))
  22. {
  23. function dropdown($dataset, $value = 'id', $text = 'name')
  24. {
  25. $ret = array();
  26. foreach ($dataset as $ds)
  27. {
  28. if (isset ($ds->$value) && isset ($ds->$text))
  29. $ret[$ds->$value] = $ds->$text;
  30. }
  31. return $ret;
  32. }
  33. }
  34. if (!function_exists('roundDown'))
  35. {
  36. function roundDown($amount, $decimals = 4)
  37. {
  38. return floor ($amount * pow(10, $decimals)) / pow(10, $decimals);
  39. }
  40. }
  41. if (!function_exists('roundUp'))
  42. {
  43. function roundUp($amount, $decimals = 4)
  44. {
  45. return ceil ($amount * pow(10, $decimals)) / pow(10, $decimals);
  46. }
  47. }
  48. if (!function_exists('validateLRAccount'))
  49. {
  50. function validateLRAccount($account)
  51. {
  52. return preg_match ('/^[U|X][0-9]{1,}$/is', $account);
  53. }
  54. }
  55. if (!function_exists('money'))
  56. {
  57. function money($amount, $currency = '$', $forceSign = false, $maxDecimals = 5)
  58. {
  59. $decimals = 2;
  60. $amount = number_format($amount, 7, '.', ''); // that should be the max decimals ever
  61. $decimalValue = substr(strrchr((string)$amount, "."), 1);
  62. if ($decimalValue)
  63. {
  64. while ($decimalValue && $decimalValue{strlen($decimalValue) - 1} == '0')
  65. $decimalValue = substr($decimalValue, 0, -1);
  66. // Max of 5 decimals, minimum of 2
  67. $decimals = max(min($maxDecimals, strlen($decimalValue)), 2);
  68. }
  69. $formattedAmount = number_format(sprintf("%01.{$decimals}f", abs($amount)), $decimals);
  70. switch ($currency)
  71. {
  72. case 'USD':
  73. case '$':
  74. $formattedAmount = "$" . $formattedAmount;
  75. break;
  76. case 'EUR':
  77. $formattedAmount = "&euro;" . $formattedAmount;
  78. break;
  79. }
  80. if ($amount < 0)
  81. return ($forceSign ? '<span class="red">' : '') . '-' . $formattedAmount . ($forceSign ? '</span>' : '');
  82. return ($forceSign ? '<span class="green">+' : '') . $formattedAmount . ($forceSign ? '</span>' : '');
  83. }
  84. }
  85. if (!function_exists('percent'))
  86. {
  87. function percent($amount)
  88. {
  89. return money($amount, '', true, 2);
  90. }
  91. }
  92. if (!function_exists('generatePagination'))
  93. {
  94. function generatePagination($url, $count, $page, $perpage, $showPages = false)
  95. {
  96. if (strpos($url, "%d") === false)
  97. $url .= '/%d/%d';
  98. $pages = ceil ($count / $perpage);
  99. if ($pages > 15) // Showing style 1, 2, 3, 4, ... , 8, 9, <10>, 11, 12, ... , 57, 58, 59
  100. {
  101. // Beginning Initial pagination, from 1 to 4
  102. $paginate = array();
  103. $end_begin = min(4, $pages);
  104. for($i=1; $i<=$end_begin; $i++)
  105. {
  106. $pageUrl = sprintf($url, $i, $perpage);
  107. $paginate[] = ($i == $page) ? '<strong>' . $i . '</strong>' :
  108. '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
  109. }
  110. // Middle
  111. $init_middle = max ($end_begin+1, $page-2);
  112. $end_middle = min($page+2, $pages);
  113. if($page > $end_begin + 2)
  114. $paginate[] = ' ... '; //To include '...' between numbers
  115. for ($i = $init_middle; $i <= $end_middle; $i++)
  116. {
  117. $pageUrl = sprintf($url, $i, $perpage);
  118. $paginate[] = ($i == $page) ? '<strong>' . $i . '</strong>' :
  119. '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
  120. }
  121. //Ending
  122. $init_ending = max ($end_middle + 1, $pages-2);
  123. if ($init_ending != $end_middle+1)
  124. $paginate[] = ' ... '; //To include '...' between numbers
  125. for ($i = $init_ending; $i <= $pages; $i++)
  126. {
  127. $pageUrl = sprintf($url, $i, $perpage);
  128. $paginate[] = '<a href="' . $pageUrl . '" class="pagination">' . $i . '</a>';
  129. }
  130. $paging = implode(', ',$paginate);
  131. }
  132. else //Showing style 1, 2, 3, 4, <5>, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
  133. {
  134. $paging = '';
  135. for ($i = 0; $i < $pages; $i++)
  136. {
  137. $start = $i * $perpage + 1;
  138. $end = min ($start + $perpage - 1, $count);
  139. $index = $i + 1;
  140. $pageUrl = sprintf($url, $index, $perpage);
  141. if ($showPages)
  142. $paging .= ($paging ? ', ' : '') . (($i + 1) == $page ? "<strong>$page</strong>" : '<a href="' . $pageUrl . '" class="pagination">' . ($i + 1) . '</a>');
  143. else $paging .= ($paging ? ', ' : '') . (($i + 1) == $page ? "<strong>$start-$end</strong>" : '<a href="' . $pageUrl . '" class="pagination">' . $start . '-' . $end . '</a>');
  144. }
  145. }
  146. return $paging;
  147. }
  148. }
  149. if (!function_exists('dateToDays'))
  150. {
  151. function dateToDays($date)
  152. {
  153. return ceil((now() - $date) / 86400);
  154. }
  155. }
  156. if (!function_exists('ellipsis'))
  157. {
  158. function ellipsis($string, $length, $stopanywhere = false)
  159. {
  160. if (strlen($string) > $length)
  161. {
  162. $string = substr($string, 0, ($length - 1));
  163. if ($stopanywhere)
  164. {
  165. $string .= '&hellip;';
  166. }
  167. else
  168. {
  169. $string = substr($string, 0, strrpos($string,' ')) . '&hellip;';
  170. }
  171. }
  172. return $string;
  173. }
  174. }
  175. //This function is used to format decimal numbers to the right
  176. if (!function_exists('deleteZeros'))
  177. {
  178. function deleteZeros($number)
  179. {
  180. $number = number_format($number, 5);
  181. //format of number
  182. $num = explode(".",$number);
  183. $long = strlen($num[1]);
  184. $dec = 5;
  185. for ($i=($long-1); $i>=0 ;$i--)
  186. {
  187. if(($num[1]{$i}) == 0)
  188. $dec--;
  189. else
  190. break;
  191. }
  192. $num = implode(".",$num);
  193. $dec = ($dec<=2) ? 2 : $dec;
  194. return number_format($num,$dec,'.',',');
  195. }
  196. }
  197. function elapsedTime($origin, $stopTime = null)
  198. {
  199. $fromNow = $stopTime == null;
  200. if (!$stopTime)
  201. $stopTime = now();
  202. $offset = $stopTime - $origin;
  203. if ($offset < 30) // Less than 30 seconds
  204. {
  205. if ($fromNow)
  206. return 'just now';
  207. else
  208. {
  209. return $offset > 0 ? $offset . ' ' . pluralise('sec', $offset) : 'instantly';
  210. }
  211. }
  212. if ($offset < 3600) // Less than 1 hour
  213. {
  214. $count = ceil($offset / 60);
  215. return $count . ' ' . pluralise('min', $count);
  216. }
  217. if ($offset < 86400) // Less than 1 day
  218. {
  219. $count = ceil($offset / 3600);
  220. return $count . ' ' . pluralise('hour', $count);
  221. }
  222. // in days
  223. $count = ceil($offset / 86400);
  224. return $count . ' ' . pluralise('day', $count);
  225. }
  226. function eta($deadline)
  227. {
  228. $offset = $deadline - now();
  229. if ($offset < 30) // Less than 60 seconds
  230. return $offset . ' ' . pluralise('sec', $offset);
  231. if ($offset < 3600) // Less than 1 hour
  232. {
  233. $count = ceil($offset / 60);
  234. return $count . ' ' . pluralise('min', $count);
  235. }
  236. if ($offset < 86400) // Less than 1 day
  237. {
  238. $count = ceil($offset / 3600);
  239. return $count . ' ' . pluralise('hour', $count);
  240. }
  241. // in days
  242. $count = ceil($offset / 86400);
  243. return $count . ' ' . pluralise('day', $count);
  244. }
  245. function displayCountDown($offset, $stringified = false)
  246. {
  247. $hours = floor($offset / 3600);
  248. $offset -= $hours * 3600;
  249. $minutes = floor($offset / 60);
  250. $offset -= $minutes * 60;
  251. $seconds = $offset;
  252. $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
  253. $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
  254. $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
  255. if ($stringified)
  256. return $hours . pluralise (' hour', $hours) . ', ' . $minutes . pluralise (' minute', $minutes) . ', ' . $seconds . pluralise (' second', $seconds);
  257. return $hours . ':' . $minutes . ':' . $seconds;
  258. }
  259. function prettify($data)
  260. {
  261. $result = '';
  262. foreach ($data as $k=>$v)
  263. {
  264. $k = str_replace ('_', ' ', $k);
  265. if ($v != '')
  266. $result .= ($result ? '<br/>' : '') . '<u>' . ucwords($k) . '</u>: ' . $v;
  267. }
  268. return $result;
  269. }
  270. function renderErrors($errorArray)
  271. {
  272. $result = '';
  273. foreach ($errorArray as $error)
  274. $result .= "<li>$error</li>";
  275. return "The following errors have been found:<ul>$result</ul>";
  276. }
  277. function stringifyBill($data, $operation, $type)
  278. {
  279. if (isset($data[$operation]) && isset($data[$operation][$type]))
  280. {
  281. $billData = $data[$operation][$type];
  282. $percent = (isset($billData->percent) && $billData->percent > 0) ? $billData->percent . '%' : null;
  283. $fixed = (isset($billData->fixed) && $billData->fixed > 0) ? money($billData->fixed) : null;
  284. $max = (isset($billData->max) && $billData->max > 0) ? 'MAX: ' . money($billData->max) : null;
  285. $string = null;
  286. if ($percent) $string .= $percent;
  287. if ($fixed) $string .= ($string ? ' + ' : '') . $fixed;
  288. if ($max) $string .= ($string ? ' ' : '') . "($max)";
  289. if ($string) return $string;
  290. }
  291. return '<span class="inactive">none</span>';
  292. }