Category.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. $this->feeds = $feedDAO->listByCategory ($this->id ());
  36. }
  37. return $this->feeds;
  38. }
  39. public function _id ($value) {
  40. $this->id = $value;
  41. }
  42. public function _name ($value) {
  43. $this->name = $value;
  44. }
  45. public function _color ($value) {
  46. if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  47. $this->color = $value;
  48. } else {
  49. $this->color = '#0062BE';
  50. }
  51. }
  52. public function _feeds ($values) {
  53. if (!is_array ($values)) {
  54. $values = array ($values);
  55. }
  56. $this->feeds = $values;
  57. }
  58. }
  59. class CategoryDAO extends Model_pdo {
  60. public function addCategory ($valuesTmp) {
  61. $sql = 'INSERT INTO ' . $this->prefix . 'category (id, name, color) VALUES(?, ?, ?)';
  62. $stm = $this->bd->prepare ($sql);
  63. $values = array (
  64. $valuesTmp['id'],
  65. $valuesTmp['name'],
  66. $valuesTmp['color'],
  67. );
  68. if ($stm && $stm->execute ($values)) {
  69. return true;
  70. } else {
  71. $info = $stm->errorInfo();
  72. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  73. return false;
  74. }
  75. }
  76. public function updateCategory ($id, $valuesTmp) {
  77. $sql = 'UPDATE ' . $this->prefix . 'category SET name=?, color=? WHERE id=?';
  78. $stm = $this->bd->prepare ($sql);
  79. $values = array (
  80. $valuesTmp['name'],
  81. $valuesTmp['color'],
  82. $id
  83. );
  84. if ($stm && $stm->execute ($values)) {
  85. return true;
  86. } else {
  87. $info = $stm->errorInfo();
  88. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  89. return false;
  90. }
  91. }
  92. public function deleteCategory ($id) {
  93. $sql = 'DELETE FROM ' . $this->prefix . 'category WHERE id=?';
  94. $stm = $this->bd->prepare ($sql);
  95. $values = array ($id);
  96. if ($stm && $stm->execute ($values)) {
  97. return true;
  98. } else {
  99. $info = $stm->errorInfo();
  100. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  101. return false;
  102. }
  103. }
  104. public function searchById ($id) {
  105. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE id=?';
  106. $stm = $this->bd->prepare ($sql);
  107. $values = array ($id);
  108. $stm->execute ($values);
  109. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  110. $cat = HelperCategory::daoToCategory ($res);
  111. if (isset ($cat[0])) {
  112. return $cat[0];
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function searchByName ($name) {
  118. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE name=?';
  119. $stm = $this->bd->prepare ($sql);
  120. $values = array ($name);
  121. $stm->execute ($values);
  122. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  123. $cat = HelperCategory::daoToCategory ($res);
  124. if (isset ($cat[0])) {
  125. return $cat[0];
  126. } else {
  127. return false;
  128. }
  129. }
  130. public function listCategories () {
  131. $sql = 'SELECT * FROM ' . $this->prefix . 'category ORDER BY name';
  132. $stm = $this->bd->prepare ($sql);
  133. $stm->execute ();
  134. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  135. }
  136. public function getDefault () {
  137. $sql = 'SELECT * FROM ' . $this->prefix . 'category WHERE id="000000"';
  138. $stm = $this->bd->prepare ($sql);
  139. $stm->execute ();
  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 checkDefault () {
  149. $def_cat = $this->searchById ('000000');
  150. if (!$def_cat) {
  151. $cat = new Category (Translate::t ('default_category'));
  152. $cat->_id ('000000');
  153. $values = array (
  154. 'id' => $cat->id (),
  155. 'name' => $cat->name (),
  156. 'color' => $cat->color ()
  157. );
  158. $this->addCategory ($values);
  159. }
  160. }
  161. public function count () {
  162. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'category';
  163. $stm = $this->bd->prepare ($sql);
  164. $stm->execute ();
  165. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  166. return $res[0]['count'];
  167. }
  168. public function countFeed ($id) {
  169. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed WHERE category=?';
  170. $stm = $this->bd->prepare ($sql);
  171. $values = array ($id);
  172. $stm->execute ($values);
  173. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  174. return $res[0]['count'];
  175. }
  176. public function countNotRead ($id) {
  177. $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';
  178. $stm = $this->bd->prepare ($sql);
  179. $values = array ($id);
  180. $stm->execute ($values);
  181. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  182. return $res[0]['count'];
  183. }
  184. }
  185. class HelperCategory {
  186. public static function daoToCategory ($listDAO) {
  187. $list = array ();
  188. if (!is_array ($listDAO)) {
  189. $listDAO = array ($listDAO);
  190. }
  191. foreach ($listDAO as $key => $dao) {
  192. $cat = new Category (
  193. $dao['name'],
  194. $dao['color']
  195. );
  196. $cat->_id ($dao['id']);
  197. $list[$key] = $cat;
  198. }
  199. return $list;
  200. }
  201. }