Preparing for Graphic Links instead of Text Links (user display option, default)
[fa-stable.git] / includes / current_user.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) 2005-2008  FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 = $username = $this->name = $this->company = "";
34                 $this->logged = false;
35
36                 $this->prefs = null;
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, $graphic_links) {
113                 update_user_display_prefs($this->username, $price_dec, 
114                         $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes, 
115                         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, 
116                         $show_hints, $profile, $rep_popup, $query_size, $graphic_links);
117
118                 // re-read the prefs
119                 $user = get_user($this->username);
120                 $this->prefs = new user_prefs($user);
121         }
122 }
123
124 //--------------------------------------------------------------------------
125
126 function number_format2($number, $decimals=0)
127 {
128         global $thoseps, $decseps;
129         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
130         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
131         return number_format($number, $decimals, $dsep, $tsep);
132 }
133 //
134 //      Current ui mode.
135 //
136 function fallback_mode() {
137     return $_SESSION["wa_current_user"]->ui_mode==0;
138 }
139
140 function price_format($number) {
141     return number_format2($number,
142         $_SESSION["wa_current_user"]->prefs->price_dec());
143 }
144 // 2008-06-15. Added extra parameter $stock_id and reference for $dec
145 //--------------------------------------------------------------------
146 function qty_format($number, $stock_id=null, &$dec) {
147         $dec = get_qty_dec($stock_id);
148     return number_format2($number, $dec);
149 }
150 // and get_qty_dec
151 function get_qty_dec($stock_id=null)
152 {
153         global $path_to_root;
154         include_once($path_to_root."/inventory/includes/db/items_units_db.inc");
155         if ($stock_id != null)
156                 $dec = get_unit_dec($stock_id);
157         if ($stock_id == null || $dec == -1 || $dec == null)
158                 $dec = $_SESSION["wa_current_user"]->prefs->qty_dec();
159         return $dec;
160 }
161 //-------------------------------------------------------------------
162 function exrate_format($number) {
163     return number_format2($number,
164         $_SESSION["wa_current_user"]->prefs->exrate_dec());
165 }
166
167 function percent_format($number) {
168     return number_format2($number,
169         $_SESSION["wa_current_user"]->prefs->percent_dec());
170 }
171
172 function user_numeric($input) {
173     global $decseps, $thoseps;
174
175     $num = trim($input);
176     $sep = $thoseps[user_tho_sep()];
177     if($sep!='') $num = str_replace( $sep, '', $num);
178         str_replace($sep, '', $num);
179     $sep = $decseps[user_dec_sep()];
180     if($sep!='.') $num = str_replace( $sep, '.', $num);
181
182     if (!is_numeric($num))
183           return false;
184     $num = (float)$num;
185     if ($num == (int)$num)
186           return (int)$num;
187     else
188           return $num;
189 }
190
191 function user_company()
192 {
193         return $_SESSION["wa_current_user"]->company;
194 }
195
196 function user_pos()
197 {
198         return $_SESSION["wa_current_user"]->pos;
199 }
200
201 function user_language()
202 {
203         return $_SESSION["wa_current_user"]->prefs->language();
204 }
205
206 function user_qty_dec()
207 {
208         return $_SESSION["wa_current_user"]->prefs->qty_dec();
209 }
210
211 function user_price_dec()
212 {
213         return $_SESSION["wa_current_user"]->prefs->price_dec();
214 }
215
216 function user_exrate_dec()
217 {
218         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
219 }
220
221 function user_percent_dec()
222 {
223         return $_SESSION["wa_current_user"]->prefs->percent_dec();
224 }
225
226 function user_show_gl_info()
227 {
228         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
229 }
230
231 function user_show_codes()
232 {
233         return $_SESSION["wa_current_user"]->prefs->show_codes();
234 }
235
236 function user_date_format()
237 {
238         return $_SESSION["wa_current_user"]->prefs->date_format();
239 }
240
241 function user_date_display()
242 {
243         return $_SESSION["wa_current_user"]->prefs->date_display();
244 }
245
246 function user_date_sep()
247 {
248         return $_SESSION["wa_current_user"]->prefs->date_sep();
249 }
250
251 function user_tho_sep()
252 {
253         return $_SESSION["wa_current_user"]->prefs->tho_sep();
254 }
255
256 function user_dec_sep()
257 {
258         return $_SESSION["wa_current_user"]->prefs->dec_sep();
259 }
260
261 function user_theme()
262 {
263         return $_SESSION["wa_current_user"]->prefs->get_theme();
264 }
265
266 function user_pagesize()
267 {
268         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
269 }
270
271 function user_hints()
272 {
273         return $_SESSION["wa_current_user"]->prefs->show_hints();
274 }
275
276 function user_print_profile()
277 {
278         return $_SESSION["wa_current_user"]->prefs->print_profile();
279 }
280
281 function user_rep_popup()
282 {
283         return $_SESSION["wa_current_user"]->prefs->rep_popup();
284 }
285
286 function user_query_size()
287 {
288         return $_SESSION["wa_current_user"]->prefs->query_size();
289 }
290
291 function user_graphic_links()
292 {
293         return $_SESSION["wa_current_user"]->prefs->graphic_links();
294 }
295
296 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
297         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
298         $print_profile, $rep_popup, $query_size, $graphic_links)
299 {
300
301         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
302                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize, $show_hints,
303                 $print_profile, $rep_popup, $query_size, $graphic_links);
304 }
305
306 function add_user_js_data() {
307         global $path_to_root, $thoseps, $decseps;
308
309         $ts = $thoseps[user_tho_sep()];
310         $ds = $decseps[user_dec_sep()];
311
312     $js = "\n<script type=\"text/javascript\">\n"
313           . "<!--\n"
314           . "var user = {\n"
315           . "theme: '". $path_to_root . '/themes/'. 'default' /*user_theme()*/.'/'."',\n"
316           . "loadtxt: '"._('Requesting data...')."',\n"
317           . "ts: '$ts',\n"
318           . "ds: '$ds',\n"
319           . "pdec : " . user_price_dec() . "}\n--></script>";
320
321   add_js_source($js);
322 }
323
324 //--------------------------------------------------------------------------
325
326 ?>