1
0
mirror of https://github.com/owncloud/bookmarks.git synced 2025-03-21 12:29:14 +01:00

Move crud function to lib and correct prefix bug

This commit is contained in:
Brice Maron 2012-07-01 15:45:37 +00:00
parent e288f479cd
commit 1c24f862f6
4 changed files with 130 additions and 142 deletions

View File

@ -38,10 +38,10 @@ if(isset($_POST['url'])) {
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);
OC_Bookmarks_Bookmarks::editBookmark($bm, $_POST['url'], $_POST['title'], $tags, $_POST['desc'], $pub);
}
else {
$bm = addBookmark($_POST['url'], $_POST['title'], $tags, $_POST['desc'], $pub);
$bm = OC_Bookmarks_Bookmarks::addBookmark($_POST['url'], $_POST['title'], $tags, $_POST['desc'], $pub);
}
OCP\JSON::success(array('id'=>$bm));
exit();

View File

@ -78,101 +78,4 @@ function analyzeTagRequest($line) {
$filterTag[] = trim($tag);
}
return $filterTag;
}
function getNowValue() {
$CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
$_ut = "strftime('%s','now')";
} elseif($CONFIG_DBTYPE == 'pgsql') {
$_ut = 'date_part(\'epoch\',now())::integer';
} else {
$_ut = "UNIX_TIMESTAMP()";
}
return $_ut;
}
function editBookmark($id, $url, $title, $tags = array(), $description='', $is_public=false) {
$is_public = $is_public ? 1 : 0;
$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)
VALUES (?, ?, ?, ?, $_ut, $_ut, ?)
");
$params=array(
htmlspecialchars_decode($url),
htmlspecialchars_decode($title),
OCP\USER::getUser(),
$is_public,
$description,
);
$query->execute($params);
$b_id = OCP\DB::insertid('*PREFIX*bookmarks');
if($b_id !== false) {
addTags($b_id, $tags);
return $b_id;
}
}
}

View File

@ -1,41 +0,0 @@
diff a/bookmarks/bookmarksHelper.php b/bookmarks/bookmarksHelper.php (rejected hunks)
@@ -79,29 +79,21 @@ function addBookmark($url, $title, $tags='') {
} else {
$_ut = "UNIX_TIMESTAMP()";
}
-
+
+ $is_public = $is_public ? 1 : 0;
//FIXME: Detect when user adds a known URL
$query = OCP\DB::prepare("
INSERT INTO *PREFIX*bookmarks
- (url, title, user_id, public, added, lastmodified)
- VALUES (?, ?, ?, 0, $_ut, $_ut)
+ (url, title, user_id, public, added, lastmodified, description)
+ VALUES (?, ?, ?, ?, $_ut, $_ut, ?)
");
-
- if(empty($title)) {
- $metadata = getURLMetadata($url);
- if(isset($metadata['title'])) // Check for problems fetching the title
- $title = $metadata['title'];
- }
-
- if(empty($title)) {
- $l = OC_L10N::get('bookmarks');
- $title = $l->t('unnamed');
- }
-
+
$params=array(
- htmlspecialchars_decode($url),
- htmlspecialchars_decode($title),
- OCP\USER::getUser()
+ htmlspecialchars_decode($url),
+ htmlspecialchars_decode($title),
+ OCP\USER::getUser(),
+ $is_public,
+ $description,
);
$query->execute($params);

View File

@ -66,7 +66,7 @@ class OC_Bookmarks_Bookmarks{
$limit = 10;
$params=array(OCP\USER::getUser());
//@TODO replace GROUP_CONCAT for postgresql
$sql = "SELECT *, (select GROUP_CONCAT(tag) from oc_bookmarks_tags where bookmark_id = b.id) as tags
$sql = "SELECT *, (select GROUP_CONCAT(tag) from *PREFIX*bookmarks_tags where bookmark_id = b.id) as tags
FROM *PREFIX*bookmarks b
WHERE user_id = ? ";
@ -163,4 +163,130 @@ class OC_Bookmarks_Bookmarks{
$result = $query->execute($params);
return true;
}
/**
* get a string corresponding to the current time depending
* of the OC database system
* @return string
*/
protected static function getNowValue() {
$CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
$_ut = "strftime('%s','now')";
} elseif($CONFIG_DBTYPE == 'pgsql') {
$_ut = 'date_part(\'epoch\',now())::integer';
} else {
$_ut = "UNIX_TIMESTAMP()";
}
return $_ut;
}
/**
* Edit a bookmark
* @param int $id The id of the bookmark to edit
* @param string $url
* @param string $title Name of the bookmark
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
* @param string $description A longer description about the bookmark
* @param boolean $is_public True if the bookmark is publishable to not registered users
* @return null
*/
public static function editBookmark($id, $url, $title, $tags = array(), $description='', $is_public=false) {
$is_public = $is_public ? 1 : 0;
$user_id = OCP\USER::getUser();
// Update the record
$query = OCP\DB::prepare("
UPDATE *PREFIX*bookmarks SET
url = ?, title = ?, public = ?, description = ?,
lastmodified = ".self::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
self::addTags($id, $tags);
}
/**
* Add a bookmark
* @param string $url
* @param string $title Name of the bookmark
* @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values)
* @param string $description A longer description about the bookmark
* @param boolean $is_public True if the bookmark is publishable to not registered users
* @return int The id of the bookmark created
*/
public static 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 = self::getNowValue();
$query = OCP\DB::prepare("
INSERT INTO *PREFIX*bookmarks
(url, title, user_id, public, added, lastmodified, description)
VALUES (?, ?, ?, ?, $_ut, $_ut, ?)
");
$params=array(
htmlspecialchars_decode($url),
htmlspecialchars_decode($title),
OCP\USER::getUser(),
$is_public,
$description,
);
$query->execute($params);
$b_id = OCP\DB::insertid('*PREFIX*bookmarks');
if($b_id !== false) {
self::addTags($b_id, $tags);
return $b_id;
}
}
/**
* Add a set of tags for a bookmark
* @param int $bookmark_id The bookmark reference
* @param array $tags Set of tags to add to the bookmark
* @return null
**/
private static 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);
}
}
}