Entry.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. base64_encode (gzdeflate (serialize ($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. if (isset ($valuesTmp['content'])) {
  115. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  116. }
  117. $set = '';
  118. foreach ($valuesTmp as $key => $v) {
  119. $set .= $key . '=?, ';
  120. }
  121. $set = substr ($set, 0, -2);
  122. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  123. $stm = $this->bd->prepare ($sql);
  124. foreach ($valuesTmp as $v) {
  125. $values[] = $v;
  126. }
  127. $values[] = $id;
  128. if ($stm && $stm->execute ($values)) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. public function updateEntries ($valuesTmp) {
  135. if (isset ($valuesTmp['content'])) {
  136. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  137. }
  138. $set = '';
  139. foreach ($valuesTmp as $key => $v) {
  140. $set .= $key . '=?, ';
  141. }
  142. $set = substr ($set, 0, -2);
  143. $sql = 'UPDATE entry SET ' . $set;
  144. $stm = $this->bd->prepare ($sql);
  145. foreach ($valuesTmp as $v) {
  146. $values[] = $v;
  147. }
  148. if ($stm && $stm->execute ($values)) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. public function cleanOldEntries ($nb_month) {
  155. $date = 60 * 60 * 24 * 30 * $nb_month;
  156. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0';
  157. $stm = $this->bd->prepare ($sql);
  158. $values = array (
  159. time () - $date
  160. );
  161. if ($stm && $stm->execute ($values)) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. public function searchById ($id) {
  168. $sql = 'SELECT * FROM entry WHERE id=?';
  169. $stm = $this->bd->prepare ($sql);
  170. $values = array ($id);
  171. $stm->execute ($values);
  172. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  173. $entry = HelperEntry::daoToEntry ($res);
  174. if (isset ($entry[0])) {
  175. return $entry[0];
  176. } else {
  177. return false;
  178. }
  179. }
  180. public function listEntries ($mode, $order = 'high_to_low') {
  181. $where = '';
  182. if ($mode == 'not_read') {
  183. $where = ' WHERE is_read=0';
  184. }
  185. if ($order == 'low_to_high') {
  186. $order = ' DESC';
  187. } else {
  188. $order = '';
  189. }
  190. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  191. $stm = $this->bd->prepare ($sql);
  192. $stm->execute ();
  193. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  194. $this->nbItems = $res[0]['count'];
  195. $deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
  196. $fin = $this->nbItemsPerPage;
  197. $sql = 'SELECT * FROM entry' . $where
  198. . ' ORDER BY date' . $order
  199. . ' LIMIT ' . $deb . ', ' . $fin;
  200. $stm = $this->bd->prepare ($sql);
  201. $stm->execute ();
  202. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  203. }
  204. public function listFavorites ($mode, $order = 'high_to_low') {
  205. $where = ' WHERE is_favorite=1';
  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 COUNT(*) AS count FROM entry' . $where;
  215. $stm = $this->bd->prepare ($sql);
  216. $stm->execute ();
  217. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  218. $this->nbItems = $res[0]['count'];
  219. if($this->nbItemsPerPage < 0) {
  220. $sql = 'SELECT * FROM entry' . $where
  221. . ' ORDER BY date' . $order;
  222. } else {
  223. $deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
  224. $fin = $this->nbItemsPerPage;
  225. $sql = 'SELECT * FROM entry' . $where
  226. . ' ORDER BY date' . $order
  227. . ' LIMIT ' . $deb . ', ' . $fin;
  228. }
  229. $stm = $this->bd->prepare ($sql);
  230. $stm->execute ();
  231. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  232. }
  233. public function listByCategory ($cat, $mode, $order = 'high_to_low') {
  234. $where = ' WHERE category=?';
  235. if ($mode == 'not_read') {
  236. $where .= ' AND is_read=0';
  237. }
  238. if ($order == 'low_to_high') {
  239. $order = ' DESC';
  240. } else {
  241. $order = '';
  242. }
  243. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  244. $stm = $this->bd->prepare ($sql);
  245. $values = array ($cat);
  246. $stm->execute ($values);
  247. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  248. $this->nbItems = $res[0]['count'];
  249. $deb = ($this->currentPage - 1) * $this->nbItemsPerPage;
  250. $fin = $this->nbItemsPerPage;
  251. $sql = 'SELECT * FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  252. . ' ORDER BY date' . $order
  253. . ' LIMIT ' . $deb . ', ' . $fin;
  254. $stm = $this->bd->prepare ($sql);
  255. $values = array ($cat);
  256. $stm->execute ($values);
  257. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  258. }
  259. public function count () {
  260. $sql = 'SELECT COUNT(*) AS count FROM entry';
  261. $stm = $this->bd->prepare ($sql);
  262. $stm->execute ();
  263. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  264. return $res[0]['count'];
  265. }
  266. public function countNotRead () {
  267. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  268. $stm = $this->bd->prepare ($sql);
  269. $stm->execute ();
  270. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  271. return $res[0]['count'];
  272. }
  273. public function countFavorites () {
  274. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  275. $stm = $this->bd->prepare ($sql);
  276. $stm->execute ();
  277. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  278. return $res[0]['count'];
  279. }
  280. // gestion de la pagination directement via le DAO
  281. private $nbItemsPerPage = 1;
  282. private $currentPage = 1;
  283. private $nbItems = 0;
  284. public function _nbItemsPerPage ($value) {
  285. $this->nbItemsPerPage = $value;
  286. }
  287. public function _currentPage ($value) {
  288. $this->currentPage = $value;
  289. }
  290. public function getPaginator ($entries) {
  291. $paginator = new Paginator ($entries);
  292. $paginator->_nbItems ($this->nbItems);
  293. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  294. $paginator->_currentPage ($this->currentPage);
  295. return $paginator;
  296. }
  297. }
  298. class HelperEntry {
  299. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  300. $list = array ();
  301. if (!is_array ($listDAO)) {
  302. $listDAO = array ($listDAO);
  303. }
  304. foreach ($listDAO as $key => $dao) {
  305. $list[$key] = new Entry (
  306. $dao['id_feed'],
  307. $dao['guid'],
  308. $dao['title'],
  309. $dao['author'],
  310. unserialize (gzinflate (base64_decode ($dao['content']))),
  311. $dao['link'],
  312. $dao['date'],
  313. $dao['is_read'],
  314. $dao['is_favorite']
  315. );
  316. }
  317. return $list;
  318. }
  319. }