1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/freshrss_ynh.git synced 2024-09-03 18:36:33 +02:00
freshrss_ynh/sources/app/Models/CategoryDAO.php

258 lines
6.3 KiB
PHP
Raw Normal View History

2014-07-23 15:52:50 +02:00
<?php
class FreshRSS_CategoryDAO extends Minz_ModelPdo {
2015-02-08 18:55:48 +01:00
public function addCategory($valuesTmp) {
$sql = 'INSERT INTO `' . $this->prefix . 'category`(name) VALUES(?)';
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array(
2014-07-23 15:52:50 +02:00
substr($valuesTmp['name'], 0, 255),
);
2015-02-08 18:55:48 +01:00
if ($stm && $stm->execute($values)) {
2014-07-23 15:52:50 +02:00
return $this->bd->lastInsertId();
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
2015-02-08 18:55:48 +01:00
Minz_Log::error('SQL error addCategory: ' . $info[2] );
2014-07-23 15:52:50 +02:00
return false;
}
}
public function addCategoryObject($category) {
$cat = $this->searchByName($category->name());
if (!$cat) {
// Category does not exist yet in DB so we add it before continue
$values = array(
'name' => $category->name(),
);
return $this->addCategory($values);
}
return $cat->id();
}
2015-02-08 18:55:48 +01:00
public function updateCategory($id, $valuesTmp) {
2014-07-23 15:52:50 +02:00
$sql = 'UPDATE `' . $this->prefix . 'category` SET name=? WHERE id=?';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array(
2014-07-23 15:52:50 +02:00
$valuesTmp['name'],
$id
);
2015-02-08 18:55:48 +01:00
if ($stm && $stm->execute($values)) {
2014-07-23 15:52:50 +02:00
return $stm->rowCount();
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
2015-02-08 18:55:48 +01:00
Minz_Log::error('SQL error updateCategory: ' . $info[2]);
2014-07-23 15:52:50 +02:00
return false;
}
}
2015-02-08 18:55:48 +01:00
public function deleteCategory($id) {
2014-07-23 15:52:50 +02:00
$sql = 'DELETE FROM `' . $this->prefix . 'category` WHERE id=?';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array($id);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if ($stm && $stm->execute($values)) {
2014-07-23 15:52:50 +02:00
return $stm->rowCount();
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
2015-02-08 18:55:48 +01:00
Minz_Log::error('SQL error deleteCategory: ' . $info[2]);
2014-07-23 15:52:50 +02:00
return false;
}
}
2015-02-08 18:55:48 +01:00
public function searchById($id) {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array($id);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (isset($cat[0])) {
2014-07-23 15:52:50 +02:00
return $cat[0];
} else {
return null;
}
}
2015-02-08 18:55:48 +01:00
public function searchByName($name) {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array($name);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (isset($cat[0])) {
2014-07-23 15:52:50 +02:00
return $cat[0];
} else {
return null;
}
}
2015-02-08 18:55:48 +01:00
public function listCategories($prePopulateFeeds = true, $details = false) {
2014-07-23 15:52:50 +02:00
if ($prePopulateFeeds) {
$sql = 'SELECT c.id AS c_id, c.name AS c_name, '
. ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.cache_nbEntries, f.cache_nbUnreads ')
. 'FROM `' . $this->prefix . 'category` c '
. 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
. 'GROUP BY f.id '
. 'ORDER BY c.name, f.name';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
$stm->execute();
return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
2014-07-23 15:52:50 +02:00
} else {
$sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
$stm->execute();
return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
2014-07-23 15:52:50 +02:00
}
}
2015-02-08 18:55:48 +01:00
public function getDefault() {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=1';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (isset($cat[0])) {
2014-07-23 15:52:50 +02:00
return $cat[0];
} else {
return false;
}
}
2015-02-08 18:55:48 +01:00
public function checkDefault() {
$def_cat = $this->searchById(1);
2014-07-23 15:52:50 +02:00
if ($def_cat == null) {
2015-02-08 18:55:48 +01:00
$cat = new FreshRSS_Category(_t('gen.short.default_category'));
$cat->_id(1);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$values = array(
'id' => $cat->id(),
'name' => $cat->name(),
2014-07-23 15:52:50 +02:00
);
2015-02-08 18:55:48 +01:00
$this->addCategory($values);
2014-07-23 15:52:50 +02:00
}
}
2015-02-08 18:55:48 +01:00
public function count() {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
$stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
2014-07-23 15:52:50 +02:00
return $res[0]['count'];
}
2015-02-08 18:55:48 +01:00
public function countFeed($id) {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
2014-07-23 15:52:50 +02:00
return $res[0]['count'];
}
2015-02-08 18:55:48 +01:00
public function countNotRead($id) {
2014-07-23 15:52:50 +02:00
$sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE category=? AND e.is_read=0';
2015-02-08 18:55:48 +01:00
$stm = $this->bd->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
2014-07-23 15:52:50 +02:00
return $res[0]['count'];
}
public static function findFeed($categories, $feed_id) {
foreach ($categories as $category) {
2015-02-08 18:55:48 +01:00
foreach ($category->feeds() as $feed) {
if ($feed->id() === $feed_id) {
2014-07-23 15:52:50 +02:00
return $feed;
}
}
}
return null;
}
public static function CountUnreads($categories, $minPriority = 0) {
$n = 0;
foreach ($categories as $category) {
2015-02-08 18:55:48 +01:00
foreach ($category->feeds() as $feed) {
if ($feed->priority() >= $minPriority) {
2014-07-23 15:52:50 +02:00
$n += $feed->nbNotRead();
}
}
}
return $n;
}
2015-02-08 18:55:48 +01:00
public static function daoToCategoryPrepopulated($listDAO) {
$list = array();
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (!is_array($listDAO)) {
$listDAO = array($listDAO);
2014-07-23 15:52:50 +02:00
}
$previousLine = null;
$feedsDao = array();
foreach ($listDAO as $line) {
if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
// End of the current category, we add it to the $list
2015-02-08 18:55:48 +01:00
$cat = new FreshRSS_Category(
2014-07-23 15:52:50 +02:00
$previousLine['c_name'],
2015-02-08 18:55:48 +01:00
FreshRSS_FeedDAO::daoToFeed($feedsDao, $previousLine['c_id'])
2014-07-23 15:52:50 +02:00
);
2015-02-08 18:55:48 +01:00
$cat->_id($previousLine['c_id']);
2014-07-23 15:52:50 +02:00
$list[$previousLine['c_id']] = $cat;
$feedsDao = array(); //Prepare for next category
}
$previousLine = $line;
$feedsDao[] = $line;
}
// add the last category
if ($previousLine != null) {
2015-02-08 18:55:48 +01:00
$cat = new FreshRSS_Category(
2014-07-23 15:52:50 +02:00
$previousLine['c_name'],
2015-02-08 18:55:48 +01:00
FreshRSS_FeedDAO::daoToFeed($feedsDao, $previousLine['c_id'])
2014-07-23 15:52:50 +02:00
);
2015-02-08 18:55:48 +01:00
$cat->_id($previousLine['c_id']);
2014-07-23 15:52:50 +02:00
$list[$previousLine['c_id']] = $cat;
}
return $list;
}
2015-02-08 18:55:48 +01:00
public static function daoToCategory($listDAO) {
$list = array();
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (!is_array($listDAO)) {
$listDAO = array($listDAO);
2014-07-23 15:52:50 +02:00
}
foreach ($listDAO as $key => $dao) {
2015-02-08 18:55:48 +01:00
$cat = new FreshRSS_Category(
2014-07-23 15:52:50 +02:00
$dao['name']
);
2015-02-08 18:55:48 +01:00
$cat->_id($dao['id']);
2014-07-23 15:52:50 +02:00
$list[$key] = $cat;
}
return $list;
}
}