CategoryDAO.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. const DEFAULTCATEGORYID = 1;
  4. public function addCategory($valuesTmp) {
  5. $sql = 'INSERT INTO `' . $this->prefix . 'category`(name) VALUES(?)';
  6. $stm = $this->bd->prepare($sql);
  7. $values = array(
  8. substr($valuesTmp['name'], 0, 255),
  9. );
  10. if ($stm && $stm->execute($values)) {
  11. return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
  12. } else {
  13. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  14. Minz_Log::error('SQL error addCategory: ' . $info[2]);
  15. return false;
  16. }
  17. }
  18. public function addCategoryObject($category) {
  19. $cat = $this->searchByName($category->name());
  20. if (!$cat) {
  21. // Category does not exist yet in DB so we add it before continue
  22. $values = array(
  23. 'name' => $category->name(),
  24. );
  25. return $this->addCategory($values);
  26. }
  27. return $cat->id();
  28. }
  29. public function updateCategory($id, $valuesTmp) {
  30. $sql = 'UPDATE `' . $this->prefix . 'category` SET name=? WHERE id=?';
  31. $stm = $this->bd->prepare($sql);
  32. $values = array(
  33. $valuesTmp['name'],
  34. $id
  35. );
  36. if ($stm && $stm->execute($values)) {
  37. return $stm->rowCount();
  38. } else {
  39. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  40. Minz_Log::error('SQL error updateCategory: ' . $info[2]);
  41. return false;
  42. }
  43. }
  44. public function deleteCategory($id) {
  45. if ($id <= self::DEFAULTCATEGORYID) {
  46. return false;
  47. }
  48. $sql = 'DELETE FROM `' . $this->prefix . 'category` WHERE id=?';
  49. $stm = $this->bd->prepare($sql);
  50. $values = array($id);
  51. if ($stm && $stm->execute($values)) {
  52. return $stm->rowCount();
  53. } else {
  54. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  55. Minz_Log::error('SQL error deleteCategory: ' . $info[2]);
  56. return false;
  57. }
  58. }
  59. public function searchById($id) {
  60. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
  61. $stm = $this->bd->prepare($sql);
  62. $values = array($id);
  63. $stm->execute($values);
  64. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  65. $cat = self::daoToCategory($res);
  66. if (isset($cat[0])) {
  67. return $cat[0];
  68. } else {
  69. return null;
  70. }
  71. }
  72. public function searchByName($name) {
  73. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
  74. $stm = $this->bd->prepare($sql);
  75. $values = array($name);
  76. $stm->execute($values);
  77. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  78. $cat = self::daoToCategory($res);
  79. if (isset($cat[0])) {
  80. return $cat[0];
  81. } else {
  82. return null;
  83. }
  84. }
  85. public function listCategories($prePopulateFeeds = true, $details = false) {
  86. if ($prePopulateFeeds) {
  87. $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
  88. . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
  89. . 'FROM `' . $this->prefix . 'category` c '
  90. . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
  91. . 'WHERE f.priority >= :priority_normal '
  92. . 'GROUP BY f.id, c_id '
  93. . 'ORDER BY c.name, f.name';
  94. $stm = $this->bd->prepare($sql);
  95. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  96. return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
  97. } else {
  98. $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
  99. $stm = $this->bd->prepare($sql);
  100. $stm->execute();
  101. return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
  102. }
  103. }
  104. public function getDefault() {
  105. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=' . self::DEFAULTCATEGORYID;
  106. $stm = $this->bd->prepare($sql);
  107. $stm->execute();
  108. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  109. $cat = self::daoToCategory($res);
  110. if (isset($cat[0])) {
  111. return $cat[0];
  112. } else {
  113. return false;
  114. }
  115. }
  116. public function checkDefault() {
  117. $def_cat = $this->searchById(self::DEFAULTCATEGORYID);
  118. if ($def_cat == null) {
  119. $cat = new FreshRSS_Category(_t('gen.short.default_category'));
  120. $cat->_id(self::DEFAULTCATEGORYID);
  121. $values = array(
  122. 'id' => $cat->id(),
  123. 'name' => $cat->name(),
  124. );
  125. $this->addCategory($values);
  126. }
  127. }
  128. public function count() {
  129. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
  130. $stm = $this->bd->prepare($sql);
  131. $stm->execute();
  132. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  133. return $res[0]['count'];
  134. }
  135. public function countFeed($id) {
  136. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
  137. $stm = $this->bd->prepare($sql);
  138. $values = array($id);
  139. $stm->execute($values);
  140. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  141. return $res[0]['count'];
  142. }
  143. public function countNotRead($id) {
  144. $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';
  145. $stm = $this->bd->prepare($sql);
  146. $values = array($id);
  147. $stm->execute($values);
  148. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  149. return $res[0]['count'];
  150. }
  151. public static function findFeed($categories, $feed_id) {
  152. foreach ($categories as $category) {
  153. foreach ($category->feeds() as $feed) {
  154. if ($feed->id() === $feed_id) {
  155. return $feed;
  156. }
  157. }
  158. }
  159. return null;
  160. }
  161. public static function CountUnreads($categories, $minPriority = 0) {
  162. $n = 0;
  163. foreach ($categories as $category) {
  164. foreach ($category->feeds() as $feed) {
  165. if ($feed->priority() >= $minPriority) {
  166. $n += $feed->nbNotRead();
  167. }
  168. }
  169. }
  170. return $n;
  171. }
  172. public static function daoToCategoryPrepopulated($listDAO) {
  173. $list = array();
  174. if (!is_array($listDAO)) {
  175. $listDAO = array($listDAO);
  176. }
  177. $previousLine = null;
  178. $feedsDao = array();
  179. $feedDao = FreshRSS_Factory::createFeedDAO();
  180. foreach ($listDAO as $line) {
  181. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  182. // End of the current category, we add it to the $list
  183. $cat = new FreshRSS_Category(
  184. $previousLine['c_name'],
  185. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  186. );
  187. $cat->_id($previousLine['c_id']);
  188. $list[$previousLine['c_id']] = $cat;
  189. $feedsDao = array(); //Prepare for next category
  190. }
  191. $previousLine = $line;
  192. $feedsDao[] = $line;
  193. }
  194. // add the last category
  195. if ($previousLine != null) {
  196. $cat = new FreshRSS_Category(
  197. $previousLine['c_name'],
  198. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  199. );
  200. $cat->_id($previousLine['c_id']);
  201. $list[$previousLine['c_id']] = $cat;
  202. }
  203. return $list;
  204. }
  205. public static function daoToCategory($listDAO) {
  206. $list = array();
  207. if (!is_array($listDAO)) {
  208. $listDAO = array($listDAO);
  209. }
  210. foreach ($listDAO as $key => $dao) {
  211. $cat = new FreshRSS_Category(
  212. $dao['name']
  213. );
  214. $cat->_id($dao['id']);
  215. $list[$key] = $cat;
  216. }
  217. return $list;
  218. }
  219. }