Version typo fixed
[fa-stable.git] / includes / current_user.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/includes/prefs/userprefs.inc");
13
14 //--------------------------------------------------------------------------
15
16 class current_user
17 {
18
19         var $loginname;
20         var $username;
21         var     $name;
22         var $company;
23         var $pos;
24         var $access;
25
26         var $logged;
27         var $ui_mode = 0;
28         
29         var $prefs;
30
31         function current_user()
32         {
33                 $this->loginname = $this->username = $this->name = $this->company = "";
34                 $this->logged = false;
35
36                 $this->prefs = new user_prefs();
37         }
38
39         function logged_in()
40         {
41                 return $this->logged;
42         }
43
44         function set_company($company)
45         {
46                 $this->company = $company;
47         }
48
49         function login($company, $loginname, $password)
50         {
51                 $this->set_company($company);
52
53                 $Auth_Result = get_user_for_login($loginname, $password);
54
55                 if (db_num_rows($Auth_Result) > 0)
56                 {
57             $myrow = db_fetch($Auth_Result);
58
59                     $this->access = $myrow["full_access"];
60                     $this->name = $myrow["real_name"];
61                     $this->pos = $myrow["pos"];
62                     $this->loginname = $loginname;
63                     $this->username = $this->loginname;
64                     $this->prefs = new user_prefs($myrow);
65
66                     update_user_visitdate($loginname);
67                     $this->logged = true;
68
69                 }
70                 else
71                 {
72                         $this->logged = false;
73                 }
74
75                 return $this->logged;
76         }
77
78         function check_user_access()
79         {
80                 global $security_groups;
81                 return is_array($security_groups[$this->access]);
82         }
83
84         function can_access_page($page_level)
85         {
86                 global $security_groups;
87                 // first registered company has site admin privileges
88                 return isset($page_level) && in_array($page_level, $security_groups[$this->access])
89                         && ($this->company == 0 || $page_level != 20); 
90         }
91
92         function get_db_connection()
93         {
94         global $db_connections;
95
96         $connection = $db_connections[$this->company];
97
98         //print_r($connection);
99
100         $db = mysql_connect($connection["host"] ,
101                 $connection["dbuser"], $connection["dbpassword"]);
102         mysql_select_db($connection["dbname"],$db);
103
104                 if (!defined('TB_PREF'))
105                         define('TB_PREF', $connection["tbpref"]);
106
107         return $db;
108         }
109
110         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, 
111                 $showgl, $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, 
112                 $theme, $pagesize, $show_hints, $profile, $rep_popup, $query_size, 
113                 $graphic_links, $lang) {
114                 update_user_display_prefs($this->username, $price_dec, 
115                         $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes, 
116                         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, 
117                         $show_hints, $profile, $rep_popup, $query_size, $graphic_links, $lang);
118
119                 // re-read the prefs
120                 $user = get_user($this->username);
121                 $this->prefs = new user_prefs($user);
122         }
123 }
124
125 //--------------------------------------------------------------------------
126
127 function round2($number, $decimals=0)
128 {
129         $delta = ($number < 0 ? -.000001 : .000001);
130         return round($number+$delta, $decimals);
131 }
132
133 function number_format2($number, $decimals=0)
134 {
135         global $thoseps, $decseps;
136         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
137         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
138         //return number_format($number, $decimals, $dsep,       $tsep);
139         $delta = ($number < 0 ? -.000001 : .000001);
140         return number_format($number+$delta, $decimals, $dsep,  $tsep);
141 }
142 //
143 //      Current ui mode.
144 //
145 function fallback_mode() {
146     return $_SESSION["wa_current_user"]->ui_mode==0;
147 }
148
149 function price_format($number) {
150     return number_format2($number,
151         $_SESSION["wa_current_user"]->prefs->price_dec());
152 }
153
154 function price_decimal_format($number, &$dec)
155 {
156         $dec = user_price_dec();
157         $str = strval($number);
158         $pos = strpos($str, '.');
159         if ($pos !== false)
160         {
161                 $len = strlen(substr($str, $pos + 1));
162                 if ($len > $dec)
163                         $dec = $len;
164         }
165         return number_format2($number, $dec);
166 }
167 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
168 //--------------------------------------------------------------------
169 function qty_format($number, $stock_id=null, &$dec) {
170         $dec = get_qty_dec($stock_id);
171     return number_format2($number, $dec);
172 }
173 // and get_qty_dec
174 function get_qty_dec($stock_id=null)
175 {
176         global $path_to_root;
177         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
178         if ($stock_id != null)
179                 $dec = get_unit_dec($stock_id);
180         if ($stock_id == null || $dec == -1 || $dec == null)
181                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
182         return $dec;
183 }
184 //-------------------------------------------------------------------
185 function exrate_format($number) {
186     return number_format2($number,
187         $_SESSION["wa_current_user"]->prefs->exrate_dec());
188 }
189
190 function percent_format($number) {
191     return number_format2($number,
192         $_SESSION["wa_current_user"]->prefs->percent_dec());
193 }
194
195 function user_numeric($input) {
196     global $decseps, $thoseps;
197
198     $num = trim($input);
199     $sep = $thoseps[user_tho_sep()];
200     if($sep!='') $num = str_replace( $sep, '', $num);
201         str_replace($sep, '', $num);
202     $sep = $decseps[user_dec_sep()];
203     if($sep!='.') $num = str_replace( $sep, '.', $num);
204
205     if (!is_numeric($num))
206           return false;
207     $num = (float)$num;
208     if ($num == (int)$num)
209           return (int)$num;
210     else
211           return $num;
212 }
213
214 function user_company()
215 {
216         return $_SESSION["wa_current_user"]->company;
217 }
218
219 function user_pos()
220 {
221         return $_SESSION["wa_current_user"]->pos;
222 }
223
224 function user_language()
225 {
226         return $_SESSION["wa_current_user"]->prefs->language();
227 }
228
229 function user_qty_dec()
230 {
231         return $_SESSION["wa_current_user"]->prefs->qty_dec();
232 }
233
234 function user_price_dec()
235 {
236         return $_SESSION["wa_current_user"]->prefs->price_dec();
237 }
238
239 function user_exrate_dec()
240 {
241         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
242 }
243
244 function user_percent_dec()
245 {
246         return $_SESSION["wa_current_user"]->prefs->percent_dec();
247 }
248
249 function user_show_gl_info()
250 {
251         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
252 }
253
254 function user_show_codes()
255 {
256         return $_SESSION["wa_current_user"]->prefs->show_codes();
257 }
258
259 function user_date_format()
260 {
261         return $_SESSION["wa_current_user"]->prefs->date_format();
262 }
263
264 function user_date_display()
265 {
266         return $_SESSION["wa_current_user"]->prefs->date_display();
267 }
268
269 function user_date_sep()
270 {
271         return $_SESSION["wa_current_user"]->prefs->date_sep();
272 }
273
274 function user_tho_sep()
275 {
276         return $_SESSION["wa_current_user"]->prefs->tho_sep();
277 }
278
279 function user_dec_sep()
280 {
281         return $_SESSION["wa_current_user"]->prefs->dec_sep();
282 }
283
284 function user_theme()
285 {
286         return $_SESSION["wa_current_user"]->prefs->get_theme();
287 }
288
289 function user_pagesize()
290 {
291         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
292 }
293
294 function user_hints()
295 {
296         return $_SESSION["wa_current_user"]->prefs->show_hints();
297 }
298
299 function user_print_profile()
300 {
301         return $_SESSION["wa_current_user"]->prefs->print_profile();
302 }
303
304 function user_rep_popup()
305 {
306         return $_SESSION["wa_current_user"]->prefs->rep_popup();
307 }
308
309 function user_query_size()
310 {
311         return $_SESSION["wa_current_user"]->prefs->query_size();
312 }
313
314 function user_graphic_links()
315 {
316         return $_SESSION["wa_current_user"]->prefs->graphic_links();
317 }
318
319 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
320         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
321         $print_profile, $rep_popup, $query_size, $graphic_links, $lang)
322 {
323
324         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
325                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
326                 $print_profile, $rep_popup, $query_size, $graphic_links, $lang);
327 }
328
329 function add_user_js_data() {
330         global $path_to_root, $thoseps, $decseps;
331
332         $ts = $thoseps[user_tho_sep()];
333         $ds = $decseps[user_dec_sep()];
334
335     $js = "\n"
336           . "var user = {\n"
337           . "theme: '". $path_to_root . '/themes/'. user_theme().'/'."',\n"
338           . "loadtxt: '"._('Requesting data...')."',\n"
339           . "ts: '$ts',\n"
340           . "ds: '$ds',\n"
341           . "pdec : " . user_price_dec() . "}\n";
342
343   add_js_source($js);
344 }
345
346 //--------------------------------------------------------------------------
347
348 ?>