JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
contractor can enter tasks for clients without switching login
[contractor-progress.git] / tasks.php
1 <?php
2
3 #  Copyright (C) 2008  Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Affero General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Affero General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Affero General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 require_once('code/tasks.php');
19
20 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
21
22
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/format.php');
26 require_once('code/wfpl/messages.php');
27 require_once('code/wfpl/email.php');
28 require_once('code/db_connect.php');
29
30 function description_has_fixmes($description) {
31         return (strpos($description, 'FIXME') !== false);
32 }
33
34 # replace every character in $str with "&nbsp;"
35 function to_nbsp($matches) {
36         return str_repeat('&nbsp;', strlen($matches[0]) * 2);
37 }
38
39 # encode as html, make it display newlines and leading spaces
40 function enc_htmlbrtab($str) {
41         $str = enc_htmlbr($str);
42         $str = preg_replace_callback("|^ *|m", to_nbsp, $str);
43         return $str;
44 }
45
46 function tasks_get_fields() {
47         $title = format_oneline($_REQUEST['title']);
48         $url = format_oneline($_REQUEST['url']);
49         $description = format_unix($_REQUEST['description']);
50         $price = format_decimal($_REQUEST['price']);
51
52         tasks_tem_sets($title, $url, $description, $price);
53
54         return array($title, $url, $description, $price);
55 }
56
57 function tasks_tem_sets($title, $url, $description, $price) {
58         tem_set('title', $title);
59         tem_set('url', $url);
60         tem_set('description', $description);
61         tem_set('price', $price);
62 }
63
64 function tasks_main() {
65         if(!logged_in()) {
66                 $GLOBALS['url'] = this_url();
67                 return 'login';
68         }
69
70         if(isset($_REQUEST['tasks_id'])) {
71                 $ret = tasks_display_main();
72                 if($ret) {
73                         return $ret;
74                 }
75                 tem_show('display_body');
76         } else {
77                 $ret = tasks_edit_main();
78                 if($ret) {
79                         return $ret;
80                 }
81                 tem_show('edit_body');
82         }
83
84         tem_show('main_body');
85 }
86
87 function tasks_display_main() {
88         $task_id = format_int($_REQUEST['tasks_id']);;
89         $client_id = logged_in();
90         if(logged_in_as_contractor()) {
91                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id', 'where id=%i', $task_id);
92         } else {
93                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id', 'where id=%i && client_id=%i', $task_id, $client_id);
94         }
95         if($row) {
96                 list($title, $url, $description, $state, $price, $owner_id) = $row;
97                 tem_set('task_id', $task_id);
98                 tem_set('title', $title);
99                 tem_set('url', $url);
100                 tem_set('description', $description);
101                 tem_set('state', task_state_pretty($state));
102                 tem_set('price', $price);
103                 if($state == TASK_BUG) {
104                         tem_show('bug_title');
105                 } else {
106                         tem_show('normal_title');
107                 }
108                 if(logged_in_as_contractor()) {
109                         switch($state) {
110                                 case TASK_DRAFT:
111                                 case TASK_NEEDS_CLARIFICATION:
112                                 case TASK_NEEDS_QUOTE:
113                                 case TASK_BUG:
114                                         tem_show('normal_edit_link');
115                                 break;
116                                 case TASK_NEEDS_GO_AHEAD:
117                                         tem_show('approve_price_link');
118                                         tem_show('normal_edit_link');
119                                         tem_show('price_row');
120                                 break;
121                                 case TASK_QUEUED:
122                                         tem_show('normal_edit_link');
123                                         tem_show('working_link');
124                                         tem_show('price_row');
125                                 break;
126                                 case TASK_WORKING:
127                                         tem_show('price_row');
128                                         tem_show('needs_testing_link');
129                                 break;
130                                 case TASK_NEEDS_TESTING:
131                                         if($owner_id == logged_in()) {
132                                                 tem_show('finished_link');
133                                         }
134                                         # FALL THROUGH
135                                 case TASK_FINISHED:
136                                         tem_show('price_row');
137                                         tem_show('mark_paid_link'); # FIXME
138                                 break;
139                         }
140                 } else {
141                         switch($state) {
142                                 case TASK_DRAFT:
143                                 case TASK_NEEDS_CLARIFICATION:
144                                 case TASK_NEEDS_QUOTE:
145                                 case TASK_BUG:
146                                         tem_show('normal_edit_link');
147                                 break;
148                                 case TASK_NEEDS_GO_AHEAD:
149                                         tem_show('price_row');
150                                         tem_show('approve_price_link');
151                                         tem_show('normal_edit_link');
152                                 break;
153                                 case TASK_QUEUED:
154                                         tem_show('price_row');
155                                         tem_show('warning_edit_link');
156                                 break;
157                                 case TASK_WORKING:
158                                         tem_show('price_row');
159                                 break;
160                                 case TASK_NEEDS_TESTING:
161                                         tem_show('price_row');
162                                         tem_show('finished_link');
163                                 break;
164                                 case TASK_FINISHED:
165                                         tem_show('price_row');
166                                 break;
167                         }
168                 }
169         } else {
170                 message("Task #$task_id not found");
171                 return './';
172         }
173 }
174
175 function tasks_edit_main() {
176         $state = TASK_DRAFT; # will be overwritten
177         $client_id = logged_in(); # fixed shortly if we're contractor
178         $edit_id = format_int($_REQUEST['tasks_edit_id']);
179         unset($_REQUEST['tasks_edit_id']);
180         if($edit_id) {
181                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
182                 if(logged_in_as_contractor()) {
183                         $client_id = $owner;
184                 } elseif($owner != $client_id) {
185                         message('Sorry, that task was entered by/for another client.');
186                         return './';
187                 }
188
189                 # add hidden field for database id of row we're editing
190                 tem_set('tasks_edit_id', $edit_id);
191                 tem_show('editing');
192
193                 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
194         }
195
196         if(isset($_REQUEST['tasks_new_bug'])) {
197                 $state = TASK_BUG;
198         }
199
200         if(isset($_REQUEST['tasks_mark_paid_id'])) {
201                 if(!logged_in_as_contractor()) {
202                         message("Error: only Jason can mark tasks as paid.");
203                         return './';
204                 }
205                 $id = $_REQUEST['tasks_mark_paid_id'];
206                 db_update('tasks', 'paid', 1, 'where id=%i', $id);
207                 message('Marked as paid.');
208                 return './';
209         }
210
211         if(isset($_REQUEST['tasks_approve_price_id'])) {
212                 $id = $_REQUEST['tasks_approve_price_id'];
213                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);
214                 if(logged_in() != $owner) {
215                         message("Error: can't approve a task entered by/for another client.");
216                         return './';
217                 }
218                 db_update('tasks', 'state', TASK_QUEUED, 'where id=%i', $id);
219                 message('Price approved.');
220                 return './';
221         }
222
223         if(isset($_REQUEST['tasks_working_id'])) {
224                 $id = $_REQUEST['tasks_working_id'];
225                 if(!logged_in_as_contractor()) {
226                         message("Error: only Jason can say what he's working on.");
227                         return './';
228                 }
229                 db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
230                 message('OK, client locked out of modifying that one.');
231                 return './';
232         }
233
234         if(isset($_REQUEST['tasks_needs_testing_id'])) {
235                 $id = $_REQUEST['tasks_needs_testing_id'];
236                 if(!logged_in_as_contractor()) {
237                         message("Error: only Jason can say when he's done.");
238                         return './';
239                 }
240                 db_update('tasks', 'state,finished_at', TASK_NEEDS_TESTING, date('Y-m-d'), 'where id=%i', $id);
241                 message('Task awaits testing.');
242                 return './';
243         }
244
245         if(isset($_REQUEST['tasks_finished_id'])) {
246                 $id = $_REQUEST['tasks_finished_id'];
247                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
248                 if(logged_in() != $owner) {
249                         message("Error: can't test a task entered by/for another client.");
250                         return './';
251                 }
252                 db_update('tasks', 'state', TASK_FINISHED, 'where id=%i', $id);
253                 message('Task marked as finished.');
254                 # FIXME also mark it as paid if client's balance can cover it
255                 return './';
256         }
257
258         $delete_id = format_int($_REQUEST['tasks_delete_id']);
259         unset($_REQUEST['tasks_delete_id']);
260         if($delete_id) {
261                 db_delete('tasks', 'where id=%i', $delete_id);
262                 message('Task deleted.');
263
264                 return './tasks.html';
265         }
266
267         if(isset($_REQUEST['title'])) {
268                 list($title, $url, $description, $price) = tasks_get_fields();
269
270                 # FIXME
271                 if(isset($_REQUEST['save_draft'])) {
272                         $state = TASK_DRAFT;
273                 } elseif(isset($_REQUEST['save_bug'])) {
274                         $state = TASK_BUG;
275                 } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
276                         $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
277                         if($price < $tiny_agreement) {
278                                 $state = TASK_QUEUED;
279                         } else {
280                                 $state = TASK_NEEDS_GO_AHEAD;
281                         }
282                 } elseif(isset($_REQUEST['needs_clarification'])) {
283                         $state = TASK_NEEDS_CLARIFICATION;
284                 } else { # better be "request_price"
285                         if(description_has_fixmes($description)) {
286                                 $state = TASK_NEEDS_CLARIFICATION;
287                                 message('The description is not ready to be priced yet because it still contains at least one "FIXME".');
288                         } else {
289                                 $state = TASK_NEEDS_QUOTE;
290                         }
291                 }
292
293                 if("you're happy with the POSTed values") { # if you change this change the one above
294                         if($edit_id) {
295                                 if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
296                                         db_update('tasks', 'title,url,description,state,price', $title, $url, $description, $state, $price, 'where id=%i', $edit_id);
297                                 } else {
298                                         db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, 'where id=%i', $edit_id);
299                                 }
300                                 message('Task updated.');
301                         } else {
302                                 # new task
303                                 $paid = 0;
304                                 $client_id = logged_in();
305                                 if(logged_in_as_contractor() && $_REQUEST['client_id']) {
306                                         $client_id = format_int($_REQUEST['client_id']);
307                                 }
308                                 db_insert('tasks', 'client_id,title,url,description,state,paid', $client_id, $title, $url, $description, $state, $paid);
309                                 message('Task saved.');
310                         }
311                         if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
312                                 $to = $GLOBALS['tasks_form_recipient'];
313                                 $from = $to;
314                                 $reply_to = '';
315                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
316                                         $reply_to = $_REQUEST['email'];
317                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
318                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
319                                         }
320                                 }
321                                 $subject = 'tasks form submitted';
322                                 $message = tem_run('tasks.email.txt');
323                                 $cc = '';
324                                 $bcc = '';
325                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
326                                         message('Due to an internal error, your message could not be sent. Please try again later.');
327                                         $error = true;
328                                 }
329                         }
330                         if($error !== true) {
331                                 return './';
332                         }
333                 }
334                 # otherwise, we display the form again. tasks_get_fields() has
335                 # already put the posted values back into the template engine, so they will
336                 # show up in the form fields. You should add some message asking people to
337                 # fix their entry in whatever way you require.
338         } elseif($edit_id) {
339                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
340                 list($title, $url, $description, $state, $paid) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
341                 tasks_tem_sets($title, $url, $description, $price);
342         } else {
343                 # form not submitted, you can set default values like so:
344                 #tem_set('client_id', 'Yes');
345         }
346
347         # display header
348         if($edit_id) {
349                 tem_show('edit_msg');
350         } elseif($state == TASK_BUG) {
351                 tem_show('bug_msg');
352         } else {
353                 tem_show('new_msg');
354                 if(logged_in_as_contractor()) {
355                         pulldown('client_id', db_get_rows('people', 'id,name', 'where id > 1'), PULLDOWN_2D);
356                         tem_set('client_id', format_int($_REQUEST['client_id']));
357                         tem_show('client_row');
358                 }
359         }
360
361         # display instructions
362         if($state == TASK_BUG) {
363                 tem_show('bug_instructions');
364                 if(logged_in_as_contractor()) {
365                         tem_show('price_field');
366                         tem_show('contractor_submits');
367                 } else {
368                         tem_show('bug_submit');
369                 }
370         } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
371                 tem_show('set_price_instructions');
372                 tem_show('price_field');
373                 tem_show('contractor_submits');
374         } else {
375                 if(description_has_fixmes($description)) {
376                         tem_show('fixme_instructions');
377                 } else {
378                         tem_show('normal_instructions');
379                 }
380                 tem_show('normal_submits');
381         }
382 }
383
384 ?>