-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobals.php
executable file
·288 lines (244 loc) · 7.41 KB
/
globals.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/*
* UWS - Universal Wealth System
* globals.php
* GPL license
* author: Fabio Barone
* date: 30. Nov. 2009
*
* This file contains global variables and functions needed in other
* sections of the application
*/
//constants defining the transaction types
$SERVICE_TYPE = 1;
$INVENTORIZE_TYPE = 2;
$CONSUME_TYPE = 3;
//default cell identification for the default import (import_db.php)
$DEFAULT_CELL = "OS";
$DEFAULT_CELL_ID = 0;
//if 1, the application has already been initialized and now further import needed
$INITIALIZED = 0;
//the link to the error page
$errorpage = "error.php?error=";
//the default language; language can currently only be set this way
$defaultlang = "de";
//$_SESSION['language'] = $defaultlang;
//this function should be called to set a new language;
//switching languages is currently not implemented
if (! isset($_SESSION['language']) ) {
$_SESSION['language'] = $defaultlang;
}
//initialize the PDO data connection
$data_accessor = new DataAccessor();
$data_accessor->init_db_connection();
$dbh = $data_accessor->get_connection();
//get the translations
$messages = array();
function add_translation($lang, $array) {
global $messages;
if (! isset($messages[$lang]) ) {
$messages[$lang] = $array;
}
}
//translate a message
function translate($s) {
$lang = $_SESSION['language'];
global $messages;
if (isset($messages[$lang][$s]) ) {
return $messages[$lang][$s];
} else {
error_log("UWS L10N error. Language: $lang, message: $s");
}
}
//include all files only once
function include_all_once ($pattern) {
foreach (glob($pattern) as $file) {
$ok = include $file;
}
}
//include all language files only once
include_all_once('langs/*.php');
//get the transaction label, which is different for every transaction type
function get_transaction($ta_type, $ta_id)
{
$transaction = "";
$sql = "";
switch ($ta_type) {
case 1: //a service
$sql = "SELECT service_id FROM service WHERE transaction_id='$ta_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$srv_id = $result[0];
$sql = "SELECT service FROM servicelist WHERE service_id='$srv_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$transaction = $result[0];
break;
case 2: //an inventorization
$sql = "SELECT asset_id FROM inventorize WHERE transaction_id='$ta_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$asset_id = $result[0];
$sql = "SELECT asset FROM assetlist WHERE asset_id='$asset_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$transaction = $result[0];
break;
case 3: // a consumation
$sql = "SELECT asset_id FROM consume WHERE transaction_id='$ta_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$asset_id = $result[0];
$sql = "SELECT asset FROM assetlist WHERE asset_id='$asset_id'";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$transaction = $result[0];
break;
}
//echo "Transaction: ".$transaction;
return $transaction;
}
//when looking for members, instead of querying the database repeatedly,
//store member names in this array
$found_members = array();
//get the member name from the id
function get_member_name_from_id($member_id)
{
global $found_members;
if (array_key_exists($member_id, $found_members))
{
//it is already in the array, had already been queried previously
return $found_members [$member_id];
}
$sql = "SELECT name FROM members WHERE member_id='$member_id'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$member = $set[0];
//store in the array for in-memory retrieval
$found_members[$member_id] = $member;
return $member;
}
//get the member id from the name
function get_member_id_from_name($member)
{
global $found_members;
if ($member_id = array_search($member, $found_members))
{ //it is already in the array, had already been queried previously
return $member_id;
}
$sql = "SELECT member_id FROM members WHERE name='$member'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$member_id = $set[0];
//store in the array
$found_members[$member_id] = $member;
return $member_id;
}
//when retrieving asset names, store in this array
//in order for faster in-memory retrieval instead of
//repeated database querying
$found_assets = array();
//get an asset name from its id
function get_asset_name_from_id($asset_id)
{
global $found_assets;
if (array_key_exists($asset_id, $found_assets))
{ //it is already in the array, had already been queried previously
return $found_assets [$asset_id];
}
$sql = "SELECT asset FROM assetlist WHERE asset_id='$asset_id'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$asset = $set[0];
//store in the array
$found_members[$asset_id] = $asset;
return $asset;
}
//get asset id from name
function get_asset_id_from_name($asset)
{
global $found_assets;
if ($asset_id = array_search($asset, $found_assets))
{
//it is already in the array, had already been queried previously
return $asset_id;
}
$sql = "SELECT asset_id FROM assetlist WHERE asset='$asset'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$asset_id = $set[0];
//store in the array
$found_assets[$asset_id] = $asset;
return $asset_id;
}
//when retrieving service names, store in this array
//in order for faster in-memory retrieval instead of
//repeated database querying
$found_services = array();
//get service id from name
function get_service_id_from_name($service)
{
global $found_services;
if ($service_id = array_search($service, $found_services))
{
//it is already in the array, had already been queried previously
return $service_id;
}
$sql = "SELECT service_id FROM servicelist WHERE service='$service'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$service_id = $set[0];
//store in the array
$found_members[$service_id] = $service;
return $service_id;
}
function get_service_name_from_id($service_id)
{
global $found_services;
if (array_key_exists($service_id, $found_services))
{
//it is already in the array, had already been queried previously
return $found_services [$service_id];
}
$sql = "SELECT service FROM servicelist WHERE service_id='$service_id'";
$query = mysql_query($sql);
$set = mysql_fetch_row($query);
$service = $set[0];
//store in the array
$found_members[$service_id] = $service;
return $service;
}
//do a standard query on the database
function do_query($query)
{
$returnval = 0;
//print $query."\n<br>";
$result = mysql_query($query);
if (!$result) {
$msg = "Query failed. Query was: " . $query . ". Error from db: " . mysql_error();
//print "Query failed. Query was: " . $query . ". Error from db: " . mysql_error();
throw new Exception($msg);
} else {
$new_entry_id = mysql_insert_id();
return $new_entry_id;
}
}
//the following is not working yet. Logging should be enabled for
//auditing and better bug fixing.
// include class
//require_once 'Log.php';
// create Log object
//$logger = &Log::singleton("file", "uws.log");
/*
$priorities = array(
PEAR_LOG_EMERG => 'emergency',
PEAR_LOG_ALERT => 'alert',
PEAR_LOG_CRIT => 'critical',
PEAR_LOG_ERR => 'error',
PEAR_LOG_WARNING => 'warning',
PEAR_LOG_NOTICE => 'notice',
PEAR_LOG_INFO => 'info',
PEAR_LOG_DEBUG => 'debug'
);
*/
?>