diff --git a/addBm.php b/addBm.php index 1ffd6154..49b9760f 100644 --- a/addBm.php +++ b/addBm.php @@ -31,24 +31,28 @@ OCP\App::checkAppEnabled('bookmarks'); require_once('bookmarksHelper.php'); +// If we go the dialog form submit +if(isset($_POST['url'])) { + $tags = isset($_POST['item']['tags']) ? $_POST['item']['tags'] : array(); + $pub = isset($_POST['is_public']) ? true : false; + + if(isset($_POST['record_id']) && is_numeric($_POST['record_id']) ) { //EDIT + $bm = $_POST['record_id']; + editBookmark($bm, $_POST['url'], $_POST['title'], $tags, $_POST['desc'], $pub); + } + else { + $bm = addBookmark($_POST['url'], $_POST['title'], $tags, $_POST['desc'], $pub); + } + OCP\JSON::success(array('id'=>$bm)); + exit(); +} + + +// Prep screen if we come from the bookmarklet $url =''; if(isset($_GET['url']) ){ $url = $_GET['url']; } - -if(isset($_POST['url'])) { - $tags = isset($_POST['item']['tags']) ? $_POST['item']['tags'] : array(); - $pub = isset($_POST['is_public']) ? true : false; - $bm = addBookmark($_POST['url'], $_POST['title'], implode(',',$tags),$_POST['desc'], $pub); - OCP\JSON::success(array('id'=>$bm)); - exit(); -} - -OCP\Util::addscript('bookmarks','tag-it'); -OCP\Util::addscript('bookmarks','addBm'); -OCP\Util::addStyle('bookmarks', 'bookmarks'); -OCP\Util::addStyle('bookmarks', 'jquery.tagit'); - if(!isset($_GET['title']) || trim($_GET['title']) == '') { $datas = getURLMetadata($url); $title = isset($datas['title']) ? $datas['title'] : ''; @@ -57,6 +61,14 @@ else{ $title = $_GET['title']; } + +OCP\Util::addscript('bookmarks','tag-it'); +OCP\Util::addscript('bookmarks','addBm'); +OCP\Util::addStyle('bookmarks', 'bookmarks'); +OCP\Util::addStyle('bookmarks', 'jquery.tagit'); + + + $bm = array('title'=> $title, 'url'=> $url, 'tags'=> array(), diff --git a/bookmarksHelper.php b/bookmarksHelper.php index 6e91a4ed..76261b4a 100644 --- a/bookmarksHelper.php +++ b/bookmarksHelper.php @@ -80,7 +80,7 @@ function analyzeTagRequest($line) { return $filterTag; } -function addBookmark($url, $title, $tags='', $description='', $is_public=false) { +function getNowValue() { $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ $_ut = "strftime('%s','now')"; @@ -89,9 +89,71 @@ function addBookmark($url, $title, $tags='', $description='', $is_public=false) } else { $_ut = "UNIX_TIMESTAMP()"; } + return $_ut; +} + +function editBookmark($id, $url, $title, $tags = array(), $description='', $is_public=false) { $is_public = $is_public ? 1 : 0; - //FIXME: Detect when user adds a known URL + $user_id = OCP\USER::getUser(); + + // Update the record + $query = OCP\DB::prepare(" + UPDATE *PREFIX*bookmarks SET + url = ?, title = ?, public = ?, description = ?, + lastmodified = ".getNowValue() ." + WHERE id = ? + AND user_id = ? + "); + + $params=array( + htmlspecialchars_decode($url), + htmlspecialchars_decode($title), + $is_public, + htmlspecialchars_decode($description), + $id, + $user_id, + ); + + $result = $query->execute($params); + + // Abort the operation if bookmark couldn't be set + // (probably because the user is not allowed to edit this bookmark) + if ($result->numRows() == 0) exit(); + + + // Remove old tags + $sql = "DELETE from *PREFIX*bookmarks_tags WHERE bookmark_id = ?"; + $query = OCP\DB::prepare($sql); + $query->execute(array($id)); + + // Add New Tags + addTags($id, $tags); +} + +function addTags($bookmark_id, $tags) { + $query = OCP\DB::prepare(" + INSERT INTO *PREFIX*bookmarks_tags + (bookmark_id, tag) + VALUES (?, ?) + "); + + foreach ($tags as $tag) { + if(empty($tag)) { + //avoid saving blankspaces + continue; + } + $params = array($bookmark_id, trim($tag)); + $query->execute($params); + } +} + +function addBookmark($url, $title, $tags=array(), $description='', $is_public=false) { + + $is_public = $is_public ? 1 : 0; + //FIXME: Detect and do smth when user adds a known URL + $_ut = getNowValue(); + $query = OCP\DB::prepare(" INSERT INTO `*PREFIX*bookmarks` (url, title, user_id, public, added, lastmodified, description) @@ -110,22 +172,7 @@ function addBookmark($url, $title, $tags='', $description='', $is_public=false) $b_id = OCP\DB::insertid('*PREFIX*bookmarks'); if($b_id !== false) { - $query = OCP\DB::prepare(" - INSERT INTO `*PREFIX*bookmarks_tags` - (`bookmark_id`, `tag`) - VALUES (?, ?) - "); - - $tags = explode(',', urldecode($tags)); - foreach ($tags as $tag) { - if(empty($tag)) { - //avoid saving blankspaces - continue; - } - $params = array($b_id, trim($tag)); - $query->execute($params); - } - + addTags($b_id, $tags); return $b_id; } } diff --git a/js/addBm.js b/js/addBm.js index ceb430a6..8e9ceefe 100644 --- a/js/addBm.js +++ b/js/addBm.js @@ -45,6 +45,26 @@ allowSpaces: true, availableTags: fullTags }); + + if(base.options['record']) { //Fill the form if it's an edit + record = base.options['record']; + base.$el.find('.record_id').val(record.id); + base.$el.find('.title').val(record.title); + base.$el.find('.url-ro code').text(record.url); + base.$el.find('.url_input').val(record.url); + base.$el.find('.desc').val(record.description); + base.$el.find('.is_public').val(record.public); + tagit_elem = base.$el.find('.tags'); + for(var i=0;i' + encodeEntities(bookmark.url) + '

' + '' ); + $('div[data-id="'+ bookmark.id +'"]').data('record', bookmark); if(taglist != '') { $('div[data-id="'+ bookmark.id +'"]').append('

' + taglist + '

'); } diff --git a/templates/addBm.php b/templates/addBm.php index 88d1fcd0..e2963db6 100644 --- a/templates/addBm.php +++ b/templates/addBm.php @@ -46,6 +46,7 @@ " /> id="is_public" name="is_public"> +