Commit | Line | Data |
---|---|---|
ac5f2ccd | 1 | <?php |
5bc392e6 EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_Number | |
5 | */ | |
ac5f2ccd TO |
6 | class CRM_Utils_Number { |
7 | /** | |
fe482240 | 8 | * Create a random number with a given precision. |
ac5f2ccd | 9 | * |
77855840 TO |
10 | * @param array $precision |
11 | * (int $significantDigits, int $postDecimalDigits). | |
f4aaa82a EM |
12 | * |
13 | * @return float | |
ac5f2ccd TO |
14 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
15 | */ | |
00be9182 | 16 | public static function createRandomDecimal($precision) { |
ac5f2ccd TO |
17 | list ($sigFigs, $decFigs) = $precision; |
18 | $rand = rand(0, pow(10, $sigFigs) - 1); | |
19 | return $rand / pow(10, $decFigs); | |
20 | } | |
21 | ||
22 | /** | |
23 | * Given a number, coerce it to meet the precision requirement. If possible, it should | |
24 | * keep the number as-is. If necessary, this may drop the least-significant digits | |
25 | * and/or move the decimal place. | |
26 | * | |
27 | * @param int|float $keyValue | |
77855840 TO |
28 | * @param array $precision |
29 | * (int $significantDigits, int $postDecimalDigits). | |
ac5f2ccd TO |
30 | * @return float |
31 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html | |
32 | */ | |
00be9182 | 33 | public static function createTruncatedDecimal($keyValue, $precision) { |
ac5f2ccd TO |
34 | list ($sigFigs, $decFigs) = $precision; |
35 | $sign = ($keyValue < 0) ? '-1' : 1; | |
36 | $val = str_replace('.', '', abs($keyValue)); // ex: -123.456 ==> 123456 | |
37 | $val = substr($val, 0, $sigFigs); // ex: 123456 => 1234 | |
38 | ||
39 | // Move any extra digits after decimal | |
40 | $extraFigs = strlen($val) - ($sigFigs - $decFigs); | |
41 | if ($extraFigs > 0) { | |
42 | return $sign * $val / pow(10, $extraFigs); // ex: 1234 => 1.234 | |
43 | } | |
44 | else { | |
45 | return $sign * $val; | |
46 | } | |
47 | } | |
96025800 | 48 | |
2e966dd5 TO |
49 | /** |
50 | * Some kind of numbery-looky-printy thing. | |
51 | */ | |
52 | public static function formatUnitSize($size, $checkForPostMax = FALSE) { | |
53 | if ($size) { | |
54 | $last = strtolower($size{strlen($size) - 1}); | |
55 | switch ($last) { | |
56 | // The 'G' modifier is available since PHP 5.1.0 | |
57 | ||
58 | case 'g': | |
59 | $size *= 1024; | |
60 | case 'm': | |
61 | $size *= 1024; | |
62 | case 'k': | |
63 | $size *= 1024; | |
64 | } | |
65 | ||
66 | if ($checkForPostMax) { | |
67 | $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize')); | |
68 | $postMaxSize = self::formatUnitSize(ini_get('post_max_size')); | |
69 | if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) { | |
70 | 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"); | |
71 | } | |
72 | //respect php.ini upload_max_filesize | |
73 | if ($size > $maxImportFileSize && $size !== $postMaxSize) { | |
74 | $size = $maxImportFileSize; | |
75 | 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"); | |
76 | } | |
77 | } | |
78 | return $size; | |
79 | } | |
80 | } | |
81 | ||
f4aaa82a | 82 | } |