4d2d208177d0af531764cf1a097c64d6acaa2f73
[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 $pos;
15         var $access;
16
17         var $logged;
18         var $ui_mode = 0;
19         
20         var $prefs;
21
22         function current_user()
23         {
24                 $this->loginname = $username = $this->name = $this->company = "";
25                 $this->logged = false;
26
27                 $this->prefs = null;
28         }
29
30         function logged_in()
31         {
32                 return $this->logged;
33         }
34
35         function set_company($company)
36         {
37                 $this->company = $company;
38         }
39
40         function login($company, $loginname, $password)
41         {
42                 $this->set_company($company);
43
44                 $Auth_Result = get_user_for_login($loginname, $password);
45
46                 if (db_num_rows($Auth_Result) > 0)
47                 {
48             $myrow = db_fetch($Auth_Result);
49
50                     $this->access = $myrow["full_access"];
51                     $this->name = $myrow["real_name"];
52                     $this->pos = $myrow["pos"];
53                     $this->loginname = $loginname;
54                     $this->username = $this->loginname;
55                     $this->prefs = new user_prefs($myrow);
56
57                     update_user_visitdate($loginname);
58                     $this->logged = true;
59
60                 }
61                 else
62                 {
63                         $this->logged = false;
64                 }
65
66                 return $this->logged;
67         }
68
69         function check_user_access()
70         {
71                 global $security_groups;
72                 return is_array($security_groups[$this->access]);
73         }
74
75         function can_access_page($page_level)
76         {
77                 global $security_groups;
78                 // first registered company has site admin privileges
79                 return isset($page_level) && in_array($page_level, $security_groups[$this->access])
80                         && ($this->company == 0 || $page_level != 20); 
81         }
82
83         function get_db_connection()
84         {
85         global $db_connections;
86
87         $connection = $db_connections[$this->company];
88
89         //print_r($connection);
90
91         $db = mysql_connect($connection["host"] ,
92                 $connection["dbuser"], $connection["dbpassword"]);
93         mysql_select_db($connection["dbname"],$db);
94
95                 if (!defined('TB_PREF'))
96                         define('TB_PREF', $connection["tbpref"]);
97
98         return $db;
99         }
100
101         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, 
102                 $showgl, $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, 
103                 $theme, $pagesize, $show_hints, $profile, $rep_popup, $query_size) {
104                 update_user_display_prefs($this->username, $price_dec, 
105                         $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes, 
106                         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, 
107                         $show_hints, $profile, $rep_popup, $query_size);
108
109                 // re-read the prefs
110                 $user = get_user($this->username);
111                 $this->prefs = new user_prefs($user);
112         }
113 }
114
115 //--------------------------------------------------------------------------
116
117 function number_format2($number, $decimals=0)
118 {
119         global $thoseps, $decseps;
120         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
121         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
122         return number_format($number, $decimals, $dsep, $tsep);
123 }
124 //
125 //      Current ui mode.
126 //
127 function fallback_mode() {
128     return $_SESSION["wa_current_user"]->ui_mode==0;
129 }
130
131 function price_format($number) {
132     return number_format2($number,
133         $_SESSION["wa_current_user"]->prefs->price_dec());
134 }
135 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
136 //--------------------------------------------------------------------
137 function qty_format($number, $stock_id=null, &$dec) {
138         $dec = get_qty_dec($stock_id);
139     return number_format2($number, $dec);
140 }
141 // and get_qty_dec
142 function get_qty_dec($stock_id=null)
143 {
144         global $path_to_root;
145         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
146         if ($stock_id != null)
147                 $dec = get_unit_dec($stock_id);
148         if ($stock_id == null || $dec == -1 || $dec == null)
149                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
150         return $dec;
151 }
152 //-------------------------------------------------------------------
153 function exrate_format($number) {
154     return number_format2($number,
155         $_SESSION["wa_current_user"]->prefs->exrate_dec());
156 }
157
158 function percent_format($number) {
159     return number_format2($number,
160         $_SESSION["wa_current_user"]->prefs->percent_dec());
161 }
162
163 function user_numeric($input) {
164     global $decseps, $thoseps;
165
166     $num = trim($input);
167     $sep = $thoseps[user_tho_sep()];
168     if($sep!='') $num = str_replace( $sep, '', $num);
169         str_replace($sep, '', $num);
170     $sep = $decseps[user_dec_sep()];
171     if($sep!='.') $num = str_replace( $sep, '.', $num);
172
173     if (!is_numeric($num))
174           return false;
175     $num = (float)$num;
176     if ($num == (int)$num)
177           return (int)$num;
178     else
179           return $num;
180 }
181
182 function user_company()
183 {
184         return $_SESSION["wa_current_user"]->company;
185 }
186
187 function user_pos()
188 {
189         return $_SESSION["wa_current_user"]->pos;
190 }
191
192 function user_language()
193 {
194         return $_SESSION["wa_current_user"]->prefs->language();
195 }
196
197 function user_qty_dec()
198 {
199         return $_SESSION["wa_current_user"]->prefs->qty_dec();
200 }
201
202 function user_price_dec()
203 {
204         return $_SESSION["wa_current_user"]->prefs->price_dec();
205 }
206
207 function user_exrate_dec()
208 {
209         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
210 }
211
212 function user_percent_dec()
213 {
214         return $_SESSION["wa_current_user"]->prefs->percent_dec();
215 }
216
217 function user_show_gl_info()
218 {
219         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
220 }
221
222 function user_show_codes()
223 {
224         return $_SESSION["wa_current_user"]->prefs->show_codes();
225 }
226
227 function user_date_format()
228 {
229         return $_SESSION["wa_current_user"]->prefs->date_format();
230 }
231
232 function user_date_display()
233 {
234         return $_SESSION["wa_current_user"]->prefs->date_display();
235 }
236
237 function user_date_sep()
238 {
239         return $_SESSION["wa_current_user"]->prefs->date_sep();
240 }
241
242 function user_tho_sep()
243 {
244         return $_SESSION["wa_current_user"]->prefs->tho_sep();
245 }
246
247 function user_dec_sep()
248 {
249         return $_SESSION["wa_current_user"]->prefs->dec_sep();
250 }
251
252 function user_theme()
253 {
254         return $_SESSION["wa_current_user"]->prefs->get_theme();
255 }
256
257 function user_pagesize()
258 {
259         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
260 }
261
262 function user_hints()
263 {
264         return $_SESSION["wa_current_user"]->prefs->show_hints();
265 }
266
267 function user_print_profile()
268 {
269         return $_SESSION["wa_current_user"]->prefs->print_profile();
270 }
271
272 function user_rep_popup()
273 {
274         return $_SESSION["wa_current_user"]->prefs->rep_popup();
275 }
276
277 function user_query_size()
278 {
279         return $_SESSION["wa_current_user"]->prefs->query_size();
280 }
281
282 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
283         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
284         $print_profile, $rep_popup, $query_size)
285 {
286
287         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
288                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
289                 $print_profile, $rep_popup, $query_size);
290 }
291
292 function add_user_js_data() {
293         global $path_to_root, $thoseps, $decseps;
294
295         $ts = $thoseps[user_tho_sep()];
296         $ds = $decseps[user_dec_sep()];
297
298     $js = "\n<script type=\"text/javascript\">\n"
299           . "<!--\n"
300           . "var user = {\n"
301           . "theme: '". $path_to_root . '/themes/'. 'default' /*user_theme()*/.'/'."',\n"
302           . "loadtxt: '"._('Requesting data...')."',\n"
303           . "ts: '$ts',\n"
304           . "ds: '$ds',\n"
305           . "pdec : " . user_price_dec() . "}\n--></script>";
306
307   add_js_source($js);
308 }
309
310 //--------------------------------------------------------------------------
311
312 ?>