JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added real logins, login administration, task viewer page, contractor main page
[contractor-progress.git] / people.php
1 <?php
2
3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
4
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
7 #
8 # http://jasonwoof.com/metaform/?form_name=people&opt_email=No&opt_db=Yes&opt_listing=Yes&opt_http_pass=No&fields=username+textbox%0D%0Apassword+textbox%0D%0Aname+textbox%0D%0Aprivs+int%0D%0Abalance+decimal&edit=yes
9
10
11 # SETUP
12
13 # To save results to a database, you'll need to create the people table
14 # (the file people.sql should help with this), and create the file
15 # 'code/db_connect.php' which calls db_connect() see:
16 # code/wfpl/examples/db_connect.php
17 #
18 # if you rename any of the database fields, you'll need to update this:
19
20 define('PEOPLE_DB_FIELDS', 'username,password,name,privs,balance');
21
22
23 require_once('code/wfpl/template.php');
24 require_once('code/wfpl/format.php');
25 require_once('code/wfpl/messages.php');
26 require_once('code/wfpl/email.php');
27 require_once('code/db_connect.php');
28
29 function people_get_fields() {
30         $username = format_oneline($_REQUEST['username']);
31         $password = format_oneline($_REQUEST['password']);
32         $name = format_oneline($_REQUEST['name']);
33         $privs = format_int($_REQUEST['privs']);
34         $balance = format_decimal($_REQUEST['balance']);
35
36         people_tem_sets($username, $password, $name, $privs, $balance);
37
38         return array($username, $password, $name, $privs, $balance);
39 }
40
41 function people_tem_sets($username, $password, $name, $privs, $balance) {
42         tem_set('username', $username);
43         tem_set('password', $password);
44         tem_set('name', $name);
45         tem_set('privs', $privs);
46         tem_set('balance', $balance);
47 }
48
49 # You may pass a "where clause" for the db query.
50 function people_display_listing($where = 'order by username limit 100') {
51         $rows = db_get_rows('people', 'id,username', $where);
52         if($rows == false || count($rows) == 0) {
53                 tem_show('empty_listing');
54                 tem_show('listings');
55                 return false;
56         }
57
58         foreach($rows as $row) {
59                 list($id, $username) = $row;
60                 tem_set('id', $id);
61                 if($username == '') {
62                         $username = '--';
63                 }
64                 tem_set('username', $username);
65                 tem_show('listing_row');
66         }
67         tem_show('populated_listing');
68         tem_show('listings');
69         return true;
70 }
71
72 function people_main() {
73         if(logged_in() != 1) { # FIXME get more sophisticated than first person in database is admin
74                 $GLOBALS['url'] = this_url();
75                 message('You must be logged in as an administrator to access that function');
76                 return 'login';
77         }
78
79         $ret = _people_main();
80         if($ret) {
81                 return $ret;
82         }
83         tem_show('main_body');
84 }
85
86 function _people_main() {
87         $edit_id = format_int($_REQUEST['people_edit_id']);
88         unset($_REQUEST['people_edit_id']);
89         if($edit_id) {
90                 # add hidden field for database id of row we're editing
91                 tem_set('people_edit_id', $edit_id);
92                 tem_show('editing');
93         }
94
95         $delete_id = format_int($_REQUEST['people_delete_id']);
96         unset($_REQUEST['people_delete_id']);
97         if($delete_id) {
98                 db_delete('people', 'where id=%i', $delete_id);
99                 message('Entry deleted.');
100
101                 return './people.html';
102         }
103
104         if(!$edit_id) {
105                 if(!isset($_REQUEST['people_new']) && !isset($_REQUEST['username'])) {
106                         people_display_listing();
107                         return;
108                 }
109                 
110                 tem_show('new_msg');
111         }
112
113         if(isset($_REQUEST['username'])) {
114                 list($username, $password, $name, $privs, $balance) = people_get_fields();
115
116                 if("you're happy with the POSTed values") {
117                         if(strlen($password) == 35 && substr($password, 32, 1) == ':') {
118                                 $password_hash = $password; # so we can edit a record, and leave the password be
119                         } else {
120                                 $password_hash = encrypt_password($password);
121                         }
122
123                         if($edit_id) {
124                                 db_update('people', PEOPLE_DB_FIELDS, $username, $password_hash, $name, $privs, $balance, 'where id=%i', $edit_id);
125                                 message('Entry updated.');
126                         } else {
127                                 db_insert('people', PEOPLE_DB_FIELDS, $username, $password_hash, $name, $privs, $balance);
128                                 message('Entry saved.');
129                         }
130                         if($error !== true) {
131                                 return './people';
132                         }
133                 }
134                 # otherwise, we display the form again. people_get_fields() has
135                 # already put the posted values back into the template engine, so they will
136                 # show up in the form fields. You should add some message asking people to
137                 # fix their entry in whatever way you require.
138         } elseif($edit_id) {
139                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
140                 list($username, $password, $name, $privs, $balance) = db_get_row('people', PEOPLE_DB_FIELDS, 'where id=%i', $edit_id);
141                 people_tem_sets($username, $password, $name, $privs, $balance);
142         } else {
143                 # form not submitted, you can set default values like so:
144                 #tem_set('username', 'Yes');
145         }
146
147         # this has to be later in the file because it requres that username be set already
148         if($edit_id) {
149                 tem_show('edit_msg');
150         }
151
152         tem_show('form');
153 }
154
155 ?>