Entry.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. class Entry extends Model {
  3. private $guid;
  4. private $title;
  5. private $author;
  6. private $content;
  7. private $link;
  8. private $date;
  9. private $is_read;
  10. private $is_favorite;
  11. private $feed;
  12. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  13. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false) {
  14. $this->_guid ($guid);
  15. $this->_title ($title);
  16. $this->_author ($author);
  17. $this->_content ($content);
  18. $this->_link ($link);
  19. $this->_date ($pubdate);
  20. $this->_isRead ($is_read);
  21. $this->_isFavorite ($is_favorite);
  22. $this->_feed ($feed);
  23. }
  24. public function id () {
  25. return small_hash ($this->guid . Configuration::selApplication ());
  26. }
  27. public function guid () {
  28. return $this->guid;
  29. }
  30. public function title () {
  31. return $this->title;
  32. }
  33. public function author () {
  34. return $this->author;
  35. }
  36. public function content () {
  37. return $this->content;
  38. }
  39. public function link () {
  40. return $this->link;
  41. }
  42. public function date ($raw = false) {
  43. if ($raw) {
  44. return $this->date;
  45. } else {
  46. return timestamptodate ($this->date);
  47. }
  48. }
  49. public function isRead () {
  50. return $this->is_read;
  51. }
  52. public function isFavorite () {
  53. return $this->is_favorite;
  54. }
  55. public function feed ($object = false) {
  56. if ($object) {
  57. $feedDAO = new FeedDAO ();
  58. return $feedDAO->searchById ($this->feed);
  59. } else {
  60. return $this->feed;
  61. }
  62. }
  63. public function _guid ($value) {
  64. $this->guid = $value;
  65. }
  66. public function _title ($value) {
  67. $this->title = $value;
  68. }
  69. public function _author ($value) {
  70. $this->author = $value;
  71. }
  72. public function _content ($value) {
  73. $this->content = $value;
  74. }
  75. public function _link ($value) {
  76. $this->link = $value;
  77. }
  78. public function _date ($value) {
  79. $this->date = $value;
  80. }
  81. public function _isRead ($value) {
  82. $this->is_read = $value;
  83. }
  84. public function _isFavorite ($value) {
  85. $this->is_favorite = $value;
  86. }
  87. public function _feed ($value) {
  88. $this->feed = $value;
  89. }
  90. }
  91. class EntryDAO extends Model_pdo {
  92. public function addEntry ($valuesTmp) {
  93. $sql = 'INSERT INTO entry (id, guid, title, author, content, link, date, is_read, is_favorite, id_feed) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  94. $stm = $this->bd->prepare ($sql);
  95. $values = array (
  96. $valuesTmp['id'],
  97. $valuesTmp['guid'],
  98. $valuesTmp['title'],
  99. $valuesTmp['author'],
  100. $valuesTmp['content'],
  101. $valuesTmp['link'],
  102. $valuesTmp['date'],
  103. $valuesTmp['is_read'],
  104. $valuesTmp['is_favorite'],
  105. $valuesTmp['id_feed'],
  106. );
  107. if ($stm && $stm->execute ($values)) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. public function updateEntry ($id, $valuesTmp) {
  114. $set = '';
  115. foreach ($valuesTmp as $key => $v) {
  116. $set .= $key . '=?, ';
  117. }
  118. $set = substr ($set, 0, -2);
  119. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  120. $stm = $this->bd->prepare ($sql);
  121. foreach ($valuesTmp as $v) {
  122. $values[] = $v;
  123. }
  124. $values[] = $id;
  125. if ($stm && $stm->execute ($values)) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. public function updateEntries ($valuesTmp) {
  132. $set = '';
  133. foreach ($valuesTmp as $key => $v) {
  134. $set .= $key . '=?, ';
  135. }
  136. $set = substr ($set, 0, -2);
  137. $sql = 'UPDATE entry SET ' . $set;
  138. $stm = $this->bd->prepare ($sql);
  139. foreach ($valuesTmp as $v) {
  140. $values[] = $v;
  141. }
  142. if ($stm && $stm->execute ($values)) {
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. public function cleanOldEntries ($nb_month) {
  149. $date = 60 * 60 * 24 * 30 * $nb_month;
  150. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0';
  151. $stm = $this->bd->prepare ($sql);
  152. $values = array (
  153. time () - $date
  154. );
  155. if ($stm && $stm->execute ($values)) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. public function searchById ($id) {
  162. $sql = 'SELECT * FROM entry WHERE id=?';
  163. $stm = $this->bd->prepare ($sql);
  164. $values = array ($id);
  165. $stm->execute ($values);
  166. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  167. $entry = HelperEntry::daoToEntry ($res);
  168. if (isset ($entry[0])) {
  169. return $entry[0];
  170. } else {
  171. return false;
  172. }
  173. }
  174. public function listEntries ($mode, $order = 'high_to_low') {
  175. $where = '';
  176. if ($mode == 'not_read') {
  177. $where = ' WHERE is_read=0';
  178. }
  179. if ($order == 'low_to_high') {
  180. $order = ' DESC';
  181. } else {
  182. $order = '';
  183. }
  184. $sql = 'SELECT * FROM entry' . $where . ' ORDER BY date' . $order;
  185. $stm = $this->bd->prepare ($sql);
  186. $stm->execute ();
  187. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  188. }
  189. public function listFavorites ($mode, $order = 'high_to_low') {
  190. $where = ' WHERE is_favorite=1';
  191. if ($mode == 'not_read') {
  192. $where .= ' AND is_read=0';
  193. }
  194. if ($order == 'low_to_high') {
  195. $order = ' DESC';
  196. } else {
  197. $order = '';
  198. }
  199. $sql = 'SELECT * FROM entry' . $where . ' ORDER BY date' . $order;
  200. $stm = $this->bd->prepare ($sql);
  201. $stm->execute ();
  202. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  203. }
  204. public function listByCategory ($cat, $mode, $order = 'high_to_low') {
  205. $where = ' WHERE category=?';
  206. if ($mode == 'not_read') {
  207. $where .= ' AND is_read=0';
  208. }
  209. if ($order == 'low_to_high') {
  210. $order = ' DESC';
  211. } else {
  212. $order = '';
  213. }
  214. $sql = 'SELECT * FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where . ' ORDER BY date' . $order;
  215. $stm = $this->bd->prepare ($sql);
  216. $values = array ($cat);
  217. $stm->execute ($values);
  218. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  219. }
  220. public function count () {
  221. $sql = 'SELECT COUNT(*) AS count FROM entry';
  222. $stm = $this->bd->prepare ($sql);
  223. $stm->execute ();
  224. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  225. return $res[0]['count'];
  226. }
  227. public function countNotRead () {
  228. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  229. $stm = $this->bd->prepare ($sql);
  230. $stm->execute ();
  231. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  232. return $res[0]['count'];
  233. }
  234. public function countFavorites () {
  235. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  236. $stm = $this->bd->prepare ($sql);
  237. $stm->execute ();
  238. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  239. return $res[0]['count'];
  240. }
  241. }
  242. class HelperEntry {
  243. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  244. $list = array ();
  245. if (!is_array ($listDAO)) {
  246. $listDAO = array ($listDAO);
  247. }
  248. foreach ($listDAO as $key => $dao) {
  249. if (($mode != 'not_read' || !$dao['is_read'])
  250. && ($favorite == false || $dao['is_favorite'])) {
  251. $list[$key] = new Entry (
  252. $dao['id_feed'],
  253. $dao['guid'],
  254. $dao['title'],
  255. $dao['author'],
  256. $dao['content'],
  257. $dao['link'],
  258. $dao['date'],
  259. $dao['is_read'],
  260. $dao['is_favorite']
  261. );
  262. }
  263. }
  264. return $list;
  265. }
  266. }