/** * Metodi päivittää muistutuksen ja sen luokan tiedot uusilla tiedoilla. * * @param type $id muistutuksen id */ public static function update($id) { $params = $_POST; $todoAttributes = array('todoId' => $params['todo_id'], 'title' => $params['title'], 'priority' => $params['priority'], 'created_at' => date("Y-m-d H:i:s")); $todo = new Todo($todoAttributes); try { $categories = $params['categories']; } catch (Exception $e) { $categoryErrors = array(); $categoryErrors[] = 'Sinun on valittava vähintään yksi luokka.'; $categories = Category::all(); View::make('/reminders/edit_reminder.html', array('categoryErrors' => $categoryErrors, 'todoAttributes' => $todoAttributes, 'todo' => $todo, 'categories' => $categories)); } $todoErrors = $todo->errors(); if (count($todoErrors) == 0) { $todo->update(); $todoCategory = new TodoCategory(array('todo_id' => $todo->getId())); $todoCategory->destroy(); foreach ($categories as $category) { $todoCategory = new TodoCategory(array('todo_id' => $todo->getId(), 'category_id' => $category)); $todoCategory->save(); } Redirect::to('/reminders', array('message' => 'Muistutusta muokattu onnistuneesti')); } else { $categories = Category::all(); View::make('/reminders/edit_reminder.html', array('categories' => $categories, 'todoErrors' => $todoErrors, 'todo' => $todoAttributes, 'category' => $categoryAttributes)); } }
<?php require_once "api/database.php"; require_once "api/todo.php"; $todo = new Todo(); switch ($_SERVER['REQUEST_METHOD']) { case 'GET': $params = str_replace('/', '', $_SERVER['QUERY_STRING']); parse_str($params, $params); $todo->read($params); break; case 'PUT': parse_str(file_get_contents('php://input'), $p_data); $object = json_decode(stripslashes($p_data['model']), true); $todo->update($object); break; case 'POST': $object = json_decode(stripslashes($_POST['model']), true); $todo->create($object); break; case 'DELETE': $id = (int) str_replace('/', '', $_SERVER['QUERY_STRING']); $todo->delete($id); break; }
include_once __DIR__ . '/src/Todo.php'; $items = Todo::batch_read(); ?> <?php //refresh page to reflect new changes function refresh() { header("Location: ."); } //submit/update an item if (isset($_POST) && isset($_POST['title'])) { if ($_POST['title']) { $title = strip_tags(stripslashes($_POST['title'])); if (isset($_POST['id'])) { Todo::update($_POST['id'], $title); } else { Todo::create($title); } } refresh(); } //delete an item if (isset($_GET['delete']) && isset($_GET['id'])) { if (filter_var($_GET['id'], FILTER_VALIDATE_INT)) { Todo::delete($_GET['id']); refresh(); } } //view a single item if (isset($_GET['id'])) {