Add support of editing bookmarks.

This commit is contained in:
Golnaz Nilieh 2011-09-13 15:38:11 +04:30
parent 9a5530a48f
commit 67addb4882
4 changed files with 161 additions and 25 deletions

View File

@ -0,0 +1,82 @@
<?php
/**
* ownCloud - bookmarks plugin - edit bookmark script
*
* @author Golnaz Nilieh
* @copyright 2011 Golnaz Nilieh <golnaz.nilieh@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
//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);
}

View File

@ -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 \' \'

View File

@ -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<len; ++i ){
tagshtml += '<a class="bookmark_tags" href="?tag=' + encodeURI(taglist[i]) + '">' + taglist[i] + '</a> ';
tagshtml += '<a class="bookmark_tag" href="?tag=' + encodeURI(taglist[i]) + '">' + taglist[i] + '</a> ';
}
$.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(
'<div class="bookmark_single">' +
'<p class="bookmark_title"><a href="' + url + '" target="_new" class="bookmark_link">' + title + '</a></p>' +
'<p class="bookmark_url">' + url + '</p>' +
'<p class="bookmark_description">' + description + '</p>' +
'<p>' + tagshtml + '</p>' +
'<p class="bookmark_actions"><span class="bookmark_delete">Delete</span></p>' +
'</div>'
);
}
});
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(
'<div class="bookmark_single">' +
'<p class="bookmark_title"><a href="' + url + '" target="_new" class="bookmark_link">' + title + '</a></p>' +
'<p class="bookmark_url">' + url + '</p>' +
'<p class="bookmark_description">' + description + '</p>' +
'<p class="bookmark_tags">' + tagshtml + '</p>' +
'<p class="bookmark_actions"><span class="bookmark_delete">Delete</span></p>' +
'</div>'
);
}
});
}
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<len; ++i ){
taglist = taglist + '<a class="bookmark_tags" href="?tag=' + encodeURI(tags[i]) + '">' + tags[i] + '</a> ';
taglist = taglist + '<a class="bookmark_tag" href="?tag=' + encodeURI(tags[i]) + '">' + tags[i] + '</a> ';
}
if(!hasProtocol(bookmark.url)) {
bookmark.url = 'http://' + bookmark.url;
}
$('.bookmarks_list').append(
'<div class="bookmark_single">' +
'<div class="bookmark_single" data-id="' + bookmark.id +'" >' +
'<p class="bookmark_title"><a href="' + encodeEntities(bookmark.url) + '" target="_new" class="bookmark_link">' + encodeEntities(bookmark.title) + '</a></p>' +
'<p class="bookmark_url">' + encodeEntities(bookmark.url) + '</p>' +
'<p class="bookmark_description">' + encodeEntities(bookmark.description) + '</p>' +
'<p>' + taglist + '</p>' +
'<p class="bookmark_actions"><span class="bookmark_delete">Delete</span></p>' +
'<p class="bookmark_tags">' + taglist + '</p>' +
'<p class="bookmark_actions"><span class="bookmark_delete">Delete</span>&nbsp;<span class="bookmark_edit">Edit</span></p>' +
'</div>'
);
}

View File

@ -5,6 +5,7 @@
<a class="bookmarks_addBml" href="javascript:var url = encodeURIComponent(location.href);window.open('<?php echo OC_Helper::linkTo('bookmarks', 'addBm.php', null, true); ?>?url='+url, 'owncloud-bookmarks');" title="Drag this to your browser bookmarks and click it, when you want to bookmark a webpage.">Add page to ownCloud</a>
</div>
<div class="bookmarks_add">
<input type="hidden" id="bookmark_add_id" value="0" />
<p><label class="bookmarks_label">Address</label><input type="text" id="bookmark_add_url" class="bookmarks_input" /></p>
<p><label class="bookmarks_label">Title</label><input type="text" id="bookmark_add_title" class="bookmarks_input" />
<img class="loading_meta" src="<?php echo OC_Helper::imagePath('core', 'loading.gif'); ?>" /></p>