2012-01-29 19:32:33 +01:00
< ? php
/**
* ownCloud - bookmarks plugin
*
* @ author Arthur Schiwon
* @ copyright 2011 Arthur Schiwon blizzz @ arthur - schiwon . de
*
* 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 Affero General Public
* License along with this library . If not , see < http :// www . gnu . org / licenses />.
*
*/
/**
* This class manages bookmarks
*/
class OC_Bookmarks_Bookmarks {
2012-06-27 17:37:38 +02:00
/**
* @ brief Find People with whome we shared bookmarks and how much
*/
public static function findSharing ( $offset = 0 , $limit = 10 ){
$query = OCP\DB :: prepare ( 'SELECT \'@public\' as name, count(*) as nbr from *PREFIX*bookmarks where public=1 group by public LIMIT ' . $offset . ', ' . $limit );
$tags = $query -> execute () -> fetchAll ();
return $tags ;
}
2012-06-26 17:43:03 +02:00
/**
* @ brief Finds all tags for bookmarks
*/
2012-06-27 23:44:20 +02:00
public static function findTags ( $filterTags = array (), $offset = 0 , $limit = 10 ){
//$query = OCP\DB::prepare('SELECT tag, count(*) as nbr from *PREFIX*bookmarks_tags group by tag LIMIT '.$offset.', '.$limit);
$params = array_merge ( $filterTags , $filterTags );
$not_in = '' ;
if ( ! empty ( $filterTags ) ) {
$not_in = ' where tag not in (' . implode ( ',' , array_fill ( 0 , count ( $filterTags ) , '?' ) ) . ')' .
str_repeat ( " AND exists (select 1 from *PREFIX*bookmarks_tags t2 where t2.bookmark_id = t.bookmark_id and tag = ?) " , count ( $filterTags ));
}
$sql = 'SELECT tag, count(*) as nbr from *PREFIX*bookmarks_tags t ' . $not_in .
'group by tag Order by nbr DESC LIMIT ' . $offset . ', ' . $limit ;
$query = OCP\DB :: prepare ( $sql );
$tags = $query -> execute ( $params ) -> fetchAll ();
2012-06-26 17:43:03 +02:00
return $tags ;
}
2012-01-29 19:32:33 +01:00
/**
* @ brief Finds all bookmarks , matching the filter
* @ param offset result offset
* @ param sqlSortColumn sort result with this column
* @ param filter can be : empty -> no filter , a string -> filter this , a string array -> filter for all strings
2012-06-27 14:14:45 +02:00
* @ param filterTagOnly if true , filter affects only tags , else filter affects url , title and tags
2012-01-29 19:32:33 +01:00
* @ return void
*/
public static function findBookmarks ( $offset , $sqlSortColumn , $filter , $filterTagOnly ){
2012-05-02 13:28:56 +02:00
$CONFIG_DBTYPE = OCP\Config :: getSystemValue ( 'dbtype' , 'sqlite' );
2012-06-27 14:14:45 +02:00
$limit = 10 ;
2012-05-01 18:50:31 +02:00
$params = array ( OCP\USER :: getUser ());
2012-06-27 23:44:20 +02:00
//@TODO replace GROUP_CONCAT for postgresql
$sql = " SELECT *, (select GROUP_CONCAT(tag) from oc_bookmarks_tags where bookmark_id = b.id) as tags
FROM * PREFIX * bookmarks b
WHERE user_id = ? " ;
2012-06-27 14:14:45 +02:00
2012-06-27 23:44:20 +02:00
if ( $filterTagOnly ) {
if ( is_string ( $filter )) $filter = array ( $filter );
2012-01-29 19:32:33 +01:00
2012-06-27 23:44:20 +02:00
$sql .= str_repeat ( " AND exists (select id from *PREFIX*bookmarks_tags t2 where t2.bookmark_id = b.id and tag = ?) " , count ( $filter ));
$params = array_merge ( $params , $filter );
2012-01-29 19:32:33 +01:00
}
2012-06-27 23:44:20 +02:00
$sql .= " ORDER BY " . $sqlSortColumn . " DESC
LIMIT $limit
OFFSET $offset " ;
2012-01-29 19:32:33 +01:00
2012-06-27 23:44:20 +02:00
$query = OCP\DB :: prepare ( $sql );
2012-06-27 14:14:45 +02:00
$results = $query -> execute ( $params ) -> fetchAll ();
$bookmarks = array ();
foreach ( $results as $result ){
$result [ 'tags' ] = explode ( ',' , $result [ 'tags' ]);
$bookmarks [] = $result ;
}
2012-01-29 19:32:33 +01:00
return $bookmarks ;
}
2012-05-03 17:29:08 +02:00
public static function deleteUrl ( $id )
{
$user = OCP\USER :: getUser ();
$query = OCP\DB :: prepare ( "
2012-07-30 20:46:14 +02:00
SELECT `id` FROM `*PREFIX*bookmarks`
WHERE `id` = ?
AND `user_id` = ?
2012-05-03 17:29:08 +02:00
" );
$result = $query -> execute ( array ( $id , $user ));
$id = $result -> fetchOne ();
if ( $id === false ) {
return false ;
}
$query = OCP\DB :: prepare ( "
2012-07-30 20:46:14 +02:00
DELETE FROM `*PREFIX*bookmarks`
WHERE `id` = $id
2012-05-03 17:29:08 +02:00
" );
$result = $query -> execute ();
$query = OCP\DB :: prepare ( "
2012-07-30 20:46:14 +02:00
DELETE FROM `*PREFIX*bookmarks_tags`
WHERE `bookmark_id` = $id
2012-05-03 17:29:08 +02:00
" );
$result = $query -> execute ();
return true ;
}
2012-01-29 19:32:33 +01:00
}