3 # Copyright (C) 2008 Jason Woofenden
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.
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.
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/>.
18 require_once('code/tasks.php');
20 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
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');
30 function description_has_fixmes($description) {
31 return (strpos($description, 'FIXME') !== false);
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(\' \', strlen($matches[0]) * 2);');
38 $str = preg_replace_callback("|^ *|m", $space_to_nbsp, $str);
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']);
48 tasks_tem_sets($title, $url, $description, $price);
50 return array($title, $url, $description, $price);
53 function tasks_tem_sets($title, $url, $description, $price) {
54 tem_set('title', $title);
56 tem_set('description', $description);
57 tem_set('price', $price);
60 function tasks_main() {
62 $GLOBALS['url'] = this_url();
66 if(isset($_REQUEST['tasks_id'])) {
67 $ret = tasks_display_main();
71 tem_show('display_body');
73 $ret = tasks_edit_main();
77 tem_show('edit_body');
80 tem_show('main_body');
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);
89 $row = db_get_row('tasks', 'title,url,description,state,price,client_id', 'where id=%i && client_id=%i', $task_id, $client_id);
92 list($title, $url, $description, $state, $price, $owner_id) = $row;
93 tem_set('task_id', $task_id);
94 tem_set('title', $title);
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');
102 tem_show('normal_title');
104 if(logged_in_as_contractor()) {
107 case TASK_NEEDS_CLARIFICATION:
108 case TASK_NEEDS_QUOTE:
110 tem_show('normal_edit_link');
112 case TASK_NEEDS_GO_AHEAD:
113 tem_show('approve_price_link');
114 tem_show('normal_edit_link');
115 tem_show('price_row');
118 tem_show('normal_edit_link');
119 tem_show('working_link');
120 tem_show('price_row');
123 tem_show('price_row');
124 tem_show('needs_testing_link');
126 case TASK_NEEDS_TESTING:
127 if($owner_id == logged_in()) {
128 tem_show('finished_link');
132 tem_show('price_row');
133 tem_show('mark_paid_link'); # FIXME
139 case TASK_NEEDS_CLARIFICATION:
140 case TASK_NEEDS_QUOTE:
142 tem_show('normal_edit_link');
144 case TASK_NEEDS_GO_AHEAD:
145 tem_show('price_row');
146 tem_show('approve_price_link');
147 tem_show('normal_edit_link');
150 tem_show('price_row');
151 tem_show('warning_edit_link');
154 tem_show('price_row');
156 case TASK_NEEDS_TESTING:
157 tem_show('price_row');
158 tem_show('finished_link');
161 tem_show('price_row');
166 message("Task #$task_id not found");
171 define('MAX_PRIORITY', 20);
172 define('MIN_PRIORITY', 0);
173 define('MID_PRIORITY', floor((MAX_PRIORITY - MIN_PRIORITY) / 2));
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) {
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);
184 return MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
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));
192 foreach($ids as $id) {
194 db_update('tasks', 'ord', $cur, 'where id=%i', $id);
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);
203 message('Database error #2242');
206 list($client_id, $ord) = $row;
210 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);
211 if($highest_id == $id) {
212 message('Already highest priority');
216 if($highest_ord == MAX_PRIORITY) {
217 reprioritize_tasks($client_id); # make room
218 $highest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
221 $new_ord = MAX_PRIORITY - floor((MAX_PRIORITY - $highest_ord) / 2);
222 db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
225 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);
226 if($lowest_id == $id) {
227 message('Already lowest priority');
231 if($lowest_ord == MIN_PRIORITY) {
232 reprioritize_tasks($client_id); # make room
233 $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
236 $new_ord = MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
237 db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
241 if($change == 'up') {
242 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
243 if($rows[0][0] == $id) {
244 message('Already highest priority');
247 if($rows[1][0] == $id) {
248 prioritize_task($id, 'top');
252 $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc, id asc', $client_id, TASK_QUEUED);
253 if($rows[0][0] == $id) {
254 message('Already lowest priority');
257 if($rows[1][0] == $id) {
258 prioritize_task($id, 'bottom');
262 # find the one we're moving
264 $done = count($rows);
265 for($i = 2; $i < $done ; ++$i) {
266 if($rows[$i][0] == $id) {
271 $before_ord = $rows[$cur_index - 1][1];
272 $before_before_ord = $rows[$cur_index - 2][1];
273 if(abs($before_before_ord - $before_ord) < 2) {
274 reprioritize_tasks($client_id);
275 $before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 1][0]);
276 $before_before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 2][0]);
277 if($before_before_ord == $before_ord) {
278 message('Programmer error #8592');
282 $new_ord = $before_ord + floor(($before_before_ord - $before_ord) / 2);
283 db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
286 message('invalid change');
292 function tasks_edit_main() {
293 $state = TASK_DRAFT; # will be overwritten
294 $client_id = logged_in(); # fixed shortly if we're contractor
295 $edit_id = format_int($_REQUEST['tasks_edit_id']);
296 unset($_REQUEST['tasks_edit_id']);
298 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
299 if(logged_in_as_contractor()) {
301 } elseif($owner != $client_id) {
302 message('Sorry, that task was entered by/for another client.');
306 # add hidden field for database id of row we're editing
307 tem_set('tasks_edit_id', $edit_id);
310 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
313 if(isset($_REQUEST['bump'])) {
314 switch($_REQUEST['bump']) {
319 prioritize_task($edit_id, $_REQUEST['bump']);
324 if(isset($_REQUEST['tasks_new_bug'])) {
328 if(isset($_REQUEST['tasks_mark_paid_id'])) {
329 if(!logged_in_as_contractor()) {
330 message("Error: only Jason can mark tasks as paid.");
333 $id = $_REQUEST['tasks_mark_paid_id'];
334 db_update('tasks', 'paid', 1, 'where id=%i', $id);
335 message('Marked as paid.');
339 if(isset($_REQUEST['tasks_approve_price_id'])) {
340 $id = $_REQUEST['tasks_approve_price_id'];
341 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);
342 if(logged_in() != $owner) {
343 message("Error: can't approve a task entered by/for another client.");
346 db_update('tasks', 'state', TASK_QUEUED, 'where id=%i', $id);
347 message('Price approved.');
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.");
357 db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
358 message('OK, client locked out of modifying that one.');
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.");
368 db_update('tasks', 'state,finished_at', TASK_NEEDS_TESTING, date('Y-m-d'), 'where id=%i', $id);
369 message('Task awaits testing.');
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.");
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
386 $delete_id = format_int($_REQUEST['tasks_delete_id']);
387 unset($_REQUEST['tasks_delete_id']);
389 db_delete('tasks', 'where id=%i', $delete_id);
390 message('Task deleted.');
392 return './tasks.html';
395 if(isset($_REQUEST['title'])) {
396 list($title, $url, $description, $price) = tasks_get_fields();
399 if(isset($_REQUEST['save_draft'])) {
401 } elseif(isset($_REQUEST['save_bug'])) {
403 } elseif(isset($_REQUEST['save_price_no_tiny']) && logged_in_as_contractor()) {
404 $state = TASK_NEEDS_GO_AHEAD;
405 } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
406 $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
407 if($price <= $tiny_agreement) {
408 $state = TASK_QUEUED;
410 $state = TASK_NEEDS_GO_AHEAD;
412 } elseif(isset($_REQUEST['needs_clarification'])) {
413 $state = TASK_NEEDS_CLARIFICATION;
414 } else { # better be "request_price"
415 if(description_has_fixmes($description)) {
416 $state = TASK_NEEDS_CLARIFICATION;
417 message('The description is not ready to be priced yet because it still contains at least one "FIXME".');
419 $state = TASK_NEEDS_QUOTE;
423 if("you're happy with the POSTed values") { # if you change this change the one above
425 if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
426 db_update('tasks', 'title,url,description,state,price', $title, $url, $description, $state, $price, 'where id=%i', $edit_id);
428 db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, 'where id=%i', $edit_id);
430 message('Task updated.');
434 $client_id = logged_in();
435 if(logged_in_as_contractor() && $_REQUEST['client_id']) {
436 $client_id = format_int($_REQUEST['client_id']);
438 # if client entered the task, no price is set
441 $ord = new_lowest_priority($client_id);
442 db_insert('tasks', 'client_id,title,url,description,state,paid,price,ord', $client_id, $title, $url, $description, $state, $paid, $price, $ord);
443 message('Task saved.');
445 if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
446 $to = $GLOBALS['tasks_form_recipient'];
449 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
450 $reply_to = $_REQUEST['email'];
451 if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
452 $reply_to = "$_REQUEST[name] <$reply_to>";
455 $subject = 'tasks form submitted';
456 $message = tem_run('tasks.email.txt');
459 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
460 message('Due to an internal error, your message could not be sent. Please try again later.');
464 if($error !== true) {
468 # otherwise, we display the form again. tasks_get_fields() has
469 # already put the posted values back into the template engine, so they will
470 # show up in the form fields. You should add some message asking people to
471 # fix their entry in whatever way you require.
473 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
474 list($title, $url, $description, $state, $paid) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
475 tasks_tem_sets($title, $url, $description, $price);
477 # form not submitted, you can set default values like so:
478 #tem_set('client_id', 'Yes');
483 tem_show('edit_msg');
484 } elseif($state == TASK_BUG) {
488 if(logged_in_as_contractor()) {
489 pulldown('client_id', db_get_rows('people', 'id,name', 'where id > 1'), PULLDOWN_2D);
490 tem_set('client_id', format_int($_REQUEST['client_id']));
491 tem_show('client_row');
495 # display instructions
496 if($state == TASK_BUG) {
497 tem_show('bug_instructions');
498 if(logged_in_as_contractor()) {
499 tem_show('price_field');
500 tem_show('contractor_submits');
502 tem_show('bug_submit');
504 } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
505 tem_show('set_price_instructions');
506 tem_show('price_field');
507 tem_show('contractor_submits');
509 if(description_has_fixmes($description)) {
510 tem_show('fixme_instructions');
512 tem_show('normal_instructions');
514 if(logged_in_as_contractor()) {
515 tem_show('contractor_submits');
518 case TASK_NEEDS_CLARIFICATION:
519 case TASK_NEEDS_QUOTE:
520 case TASK_NEEDS_GO_AHEAD:
523 tem_show('price_field');
526 tem_show('normal_submits');