Category.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. class Category extends Model {
  3. private $id = false;
  4. private $name;
  5. private $color;
  6. private $feeds = null;
  7. public function __construct ($name = '', $color = '#0062BE') {
  8. $this->_name ($name);
  9. $this->_color ($color);
  10. }
  11. public function id () {
  12. if (!$this->id) {
  13. return small_hash ($this->name . time () . Configuration::selApplication ());
  14. } else {
  15. return $this->id;
  16. }
  17. }
  18. public function name () {
  19. return $this->name;
  20. }
  21. public function color () {
  22. return $this->color;
  23. }
  24. public function nbFeed () {
  25. $catDAO = new CategoryDAO ();
  26. return $catDAO->countFeed ($this->id ());
  27. }
  28. public function nbNotRead () {
  29. $catDAO = new CategoryDAO ();
  30. return $catDAO->countNotRead ($this->id ());
  31. }
  32. public function feeds () {
  33. if (is_null ($this->feeds)) {
  34. $feedDAO = new FeedDAO ();
  35. return $feedDAO->listByCategory ($this->id ());
  36. } else {
  37. return $this->feeds;
  38. }
  39. }
  40. public function _id ($value) {
  41. $this->id = $value;
  42. }
  43. public function _name ($value) {
  44. $this->name = $value;
  45. }
  46. public function _color ($value) {
  47. if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  48. $this->color = $value;
  49. } else {
  50. $this->color = '#0062BE';
  51. }
  52. }
  53. public function _feeds ($values) {
  54. if (!is_array ($values)) {
  55. $values = array ($values);
  56. }
  57. $this->feeds = $values;
  58. }
  59. }
  60. class CategoryDAO extends Model_pdo {
  61. public function addCategory ($valuesTmp) {
  62. $sql = 'INSERT INTO category (id, name, color) VALUES(?, ?, ?)';
  63. $stm = $this->bd->prepare ($sql);
  64. $values = array (
  65. $valuesTmp['id'],
  66. $valuesTmp['name'],
  67. $valuesTmp['color'],
  68. );
  69. if ($stm && $stm->execute ($values)) {
  70. return true;
  71. } else {
  72. $info = $stm->errorInfo();
  73. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  74. return false;
  75. }
  76. }
  77. public function updateCategory ($id, $valuesTmp) {
  78. $sql = 'UPDATE category SET name=?, color=? WHERE id=?';
  79. $stm = $this->bd->prepare ($sql);
  80. $values = array (
  81. $valuesTmp['name'],
  82. $valuesTmp['color'],
  83. $id
  84. );
  85. if ($stm && $stm->execute ($values)) {
  86. return true;
  87. } else {
  88. $info = $stm->errorInfo();
  89. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  90. return false;
  91. }
  92. }
  93. public function deleteCategory ($id) {
  94. $sql = 'DELETE FROM category WHERE id=?';
  95. $stm = $this->bd->prepare ($sql);
  96. $values = array ($id);
  97. if ($stm && $stm->execute ($values)) {
  98. return true;
  99. } else {
  100. $info = $stm->errorInfo();
  101. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  102. return false;
  103. }
  104. }
  105. public function searchById ($id) {
  106. $sql = 'SELECT * FROM category WHERE id=?';
  107. $stm = $this->bd->prepare ($sql);
  108. $values = array ($id);
  109. $stm->execute ($values);
  110. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  111. $cat = HelperCategory::daoToCategory ($res);
  112. if (isset ($cat[0])) {
  113. return $cat[0];
  114. } else {
  115. $info = $stm->errorInfo();
  116. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  117. return false;
  118. }
  119. }
  120. public function searchByName ($name) {
  121. $sql = 'SELECT * FROM category WHERE name=?';
  122. $stm = $this->bd->prepare ($sql);
  123. $values = array ($name);
  124. $stm->execute ($values);
  125. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  126. $cat = HelperCategory::daoToCategory ($res);
  127. if (isset ($cat[0])) {
  128. return $cat[0];
  129. } else {
  130. $info = $stm->errorInfo();
  131. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  132. return false;
  133. }
  134. }
  135. public function listCategories () {
  136. $sql = 'SELECT * FROM category ORDER BY name';
  137. $stm = $this->bd->prepare ($sql);
  138. $stm->execute ();
  139. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  140. }
  141. public function checkDefault () {
  142. $def_cat = $this->searchById ('000000');
  143. if (!$def_cat) {
  144. $cat = new Category ('Sans catégorie');
  145. $cat->_id ('000000');
  146. $values = array (
  147. 'id' => $cat->id (),
  148. 'name' => $cat->name (),
  149. 'color' => $cat->color ()
  150. );
  151. $this->addCategory ($values);
  152. }
  153. }
  154. public function count () {
  155. $sql = 'SELECT COUNT(*) AS count FROM category';
  156. $stm = $this->bd->prepare ($sql);
  157. $stm->execute ();
  158. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  159. return $res[0]['count'];
  160. }
  161. public function countFeed ($id) {
  162. $sql = 'SELECT COUNT(*) AS count FROM feed WHERE category=?';
  163. $stm = $this->bd->prepare ($sql);
  164. $values = array ($id);
  165. $stm->execute ($values);
  166. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  167. return $res[0]['count'];
  168. }
  169. public function countNotRead ($id) {
  170. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE category=? AND e.is_read=0';
  171. $stm = $this->bd->prepare ($sql);
  172. $values = array ($id);
  173. $stm->execute ($values);
  174. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  175. return $res[0]['count'];
  176. }
  177. }
  178. class HelperCategory {
  179. public static function daoToCategory ($listDAO) {
  180. $list = array ();
  181. if (!is_array ($listDAO)) {
  182. $listDAO = array ($listDAO);
  183. }
  184. foreach ($listDAO as $key => $dao) {
  185. $cat = new Category (
  186. $dao['name'],
  187. $dao['color']
  188. );
  189. $cat->_id ($dao['id']);
  190. $list[$key] = $cat;
  191. }
  192. return $list;
  193. }
  194. }