+<?php
+
+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');
+require_once('code/wfpl/format.php');
+require_once('code/wfpl/messages.php');
+require_once('code/wfpl/email.php');
+require_once('code/db_connect.php');
+
+function tasks_get_fields() {
+ $title = format_oneline($_REQUEST['title']);
+ $url = format_oneline($_REQUEST['url']);
+ $description = format_unix($_REQUEST['description']);
+
+ tasks_tem_sets($title, $url, $description);
+
+ return array($title, $url, $description);
+}
+
+function tasks_tem_sets($title, $url, $description) {
+ tem_set('title', $title);
+ tem_set('url', $url);
+ tem_set('description', $description);
+}
+
+function tasks_main() {
+ $ret = _tasks_main();
+ if($ret) {
+ return $ret;
+ }
+ tem_show('main_body');
+}
+
+function _tasks_main() {
+ $edit_id = format_int($_REQUEST['tasks_edit_id']);
+ unset($_REQUEST['tasks_edit_id']);
+ if($edit_id) {
+ # add hidden field for database id of row we're editing
+ tem_set('tasks_edit_id', $edit_id);
+ tem_show('editing');
+ }
+
+ $delete_id = format_int($_REQUEST['tasks_delete_id']);
+ unset($_REQUEST['tasks_delete_id']);
+ if($delete_id) {
+ db_delete('tasks', 'where id=%i', $delete_id);
+ message('Task deleted.');
+
+ return './tasks.html';
+ }
+
+ if(!$edit_id) {
+ tem_show('new_msg');
+ }
+
+ if(isset($_REQUEST['title'])) {
+ list($title, $url, $description) = tasks_get_fields();
+
+ 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);
+ 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
+ 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'];
+ $from = $to;
+ $reply_to = '';
+ if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
+ $reply_to = $_REQUEST['email'];
+ if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
+ $reply_to = "$_REQUEST[name] <$reply_to>";
+ }
+ }
+ $subject = 'tasks form submitted';
+ $message = tem_run('tasks.email.txt');
+ $cc = '';
+ $bcc = '';
+ if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
+ message('Due to an internal error, your message could not be sent. Please try again later.');
+ $error = true;
+ }
+ }
+ if($error !== true) {
+ tem_show('thankyou');
+ return;
+ }
+ }
+ # otherwise, we display the form again. tasks_get_fields() has
+ # already put the posted values back into the template engine, so they will
+ # show up in the form fields. You should add some message asking people to
+ # 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);
+ } 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
+ if($edit_id) {
+ tem_show('edit_msg');
+ }
+
+ tem_show('form');
+}
+
+?>