Skip to content

Commit

Permalink
make it possible to request specific extra items in item list
Browse files Browse the repository at this point in the history
  • Loading branch information
niol committed Dec 22, 2016
1 parent 90fdba0 commit 2f87dd1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
42 changes: 26 additions & 16 deletions daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ public function cleanup(\DateTime $date = NULL) {
*/
public function get($options = array()) {
$params = array();
$where = '';
$where = array('TRUE');
$order = 'DESC';

// only starred
if(isset($options['type']) && $options['type']=='starred')
$where .= ' AND '.$this->stmt->isTrue('starred');
$where[] = $this->stmt->isTrue('starred');

// only unread
else if(isset($options['type']) && $options['type']=='unread'){
$where .= ' AND '.$this->stmt->isTrue('unread');
$where[] = $this->stmt->isTrue('unread');
if(\F3::get('unread_order')=='asc'){
$order = 'ASC';
}
Expand All @@ -212,30 +212,40 @@ public function get($options = array()) {
if(isset($options['search']) && strlen($options['search'])>0) {
$search = implode('%', \helpers\Search::splitTerms($options['search']));
$params[':search'] = $params[':search2'] = $params[':search3'] = array("%".$search."%", \PDO::PARAM_STR);
$where .= ' AND (items.title LIKE :search OR items.content LIKE :search2 OR sources.title LIKE :search3) ';
$where[] = '(items.title LIKE :search OR items.content LIKE :search2 OR sources.title LIKE :search3) ';
}

// tag filter
if(isset($options['tag']) && strlen($options['tag'])>0) {
$params[':tag'] = array( "%,".$options['tag'].",%" , \PDO::PARAM_STR );
if ( \F3::get( 'db_type' ) == 'mysql' ) {
$where .= " AND ( CONCAT( ',' , sources.tags , ',' ) LIKE _utf8mb4 :tag COLLATE utf8mb4_general_ci ) ";
} else {
$where .= " AND ( (',' || sources.tags || ',') LIKE :tag ) ";
}
$params[':tag'] = $options['tag'];
$where[] = "items.source=sources.id";
$where[] = $this->stmt->csvRowMatches('sources.tags', ':tag');
}
// source filter
elseif(isset($options['source']) && strlen($options['source'])>0) {
$params[':source'] = array($options['source'], \PDO::PARAM_INT);
$where .= " AND items.source=:source ";
$where[] = "items.source=:source ";
}

// update time filter
if(isset($options['updatedsince']) && strlen($options['updatedsince'])>0) {
$params[':updatedsince'] = array($options['updatedsince'], \PDO::PARAM_STR);
$where .= " AND items.updatetime > :updatedsince ";
$where[] = "items.updatetime > :updatedsince ";
}

$where_ids = '';
// extra ids to include in stream
if( isset($options['extra_ids']) ) {
$extra_ids_stmt = $this->stmt->intRowMatches('items.id',
$options['extra_ids']);
if( !is_null($extra_ids_stmt) )
$where_ids = $extra_ids_stmt;
}

// finalize items filter
$where_sql = implode(' AND ', $where);
if( $where_ids != '' ) $where_sql = "(($where_sql) OR $where_ids)";

// set limit
if(!is_numeric($options['items']) || $options['items']>200)
$options['items'] = \F3::get('items_perpage');
Expand All @@ -247,16 +257,16 @@ public function get($options = array()) {
// first check whether more items are available
$result = \F3::get('db')->exec('SELECT items.id
FROM '.\F3::get('db_prefix').'items AS items, '.\F3::get('db_prefix').'sources AS sources
WHERE items.source=sources.id '.$where.'
WHERE items.source=sources.id AND '.$where_sql.'
LIMIT 1 OFFSET ' . ($options['offset']+$options['items']), $params);
$this->hasMore = count($result);

// get items from database
return \F3::get('db')->exec('SELECT
return \F3::get('db')->exec('SELECT
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, author, sources.title as sourcetitle, sources.tags as tags
FROM '.\F3::get('db_prefix').'items AS items, '.\F3::get('db_prefix').'sources AS sources
WHERE items.source=sources.id '.$where.'
ORDER BY items.datetime '.$order.'
WHERE items.source=sources.id AND '.$where_sql.'
ORDER BY items.datetime '.$order.'
LIMIT ' . $options['items'] . ' OFFSET '. $options['offset'], $params);
}

Expand Down
24 changes: 24 additions & 0 deletions daos/mysql/Statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,28 @@ public static function csvRowMatches($column, $value) {

return "CONCAT(',', $column, ',') LIKE CONCAT('%,', $value, ',%') COLLATE utf8mb4_general_ci";
}

/**
* check column against int list.
*
* @param int column to check
* @param array of string or int values to match column against
* @return full statement
*/
public static function intRowMatches($column, $ints) {
// checks types
if( !is_array($ints) && sizeof($ints) < 1 ) return null;
$all_ints = array();
foreach( $ints as $ints_str ) {
$i = (int)$ints_str;
if( $i > 0 ) $all_ints[] = $i;
}

if( sizeof($all_ints) > 0 ) {
$comma_ints = implode(',', $all_ints);
return $column." IN ($comma_ints)";
}

return null;
}
}
1 change: 1 addition & 0 deletions public/js/selfoss-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var selfoss = {
tag: '',
source: '',
sourcesNav: false,
extra_ids: [],
ajax: true
},

Expand Down

0 comments on commit 2f87dd1

Please sign in to comment.