Merging version 2.1 RC to main trunk.
[fa-stable.git] / admin / users.php
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 $page_security=15;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Users"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20
21 include_once($path_to_root . "/admin/db/users_db.inc");
22
23 simple_page_mode(false);
24 //-------------------------------------------------------------------------------------------------
25
26 function can_process() 
27 {
28
29         if (strlen($_POST['user_id']) < 4)
30         {
31                 display_error( _("The user login entered must be at least 4 characters long."));
32                 set_focus('user_id');
33                 return false;
34         }
35
36         if ($_POST['password'] != "") 
37         {
38         if (strlen($_POST['password']) < 4)
39         {
40                 display_error( _("The password entered must be at least 4 characters long."));
41                         set_focus('password');
42                 return false;
43         }
44
45         if (strstr($_POST['password'], $_POST['user_id']) != false)
46         {
47                 display_error( _("The password cannot contain the user login."));
48                         set_focus('password');
49                 return false;
50         }
51         }
52
53         return true;
54 }
55
56 //-------------------------------------------------------------------------------------------------
57
58 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
59 {
60
61         if (can_process())
62         {
63         if ($selected_id != '') 
64         {
65                 update_user($_POST['user_id'], $_POST['real_name'], $_POST['phone'],
66                         $_POST['email'], $_POST['Access'], $_POST['language'], 
67                                 $_POST['profile'], check_value('rep_popup'), $_POST['pos']);
68
69                 if ($_POST['password'] != "")
70                         update_user_password($_POST['user_id'], md5($_POST['password']));
71
72                 display_notification_centered(_("The selected user has been updated."));
73         } 
74         else 
75         {
76                 add_user($_POST['user_id'], $_POST['real_name'], md5($_POST['password']),
77                                 $_POST['phone'], $_POST['email'], $_POST['Access'], $_POST['language'],
78                                 $_POST['profile'], check_value('rep_popup'), $_POST['pos']);
79
80                         // use current user display preferences as start point for new user
81                         update_user_display_prefs($_POST['user_id'], 
82                                 user_price_dec(), user_qty_dec(), user_exrate_dec(), 
83                                 user_percent_dec(), user_show_gl_info(), user_show_codes(), 
84                                 user_date_format(), user_date_sep(), user_tho_sep(), 
85                                 user_dec_sep(), user_theme(), user_pagesize(), user_hints(), 
86                                 $_POST['profile'], check_value('rep_popup'), user_query_size(), 
87                                 user_graphic_links(), $_POST['language']);
88
89                         display_notification_centered(_("A new user has been added."));
90         }
91                 $Mode = 'RESET';
92         }
93 }
94
95 //-------------------------------------------------------------------------------------------------
96
97 if ($Mode == 'Delete')
98 {
99         delete_user($selected_id);
100         display_notification_centered(_("User has been deleted."));
101         $Mode = 'RESET';
102 }
103
104 //-------------------------------------------------------------------------------------------------
105 if ($Mode == 'RESET')
106 {
107         $selected_id = '';
108         unset($_POST); // clean all input fields
109 }
110
111 $result = get_users();
112 start_form();
113 start_table($table_style);
114
115 if ($_SESSION["wa_current_user"]->access == 2)
116         $th = array(_("User login"), _("Full Name"), _("Phone"),
117                 _("E-mail"), _("Last Visit"), _("Access Level"), "", "");
118 else            
119         $th = array(_("User login"), _("Full Name"), _("Phone"),
120                 _("E-mail"), _("Last Visit"), _("Access Level"), "");
121 table_header($th);      
122
123 $k = 0; //row colour counter
124
125 while ($myrow = db_fetch($result)) 
126 {
127
128         alt_table_row_color($k);
129
130         $last_visit_date = sql2date($myrow["last_visit_date"]);
131
132         /*The security_headings array is defined in config.php */
133
134         label_cell($myrow["user_id"]);
135         label_cell($myrow["real_name"]);
136         label_cell($myrow["phone"]);
137         email_cell($myrow["email"]);
138         label_cell($last_visit_date, "nowrap");
139         label_cell($security_headings[$myrow["full_access"]]);
140         edit_button_cell("Edit".$myrow["user_id"], _("Edit"));
141     if (strcasecmp($myrow["user_id"], $_SESSION["wa_current_user"]->username) &&
142         $_SESSION["wa_current_user"]->access == 2)
143                 delete_button_cell("Delete".$myrow["user_id"], _("Delete"));
144         else
145                 label_cell('');
146         end_row();
147
148 } //END WHILE LIST LOOP
149
150 end_table();
151 end_form();
152 echo '<br>';
153
154 //-------------------------------------------------------------------------------------------------
155 start_form();
156
157 start_table($table_style2);
158
159 $_POST['email'] = "";
160 if ($selected_id != '') 
161 {
162         if ($Mode == 'Edit') {
163                 //editing an existing User
164                 $myrow = get_user($selected_id);
165
166                 $_POST['user_id'] = $myrow["user_id"];
167                 $_POST['real_name'] = $myrow["real_name"];
168                 $_POST['phone'] = $myrow["phone"];
169                 $_POST['email'] = $myrow["email"];
170                 $_POST['Access'] = $myrow["full_access"];
171                 $_POST['language'] = $myrow["language"];
172                 $_POST['profile'] = $myrow["print_profile"];
173                 $_POST['rep_popup'] = $myrow["rep_popup"];
174                 $_POST['pos'] = $myrow["pos"];
175         }
176         hidden('selected_id', $selected_id);
177         hidden('user_id');
178
179         start_row();
180         label_row(_("User login:"), $_POST['user_id']);
181
182 else 
183 { //end of if $selected_id only do the else when a new record is being entered
184         text_row(_("User Login:"), "user_id",  null, 22, 20);
185         $_POST['language'] = user_language();
186         $_POST['profile'] = user_print_profile();
187         $_POST['rep_popup'] = user_rep_popup();
188         $_POST['pos'] = user_pos();
189 }
190 $_POST['password'] = "";
191 start_row();
192 label_cell(_("Password:"));
193 label_cell("<input type='password' name='password' size=22 maxlength=20 value='" . $_POST['password'] . "'>");
194 end_row();
195
196 if ($selected_id != '') 
197 {
198         table_section_title(_("Enter a new password to change, leave empty to keep current."));
199 }
200
201 text_row_ex(_("Full Name").":", 'real_name',  50);
202
203 text_row_ex(_("Telephone No.:"), 'phone', 30);
204
205 email_row_ex(_("Email Address:"), 'email', 50);
206
207 security_headings_list_row(_("Access Level:"), 'Access', null); 
208
209 languages_list_row(_("Language:"), 'language', null);
210
211 pos_list_row(_("User's POS"). ':', 'pos', null);
212
213 print_profiles_list_row(_("Printing profile"). ':', 'profile', null,
214         _('Browser printing support'));
215
216 check_row(_("Use popup window for reports:"), 'rep_popup', $_POST['rep_popup'],
217         false, _('Set this option to on if your browser directly supports pdf files'));
218
219 end_table(1);
220
221 submit_add_or_update_center($selected_id == '', '', true);
222
223 end_form();
224 end_page();
225 ?>