A lot of annoying layout bugs taken. Mostly from views and inquiries.
[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                 } 
58                 else 
59                 {
60                         $this->logged = false;
61                 }
62
63                 return $this->logged;
64         }
65
66         function check_user_access()
67         {
68                 global $security_groups;
69                 return is_array($security_groups[$this->access]);
70         }
71
72         function can_access_page($page_level)
73         {
74                 global $security_groups;
75                 return isset($page_level) && in_array($page_level, $security_groups[$this->access]);
76         }
77
78         function get_db_connection()
79         {
80         global $db_connections;
81
82         $connection = $db_connections[$this->company];
83
84         //print_r($connection);
85
86         $db = mysql_connect($connection["host"] ,
87                 $connection["dbuser"], $connection["dbpassword"]);
88         mysql_select_db($connection["dbname"],$db);
89                 
90                 if (!defined('TB_PREF'))
91                         define('TB_PREF', $connection["tbpref"]);
92                 
93         return $db;
94         }
95
96         function update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
97                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize) {
98                 update_user_display_prefs($this->username, $price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl,
99                         $showcodes, $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize);
100
101                 // re-read the prefs
102                 $user = get_user($this->username);
103                 $this->prefs = new user_prefs($user);
104         }
105 }
106
107 //--------------------------------------------------------------------------
108
109 function number_format2($number, $decimals=0) 
110 {
111         global $thoseps, $decseps;
112         $tsep = $thoseps[$_SESSION["wa_current_user"]->prefs->tho_sep()];
113         $dsep = $decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()];
114         return number_format($number, $decimals, $dsep, $tsep);
115 }
116
117 function price_format($number) {
118     return number_format2($number, 
119         $_SESSION["wa_current_user"]->prefs->price_dec());
120 }
121
122 function qty_format($number) {
123     return number_format2($number, 
124         $_SESSION["wa_current_user"]->prefs->qty_dec());
125 }
126
127 function exrate_format($number) {
128     return number_format2($number, 
129         $_SESSION["wa_current_user"]->prefs->exrate_dec());
130 }
131
132 function percent_format($number) {
133     return number_format2($number, 
134         $_SESSION["wa_current_user"]->prefs->percent_dec());
135 }
136
137 function user_numeric($input) {
138     global $decseps, $thoseps;
139
140     $num = trim($input);
141     $sep = $thoseps[user_tho_sep()];
142     if($sep!='') $num = str_replace( $sep, '', $num);
143         str_replace($sep, '', $num);
144     $sep = $decseps[user_dec_sep()];
145     if($sep!='.') $num = str_replace( $sep, '.', $num);
146     
147     if (!is_numeric($num))
148           return false;
149     $num = (float)$num;
150     if ($num == (int)$num)
151           return (int)$num;
152     else
153           return $num;
154 }
155
156 function user_company() 
157 {
158         return $_SESSION["wa_current_user"]->company;
159 }
160
161 function user_language() 
162 {
163         return $_SESSION["wa_current_user"]->prefs->language();
164 }
165
166 function user_qty_dec() 
167 {
168         return $_SESSION["wa_current_user"]->prefs->qty_dec();
169 }
170
171 function user_price_dec() 
172 {
173         return $_SESSION["wa_current_user"]->prefs->price_dec();
174 }
175
176 function user_exrate_dec() 
177 {
178         return $_SESSION["wa_current_user"]->prefs->exrate_dec();
179 }
180
181 function user_percent_dec() 
182 {
183         return $_SESSION["wa_current_user"]->prefs->percent_dec();
184 }
185
186 function user_show_gl_info() 
187 {
188         return $_SESSION["wa_current_user"]->prefs->show_gl_info();
189 }
190
191 function user_show_codes() 
192 {
193         return $_SESSION["wa_current_user"]->prefs->show_codes();
194 }
195
196 function user_date_format() 
197 {
198         return $_SESSION["wa_current_user"]->prefs->date_format();
199 }
200
201 function user_date_display() 
202 {
203         return $_SESSION["wa_current_user"]->prefs->date_display();
204 }
205
206 function user_date_sep() 
207 {
208         return $_SESSION["wa_current_user"]->prefs->date_sep();
209 }
210
211 function user_tho_sep() 
212 {
213         return $_SESSION["wa_current_user"]->prefs->tho_sep();
214 }
215
216 function user_dec_sep() 
217 {
218         return $_SESSION["wa_current_user"]->prefs->dec_sep();
219 }
220
221 function user_theme() 
222 {
223         return $_SESSION["wa_current_user"]->prefs->get_theme();
224 }
225
226 function user_pagesize() 
227 {
228         return $_SESSION["wa_current_user"]->prefs->get_pagesize();
229 }
230
231 function set_user_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
232         $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize) 
233 {
234
235         $_SESSION["wa_current_user"]->update_prefs($price_dec, $qty_dec, $exrate_dec, $percent_dec, $showgl, $showcodes,
236                 $date_format, $date_sep, $tho_sep, $dec_sep, $theme, $pagesize);
237 }
238
239 function add_user_js_data() {
240         global $path_to_root, $thoseps, $decseps;
241
242         $ts = $thoseps[user_tho_sep()];
243         $ds = $decseps[user_dec_sep()];
244   
245     $js = "\n<script type=\"text/javascript\">\n"
246           . "<!--\n"
247           . "var user = {\n"
248           . "theme: '". $path_to_root . '/themes/'. 'default' /*user_theme()*/.'/'."',\n"
249           . "loadtxt: '"._('Requesting data...')."',\n"
250           . "ts: '$ts',\n"
251           . "ds: '$ds',\n"
252           . "pdec : " . user_price_dec() . "}\n--></script>";
253
254   add_js_source($js);
255 }
256
257 //--------------------------------------------------------------------------
258
259 ?>