JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
starting to work
[contractor-progress.git] / tasks.php
1 <?php
2
3 require_once('code/tasks.php');
4
5 $GLOBALS['tasks_form_recipient'] = "fixme@example.com";
6
7 define('TASKS_DB_FIELDS', 'title,url,description,state');
8
9
10 require_once('code/wfpl/template.php');
11 require_once('code/wfpl/format.php');
12 require_once('code/wfpl/messages.php');
13 require_once('code/wfpl/email.php');
14 require_once('code/db_connect.php');
15
16 function tasks_get_fields() {
17         $title = format_oneline($_REQUEST['title']);
18         $url = format_oneline($_REQUEST['url']);
19         $description = format_unix($_REQUEST['description']);
20
21         tasks_tem_sets($title, $url, $description);
22
23         return array($title, $url, $description);
24 }
25
26 function tasks_tem_sets($title, $url, $description) {
27         tem_set('title', $title);
28         tem_set('url', $url);
29         tem_set('description', $description);
30 }
31
32 function tasks_main() {
33         $ret = _tasks_main();
34         if($ret) {
35                 return $ret;
36         }
37         tem_show('main_body');
38 }
39
40 function _tasks_main() {
41         $edit_id = format_int($_REQUEST['tasks_edit_id']);
42         unset($_REQUEST['tasks_edit_id']);
43         if($edit_id) {
44                 # add hidden field for database id of row we're editing
45                 tem_set('tasks_edit_id', $edit_id);
46                 tem_show('editing');
47         }
48
49         $delete_id = format_int($_REQUEST['tasks_delete_id']);
50         unset($_REQUEST['tasks_delete_id']);
51         if($delete_id) {
52                 db_delete('tasks', 'where id=%i', $delete_id);
53                 message('Task deleted.');
54
55                 return './tasks.html';
56         }
57
58         if(!$edit_id) {
59                 tem_show('new_msg');
60         }
61
62         if(isset($_REQUEST['title'])) {
63                 list($title, $url, $description) = tasks_get_fields();
64
65                 if("you're happy with the POSTed values") {
66                         if($edit_id) {
67                                 db_update('tasks', 'title,url,description,state', $title, $url, $description, $state, $paid = 0, 'where id=%i', $edit_id);
68                                 message('Task updated.');
69                         } else {
70                                 # new task
71                                 $paid = 0;
72                                 if(isset($_REQUEST['save_draft'])) {
73                                         $state = TASK_DRAFT;
74                                 } else {
75                                         $state = TASK_NEEDS_QUOTE;
76                                 }
77                                 $client_id = 4; # FIXME
78                                 db_insert('tasks', 'client_id,title,url,description,state,paid', $client_id, $title, $url, $description, $state, $paid);
79                                 message('Task saved.');
80                                 return './';
81                         }
82                         if($GLOBALS['tasks_form_recipient'] != "fixme@example.com") {
83                                 $to = $GLOBALS['tasks_form_recipient'];
84                                 $from = $to;
85                                 $reply_to = '';
86                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
87                                         $reply_to = $_REQUEST['email'];
88                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
89                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
90                                         }
91                                 }
92                                 $subject = 'tasks form submitted';
93                                 $message = tem_run('tasks.email.txt');
94                                 $cc = '';
95                                 $bcc = '';
96                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
97                                         message('Due to an internal error, your message could not be sent. Please try again later.');
98                                         $error = true;
99                                 }
100                         }
101                         if($error !== true) {
102                                 tem_show('thankyou');
103                                 return;
104                         }
105                 }
106                 # otherwise, we display the form again. tasks_get_fields() has
107                 # already put the posted values back into the template engine, so they will
108                 # show up in the form fields. You should add some message asking people to
109                 # fix their entry in whatever way you require.
110         } elseif($edit_id) {
111                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
112                 list($client_id, $ord, $title, $url, $description, $state, $paid) = db_get_row('tasks', TASKS_DB_FIELDS, 'where id=%i', $edit_id);
113                 tasks_tem_sets($client_id, $ord, $title, $url, $description, $state, $paid);
114         } else {
115                 # form not submitted, you can set default values like so:
116                 #tem_set('client_id', 'Yes');
117         }
118
119         # this has to be later in the file because it requres that client_id be set already
120         if($edit_id) {
121                 tem_show('edit_msg');
122         }
123
124         tem_show('form');
125 }
126
127 ?>