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 # replace every character in $str with " "
35 function tonbsp($matches) {
36 return str_repeat(' ', strlen($matches[0]) * 2);
39 # encode as html, make it display newlines and leading spaces
40 function enc_htmlbrtab($str) {
41 $str = enc_htmlbr($str);
42 $str = preg_replace_callback("|^ *|m", tonbsp, $str);
46 function tasks_get_fields() {
47 $title = format_oneline($_REQUEST['title']);
48 $url = format_oneline($_REQUEST['url']);
49 $description = format_unix($_REQUEST['description']);
50 $price = format_decimal($_REQUEST['price']);
52 tasks_tem_sets($title, $url, $description, $price);
54 return array($title, $url, $description, $price);
57 function tasks_tem_sets($title, $url, $description, $price) {
58 tem_set('title', $title);
60 tem_set('description', $description);
61 tem_set('price', $price);
64 function tasks_main() {
66 $GLOBALS['url'] = this_url();
70 if(isset($_REQUEST['tasks_id'])) {
71 $ret = tasks_display_main();
75 tem_show('display_body');
77 $ret = tasks_edit_main();
81 tem_show('edit_body');
84 tem_show('main_body');
87 function tasks_display_main() {
88 $task_id = format_int($_REQUEST['tasks_id']);;
89 $client_id = logged_in();
90 if(logged_in_as_contractor()) {
91 $row = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $task_id);
93 $row = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i && client_id=%i', $task_id, $client_id);
96 list($title, $url, $description, $state, $price) = $row;
97 tem_set('task_id', $task_id);
98 tem_set('title', $title);
100 tem_set('description', $description);
101 tem_set('state', task_state_pretty($state));
102 tem_set('price', $price);
103 if($state == TASK_BUG) {
104 tem_show('bug_title');
106 tem_show('normal_title');
108 if(logged_in_as_contractor()) {
111 case TASK_NEEDS_CLARIFICATION:
112 case TASK_NEEDS_QUOTE:
114 case TASK_NEEDS_GO_AHEAD:
115 tem_show('normal_edit_link');
116 tem_show('price_row');
119 tem_show('normal_edit_link');
120 tem_show('working_link');
121 tem_show('price_row');
123 tem_show('price_row');
124 tem_show('needs_testing_link');
126 case TASK_NEEDS_TESTING:
128 tem_show('price_row');
129 tem_show('mark_paid_link'); # FIXME
135 case TASK_NEEDS_CLARIFICATION:
136 case TASK_NEEDS_QUOTE:
138 tem_show('normal_edit_link');
140 case TASK_NEEDS_GO_AHEAD:
141 tem_show('price_row');
142 tem_show('approve_price_link');
143 tem_show('normal_edit_link');
146 tem_show('price_row');
147 tem_show('warning_edit_link');
150 tem_show('price_row');
152 case TASK_NEEDS_TESTING:
153 tem_show('price_row');
154 tem_show('finished_link');
157 tem_show('price_row');
162 message("Task #$task_id not found");
167 function tasks_edit_main() {
168 $state = TASK_DRAFT; # will be overwritten
169 $client_id = logged_in(); # fixed shortly if we're contractor
170 $edit_id = format_int($_REQUEST['tasks_edit_id']);
171 unset($_REQUEST['tasks_edit_id']);
173 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $edit_id);
174 if(logged_in_as_contractor()) {
176 } elseif($owner != $client_id) {
177 message('Sorry, that task was entered by/for another client.');
181 # add hidden field for database id of row we're editing
182 tem_set('tasks_edit_id', $edit_id);
185 $state = db_get_value('tasks', 'state', 'where id=%i', $edit_id);
188 if(isset($_REQUEST['tasks_new_bug'])) {
192 if(isset($_REQUEST['tasks_mark_paid_id'])) {
193 if(!logged_in_as_contractor()) {
194 message("Error: only Jason can mark tasks as paid.");
197 $id = $_REQUEST['tasks_mark_paid_id'];
198 db_update('tasks', 'paid', 1, 'where id=%i', $id);
199 message('Marked as paid.');
203 if(isset($_REQUEST['tasks_approve_price_id'])) {
204 $id = $_REQUEST['tasks_approve_price_id'];
205 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
206 if(logged_in() != $owner) {
207 message("Error: can't approve a task entered by/for another client.");
210 db_update('tasks', 'state', TASK_QUEUED, 'where id=%i', $id);
211 message('Price approved.');
215 if(isset($_REQUEST['tasks_working_id'])) {
216 $id = $_REQUEST['tasks_working_id'];
217 if(!logged_in_as_contractor()) {
218 message("Error: only Jason can say what he's working on.");
221 db_update('tasks', 'state', TASK_WORKING, 'where id=%i', $id);
222 message('OK, client locked out of modifying that one.');
226 if(isset($_REQUEST['tasks_needs_testing_id'])) {
227 $id = $_REQUEST['tasks_needs_testing_id'];
228 if(!logged_in_as_contractor()) {
229 message("Error: only Jason can say when he's done.");
232 db_update('tasks', 'state', TASK_NEEDS_TESTING, 'where id=%i', $id);
233 message('Task awaits testing.');
237 if(isset($_REQUEST['tasks_finished_id'])) {
238 $id = $_REQUEST['tasks_finished_id'];
239 $owner = db_get_value('tasks', 'client_id', 'where id=%i', $id);;
240 if(logged_in() != $owner) {
241 message("Error: can't test a task entered by/for another client.");
244 db_update('tasks', 'state', TASK_FINISHED, 'where id=%i', $id);
245 message('Task marked as finished.');
246 # FIXME also mark it as paid if client's balance can cover it
250 $delete_id = format_int($_REQUEST['tasks_delete_id']);
251 unset($_REQUEST['tasks_delete_id']);
253 db_delete('tasks', 'where id=%i', $delete_id);
254 message('Task deleted.');
256 return './tasks.html';
259 if(isset($_REQUEST['title'])) {
260 list($title, $url, $description, $price) = tasks_get_fields();
263 if(isset($_REQUEST['save_draft'])) {
265 } elseif(isset($_REQUEST['save_bug'])) {
267 } elseif(isset($_REQUEST['save_price']) && logged_in_as_contractor()) {
268 $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
269 if($price < $tiny_agreement) {
270 $state = TASK_QUEUED;
272 $state = TASK_NEEDS_GO_AHEAD;
274 } elseif(isset($_REQUEST['needs_clarification'])) {
275 $state = TASK_NEEDS_CLARIFICATION;
276 } else { # better be "request_price"
277 if(description_has_fixmes($description)) {
278 $state = TASK_NEEDS_CLARIFICATION;
279 message('The description is not ready to be priced yet because it still contains at least one "FIXME".');
281 $state = TASK_NEEDS_QUOTE;
285 if("you're happy with the POSTed values") {
287 if(isset($_REQUEST['price']) && logged_in_as_contractor()) {
288 db_update('tasks', 'title,url,description,state,price', $title, $url, $description, $state, $price, 'where id=%i', $edit_id);
290 db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, 'where id=%i', $edit_id);
292 message('Task updated.');
296 $client_id = logged_in();
297 db_insert('tasks', 'client_id,title,url,description,state,paid', $client_id, $title, $url, $description, $state, $paid);
298 message('Task saved.');
300 if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
301 $to = $GLOBALS['tasks_form_recipient'];
304 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
305 $reply_to = $_REQUEST['email'];
306 if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
307 $reply_to = "$_REQUEST[name] <$reply_to>";
310 $subject = 'tasks form submitted';
311 $message = tem_run('tasks.email.txt');
314 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
315 message('Due to an internal error, your message could not be sent. Please try again later.');
319 if($error !== true) {
323 # otherwise, we display the form again. tasks_get_fields() has
324 # already put the posted values back into the template engine, so they will
325 # show up in the form fields. You should add some message asking people to
326 # fix their entry in whatever way you require.
328 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
329 list($title, $url, $description, $state, $paid) = db_get_row('tasks', 'title,url,description,state,price', 'where id=%i', $edit_id);
330 tasks_tem_sets($title, $url, $description, $price);
332 # form not submitted, you can set default values like so:
333 #tem_set('client_id', 'Yes');
338 tem_show('edit_msg');
339 } elseif($state == TASK_BUG) {
345 # display instructions
346 if($state == TASK_BUG) {
347 tem_show('bug_instructions');
348 if(logged_in_as_contractor()) {
349 tem_show('price_field');
350 tem_show('contractor_submits');
352 tem_show('bug_submit');
354 } elseif($state == TASK_NEEDS_QUOTE && logged_in_as_contractor()) {
355 tem_show('set_price_instructions');
356 tem_show('price_field');
357 tem_show('contractor_submits');
359 if(description_has_fixmes($description)) {
360 tem_show('fixme_instructions');
362 tem_show('normal_instructions');
364 tem_show('normal_submits');