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