Category.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. class Category extends Model {
  3. private $id = 0;
  4. private $name;
  5. private $color;
  6. private $nbFeed = -1;
  7. private $nbNotRead = -1;
  8. private $feeds = null;
  9. public function __construct ($name = '', $color = '#0062BE', $feeds = null) {
  10. $this->_name ($name);
  11. $this->_color ($color);
  12. if (isset ($feeds)) {
  13. $this->_feeds ($feeds);
  14. $this->nbFeed = 0;
  15. $this->nbNotRead = 0;
  16. foreach ($feeds as $feed) {
  17. $this->nbFeed++;
  18. $this->nbNotRead += $feed->nbNotRead ();
  19. }
  20. }
  21. }
  22. public function id () {
  23. return $this->id;
  24. }
  25. public function name () {
  26. return $this->name;
  27. }
  28. public function color () {
  29. return $this->color;
  30. }
  31. public function nbFeed () {
  32. if ($this->nbFeed < 0) {
  33. $catDAO = new CategoryDAO ();
  34. $this->nbFeed = $catDAO->countFeed ($this->id ());
  35. }
  36. return $this->nbFeed;
  37. }
  38. public function nbNotRead () {
  39. if ($this->nbNotRead < 0) {
  40. $catDAO = new CategoryDAO ();
  41. $this->nbNotRead = $catDAO->countNotRead ($this->id ());
  42. }
  43. return $this->nbNotRead;
  44. }
  45. public function feeds () {
  46. if (is_null ($this->feeds)) {
  47. $feedDAO = new FeedDAO ();
  48. $this->feeds = $feedDAO->listByCategory ($this->id ());
  49. $this->nbFeed = 0;
  50. $this->nbNotRead = 0;
  51. foreach ($this->feeds as $feed) {
  52. $this->nbFeed++;
  53. $this->nbNotRead += $feed->nbNotRead ();
  54. }
  55. }
  56. return $this->feeds;
  57. }
  58. public function _id ($value) {
  59. $this->id = $value;
  60. }
  61. public function _name ($value) {
  62. $this->name = $value;
  63. }
  64. public function _color ($value) {
  65. if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  66. $this->color = $value;
  67. } else {
  68. $this->color = '#0062BE';
  69. }
  70. }
  71. public function _feeds ($values) {
  72. if (!is_array ($values)) {
  73. $values = array ($values);
  74. }
  75. $this->feeds = $values;
  76. }
  77. }
  78. class CategoryDAO extends Model_pdo {
  79. public function addCategory ($valuesTmp) {
  80. $sql = 'INSERT INTO ' . $this->prefix . 'category (name, color) VALUES(?, ?)';
  81. $stm = $this->bd->prepare ($sql);
  82. $values = array (
  83. substr($valuesTmp['name'], 0, 255),
  84. substr($valuesTmp['color'], 0, 7),
  85. );
  86. if ($stm && $stm->execute ($values)) {
  87. return $stm->rowCount();
  88. } else {
  89. $info = $stm->errorInfo();
  90. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  91. return false;
  92. }
  93. }
  94. public function updateCategory ($id, $valuesTmp) {
  95. $sql = 'UPDATE ' . $this->prefix . 'category SET name=?, color=? WHERE id=?';
  96. $stm = $this->bd->prepare ($sql);
  97. $values = array (
  98. $valuesTmp['name'],
  99. $valuesTmp['color'],
  100. $id
  101. );
  102. if ($stm && $stm->execute ($values)) {
  103. return $stm->rowCount();
  104. } else {
  105. $info = $stm->errorInfo();
  106. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  107. return false;
  108. }
  109. }
  110. public function deleteCategory ($id) {
  111. $sql = 'DELETE FROM ' . $this->prefix . 'category WHERE id=?';
  112. $stm = $this->bd->prepare ($sql);
  113. $values = array ($id);
  114. if ($stm && $stm->execute ($values)) {
  115. return $stm->rowCount();
  116. } else {
  117. $info = $stm->errorInfo();
  118. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  119. return false;
  120. }
  121. }
  122. public function searchById ($id) {
  123. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE id=?';
  124. $stm = $this->bd->prepare ($sql);
  125. $values = array ($id);
  126. $stm->execute ($values);
  127. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  128. $cat = HelperCategory::daoToCategory ($res);
  129. if (isset ($cat[0])) {
  130. return $cat[0];
  131. } else {
  132. return false;
  133. }
  134. }
  135. public function searchByName ($name) {
  136. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE name=?';
  137. $stm = $this->bd->prepare ($sql);
  138. $values = array ($name);
  139. $stm->execute ($values);
  140. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  141. $cat = HelperCategory::daoToCategory ($res);
  142. if (isset ($cat[0])) {
  143. return $cat[0];
  144. } else {
  145. return false;
  146. }
  147. }
  148. public function listCategories ($prePopulateFeeds = true, $details = false) {
  149. if ($prePopulateFeeds) {
  150. $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
  151. . ($details ? 'c.color AS c_color, ' : '')
  152. . ($details ? 'f.* ' : 'f.id, f.name, f.website, f.priority, f.error, f.cache_nbEntries, f.cache_nbUnreads ')
  153. . 'FROM ' . $this->prefix . 'category c '
  154. . 'LEFT OUTER JOIN ' . $this->prefix . 'feed f ON f.category = c.id '
  155. . 'GROUP BY f.id '
  156. . 'ORDER BY c.name, f.name';
  157. $stm = $this->bd->prepare ($sql);
  158. $stm->execute ();
  159. return HelperCategory::daoToCategoryPrepopulated ($stm->fetchAll (PDO::FETCH_ASSOC));
  160. } else {
  161. $sql = 'SELECT * FROM ' . $this->prefix . 'category ORDER BY name';
  162. $stm = $this->bd->prepare ($sql);
  163. $stm->execute ();
  164. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  165. }
  166. }
  167. public function getDefault () {
  168. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE id=1';
  169. $stm = $this->bd->prepare ($sql);
  170. $stm->execute ();
  171. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  172. $cat = HelperCategory::daoToCategory ($res);
  173. if (isset ($cat[0])) {
  174. return $cat[0];
  175. } else {
  176. return false;
  177. }
  178. }
  179. public function checkDefault () {
  180. $def_cat = $this->searchById (1);
  181. if ($def_cat === false) {
  182. $cat = new Category (Translate::t ('default_category'));
  183. $cat->_id (1);
  184. $values = array (
  185. 'id' => $cat->id (),
  186. 'name' => $cat->name (),
  187. 'color' => $cat->color ()
  188. );
  189. $this->addCategory ($values);
  190. }
  191. }
  192. public function count () {
  193. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'category';
  194. $stm = $this->bd->prepare ($sql);
  195. $stm->execute ();
  196. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  197. return $res[0]['count'];
  198. }
  199. public function countFeed ($id) {
  200. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed WHERE category=?';
  201. $stm = $this->bd->prepare ($sql);
  202. $values = array ($id);
  203. $stm->execute ($values);
  204. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  205. return $res[0]['count'];
  206. }
  207. public function countNotRead ($id) {
  208. $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';
  209. $stm = $this->bd->prepare ($sql);
  210. $values = array ($id);
  211. $stm->execute ($values);
  212. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  213. return $res[0]['count'];
  214. }
  215. }
  216. class HelperCategory {
  217. public static function findFeed($categories, $feed_id) {
  218. foreach ($categories as $category) {
  219. foreach ($category->feeds () as $feed) {
  220. if ($feed->id () === $feed_id) {
  221. return $feed;
  222. }
  223. }
  224. }
  225. return null;
  226. }
  227. public static function CountUnreads($categories, $minPriority = 0) {
  228. $n = 0;
  229. foreach ($categories as $category) {
  230. foreach ($category->feeds () as $feed) {
  231. if ($feed->priority () >= $minPriority) {
  232. $n += $feed->nbNotRead();
  233. }
  234. }
  235. }
  236. return $n;
  237. }
  238. public static function daoToCategoryPrepopulated ($listDAO) {
  239. $list = array ();
  240. if (!is_array ($listDAO)) {
  241. $listDAO = array ($listDAO);
  242. }
  243. $previousLine = null;
  244. $feedsDao = array();
  245. foreach ($listDAO as $line) {
  246. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  247. // End of the current category, we add it to the $list
  248. $cat = new Category (
  249. $previousLine['c_name'],
  250. isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
  251. HelperFeed::daoToFeed ($feedsDao, $previousLine['c_id'])
  252. );
  253. $cat->_id ($previousLine['c_id']);
  254. $list[$previousLine['c_id']] = $cat;
  255. $feedsDao = array(); //Prepare for next category
  256. }
  257. $previousLine = $line;
  258. $feedsDao[] = $line;
  259. }
  260. // add the last category
  261. if ($previousLine != null) {
  262. $cat = new Category (
  263. $previousLine['c_name'],
  264. isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
  265. HelperFeed::daoToFeed ($feedsDao, $previousLine['c_id'])
  266. );
  267. $cat->_id ($previousLine['c_id']);
  268. $list[$previousLine['c_id']] = $cat;
  269. }
  270. return $list;
  271. }
  272. public static function daoToCategory ($listDAO) {
  273. $list = array ();
  274. if (!is_array ($listDAO)) {
  275. $listDAO = array ($listDAO);
  276. }
  277. foreach ($listDAO as $key => $dao) {
  278. $cat = new Category (
  279. $dao['name'],
  280. $dao['color']
  281. );
  282. $cat->_id ($dao['id']);
  283. $list[$key] = $cat;
  284. }
  285. return $list;
  286. }
  287. }