/
common-functions.php
105 lines (94 loc) · 3.03 KB
/
common-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* @param $key The URL key
* @param null $default_value A default value in case it is missing
* @param array $allowed_values A list of allowed values
* @return mixed|null
*/
function getOrDefault($key, $default_value = NULL, $allowed_values = []) {
$value = isset($_GET[$key]) ? $_GET[$key] : $default_value;
if (!isset($value)
|| (!empty($allowed_values) && !in_array($value, $allowed_values))) {
$value = $default_value;
}
return $value;
}
function getPostedOrDefault($key, $default_value = NULL, $allowed_values = []) {
$value = isset($_POST[$key]) ? $_POST[$key] : $default_value;
if (!isset($value)
|| (!empty($allowed_values) && !in_array($value, $allowed_values))) {
$value = $default_value;
}
return $value;
}
function createSmarty($templateDir) {
define('APPLICATION_DIR', __DIR__);
define('_SMARTY', APPLICATION_DIR . '/_smarty/');
$smarty = new Smarty();
$smarty->setTemplateDir(getcwd() . '/' . $templateDir);
$smarty->setCompileDir(_SMARTY . '/templates_c/');
$smarty->setConfigDir(_SMARTY . '/configs/');
$smarty->setCacheDir(_SMARTY . '/cache/');
$smarty->addPluginsDir(APPLICATION_DIR . '/common/smarty_plugins/');
// standard PHP function
$smarty->registerPlugin("modifier", "str_replace", "str_replace");
$smarty->registerPlugin("modifier", "number_format", "number_format");
$smarty->registerPlugin("function", "_t", "_t");
return $smarty;
}
function readCsv($csvFile, $id = '') {
$records = [];
if (file_exists($csvFile)) {
$lineNumber = 0;
$header = [];
$handle = fopen($csvFile, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lineNumber++;
$values = str_getcsv($line);
if ($lineNumber == 1) {
$header = $values;
} else {
if (count($header) != count($values)) {
error_log(sprintf('error in %s line #%d: %d vs %d', $csvFile, $lineNumber, count($header), count($values)));
}
$record = (object)array_combine($header, $values);
if ($id != '' && isset($record->{$id})) {
$records[$record->{$id}] = $record;
} else {
$records[] = $record;
}
}
}
}
} else {
error_log('file does not exist! ' . $csvFile);
}
return $records;
}
function setLanguage($language) {
global $languages;
$lang = isset($languages[$language]) ? $languages[$language] : $languages['en'];
// putenv('LANG=' . $lang);
setlocale(LC_ALL, $lang);
bindtextdomain('messages', './locale');
textdomain('messages');
}
if (!function_exists('array_is_list')) {
function array_is_list(array $arr) {
if ($arr === [])
return true;
return array_keys($arr) === range(0, count($arr) - 1);
}
}
/**
* Translate string with variables
* @return mixed|string The first argument should be the msgid, the rest should be the variables to inject
*/
function _t() {
$args = func_get_args();
$args[0] = _($args[0]);
if(func_num_args() <= 1)
return $args[0];
return call_user_func_array('sprintf', $args);
}