JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added real logins, login administration, task viewer page, contractor main page
[contractor-progress.git] / tasks.php
1 <?php
2
3 require_once('code/tasks.php');
4
5 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
6
7 define('TASKS_DB_FIELDS', 'title,url,description,state');
8
9
10 require_once('code/wfpl/template.php');
11 require_once('code/wfpl/format.php');
12 require_once('code/wfpl/messages.php');
13 require_once('code/wfpl/email.php');
14 require_once('code/db_connect.php');
15
16 # replace every character in $str with "&nbsp;"
17 function tonbsp($matches) {
18         return str_repeat('&nbsp;', strlen($matches[0]) * 2);
19 }
20
21 # encode as html, make it display newlines and leading spaces
22 function enc_htmlbrtab($str) {
23         $str = enc_htmlbr($str);
24         $str = preg_replace_callback("|^ *|m", tonbsp, $str);
25         return $str;
26 }
27
28 function tasks_get_fields() {
29         $title = format_oneline($_REQUEST['title']);
30         $url = format_oneline($_REQUEST['url']);
31         $description = format_unix($_REQUEST['description']);
32
33         tasks_tem_sets($title, $url, $description);
34
35         return array($title, $url, $description);
36 }
37
38 function tasks_tem_sets($title, $url, $description) {
39         tem_set('title', $title);
40         tem_set('url', $url);
41         tem_set('description', $description);
42 }
43
44 function tasks_main() {
45         if(!logged_in()) {
46                 $GLOBALS['url'] = this_url();
47                 return 'login';
48         }
49
50         if(isset($_REQUEST['tasks_id'])) {
51                 $ret = tasks_display_main();
52                 if($ret) {
53                         return $ret;
54                 }
55                 tem_show('display_body');
56         } else {
57                 $ret = tasks_edit_main();
58                 if($ret) {
59                         return $ret;
60                 }
61                 tem_show('edit_body');
62         }
63
64         tem_show('main_body');
65 }
66
67 function tasks_display_main() {
68         $task_id = format_int($_REQUEST['tasks_id']);;
69         $row = db_get_row('tasks', 'title,url,description,state', 'where id=%i', $task_id);
70         if($row) {
71                 list($title, $url, $description, $state) = $row;
72                 tem_set('task_id', $task_id);
73                 tem_set('title', $title);
74                 tem_set('url', $url);
75                 tem_set('description', $description);
76                 tem_set('state', task_state_pretty($state));
77                 if($state == TASK_BUG) {
78                         tem_show('bug_title');
79                 } else {
80                         tem_show('normal_title');
81                 }
82         } else {
83                 message("Task #$task_id not found");
84                 return './';
85         }
86 }
87
88 function tasks_edit_main() {
89         $state = TASK_DRAFT; # will be overwritten
90         $edit_id = format_int($_REQUEST['tasks_edit_id']);
91         unset($_REQUEST['tasks_edit_id']);
92         if($edit_id) {
93                 # add hidden field for database id of row we're editing
94                 tem_set('tasks_edit_id', $edit_id);
95                 tem_show('editing');
96
97                 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
98         }
99
100         if(isset($_REQUEST['tasks_new_bug'])) {
101                 tem_show('bug_submit');
102                 $state = TASK_BUG;
103         } else {
104                 tem_show('normal_submits');
105         }
106
107         if($state == TASK_BUG) {
108                 tem_show('bug_instructions');
109         } else {
110                 tem_show('normal_instructions');
111         }
112
113         $delete_id = format_int($_REQUEST['tasks_delete_id']);
114         unset($_REQUEST['tasks_delete_id']);
115         if($delete_id) {
116                 db_delete('tasks', 'where id=%i', $delete_id);
117                 message('Task deleted.');
118
119                 return './tasks.html';
120         }
121
122         if(isset($_REQUEST['title'])) {
123                 list($title, $url, $description) = tasks_get_fields();
124
125                 # FIXME
126                 if(isset($_REQUEST['save_draft'])) {
127                         $state = TASK_DRAFT;
128                 } elseif(isset($_REQUEST['save_bug'])) {
129                         $state = TASK_BUG;
130                 } else {
131                         $state = TASK_NEEDS_QUOTE;
132                 }
133
134                 if("you're happy with the POSTed values") {
135                         if($edit_id) {
136                                 db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, 'where id=%i', $edit_id);
137                                 message('Task updated.');
138                         } else {
139                                 # new task
140                                 $paid = 0;
141                                 $client_id = 4; # FIXME
142                                 db_insert('tasks', 'client_id,title,url,description,state,paid', $client_id, $title, $url, $description, $state, $paid);
143                                 message('Task saved.');
144                         }
145                         if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
146                                 $to = $GLOBALS['tasks_form_recipient'];
147                                 $from = $to;
148                                 $reply_to = '';
149                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
150                                         $reply_to = $_REQUEST['email'];
151                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
152                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
153                                         }
154                                 }
155                                 $subject = 'tasks form submitted';
156                                 $message = tem_run('tasks.email.txt');
157                                 $cc = '';
158                                 $bcc = '';
159                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
160                                         message('Due to an internal error, your message could not be sent. Please try again later.');
161                                         $error = true;
162                                 }
163                         }
164                         if($error !== true) {
165                                 return './';
166                         }
167                 }
168                 # otherwise, we display the form again. tasks_get_fields() has
169                 # already put the posted values back into the template engine, so they will
170                 # show up in the form fields. You should add some message asking people to
171                 # fix their entry in whatever way you require.
172         } elseif($edit_id) {
173                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
174                 list($client_id, $ord, $title, $url, $description, $state, $paid) = db_get_row('tasks', TASKS_DB_FIELDS, 'where id=%i', $edit_id);
175                 tasks_tem_sets($client_id, $ord, $title, $url, $description, $state, $paid);
176         } else {
177                 # form not submitted, you can set default values like so:
178                 #tem_set('client_id', 'Yes');
179         }
180
181         # this has to be later in the file because it requres that client_id be set already
182         if($edit_id) {
183                 tem_show('edit_msg');
184         } elseif($state == TASK_BUG) {
185                 tem_show('bug_msg');
186         } else {
187                 tem_show('new_msg');
188         }
189 }
190
191 ?>