Category.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 (!empty($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. $valuesTmp['name'],
  89. $valuesTmp['color'],
  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) {
  154. if ($prePopulateFeeds) {
  155. $sql = 'SELECT c.id as c_id, c.name as c_name, c.color as c_color, count(e.id) as nbNotRead, f.* '
  156. . 'FROM ' . $this->prefix . 'category c '
  157. . 'LEFT OUTER JOIN ' . $this->prefix . 'feed f ON f.category = c.id '
  158. . 'LEFT OUTER JOIN ' . $this->prefix . 'entry e ON e.id_feed = f.id AND e.is_read = 0 '
  159. . 'GROUP BY f.id '
  160. . 'ORDER BY c.name, f.name';
  161. $stm = $this->bd->prepare ($sql);
  162. $stm->execute ();
  163. return HelperCategory::daoToCategoryPrepopulated ($stm->fetchAll (PDO::FETCH_ASSOC));
  164. } else {
  165. $sql = 'SELECT * FROM ' . $this->prefix . 'category ORDER BY name';
  166. $stm = $this->bd->prepare ($sql);
  167. $stm->execute ();
  168. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  169. }
  170. }
  171. public function getDefault () {
  172. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE id="000000"';
  173. $stm = $this->bd->prepare ($sql);
  174. $stm->execute ();
  175. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  176. $cat = HelperCategory::daoToCategory ($res);
  177. if (isset ($cat[0])) {
  178. return $cat[0];
  179. } else {
  180. return false;
  181. }
  182. }
  183. public function checkDefault () {
  184. $def_cat = $this->searchById ('000000');
  185. if ($def_cat === false) {
  186. $cat = new Category (Translate::t ('default_category'));
  187. $cat->_id ('000000');
  188. $values = array (
  189. 'id' => $cat->id (),
  190. 'name' => $cat->name (),
  191. 'color' => $cat->color ()
  192. );
  193. $this->addCategory ($values);
  194. }
  195. }
  196. public function count () {
  197. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'category';
  198. $stm = $this->bd->prepare ($sql);
  199. $stm->execute ();
  200. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  201. return $res[0]['count'];
  202. }
  203. public function countFeed ($id) {
  204. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed WHERE category=?';
  205. $stm = $this->bd->prepare ($sql);
  206. $values = array ($id);
  207. $stm->execute ($values);
  208. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  209. return $res[0]['count'];
  210. }
  211. public function countNotRead ($id) {
  212. $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';
  213. $stm = $this->bd->prepare ($sql);
  214. $values = array ($id);
  215. $stm->execute ($values);
  216. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  217. return $res[0]['count'];
  218. }
  219. }
  220. class HelperCategory {
  221. public static function findFeed($categories, $feed_id) {
  222. foreach ($categories as $category) {
  223. foreach ($category->feeds () as $feed) {
  224. if ($feed->id () === $feed_id) {
  225. return $feed;
  226. }
  227. }
  228. }
  229. return null;
  230. }
  231. public static function daoToCategoryPrepopulated ($listDAO) {
  232. $list = array ();
  233. if (!is_array ($listDAO)) {
  234. $listDAO = array ($listDAO);
  235. }
  236. $previousLine = null;
  237. $feedsDao = array();
  238. foreach ($listDAO as $line) {
  239. if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
  240. // End of the current category, we add it to the $list
  241. $cat = new Category (
  242. $previousLine['c_name'],
  243. $previousLine['c_color'],
  244. HelperFeed::daoToFeed ($feedsDao)
  245. );
  246. $cat->_id ($previousLine['c_id']);
  247. $list[] = $cat;
  248. $feedsDao = array(); //Prepare for next category
  249. }
  250. $previousLine = $line;
  251. $feedsDao[] = $line;
  252. }
  253. // add the last category
  254. if ($previousLine != null) {
  255. $cat = new Category (
  256. $previousLine['c_name'],
  257. $previousLine['c_color'],
  258. HelperFeed::daoToFeed ($feedsDao)
  259. );
  260. $cat->_id ($previousLine['c_id']);
  261. $list[] = $cat;
  262. }
  263. return $list;
  264. }
  265. public static function daoToCategory ($listDAO) {
  266. $list = array ();
  267. if (!is_array ($listDAO)) {
  268. $listDAO = array ($listDAO);
  269. }
  270. foreach ($listDAO as $key => $dao) {
  271. $cat = new Category (
  272. $dao['name'],
  273. $dao['color']
  274. );
  275. $cat->_id ($dao['id']);
  276. $list[$key] = $cat;
  277. }
  278. return $list;
  279. }
  280. }