From 1de8a604591b9a11c9e00a1dec165783cf39e99c Mon Sep 17 00:00:00 2001 From: Gabriel Dunne Date: Sun, 18 Dec 2011 11:25:32 -0800 Subject: [PATCH] Implemented ability to delete tags --- clmpr.sql | 1 - data.php | 2 +- delete.php | 78 ++++++++++++++++++++++++++++++++++++------------------ get.php | 1 - put.php | 9 +++---- 5 files changed, 56 insertions(+), 35 deletions(-) diff --git a/clmpr.sql b/clmpr.sql index 6a9a88a..8569743 100644 --- a/clmpr.sql +++ b/clmpr.sql @@ -29,4 +29,3 @@ CREATE TABLE `users` ( PRIMARY KEY (`id`), UNIQUE KEY `user` (`user`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; - diff --git a/data.php b/data.php index f071328..fb59b3d 100644 --- a/data.php +++ b/data.php @@ -37,4 +37,4 @@ function get_users(&$dbh, $args) } return false; -} \ No newline at end of file +} diff --git a/delete.php b/delete.php index f5324f2..f37b26f 100644 --- a/delete.php +++ b/delete.php @@ -1,30 +1,56 @@ beginTransaction(); - - if ($user = get_user()) { - $sql = "DELETE FROM clmpr.clumps WHERE id = ? AND user_id = ?"; - $q = $dbh->prepare($sql); - $count = $q->execute( array( $params['clump_id'], $user['id'] )); - $dbh = null; - echo json_encode(array('deleted' => true)); - exit; - } else { - echo json_encode(array('mssg' => 'must be logged in')); - } - exit; - } - catch(PDOException $e) - { - echo $e->getMessage(); +include 'init.php'; + +$params = array(); +$params['clump_id'] = isset($_POST['clump_id']) ? $_POST['clump_id'] : null; + +try { + + $dbh = get_db_connection(); + $dbh->beginTransaction(); + $user = get_user(); + + if ($user && $params['clump_id']) { + + # get current clump + $q = $dbh->prepare("SELECT * + FROM clumps + JOIN users ON users.id = clumps.user_id + WHERE user_id = ? AND clumps.id = ? + LIMIT 1"); + $q->execute( array( $user['id'], $params['clump_id'] )); + $row = $q->fetch(); + + # decrement tag count in DB + # note: leaves tags in database with count of '0' if not used + $tags = explode(" ", $row['tags']); + if (count($tags) > 0) { + foreach($tags as $key => $tag) { + $sql = "UPDATE tags + SET count = count - 1 + WHERE tag = ? AND count > 0"; + $q = $dbh->prepare($sql); + $q->execute( array( $tag )); + } + } + + # delete clump + $sql = "DELETE FROM clumps WHERE id = ? AND user_id = ?"; + $q = $dbh->prepare($sql); + $count = $q->execute( array( $params['clump_id'], $user['id'] )); + echo json_encode(array('deleted' => true)); + } else { + echo json_encode(array('mssg' => 'must be logged in')); } - exit; \ No newline at end of file + $dbh = null; + $q = null; + +} +catch(PDOException $e) +{ + echo $e->getMessage(); +} + +exit; diff --git a/get.php b/get.php index d51cac8..a004ff8 100644 --- a/get.php +++ b/get.php @@ -81,7 +81,6 @@ case 'xml' :   - diff --git a/put.php b/put.php index a78631b..b874a95 100644 --- a/put.php +++ b/put.php @@ -33,22 +33,19 @@ try { # insert clump $sql = "INSERT INTO `clmpr`.`clumps` ( `user_id`, `title` , `url` , `tags`, `description`, `date` ) - VALUES ( ?, ?, ?, ?, ?, NOW() ) "; + VALUES ( ?, ?, ?, ?, ?, NOW() ) "; $q = $dbh->prepare($sql); $insert = $q->execute( array( $user['id'], $params['title'], $params['url'], implode(" ", $tags), htmlentities($params['description']) )); - - $dbh = null; - echo "clumped.

"; echo '
ok'; echo ''; + $dbh = null; + $q = null; } else { - include 'signin.php'; - } } catch(PDOException $e) -- 2.34.1