JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added licensing information
[contractor-progress.git] / tasks.php
index 0443889..779cb31 100644 (file)
--- a/tasks.php
+++ b/tasks.php
@@ -1,10 +1,24 @@
 <?php
 
+#  Copyright (C) 2008  Jason Woofenden
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU Affero General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU Affero General Public License for more details.
+#
+#  You should have received a copy of the GNU Affero General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 require_once('code/tasks.php');
 
 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
 
-define('TASKS_DB_FIELDS', 'title,url,description,state');
 
 
 require_once('code/wfpl/template.php');
@@ -13,37 +27,212 @@ require_once('code/wfpl/messages.php');
 require_once('code/wfpl/email.php');
 require_once('code/db_connect.php');
 
+function description_has_fixmes($description) {
+       return (strpos($description, 'FIXME') !== false);
+}
+
+# replace every character in $str with "&nbsp;"
+function tonbsp($matches) {
+       return str_repeat('&nbsp;', strlen($matches[0]) * 2);
+}
+
+# encode as html, make it display newlines and leading spaces
+function enc_htmlbrtab($str) {
+       $str = enc_htmlbr($str);
+       $str = preg_replace_callback("|^ *|m", tonbsp, $str);
+       return $str;
+}
+
 function tasks_get_fields() {
        $title = format_oneline($_REQUEST['title']);
        $url = format_oneline($_REQUEST['url']);
        $description = format_unix($_REQUEST['description']);
+       $price = format_decimal($_REQUEST['price']);
 
-       tasks_tem_sets($title, $url, $description);
+       tasks_tem_sets($title, $url, $description, $price);
 
-       return array($title, $url, $description);
+       return array($title, $url, $description, $price);
 }
 
-function tasks_tem_sets($title, $url, $description) {
+function tasks_tem_sets($title, $url, $description, $price) {
        tem_set('title', $title);
        tem_set('url', $url);
        tem_set('description', $description);
+       tem_set('price', $price);
 }
 
 function tasks_main() {
-       $ret = _tasks_main();
-       if($ret) {
-               return $ret;
+       if(!logged_in()) {
+               $GLOBALS['url'] = this_url();
+               return 'login';
+       }
+
+       if(isset($_REQUEST['tasks_id'])) {
+               $ret = tasks_display_main();
+               if($ret) {
+                       return $ret;
+               }
+               tem_show('display_body');
+       } else {
+               $ret = tasks_edit_main();
+               if($ret) {
+                       return $ret;
+               }
+               tem_show('edit_body');
        }
+
        tem_show('main_body');
 }
 
-function _tasks_main() {
+function tasks_display_main() {
+       $task_id = format_int($_REQUEST['tasks_id']);;
+       $client_id = logged_in();
+       if(logged_in_as_contractor()) {
+               $row = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $task_id);
+       } else {
+               $row = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i && client_id=%i', $task_id, $client_id);
+       }
+       if($row) {
+               list($title, $url, $description, $state, $price) = $row;
+               tem_set('task_id', $task_id);
+               tem_set('title', $title);
+               tem_set('url', $url);
+               tem_set('description', $description);
+               tem_set('state', task_state_pretty($state));
+               tem_set('price', $price);
+               if($state == TASK_BUG) {
+                       tem_show('bug_title');
+               } else {
+                       tem_show('normal_title');
+               }
+               if(logged_in_as_contractor()) {
+                       switch($state) {
+                               case TASK_DRAFT:
+                               case TASK_NEEDS_CLARIFICATION:
+                               case TASK_NEEDS_QUOTE:
+                               case TASK_BUG:
+                               case TASK_NEEDS_GO_AHEAD:
+                                       tem_show('normal_edit_link');
+                                       tem_show('price_row');
+                               break;
+                               case TASK_QUEUED:
+                                       tem_show('normal_edit_link');
+                                       tem_show('working_link');
+                                       tem_show('price_row');
+                               case TASK_WORKING:
+                                       tem_show('price_row');
+                                       tem_show('needs_testing_link');
+                               break;
+                               case TASK_NEEDS_TESTING:
+                               case TASK_FINISHED:
+                                       tem_show('price_row');
+                               break;
+                       }
+               } else {
+                       switch($state) {
+                               case TASK_DRAFT:
+                               case TASK_NEEDS_CLARIFICATION:
+                               case TASK_NEEDS_QUOTE:
+                               case TASK_BUG:
+                                       tem_show('normal_edit_link');
+                               break;
+                               case TASK_NEEDS_GO_AHEAD:
+                                       tem_show('price_row');
+                                       tem_show('approve_price_link');
+                                       tem_show('normal_edit_link');
+                               break;
+                               case TASK_QUEUED:
+                                       tem_show('price_row');
+                                       tem_show('warning_edit_link');
+                               break;
+                               case TASK_WORKING:
+                                       tem_show('price_row');
+                               break;
+                               case TASK_NEEDS_TESTING:
+                                       tem_show('price_row');
+                                       tem_show('finished_link');
+                               break;
+                               case TASK_FINISHED:
+                                       tem_show('price_row');
+                               break;
+                       }
+               }
+       } else {
+               message("Task #$task_id not found");
+               return './';
+       }
+}
+
+function tasks_edit_main() {
+       $state = TASK_DRAFT; # will be overwritten
+       $client_id = logged_in(); # fixed shortly if we're contractor
        $edit_id = format_int($_REQUEST['tasks_edit_id']);
        unset($_REQUEST['tasks_edit_id']);
        if($edit_id) {
+               $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
+               if(logged_in_as_contractor()) {
+                       $client_id = $owner;
+               } elseif($owner != $client_id) {
+                       message('Sorry, that task was entered by/for another client.');
+                       return './';
+               }
+
                # add hidden field for database id of row we're editing
                tem_set('tasks_edit_id', $edit_id);
                tem_show('editing');
+
+               $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
+       }
+
+       if(isset($_REQUEST['tasks_new_bug'])) {
+               $state = TASK_BUG;
+       }
+
+       if(isset($_REQUEST['tasks_approve_price_id'])) {
+               $id = $_REQUEST['tasks_approve_price_id'];
+               $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
+               if(logged_in() != $owner) {
+                       message("Error: can't approve a task entered by/for another client.");
+                       return './';
+               }
+               db_update('tasks', 'state', TASK_QUEUED, 'where id=%i', $id);
+               message('Price approved.');
+               return './';
+       }
+
+       if(isset($_REQUEST['tasks_working_id'])) {
+               $id = $_REQUEST['tasks_working_id'];
+               if(!logged_in_as_contractor()) {
+                       message("Error: only Jason can say what he's working on.");
+                       return './';
+               }
+               db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
+               message('OK, client locked out of modifying that one.');
+               return './';
+       }
+
+       if(isset($_REQUEST['tasks_needs_testing_id'])) {
+               $id = $_REQUEST['tasks_needs_testing_id'];
+               if(!logged_in_as_contractor()) {
+                       message("Error: only Jason can say when he's done.");
+                       return './';
+               }
+               db_update('tasks', 'state', TASK_NEEDS_TESTING, 'where id=%i', $id);
+               message('Task awaits testing.');
+               return './';
+       }
+
+       if(isset($_REQUEST['tasks_finished_id'])) {
+               $id = $_REQUEST['tasks_finished_id'];
+               $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
+               if(logged_in() != $owner) {
+                       message("Error: can't test a task entered by/for another client.");
+                       return './';
+               }
+               db_update('tasks', 'state', TASK_FINISHED, 'where id=%i', $id);
+               message('Task marked as finished.');
+               # FIXME also mark it as paid if client's balance can cover it
+               return './';
        }
 
        $delete_id = format_int($_REQUEST['tasks_delete_id']);
@@ -55,29 +244,46 @@ function _tasks_main() {
                return './tasks.html';
        }
 
-       if(!$edit_id) {
-               tem_show('new_msg');
-       }
-
        if(isset($_REQUEST['title'])) {
-               list($title, $url, $description) = tasks_get_fields();
+               list($title, $url, $description, $price) = tasks_get_fields();
+
+               # FIXME
+               if(isset($_REQUEST['save_draft'])) {
+                       $state = TASK_DRAFT;
+               } elseif(isset($_REQUEST['save_bug'])) {
+                       $state = TASK_BUG;
+               } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
+                       $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
+                       if($price < $tiny_agreement) {
+                               $state = TASK_QUEUED;
+                       } else {
+                               $state = TASK_NEEDS_GO_AHEAD;
+                       }
+               } elseif(isset($_REQUEST['needs_clarification'])) {
+                       $state = TASK_NEEDS_CLARIFICATION;
+               } else { # better be "request_price"
+                       if(description_has_fixmes($description)) {
+                               $state = TASK_NEEDS_CLARIFICATION;
+                               message('The description is not ready to be priced yet because it still contains at least one "FIXME".');
+                       } else {
+                               $state = TASK_NEEDS_QUOTE;
+                       }
+               }
 
                if("you're happy with the POSTed values") {
                        if($edit_id) {
-                               db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, $paid = 0, 'where id=%i', $edit_id);
+                               if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
+                                       db_update('tasks', 'title,url,description,state,price', $title, $url, $description, $state, $price, 'where id=%i', $edit_id);
+                               } else {
+                                       db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, 'where id=%i', $edit_id);
+                               }
                                message('Task updated.');
                        } else {
                                # new task
                                $paid = 0;
-                               if(isset($_REQUEST['save_draft'])) {
-                                       $state = TASK_DRAFT;
-                               } else {
-                                       $state = TASK_NEEDS_QUOTE;
-                               }
-                               $client_id = 4; # FIXME
+                               $client_id = logged_in();
                                db_insert('tasks', 'client_id,title,url,description,state,paid', $client_id, $title, $url, $description, $state, $paid);
                                message('Task saved.');
-                               return './';
                        }
                        if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
                                $to = $GLOBALS['tasks_form_recipient'];
@@ -99,8 +305,7 @@ function _tasks_main() {
                                }
                        }
                        if($error !== true) {
-                               tem_show('thankyou');
-                               return;
+                               return './';
                        }
                }
                # otherwise, we display the form again. tasks_get_fields() has
@@ -109,19 +314,43 @@ function _tasks_main() {
                # fix their entry in whatever way you require.
        } elseif($edit_id) {
                # we've recieved an edit id, but no data. So we grab the values to be edited from the database
-               list($client_id, $ord, $title, $url, $description, $state, $paid) = db_get_row('tasks', TASKS_DB_FIELDS, 'where id=%i', $edit_id);
-               tasks_tem_sets($client_id, $ord, $title, $url, $description, $state, $paid);
+               list($title, $url, $description, $state, $paid) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
+               tasks_tem_sets($title, $url, $description, $price);
        } else {
                # form not submitted, you can set default values like so:
                #tem_set('client_id', 'Yes');
        }
 
-       # this has to be later in the file because it requres that client_id be set already
+       # display header
        if($edit_id) {
                tem_show('edit_msg');
+       } elseif($state == TASK_BUG) {
+               tem_show('bug_msg');
+       } else {
+               tem_show('new_msg');
        }
 
-       tem_show('form');
+       # display instructions
+       if($state == TASK_BUG) {
+               tem_show('bug_instructions');
+               if(logged_in_as_contractor()) {
+                       tem_show('price_field');
+                       tem_show('contractor_submits');
+               } else {
+                       tem_show('bug_submit');
+               }
+       } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
+               tem_show('set_price_instructions');
+               tem_show('price_field');
+               tem_show('contractor_submits');
+       } else {
+               if(description_has_fixmes($description)) {
+                       tem_show('fixme_instructions');
+               } else {
+                       tem_show('normal_instructions');
+               }
+               tem_show('normal_submits');
+       }
 }
 
 ?>