Added formating functions for price/percent/exrate/qty fields in user native format.
[fa-stable.git] / includes / current_user.inc
1 <?php
2
3 include_once($path_to_root . "/includes/prefs/userprefs.inc");
4
5 //--------------------------------------------------------------------------
6
7 class current_user 
8 {
9
10         var $loginname;
11         var $username;
12         var     $name;
13         var $company;
14         var $access;
15
16         var $logged;
17
18         var $prefs;
19
20         function current_user()
21         {
22                 $this->loginname = $username = $this->name = $this->company = "";
23                 $this->logged = false;
24
25                 $this->prefs = null;
26         }
27
28         function logged_in()
29         {
30                 return $this->logged;
31         }
32
33         function set_company($company)
34         {
35                 $this->company = $company;
36         }
37
38         function login($company, $loginname, $password)
39         {
40                 $this->set_company($company);
41
42                 $Auth_Result = get_user_for_login($loginname, $password);
43
44                 if (db_num_rows($Auth_Result) > 0)
45                 {
46             $myrow = db_fetch($Auth_Result);
47
48             $this->access = $myrow["full_access"];
49             $this->name = $myrow["real_name"];
50             $this->loginname = $loginname;
51             $this->username = $this->loginname;
52             $this->prefs = new user_prefs($myrow);
53
54                         update_user_visitdate($loginname);
55                         $this->logged = true;
56                 } 
57                 else 
58                 {
59                         $this->logged = false;
60                 }
61
62                 return $this->logged;
63         }
64
65         function check_user_access()
66         {
67                 global $security_groups;
68                 return is_array($security_groups[$this->access]);
69         }
70
71         function can_access_page($page_level)
72         {
73                 global $security_groups;
74                 return isset($page_level) && in_array($page_level, $security_groups[$this->access]);
75         }
76
77         function get_db_connection()
78         {
79         global $db_connections;
80
81         $connection = $db_connections[$this->company];
82
83         //print_r($connection);
84
85         $db = mysql_connect($connection["host"] ,
86                 $connection["dbuser"], $connection["dbpassword"]);
87         mysql_select_db($connection["dbname"],$db);
88                 
89                 if (!defined('TB_PREF'))
90                         define('TB_PREF', $connection["tbpref"]);
91                 
92         return $db;
93         }
94
95         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
96                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize) {
97                 update_user_display_prefs($this->username, $price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl,
98                         $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize);
99
100                 // re-read the prefs
101                 $user = get_user($this->username);
102                 $this->prefs = new user_prefs($user);
103         }
104 }
105
106 //--------------------------------------------------------------------------
107
108 function number_format2($number, $decimals=0) 
109 {
110         global $thoseps, $decseps;
111         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
112         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
113         return number_format($number, $decimals, $dsep, $tsep);
114 }
115
116 function price_format($number) {
117     return number_format2($number, 
118         $_SESSION["wa_current_user"]->prefs->price_dec());
119 }
120
121 function qty_format($number) {
122     return number_format2($number, 
123         $_SESSION["wa_current_user"]->prefs->qty_dec());
124 }
125
126 function exrate_format($number) {
127     return number_format2($number, 
128         $_SESSION["wa_current_user"]->prefs->exrate_dec());
129 }
130
131 function percent_format($number) {
132     return number_format2($number, 
133         $_SESSION["wa_current_user"]->prefs->percent_dec());
134 }
135
136 function user_numeric($input) {
137     global $decseps, $thoseps;
138
139     $num = trim($input);
140     $sep = $thoseps[user_tho_sep()];
141     if($sep!='') $num = str_replace( $sep, '', $num);
142         str_replace($sep, '', $num);
143     $sep = $decseps[user_dec_sep()];
144     if($sep!='.') $num = str_replace( $sep, '.', $num);
145     
146     if (!is_numeric($num))
147           return false;
148     $num = (float)$num;
149     if ($num == (int)$num)
150           return (int)$num;
151     else
152           return $num;
153 }
154
155 function user_company() 
156 {
157         return $_SESSION["wa_current_user"]->company;
158 }
159
160 function user_language() 
161 {
162         return $_SESSION["wa_current_user"]->prefs->language();
163 }
164
165 function user_qty_dec() 
166 {
167         return $_SESSION["wa_current_user"]->prefs->qty_dec();
168 }
169
170 function user_price_dec() 
171 {
172         return $_SESSION["wa_current_user"]->prefs->price_dec();
173 }
174
175 function user_exrate_dec() 
176 {
177         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
178 }
179
180 function user_percent_dec() 
181 {
182         return $_SESSION["wa_current_user"]->prefs->percent_dec();
183 }
184
185 function user_show_gl_info() 
186 {
187         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
188 }
189
190 function user_show_codes() 
191 {
192         return $_SESSION["wa_current_user"]->prefs->show_codes();
193 }
194
195 function user_date_format() 
196 {
197         return $_SESSION["wa_current_user"]->prefs->date_format();
198 }
199
200 function user_date_display() 
201 {
202         return $_SESSION["wa_current_user"]->prefs->date_display();
203 }
204
205 function user_date_sep() 
206 {
207         return $_SESSION["wa_current_user"]->prefs->date_sep();
208 }
209
210 function user_tho_sep() 
211 {
212         return $_SESSION["wa_current_user"]->prefs->tho_sep();
213 }
214
215 function user_dec_sep() 
216 {
217         return $_SESSION["wa_current_user"]->prefs->dec_sep();
218 }
219
220 function user_theme() 
221 {
222         return $_SESSION["wa_current_user"]->prefs->get_theme();
223 }
224
225 function user_pagesize() 
226 {
227         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
228 }
229
230 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
231         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize) 
232 {
233
234         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
235                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize);
236 }
237
238 //--------------------------------------------------------------------------
239
240 ?>