aboutsummaryrefslogtreecommitdiff
path: root/app/models/Category.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-12-15 03:30:24 +0100
commit878e96202e8a22e4857b98e29b0a1fce68eccbc9 (patch)
treef9233c3b48a0cd6e0ac2536ddcc1897201595ad4 /app/models/Category.php
parent4af233e1f736eb2256e5e1696418635165467855 (diff)
Grosse refactorisation pour permettre le chargement automatique des classes
C'est parti de changements pour https://github.com/marienfressinaud/FreshRSS/issues/255 et finalement j'ai continué la refactorisation... Ajout de préfixes FreshRSS_ et Minz_ sur le modèle de SimplePie_. Toutes les classes sont maintenant en chargement automatique (devrait améliorer les performances en évitant de charger plein de classes inutilisées, et faciliter la maintenance). Suppression de set_include_path(). Si souhaité, certaines classes de Minz pourraient être déplacées dans un sous-répertoire, par exemple les exceptions. Tests et relecture nécessaires.
Diffstat (limited to 'app/models/Category.php')
-rwxr-xr-xapp/models/Category.php260
1 files changed, 4 insertions, 256 deletions
diff --git a/app/models/Category.php b/app/models/Category.php
index 5915132f4..e70d1303f 100755
--- a/app/models/Category.php
+++ b/app/models/Category.php
@@ -1,6 +1,6 @@
<?php
-class Category extends Model {
+class FreshRSS_Category extends Minz_Model {
private $id = 0;
private $name;
private $color;
@@ -33,7 +33,7 @@ class Category extends Model {
}
public function nbFeed () {
if ($this->nbFeed < 0) {
- $catDAO = new CategoryDAO ();
+ $catDAO = new FreshRSS_CategoryDAO ();
$this->nbFeed = $catDAO->countFeed ($this->id ());
}
@@ -41,7 +41,7 @@ class Category extends Model {
}
public function nbNotRead () {
if ($this->nbNotRead < 0) {
- $catDAO = new CategoryDAO ();
+ $catDAO = new FreshRSS_CategoryDAO ();
$this->nbNotRead = $catDAO->countNotRead ($this->id ());
}
@@ -49,7 +49,7 @@ class Category extends Model {
}
public function feeds () {
if (is_null ($this->feeds)) {
- $feedDAO = new FeedDAO ();
+ $feedDAO = new FreshRSS_FeedDAO ();
$this->feeds = $feedDAO->listByCategory ($this->id ());
$this->nbFeed = 0;
$this->nbNotRead = 0;
@@ -83,255 +83,3 @@ class Category extends Model {
$this->feeds = $values;
}
}
-
-class CategoryDAO extends Model_pdo {
- public function addCategory ($valuesTmp) {
- $sql = 'INSERT INTO `' . $this->prefix . 'category` (name, color) VALUES(?, ?)';
- $stm = $this->bd->prepare ($sql);
-
- $values = array (
- substr($valuesTmp['name'], 0, 255),
- substr($valuesTmp['color'], 0, 7),
- );
-
- if ($stm && $stm->execute ($values)) {
- return $this->bd->lastInsertId();
- } else {
- $info = $stm->errorInfo();
- Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
- return false;
- }
- }
-
- public function updateCategory ($id, $valuesTmp) {
- $sql = 'UPDATE `' . $this->prefix . 'category` SET name=?, color=? WHERE id=?';
- $stm = $this->bd->prepare ($sql);
-
- $values = array (
- $valuesTmp['name'],
- $valuesTmp['color'],
- $id
- );
-
- if ($stm && $stm->execute ($values)) {
- return $stm->rowCount();
- } else {
- $info = $stm->errorInfo();
- Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
- return false;
- }
- }
-
- public function deleteCategory ($id) {
- $sql = 'DELETE FROM `' . $this->prefix . 'category` WHERE id=?';
- $stm = $this->bd->prepare ($sql);
-
- $values = array ($id);
-
- if ($stm && $stm->execute ($values)) {
- return $stm->rowCount();
- } else {
- $info = $stm->errorInfo();
- Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
- return false;
- }
- }
-
- public function searchById ($id) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
- $stm = $this->bd->prepare ($sql);
-
- $values = array ($id);
-
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $cat = HelperCategory::daoToCategory ($res);
-
- if (isset ($cat[0])) {
- return $cat[0];
- } else {
- return false;
- }
- }
- public function searchByName ($name) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
- $stm = $this->bd->prepare ($sql);
-
- $values = array ($name);
-
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $cat = HelperCategory::daoToCategory ($res);
-
- if (isset ($cat[0])) {
- return $cat[0];
- } else {
- return false;
- }
- }
-
- public function listCategories ($prePopulateFeeds = true, $details = false) {
- if ($prePopulateFeeds) {
- $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
- . ($details ? 'c.color AS c_color, ' : '')
- . ($details ? 'f.* ' : 'f.id, f.name, 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';
- $stm = $this->bd->prepare ($sql);
- $stm->execute ();
- return HelperCategory::daoToCategoryPrepopulated ($stm->fetchAll (PDO::FETCH_ASSOC));
- } else {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
- $stm = $this->bd->prepare ($sql);
- $stm->execute ();
- return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
- }
- }
-
- public function getDefault () {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=1';
- $stm = $this->bd->prepare ($sql);
-
- $stm->execute ();
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $cat = HelperCategory::daoToCategory ($res);
-
- if (isset ($cat[0])) {
- return $cat[0];
- } else {
- return false;
- }
- }
- public function checkDefault () {
- $def_cat = $this->searchById (1);
-
- if ($def_cat === false) {
- $cat = new Category (Translate::t ('default_category'));
- $cat->_id (1);
-
- $values = array (
- 'id' => $cat->id (),
- 'name' => $cat->name (),
- 'color' => $cat->color ()
- );
-
- $this->addCategory ($values);
- }
- }
-
- public function count () {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
- $stm = $this->bd->prepare ($sql);
- $stm->execute ();
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
-
- return $res[0]['count'];
- }
-
- public function countFeed ($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
- $stm = $this->bd->prepare ($sql);
- $values = array ($id);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
-
- return $res[0]['count'];
- }
-
- public function countNotRead ($id) {
- $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';
- $stm = $this->bd->prepare ($sql);
- $values = array ($id);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
-
- return $res[0]['count'];
- }
-}
-
-class HelperCategory {
- public static function findFeed($categories, $feed_id) {
- foreach ($categories as $category) {
- foreach ($category->feeds () as $feed) {
- if ($feed->id () === $feed_id) {
- return $feed;
- }
- }
- }
- return null;
- }
-
- public static function CountUnreads($categories, $minPriority = 0) {
- $n = 0;
- foreach ($categories as $category) {
- foreach ($category->feeds () as $feed) {
- if ($feed->priority () >= $minPriority) {
- $n += $feed->nbNotRead();
- }
- }
- }
- return $n;
- }
-
- public static function daoToCategoryPrepopulated ($listDAO) {
- $list = array ();
-
- if (!is_array ($listDAO)) {
- $listDAO = array ($listDAO);
- }
-
- $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
- $cat = new Category (
- $previousLine['c_name'],
- isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
- HelperFeed::daoToFeed ($feedsDao, $previousLine['c_id'])
- );
- $cat->_id ($previousLine['c_id']);
- $list[$previousLine['c_id']] = $cat;
-
- $feedsDao = array(); //Prepare for next category
- }
-
- $previousLine = $line;
- $feedsDao[] = $line;
- }
-
- // add the last category
- if ($previousLine != null) {
- $cat = new Category (
- $previousLine['c_name'],
- isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
- HelperFeed::daoToFeed ($feedsDao, $previousLine['c_id'])
- );
- $cat->_id ($previousLine['c_id']);
- $list[$previousLine['c_id']] = $cat;
- }
-
- return $list;
- }
-
- public static function daoToCategory ($listDAO) {
- $list = array ();
-
- if (!is_array ($listDAO)) {
- $listDAO = array ($listDAO);
- }
-
- foreach ($listDAO as $key => $dao) {
- $cat = new Category (
- $dao['name'],
- $dao['color']
- );
- $cat->_id ($dao['id']);
- $list[$key] = $cat;
- }
-
- return $list;
- }
-}