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