Merge branch 'master' of https://github.com/rollox/civicrm-core into CRM-18317
[civicrm-core.git] / CRM / Utils / Number.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2016
31 */
32
33 /**
34 * Class CRM_Utils_Number
35 */
36 class CRM_Utils_Number {
37 /**
38 * Create a random number with a given precision.
39 *
40 * @param array $precision
41 * (int $significantDigits, int $postDecimalDigits).
42 *
43 * @return float
44 *
45 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
46 */
47 public static function createRandomDecimal($precision) {
48 list ($sigFigs, $decFigs) = $precision;
49 $rand = rand(0, pow(10, $sigFigs) - 1);
50 return $rand / pow(10, $decFigs);
51 }
52
53 /**
54 * Given a number, coerce it to meet the precision requirement. If possible, it should
55 * keep the number as-is. If necessary, this may drop the least-significant digits
56 * and/or move the decimal place.
57 *
58 * @param int|float $keyValue
59 * @param array $precision
60 * (int $significantDigits, int $postDecimalDigits).
61 * @return float
62 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
63 */
64 public static function createTruncatedDecimal($keyValue, $precision) {
65 list ($sigFigs, $decFigs) = $precision;
66 $sign = ($keyValue < 0) ? '-1' : 1;
67 $val = str_replace('.', '', abs($keyValue)); // ex: -123.456 ==> 123456
68 $val = substr($val, 0, $sigFigs); // ex: 123456 => 1234
69
70 // Move any extra digits after decimal
71 $extraFigs = strlen($val) - ($sigFigs - $decFigs);
72 if ($extraFigs > 0) {
73 return $sign * $val / pow(10, $extraFigs); // ex: 1234 => 1.234
74 }
75 else {
76 return $sign * $val;
77 }
78 }
79
80 /**
81 * Some kind of numbery-looky-printy thing.
82 *
83 * @param string $size
84 * @param bool $checkForPostMax
85 *
86 * @return int
87 */
88 public static function formatUnitSize($size, $checkForPostMax = FALSE) {
89 if ($size) {
90 $last = strtolower($size{strlen($size) - 1});
91 switch ($last) {
92 // The 'G' modifier is available since PHP 5.1.0
93
94 case 'g':
95 $size *= 1024;
96 case 'm':
97 $size *= 1024;
98 case 'k':
99 $size *= 1024;
100 }
101
102 if ($checkForPostMax) {
103 $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize'));
104 $postMaxSize = self::formatUnitSize(ini_get('post_max_size'));
105 if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) {
106 CRM_Core_Session::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert");
107 }
108 // respect php.ini upload_max_filesize
109 if ($size > $maxImportFileSize && $size !== $postMaxSize) {
110 $size = $maxImportFileSize;
111 CRM_Core_Session::setStatus(ts("Note: Please verify your configuration for Maximum File Size (in MB) <a href='%1'>Administrator >> System Settings >> Misc</a>. It should support 'upload_max_size' as defined in PHP.ini.Please check with your system administrator.", array(1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1'))), ts("Warning"), "alert");
112 }
113 }
114 return $size;
115 }
116 }
117
118 }