/** * Creates a new item in the database, and loads it into the item property. * Again, wordpress to handle, but need this for now...kinda'. * @param string unique item name * @return boolean TRUE on success, FALSE on failure */ protected function create_item($name) { // Check whether the item name is still available if (ThumbsUp_Database::item_name_exists($name)) { return FALSE; } // Initialize the item fields $this->item = array('name' => (string) $name, 'closed' => 0, 'date' => time()); // Insert the item into the database // TODO DB ThumbsUp_Database::db()->queryExec("\n\t\t\tINSERT INTO items (id, name, closed, date)\n\t\t\tVALUES (NULL, '" . sqlite_escape_string($this->item['name']) . "', {$this->item['closed']}, {$this->item['date']})"); // Load the item id $this->item['ID'] = ThumbsUp_Database::db()->lastInsertRowid(); // All done successfully return TRUE; }
/** * Renames an item. * * @return void */ public function action_rename() { // We need an item id, and a new name if (empty($_POST['item_id']) or !isset($_POST['item_name'])) { $error = 'No item ID or new name posted.'; } else { // Clean item id and name $item_id = (int) $_POST['item_id']; $name = (string) $_POST['item_name']; // Look up new name if (ThumbsUp_Database::item_name_exists($name)) { $error = 'This new name is already in use. It needs to be unique.'; } else { // Update new name ThumbsUp_Database::db()->queryExec("\n\t\t\t\t\tUPDATE items\n\t\t\t\t\tSET name = '" . sqlite_escape_string($name) . "'\n\t\t\t\t\tWHERE id = {$item_id}"); // Count changes to verify id if (ThumbsUp_Database::db()->changes() !== 1) { $error = 'Non-existing item ID.'; } } } if ($this->is_ajax) { // Return an error, or null if everything went fine exit(json_encode(isset($error) ? $error : NULL)); } // If this is not an AJAX request, print the error if (!empty($error)) { $this->template->error = $error; } // Show the item overview again $this->action_dashboard(); }