mirror of
https://github.com/owncloud/bookmarks.git
synced 2025-02-20 17:54:25 +01:00
Add possibility to add/edit bookmark
This commit is contained in:
parent
d1fa11e936
commit
ce589e2fa7
40
addBm.php
40
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(),
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
23
js/addBm.js
23
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<record.tags.length;i++) {
|
||||
tagit_elem.tagit('createTag', record.tags[i]);
|
||||
console.log(record.tags[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(base.$el.find('.url-ro code').text() != '')
|
||||
base.$el.find('.url_input').hide();
|
||||
else
|
||||
base.$el.find('.url_input').shwo()
|
||||
};
|
||||
|
||||
base.init();
|
||||
@ -54,7 +74,8 @@
|
||||
|
||||
|
||||
$.bookmark_dialog.defaultOptions = {
|
||||
on_success: function(){}
|
||||
on_success: function(){},
|
||||
bookmark_record: undefined
|
||||
};
|
||||
|
||||
$.fn.bookmark_dialog = function(options){
|
||||
|
@ -4,7 +4,7 @@ var bookmarks_loading = false;
|
||||
var bookmarks_sorting = 'bookmarks_sorting_recent';
|
||||
|
||||
$(document).ready(function() {
|
||||
$('#bookmark_add_submit').click(addOrEditBookmark);
|
||||
$('#bookmark_add_submit').click(addBookmark);
|
||||
$(window).resize(function () {
|
||||
fillWindow($('.bookmarks_list'));
|
||||
});
|
||||
@ -20,10 +20,9 @@ $(document).ready(function() {
|
||||
getBookmarks();
|
||||
});
|
||||
|
||||
function addFilterTag(event)
|
||||
{
|
||||
event.preventDefault();
|
||||
$('#tag_filter input').tagit('createTag', $(this).text());
|
||||
function addFilterTag(event) {
|
||||
event.preventDefault();
|
||||
$('#tag_filter input').tagit('createTag', $(this).text());
|
||||
}
|
||||
|
||||
function updateTagsList(tag) {
|
||||
@ -43,7 +42,6 @@ function updateTagsList(tag) {
|
||||
function filterTagsChanged()
|
||||
{
|
||||
$('#bookmarkFilterTag').val($('#tag_filter input:hidden').val());
|
||||
console.log($('#bookmarkFilterTag').val());
|
||||
bookmarks_page = 0;
|
||||
$('.bookmarks_list').empty();
|
||||
getBookmarks();
|
||||
@ -77,7 +75,7 @@ function getBookmarks() {
|
||||
}
|
||||
$('.bookmark_link').unbind('click', recordClick);
|
||||
$('.bookmark_delete').unbind('click', delBookmark);
|
||||
$('.bookmark_edit').unbind('click', showBookmark);
|
||||
$('.bookmark_edit').unbind('click', editBookmark);
|
||||
|
||||
for(var i in bookmarks.data) {
|
||||
updateBookmarksList(bookmarks.data[i]);
|
||||
@ -89,7 +87,7 @@ function getBookmarks() {
|
||||
|
||||
$('.bookmark_link').click(recordClick);
|
||||
$('.bookmark_delete').click(delBookmark);
|
||||
$('.bookmark_edit').click(showBookmark);
|
||||
$('.bookmark_edit').click(editBookmark);
|
||||
|
||||
bookmarks_loading = false;
|
||||
if (bookmarks.data.length) {
|
||||
@ -99,11 +97,8 @@ function getBookmarks() {
|
||||
});
|
||||
}
|
||||
|
||||
// function addBookmark() {
|
||||
// Instead of creating editBookmark() function, Converted the one above to
|
||||
// addOrEditBookmark() to make .js file more compact.
|
||||
|
||||
function addOrEditBookmark(event) {
|
||||
function createEditDialog(record){
|
||||
dialog_html = $('#edit_dialog').html();
|
||||
var dialog = $(dialog_html).dialog({
|
||||
width : 620,
|
||||
@ -116,53 +111,16 @@ function addOrEditBookmark(event) {
|
||||
});
|
||||
|
||||
$('.ui-dialog').bookmark_dialog({
|
||||
'on_success': function(){
|
||||
on_success: function(){
|
||||
dialog.dialog('destroy').remove();
|
||||
filterTagsChanged();
|
||||
}
|
||||
},
|
||||
record: record
|
||||
});
|
||||
}
|
||||
|
||||
/*var id = $('#bookmark_add_id').val();
|
||||
var url = encodeEntities($('#bookmark_add_url').val());
|
||||
var title = encodeEntities($('#bookmark_add_title').val());
|
||||
var tags = encodeEntities($('#bookmark_add_tags').val());
|
||||
$("#firstrun").hide();
|
||||
if($.trim(url) == '') {
|
||||
OC.dialogs.alert('A valid bookmark url must be provided', 'Error creating bookmark');
|
||||
return false;
|
||||
}
|
||||
if($.trim(title) == '') {
|
||||
OC.dialogs.alert('A valid bookmark title must be provided', 'Error creating bookmark');
|
||||
return false;
|
||||
}
|
||||
if (id == 0) {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: OC.filePath('bookmarks', 'ajax', 'addBookmark.php'),
|
||||
data: 'url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&tags=' + encodeURIComponent(tags),
|
||||
success: function(response){
|
||||
$('.bookmarks_input').val('');
|
||||
$('.bookmarks_list').empty();
|
||||
bookmarks_page = 0;
|
||||
getBookmarks();
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: OC.filePath('bookmarks', 'ajax', 'editBookmark.php'),
|
||||
data: 'id=' + id + '&url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&tags=' + encodeURIComponent(tags),
|
||||
success: function(){
|
||||
$('.bookmarks_input').val('');
|
||||
$('#bookmark_add_id').val('0');
|
||||
$('.bookmarks_list').empty();
|
||||
bookmarks_page = 0;
|
||||
getBookmarks();
|
||||
}
|
||||
});
|
||||
}
|
||||
*/
|
||||
function addBookmark(event) {
|
||||
createEditDialog();
|
||||
}
|
||||
|
||||
function delBookmark(event) {
|
||||
@ -182,16 +140,10 @@ function delBookmark(event) {
|
||||
});
|
||||
}
|
||||
|
||||
function showBookmark(event) {
|
||||
function editBookmark(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_tags').val(record.children('.bookmark_tags:first').text());
|
||||
|
||||
if ($('.bookmarks_add').css('display') == 'none') {
|
||||
$('.bookmarks_add').slideToggle();
|
||||
}
|
||||
bookmark = record.data('record');
|
||||
createEditDialog(bookmark);
|
||||
}
|
||||
|
||||
function replaceQueryString(url,param,value) {
|
||||
@ -229,6 +181,7 @@ function updateBookmarksList(bookmark) {
|
||||
'<p class="bookmark_url"><a href="' + encodeEntities(bookmark.url) + '" target="_blank" class="bookmark_link">' + encodeEntities(bookmark.url) + '</a></p>' +
|
||||
'</div>'
|
||||
);
|
||||
$('div[data-id="'+ bookmark.id +'"]').data('record', bookmark);
|
||||
if(taglist != '') {
|
||||
$('div[data-id="'+ bookmark.id +'"]').append('<p class="bookmark_tags">' + taglist + '</p>');
|
||||
}
|
||||
|
@ -46,6 +46,7 @@
|
||||
<input type="submit" class="submit" value="<?php echo $l->t("Submit");?>" />
|
||||
<input type="checkbox" <?php if($_['bookmark']['is_public']){echo 'checked="checked"';} ?> id="is_public" name="is_public">
|
||||
<label for="is_public" class="is_public_label"><?php echo $l->t("Make this link public");?></label>
|
||||
<input type="hidden" class="record_id" value="" name="record_id" />
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
Loading…
x
Reference in New Issue
Block a user