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