CategoryDAO.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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` ')
  89. . 'FROM `' . $this->prefix . 'category` c '
  90. . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
  91. . 'GROUP BY f.id, c_id '
  92. . 'ORDER BY c.name, f.name';
  93. $stm = $this->bd->prepare($sql);
  94. $stm->execute();
  95. return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
  96. } else {
  97. $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
  98. $stm = $this->bd->prepare($sql);
  99. $stm->execute();
  100. return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
  101. }
  102. }
  103. public function getDefault() {
  104. $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=' . self::defaultCategoryId;
  105. $stm = $this->bd->prepare($sql);
  106. $stm->execute();
  107. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  108. $cat = self::daoToCategory($res);
  109. if (isset($cat[0])) {
  110. return $cat[0];
  111. } else {
  112. return false;
  113. }
  114. }
  115. public function checkDefault() {
  116. $def_cat = $this->searchById(self::defaultCategoryId);
  117. if ($def_cat == null) {
  118. $cat = new FreshRSS_Category(_t('gen.short.default_category'));
  119. $cat->_id(self::defaultCategoryId);
  120. $values = array(
  121. 'id' => $cat->id(),
  122. 'name' => $cat->name(),
  123. );
  124. $this->addCategory($values);
  125. }
  126. }
  127. public function count() {
  128. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
  129. $stm = $this->bd->prepare($sql);
  130. $stm->execute();
  131. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  132. return $res[0]['count'];
  133. }
  134. public function countFeed($id) {
  135. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
  136. $stm = $this->bd->prepare($sql);
  137. $values = array($id);
  138. $stm->execute($values);
  139. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  140. return $res[0]['count'];
  141. }
  142. public function countNotRead($id) {
  143. $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';
  144. $stm = $this->bd->prepare($sql);
  145. $values = array($id);
  146. $stm->execute($values);
  147. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  148. return $res[0]['count'];
  149. }
  150. public static function findFeed($categories, $feed_id) {
  151. foreach ($categories as $category) {
  152. foreach ($category->feeds() as $feed) {
  153. if ($feed->id() === $feed_id) {
  154. return $feed;
  155. }
  156. }
  157. }
  158. return null;
  159. }
  160. public static function CountUnreads($categories, $minPriority = 0) {
  161. $n = 0;
  162. foreach ($categories as $category) {
  163. foreach ($category->feeds() as $feed) {
  164. if ($feed->priority() >= $minPriority) {
  165. $n += $feed->nbNotRead();
  166. }
  167. }
  168. }
  169. return $n;
  170. }
  171. public static function daoToCategoryPrepopulated($listDAO) {
  172. $list = array();
  173. if (!is_array($listDAO)) {
  174. $listDAO = array($listDAO);
  175. }
  176. $previousLine = null;
  177. $feedsDao = array();
  178. $feedDao = FreshRSS_Factory::createFeedDAO();
  179. foreach ($listDAO as $line) {
  180. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  181. // End of the current category, we add it to the $list
  182. $cat = new FreshRSS_Category(
  183. $previousLine['c_name'],
  184. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  185. );
  186. $cat->_id($previousLine['c_id']);
  187. $list[$previousLine['c_id']] = $cat;
  188. $feedsDao = array(); //Prepare for next category
  189. }
  190. $previousLine = $line;
  191. $feedsDao[] = $line;
  192. }
  193. // add the last category
  194. if ($previousLine != null) {
  195. $cat = new FreshRSS_Category(
  196. $previousLine['c_name'],
  197. $feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
  198. );
  199. $cat->_id($previousLine['c_id']);
  200. $list[$previousLine['c_id']] = $cat;
  201. }
  202. return $list;
  203. }
  204. public static function daoToCategory($listDAO) {
  205. $list = array();
  206. if (!is_array($listDAO)) {
  207. $listDAO = array($listDAO);
  208. }
  209. foreach ($listDAO as $key => $dao) {
  210. $cat = new FreshRSS_Category(
  211. $dao['name']
  212. );
  213. $cat->_id($dao['id']);
  214. $list[$key] = $cat;
  215. }
  216. return $list;
  217. }
  218. }