diff --git a/apps/bookmarks/ajax/editBookmark.php b/apps/bookmarks/ajax/editBookmark.php new file mode 100644 index 0000000000..1bd2fc08bc --- /dev/null +++ b/apps/bookmarks/ajax/editBookmark.php @@ -0,0 +1,82 @@ + +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library. If not, see . +* +*/ + +//no apps or filesystem +$RUNTIME_NOSETUPFS=true; + +require_once('../../../lib/base.php'); + +// We send json data +header( 'Content-Type: application/jsonrequest' ); + +// Check if we are a user +if( !OC_User::isLoggedIn()){ + echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => 'Authentication error' ))); + exit(); +} + +$CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" ); +if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ + $_ut = "strftime('%s','now')"; +} else { + $_ut = "UNIX_TIMESTAMP()"; +} + +$bookmark_id = (int)$_GET["id"]; + +$query = OC_DB::prepare(" + UPDATE *PREFIX*bookmarks + SET url = ?, title =?, description = ?, lastmodified = $_ut + WHERE id = $bookmark_id + "); + +$params=array( + htmlspecialchars_decode($_GET["url"]), + htmlspecialchars_decode($_GET["title"]), + htmlspecialchars_decode($_GET["description"]), + ); +$query->execute($params); + +# Remove old tags and insert new ones. +$query = OC_DB::prepare(" + DELETE FROM *PREFIX*bookmarks_tags + WHERE bookmark_id = $bookmark_id + "); + +$query->execute(); + +$query = OC_DB::prepare(" + INSERT INTO *PREFIX*bookmarks_tags + (bookmark_id, tag) + VALUES (?, ?) + "); + +$tags = explode(' ', urldecode($_GET["tags"])); +foreach ($tags as $tag) { + if(empty($tag)) { + //avoid saving blankspaces + continue; + } + $params = array($bookmark_id, trim($tag)); + $query->execute($params); +} diff --git a/apps/bookmarks/ajax/updateList.php b/apps/bookmarks/ajax/updateList.php index ceecc5b7ef..67acb2190c 100644 --- a/apps/bookmarks/ajax/updateList.php +++ b/apps/bookmarks/ajax/updateList.php @@ -64,7 +64,7 @@ if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ } $query = OC_DB::prepare(' - SELECT url, title, description, + SELECT id, url, title, description, CASE WHEN *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id THEN GROUP_CONCAT( tag ' .$_gc_separator. ' ) ELSE \' \' diff --git a/apps/bookmarks/js/bookmarks.js b/apps/bookmarks/js/bookmarks.js index fe8a266686..e26a9a80fb 100644 --- a/apps/bookmarks/js/bookmarks.js +++ b/apps/bookmarks/js/bookmarks.js @@ -8,7 +8,7 @@ $(document).ready(function() { $('.bookmarks_add').slideToggle(); }); - $('#bookmark_add_submit').click(addBookmark); + $('#bookmark_add_submit').click(addOrEditBookmark); $(window).scroll(updateOnBottom); $('#bookmark_add_url').focusout(getMetadata); @@ -34,12 +34,15 @@ function getBookmarks() { bookmarks_page += 1; $('.bookmark_link').unbind('click', recordClick); $('.bookmark_delete').unbind('click', delBookmark); + $('.bookmark_edit').unbind('click', showBookmark); for(var i in bookmarks.data) { updateBookmarksList(bookmarks.data[i]); } $('.bookmark_link').click(recordClick); $('.bookmark_delete').click(delBookmark); + $('.bookmark_edit').click(showBookmark); + bookmarks_loading = false; } }); @@ -73,7 +76,12 @@ function changeSorting(sortEl) { getBookmarks(); } -function addBookmark(event) { +// function addBookmark() { +// Instead of creating editBookmark() function, Converted the one above to +// addOrEditBookmark() to make .js file more compact. + +function addOrEditBookmark(event) { + var id = $('#bookmark_add_id').val(); var url = encodeEntities($('#bookmark_add_url').val()) var title = encodeEntities($('#bookmark_add_title').val()) var description = encodeEntities($('#bookmark_add_description').val()) @@ -81,25 +89,49 @@ function addBookmark(event) { var taglist = tags.split(' ') var tagshtml = ''; for ( var i=0, len=taglist.length; i' + taglist[i] + ' '; + tagshtml += '' + taglist[i] + ' '; } - $.ajax({ - url: 'ajax/addBookmark.php', - data: 'url=' + encodeURI(url) + '&title=' + encodeURI(title) + '&description=' + encodeURI(description) + '&tags=' + encodeURI(tags), - success: function(data){ - $('.bookmarks_add').slideToggle(); - $('.bookmarks_add').children('p').children('.bookmarks_input').val(''); - $('.bookmarks_list').prepend( - '
' + - '

' + title + '

' + - '

' + url + '

' + - '

' + description + '

' + - '

' + tagshtml + '

' + - '

Delete

' + - '
' - ); - } - }); + + if (id == 0) { + $.ajax({ + url: 'ajax/addBookmark.php', + data: 'url=' + encodeURI(url) + '&title=' + encodeURI(title) + '&description=' + encodeURI(description) + '&tags=' + encodeURI(tags), + success: function(data){ + $('.bookmarks_add').slideToggle(); + $('.bookmarks_add').children('p').children('.bookmarks_input').val(''); + $('.bookmarks_list').prepend( + '
' + + '

' + title + '

' + + '

' + url + '

' + + '

' + description + '

' + + '

' + tagshtml + '

' + + '

Delete

' + + '
' + ); + } + }); + } + else { + $.ajax({ + url: 'ajax/editBookmark.php', + data: 'id=' + id + '&url=' + encodeURI(url) + '&title=' + encodeURI(title) + '&description=' + + encodeURI(description) + '&tags=' + encodeURI(tags), + success: function(){ + $('.bookmarks_add').slideToggle(); + $('.bookmarks_add').children('p').children('.bookmarks_input').val(''); + var record = $('.bookmark_single[data-id = "' + id + '"]'); + record.children('.bookmark_url:first').text(url); + record.children('.bookmark_description:first').text(description); + + var record_title = record.children('.bookmark_title:first').children('a:first'); + record_title.attr('href', url); + record_title.text(title); + + record.children('.bookmark_tags:first').html(tagshtml); + } + }); + } + } function delBookmark(event) { @@ -111,22 +143,43 @@ function delBookmark(event) { }); } +function showBookmark(event) { + var record = $(this).parent().parent(); + $('#bookmark_add_id').val(record.attr('data-id')); + $('#bookmark_add_url').val(record.children('.bookmark_url:first').text()); + $('#bookmark_add_title').val(record.children('.bookmark_title:first').text()); + $('#bookmark_add_description').val(record.children('.bookmark_description:first').text()); + $('#bookmark_add_tags').val(record.children('.bookmark_tags:first').text()); + + if ($('.bookmarks_add').css('display') == 'none') { + $('.bookmarks_add').slideToggle(); + } + $('html, body').animate({ + scrollTop: $('.bookmarks_menu').offset().top + }, 500); + +} + +function editBookmark(event) { + +} + function updateBookmarksList(bookmark) { var tags = encodeEntities(bookmark.tags).split(' '); var taglist = ''; for ( var i=0, len=tags.length; i' + tags[i] + ' '; + taglist = taglist + '' + tags[i] + ' '; } if(!hasProtocol(bookmark.url)) { bookmark.url = 'http://' + bookmark.url; } $('.bookmarks_list').append( - '
' + + '
' + '

' + encodeEntities(bookmark.title) + '

' + '

' + encodeEntities(bookmark.url) + '

' + '

' + encodeEntities(bookmark.description) + '

' + - '

' + taglist + '

' + - '

Delete

' + + '

' + taglist + '

' + + '

Delete Edit

' + '
' ); } diff --git a/apps/bookmarks/templates/list.php b/apps/bookmarks/templates/list.php index d4215e82dd..2aa5093c82 100644 --- a/apps/bookmarks/templates/list.php +++ b/apps/bookmarks/templates/list.php @@ -5,6 +5,7 @@ Add page to ownCloud
+