function edit_POST($w) { $p = $w->pathMatch("id"); $task = !empty($p["id"]) ? $w->Task->getTask($p["id"]) : new Task($w); $taskdata = null; if (!empty($p["id"])) { $taskdata = $w->Task->getTaskData($p['id']); } $task->fill($_POST['edit']); $task->assignee_id = intval($_POST['edit']['assignee_id']); if (empty($task->dt_due)) { $task->dt_due = $w->Task->getNextMonth(); } $task->insertOrUpdate(); // Tell the template what the task id is (this post action is being called via ajax) $w->setLayout(null); $w->out($task->id); // Get existing task_data objects for this task and update them $existing_task_data = $w->Task->getTaskData($task->id); if (!empty($existing_task_data)) { foreach ($existing_task_data as $e_task_data) { foreach ($_POST["extra"] as $key => $data) { if ($key == \CSRF::getTokenId()) { unset($_POST["extra"][\CSRF::getTokenID()]); continue; } if ($e_task_data->data_key == $key) { $e_task_data->value = $data; $e_task_data->update(); unset($_POST["extra"][$key]); continue; } // If we get here then remove the existing data? // $e_task_data->delete(); } } } // Insert data that didn't exist above as new task_data objects if (!empty($_POST["extra"])) { foreach ($_POST["extra"] as $key => $data) { $tdata = new TaskData($w); $tdata->task_id = $task->id; $tdata->data_key = $key; $tdata->value = $data; $tdata->insert(); } } }
var edit_form = {}; var extras_form = {}; $.each($('#edit_form').serializeArray(), function(){ edit_form[this.name] = this.value; }); $.each($('#form_fields_form').serializeArray(), function(){ extras_form[this.name] = this.value; }); var action = $(this).attr('action'); $.ajax({ url : action, type : 'POST', data : { '<?php echo \CSRF::getTokenId(); ?> ': '<?php echo \CSRF::getTokenValue(); ?> ', 'edit': edit_form, 'extra': extras_form }, complete: function(response) { if ($.isNumeric(response.responseText)) { window.location.href = "/task/edit/" + response.responseText; } else { window.location.reload(); } }