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