JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made css_mangler.php handle versioning (strips numerical postfixes) and made contract...
[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 function tasks_get_fields() {
35         $title = format_oneline($_REQUEST['title']);
36         $url = format_oneline($_REQUEST['url']);
37         $description = format_unix($_REQUEST['description']);
38         $price = format_decimal($_REQUEST['price']);
39
40         tasks_tem_sets($title, $url, $description, $price);
41
42         return array($title, $url, $description, $price);
43 }
44
45 function tasks_tem_sets($title, $url, $description, $price) {
46         tem_set('title', $title);
47         tem_set('url', $url);
48         tem_set('description', $description);
49         tem_set('price', $price);
50 }
51
52 function tasks_main() {
53         if(!logged_in()) {
54                 $GLOBALS['url'] = this_url();
55                 return 'login';
56         }
57
58         if(isset($_REQUEST['tasks_id'])) {
59                 $ret = tasks_display_main();
60                 if($ret) {
61                         return $ret;
62                 }
63                 tem_show('display_body');
64         } else {
65                 $ret = tasks_edit_main();
66                 if($ret) {
67                         return $ret;
68                 }
69                 tem_show('edit_body');
70         }
71
72         tem_show('main_body');
73 }
74
75 function tasks_display_main() {
76         $task_id = format_int($_REQUEST['tasks_id']);;
77         $client_id = logged_in();
78         if(logged_in_as_contractor()) {
79                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id,paid,finished_at,tested_at', 'where id=%i', $task_id);
80         } else {
81                 $row = db_get_row('tasks', 'title,url,description,state,price,client_id,paid,finished_at,tested_at', 'where id=%i && client_id=%i', $task_id, $client_id);
82         }
83         if($row) {
84                 list($title, $url, $description, $state, $price, $owner_id, $paid, $finished_at, $tested_at) = $row;
85                 tem_set('task_id', $task_id);
86                 tem_set('title', $title);
87                 tem_set('url', $url);
88                 tem_set('description', $description);
89                 tem_set('state', task_state_pretty($state));
90                 tem_set('price', $price);
91                 if($finished_at) {
92                         tem_set('finished_at', $finished_at);
93                         tem_show('finished_at_section');
94                 }
95                 if($tested_at) {
96                         tem_set('tested_at', $tested_at);
97                         tem_show('tested_at_section');
98                 }
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                                         if($paid) {
133                                                 tem_show('marked_paid');
134                                         } else {
135                                                 tem_show('mark_paid_link');
136                                         }
137                                         tem_show('price_row');
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 define('MAX_PRIORITY', 2000000000);
176 define('MIN_PRIORITY', 0);
177 define('MID_PRIORITY', floor((MAX_PRIORITY - MIN_PRIORITY) / 2));
178
179 function new_lowest_priority($client_id) {
180         $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
181         if($lowest_ord === false) {
182                 return MID_PRIORITY;
183         }
184         if($lowest_ord == MIN_PRIORITY) {
185                 reprioritize_tasks($client_id); # make room
186                 $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
187         }
188         return MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
189 }
190
191 # keep everything in the same order, but space them out so there's room to squeeze things in anywhere
192 function reprioritize_tasks($client_id) {
193         $ids = db_get_column('tasks', 'id', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
194         $step = floor((MAX_PRIORITY - MIN_PRIORITY) / (count($ids) + 1));
195         $cur = MAX_PRIORITY;
196         foreach($ids as $id) {
197                 $cur -= $step;
198                 db_update('tasks', 'ord', $cur, 'where id=%i', $id);
199         }
200 }
201
202
203 # pass the task id and one of (up,down,top,bottom)
204 function prioritize_task($id, $change) {
205         $row = db_get_row('tasks', 'client_id,ord', 'where id=%i', $id);
206         if(!$row) {
207                 message('Database error #2242');
208                 return;
209         }
210         list($client_id, $ord) = $row;
211         switch($change) {
212                 case 'top':
213                         list($highest_id, $highest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
214                         if($highest_id == $id) {
215                                 message('Already highest priority');
216                                 return;
217                         }
218
219                         if($highest_ord == MAX_PRIORITY) {
220                                 reprioritize_tasks($client_id); # make room
221                                 $highest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
222                         }
223
224                         $new_ord = MAX_PRIORITY - floor((MAX_PRIORITY - $highest_ord) / 2);
225                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
226                         return;
227                 case 'bottom':
228                         list($lowest_id, $lowest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
229                         if($lowest_id == $id) {
230                                 message('Already lowest priority');
231                                 return $lowest_ord;
232                         }
233
234                         if($lowest_ord == MIN_PRIORITY) {
235                                 reprioritize_tasks($client_id); # make room
236                                 $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
237                         }
238
239                         $new_ord = MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
240                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
241                         return;
242                 case 'up':
243                 case 'down':
244                         if($change == 'up') {
245                                 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
246                                 if($rows[0][0] == $id) {
247                                         message('Already highest priority');
248                                         return;
249                                 }
250                                 if($rows[1][0] == $id) {
251                                         prioritize_task($id, 'top');
252                                         return;
253                                 }
254                         } else {
255                                 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc, id asc', $client_id, TASK_QUEUED);
256                                 if($rows[0][0] == $id) {
257                                         message('Already lowest priority');
258                                         return;
259                                 }
260                                 if($rows[1][0] == $id) {
261                                         prioritize_task($id, 'bottom');
262                                         return;
263                                 }
264                         }
265                         # find the one we're moving
266                         $cur_index = 0;
267                         $done = count($rows);
268                         for($i = 2; $i < $done ; ++$i) {
269                                 if($rows[$i][0] == $id) {
270                                         $cur_index = $i;
271                                         break;
272                                 }
273                         }
274                         $before_ord = $rows[$cur_index - 1][1];
275                         $before_before_ord = $rows[$cur_index - 2][1];
276                         if(abs($before_before_ord - $before_ord) < 2) {
277                                 reprioritize_tasks($client_id);
278                                 $before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 1][0]);
279                                 $before_before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 2][0]);
280                                 if($before_before_ord == $before_ord) {
281                                         message('Programmer error #8592');
282                                         return;
283                                 }
284                         }
285                         $new_ord = $before_ord + floor(($before_before_ord - $before_ord) / 2);
286                         db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
287                         return;
288                 default:
289                         message('invalid change');
290                         return;
291         }
292
293 }
294
295 function tasks_edit_main() {
296         $state = TASK_DRAFT; # will be overwritten
297         $client_id = logged_in(); # fixed shortly if we're contractor
298         $edit_id = format_int($_REQUEST['tasks_edit_id']);
299         unset($_REQUEST['tasks_edit_id']);
300         if($edit_id) {
301                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
302                 if(logged_in_as_contractor()) {
303                         $client_id = $owner;
304                 } elseif($owner != $client_id) {
305                         message('Sorry, that task was entered by/for another client.');
306                         return './';
307                 }
308
309                 # add hidden field for database id of row we're editing
310                 tem_set('tasks_edit_id', $edit_id);
311                 tem_show('editing');
312
313                 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
314         }
315
316         if(isset($_REQUEST['bump'])) {
317                 switch($_REQUEST['bump']) {
318                         case 'up':
319                         case 'down':
320                         case 'top':
321                         case 'bottom':
322                                 prioritize_task($edit_id, $_REQUEST['bump']);
323                                 return './';
324                 }
325         }
326
327         if(isset($_REQUEST['tasks_new_bug'])) {
328                 $state = TASK_BUG;
329         }
330
331         if(isset($_REQUEST['tasks_mark_paid_id'])) {
332                 if(!logged_in_as_contractor()) {
333                         message("Error: only Jason can mark tasks as paid.");
334                         return './';
335                 }
336                 $id = $_REQUEST['tasks_mark_paid_id'];
337                 db_update('tasks', 'paid', 1, 'where id=%i', $id);
338                 message('Marked as paid.');
339                 return './';
340         }
341
342         if(isset($_REQUEST['tasks_approve_price_id'])) {
343                 $id = $_REQUEST['tasks_approve_price_id'];
344                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);
345                 if(logged_in() != $owner) {
346                         message("Error: can't approve a task entered by/for another client.");
347                         return './';
348                 }
349                 $ord = new_lowest_priority($owner);
350                 db_update('tasks', 'state,ord', TASK_QUEUED, $ord, 'where id=%i', $id);
351                 message('Price approved.');
352                 return './';
353         }
354
355         if(isset($_REQUEST['tasks_working_id'])) {
356                 $id = $_REQUEST['tasks_working_id'];
357                 if(!logged_in_as_contractor()) {
358                         message("Error: only Jason can say what he's working on.");
359                         return './';
360                 }
361                 db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
362                 message('Task marked as "in progress".');
363                 return './tasks?tasks_id=' . $id;
364         }
365
366         if(isset($_REQUEST['tasks_needs_testing_id'])) {
367                 $id = $_REQUEST['tasks_needs_testing_id'];
368                 if(!logged_in_as_contractor()) {
369                         message("Error: only Jason can say when he's done.");
370                         return './';
371                 }
372                 db_update('tasks', 'state,finished_at', TASK_NEEDS_TESTING, date('Y-m-d'), 'where id=%i', $id);
373                 message('Task awaits testing.');
374                 return './';
375         }
376
377         if(isset($_REQUEST['tasks_finished_id'])) {
378                 $id = $_REQUEST['tasks_finished_id'];
379                 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
380                 if(logged_in() != $owner) {
381                         message("Error: can't test a task entered by/for another client.");
382                         return './';
383                 }
384                 db_update('tasks', 'state,tested_at', TASK_FINISHED, date('Y-m-d'), 'where id=%i', $id);
385                 message('Task marked as finished.');
386                 # FIXME also mark it as paid if client's balance can cover it
387                 return './';
388         }
389
390         $delete_id = format_int($_REQUEST['tasks_delete_id']);
391         unset($_REQUEST['tasks_delete_id']);
392         if($delete_id) {
393                 db_delete('tasks', 'where id=%i', $delete_id);
394                 message('Task deleted.');
395
396                 return './tasks.html';
397         }
398
399         if(isset($_REQUEST['title'])) {
400                 list($title, $url, $description, $price) = tasks_get_fields();
401                 $queuing = false;
402
403                 # FIXME
404                 if(isset($_REQUEST['save_draft'])) {
405                         $state = TASK_DRAFT;
406                 } elseif(isset($_REQUEST['save_bug'])) {
407                         $state = TASK_BUG;
408                 } elseif(isset($_REQUEST['save_price_no_tiny']) && logged_in_as_contractor()) {
409                         $state = TASK_NEEDS_GO_AHEAD;
410                 } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
411                         $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
412                         if($price <= $tiny_agreement) {
413                                 $state = TASK_QUEUED;
414                                 $queuing = true;
415                         } else {
416                                 $state = TASK_NEEDS_GO_AHEAD;
417                         }
418                 } elseif(isset($_REQUEST['needs_clarification'])) {
419                         $state = TASK_NEEDS_CLARIFICATION;
420                 } else { # better be "request_price"
421                         if(description_has_fixmes($description)) {
422                                 $state = TASK_NEEDS_CLARIFICATION;
423                                 message('The description is not ready to be priced yet because it still contains at least one "FIXME".');
424                         } else {
425                                 $state = TASK_NEEDS_QUOTE;
426                         }
427                 }
428
429                 if(!logged_in_as_contractor() || $_REQUEST['client_id']) { # if you change this change the one above
430                         if($edit_id) {
431                                 $tables = 'title,url,description,state';
432                                 $values = array($title, $url, $description, $state);
433                                 if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
434                                         $tables .= ',price';
435                                         array_push($values, $price);
436                                 }
437                                 if($queuing) {
438                                         $client_id = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
439                                         $tables .= ',ord';
440                                         array_push($values, new_lowest_priority($client_id));
441                                 }
442                                 db_update('tasks', $tables, $values, 'where id=%i', $edit_id);
443                                 message('Task updated.');
444                         } else {
445                                 # new task
446                                 $paid = 0;
447                                 $client_id = logged_in();
448                                 if(logged_in_as_contractor() && $_REQUEST['client_id']) {
449                                         $client_id = format_int($_REQUEST['client_id']);
450                                         $client_name = db_get_value('people', 'name', 'where id=%i', $client_id);
451                                 } else {
452                                         # if client entered the task, no price is set
453                                         $price = 0;
454                                 }
455                                 if($state == TASK_QUEUED) {
456                                         $ord = new_lowest_priority($client_id);
457                                 } else {
458                                         $ord = 0;
459                                 }
460                                 db_insert('tasks', 'client_id,title,url,description,state,paid,price,ord', $client_id, $title, $url, $description, $state, $paid, $price, $ord);
461                                 if(logged_in_as_contractor()) {
462                                         message("Task saved for $client_name.");
463                                 } else {
464                                         message('Task saved.');
465                                 }
466                         }
467                         if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
468                                 $to = $GLOBALS['tasks_form_recipient'];
469                                 $from = $to;
470                                 $reply_to = '';
471                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
472                                         $reply_to = $_REQUEST['email'];
473                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
474                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
475                                         }
476                                 }
477                                 $subject = 'tasks form submitted';
478                                 $message = tem_run('tasks.email.txt');
479                                 $cc = '';
480                                 $bcc = '';
481                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
482                                         message('Due to an internal error, your message could not be sent. Please try again later.');
483                                         $error = true;
484                                 }
485                         }
486                         if($error !== true) {
487                                 return './';
488                         }
489                 } else {
490                         message('Error: you must select a client for the task');
491                 }
492                 # otherwise, we display the form again. tasks_get_fields() has
493                 # already put the posted values back into the template engine, so they will
494                 # show up in the form fields. You should add some message asking people to
495                 # fix their entry in whatever way you require.
496         } elseif($edit_id) {
497                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
498                 list($title, $url, $description, $state, $paid) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
499                 tasks_tem_sets($title, $url, $description, $price);
500         } else {
501                 # form not submitted, you can set default values like so:
502                 #tem_set('client_id', 'Yes');
503         }
504
505         # display header
506         if($edit_id) {
507                 tem_show('edit_msg');
508         } elseif($state == TASK_BUG) {
509                 tem_show('bug_msg');
510         } else {
511                 tem_show('new_msg');
512                 if(logged_in_as_contractor()) {
513                         pulldown('client_id', db_get_rows('people', 'id,name', 'where id > 1'), PULLDOWN_2D);
514                         tem_set('client_id', format_int($_REQUEST['client_id']));
515                         tem_show('client_row');
516                 }
517         }
518
519         # display instructions
520         if($state == TASK_BUG) {
521                 tem_show('bug_instructions');
522                 if(logged_in_as_contractor()) {
523                         tem_show('price_field');
524                         tem_show('contractor_submits');
525                 } else {
526                         tem_show('bug_submit');
527                 }
528         } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
529                 tem_show('set_price_instructions');
530                 tem_show('price_field');
531                 tem_show('contractor_submits');
532         } else {
533                 if(description_has_fixmes($description)) {
534                         tem_show('fixme_instructions');
535                 } else {
536                         tem_show('normal_instructions');
537                 }
538                 if(logged_in_as_contractor()) {
539                         tem_show('contractor_submits');
540                         switch($state) {
541                                 case TASK_DRAFT:
542                                 case TASK_NEEDS_CLARIFICATION:
543                                 case TASK_NEEDS_QUOTE:
544                                 case TASK_NEEDS_GO_AHEAD:
545                                 case TASK_QUEUED:
546                                 case TASK_BUG:
547                                 tem_show('price_field');
548                         }
549                 } else {
550                         tem_show('normal_submits');
551                 }
552         }
553 }
554
555 ?>