Category.php 8.3 KB

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