CategoryDAO.php 6.5 KB

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