Entry.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. class Entry extends Model {
  3. private $id = null;
  4. private $guid;
  5. private $title;
  6. private $author;
  7. private $content;
  8. private $link;
  9. private $date;
  10. private $is_read;
  11. private $is_favorite;
  12. private $feed;
  13. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  14. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false) {
  15. $this->_guid ($guid);
  16. $this->_title ($title);
  17. $this->_author ($author);
  18. $this->_content ($content);
  19. $this->_link ($link);
  20. $this->_date ($pubdate);
  21. $this->_isRead ($is_read);
  22. $this->_isFavorite ($is_favorite);
  23. $this->_feed ($feed);
  24. }
  25. public function id () {
  26. if(is_null($this->id)) {
  27. return small_hash ($this->guid . Configuration::selApplication ());
  28. } else {
  29. return $this->id;
  30. }
  31. }
  32. public function guid () {
  33. return $this->guid;
  34. }
  35. public function title () {
  36. return $this->title;
  37. }
  38. public function author () {
  39. return $this->author;
  40. }
  41. public function content () {
  42. return $this->content;
  43. }
  44. public function link () {
  45. return $this->link;
  46. }
  47. public function date ($raw = false) {
  48. if ($raw) {
  49. return $this->date;
  50. } else {
  51. return timestamptodate ($this->date);
  52. }
  53. }
  54. public function isRead () {
  55. return $this->is_read;
  56. }
  57. public function isFavorite () {
  58. return $this->is_favorite;
  59. }
  60. public function feed ($object = false) {
  61. if ($object) {
  62. $feedDAO = new FeedDAO ();
  63. return $feedDAO->searchById ($this->feed);
  64. } else {
  65. return $this->feed;
  66. }
  67. }
  68. public function _id ($value) {
  69. $this->id = $value;
  70. }
  71. public function _guid ($value) {
  72. $this->guid = $value;
  73. }
  74. public function _title ($value) {
  75. $this->title = $value;
  76. }
  77. public function _author ($value) {
  78. $this->author = $value;
  79. }
  80. public function _content ($value) {
  81. $this->content = $value;
  82. }
  83. public function _link ($value) {
  84. $this->link = $value;
  85. }
  86. public function _date ($value) {
  87. $this->date = $value;
  88. }
  89. public function _isRead ($value) {
  90. $this->is_read = $value;
  91. }
  92. public function _isFavorite ($value) {
  93. $this->is_favorite = $value;
  94. }
  95. public function _feed ($value) {
  96. $this->feed = $value;
  97. }
  98. public function isDay ($day) {
  99. $date = getdate ();
  100. $today = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  101. $yesterday = $today - 86400;
  102. if ($day == Days::TODAY &&
  103. $this->date >= $today && $this->date < $today + 86400) {
  104. return true;
  105. } elseif ($day == Days::YESTERDAY &&
  106. $this->date >= $yesterday && $this->date < $yesterday + 86400) {
  107. return true;
  108. } elseif ($day == Days::BEFORE_YESTERDAY && $this->date < $yesterday) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. }
  115. class EntryDAO extends Model_pdo {
  116. public function addEntry ($valuesTmp) {
  117. $sql = 'INSERT INTO entry (id, guid, title, author, content, link, date, is_read, is_favorite, id_feed) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  118. $stm = $this->bd->prepare ($sql);
  119. $values = array (
  120. $valuesTmp['id'],
  121. $valuesTmp['guid'],
  122. $valuesTmp['title'],
  123. $valuesTmp['author'],
  124. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  125. $valuesTmp['link'],
  126. $valuesTmp['date'],
  127. $valuesTmp['is_read'],
  128. $valuesTmp['is_favorite'],
  129. $valuesTmp['id_feed'],
  130. );
  131. if ($stm && $stm->execute ($values)) {
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. public function updateEntry ($id, $valuesTmp) {
  138. if (isset ($valuesTmp['content'])) {
  139. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  140. }
  141. $set = '';
  142. foreach ($valuesTmp as $key => $v) {
  143. $set .= $key . '=?, ';
  144. }
  145. $set = substr ($set, 0, -2);
  146. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  147. $stm = $this->bd->prepare ($sql);
  148. foreach ($valuesTmp as $v) {
  149. $values[] = $v;
  150. }
  151. $values[] = $id;
  152. if ($stm && $stm->execute ($values)) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. public function updateEntries ($valuesTmp) {
  159. if (isset ($valuesTmp['content'])) {
  160. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  161. }
  162. $set = '';
  163. foreach ($valuesTmp as $key => $v) {
  164. $set .= $key . '=?, ';
  165. }
  166. $set = substr ($set, 0, -2);
  167. $sql = 'UPDATE entry SET ' . $set;
  168. $stm = $this->bd->prepare ($sql);
  169. foreach ($valuesTmp as $v) {
  170. $values[] = $v;
  171. }
  172. if ($stm && $stm->execute ($values)) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. public function cleanOldEntries ($nb_month) {
  179. $date = 60 * 60 * 24 * 30 * $nb_month;
  180. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0';
  181. $stm = $this->bd->prepare ($sql);
  182. $values = array (
  183. time () - $date
  184. );
  185. if ($stm && $stm->execute ($values)) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. }
  191. public function searchById ($id) {
  192. $sql = 'SELECT * FROM entry WHERE id=?';
  193. $stm = $this->bd->prepare ($sql);
  194. $values = array ($id);
  195. $stm->execute ($values);
  196. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  197. $entry = HelperEntry::daoToEntry ($res);
  198. if (isset ($entry[0])) {
  199. return $entry[0];
  200. } else {
  201. return false;
  202. }
  203. }
  204. public function listEntries ($mode, $order = 'high_to_low') {
  205. $where = '';
  206. if ($mode == 'not_read') {
  207. $where = ' WHERE 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. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  220. $fin = $this->nbItemsPerPage;
  221. $sql = 'SELECT * FROM entry' . $where
  222. . ' ORDER BY date' . $order
  223. . ' LIMIT ' . $deb . ', ' . $fin;
  224. $stm = $this->bd->prepare ($sql);
  225. $stm->execute ();
  226. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  227. }
  228. public function listFavorites ($mode, $order = 'high_to_low') {
  229. $where = ' WHERE is_favorite=1';
  230. if ($mode == 'not_read') {
  231. $where .= ' AND is_read=0';
  232. }
  233. if ($order == 'low_to_high') {
  234. $order = ' DESC';
  235. } else {
  236. $order = '';
  237. }
  238. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  239. $stm = $this->bd->prepare ($sql);
  240. $stm->execute ();
  241. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  242. $this->nbItems = $res[0]['count'];
  243. if($this->nbItemsPerPage < 0) {
  244. $sql = 'SELECT * FROM entry' . $where
  245. . ' ORDER BY date' . $order;
  246. } else {
  247. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  248. $fin = $this->nbItemsPerPage;
  249. $sql = 'SELECT * FROM entry' . $where
  250. . ' ORDER BY date' . $order
  251. . ' LIMIT ' . $deb . ', ' . $fin;
  252. }
  253. $stm = $this->bd->prepare ($sql);
  254. $stm->execute ();
  255. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  256. }
  257. public function listByCategory ($cat, $mode, $order = 'high_to_low') {
  258. $where = ' WHERE category=?';
  259. if ($mode == 'not_read') {
  260. $where .= ' AND is_read=0';
  261. }
  262. if ($order == 'low_to_high') {
  263. $order = ' DESC';
  264. } else {
  265. $order = '';
  266. }
  267. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  268. $stm = $this->bd->prepare ($sql);
  269. $values = array ($cat);
  270. $stm->execute ($values);
  271. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  272. $this->nbItems = $res[0]['count'];
  273. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  274. $fin = $this->nbItemsPerPage;
  275. $sql = 'SELECT e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  276. . ' ORDER BY date' . $order
  277. . ' LIMIT ' . $deb . ', ' . $fin;
  278. $stm = $this->bd->prepare ($sql);
  279. $values = array ($cat);
  280. $stm->execute ($values);
  281. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  282. }
  283. public function listByFeed ($feed, $mode, $order = 'high_to_low') {
  284. $where = ' WHERE id_feed=?';
  285. if ($mode == 'not_read') {
  286. $where .= ' AND is_read=0';
  287. }
  288. if ($order == 'low_to_high') {
  289. $order = ' DESC';
  290. } else {
  291. $order = '';
  292. }
  293. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  294. $stm = $this->bd->prepare ($sql);
  295. $values = array ($feed);
  296. $stm->execute ($values);
  297. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  298. $this->nbItems = $res[0]['count'];
  299. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  300. $fin = $this->nbItemsPerPage;
  301. $sql = 'SELECT * FROM entry e' . $where
  302. . ' ORDER BY date' . $order
  303. . ' LIMIT ' . $deb . ', ' . $fin;
  304. $stm = $this->bd->prepare ($sql);
  305. $values = array ($feed);
  306. $stm->execute ($values);
  307. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  308. }
  309. public function count () {
  310. $sql = 'SELECT COUNT(*) AS count FROM entry';
  311. $stm = $this->bd->prepare ($sql);
  312. $stm->execute ();
  313. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  314. return $res[0]['count'];
  315. }
  316. public function countNotRead () {
  317. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  318. $stm = $this->bd->prepare ($sql);
  319. $stm->execute ();
  320. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  321. return $res[0]['count'];
  322. }
  323. public function countFavorites () {
  324. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  325. $stm = $this->bd->prepare ($sql);
  326. $stm->execute ();
  327. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  328. return $res[0]['count'];
  329. }
  330. // gestion de la pagination directement via le DAO
  331. private $nbItemsPerPage = 1;
  332. private $currentPage = 1;
  333. private $nbItems = 0;
  334. public function _nbItemsPerPage ($value) {
  335. $this->nbItemsPerPage = $value;
  336. }
  337. public function _currentPage ($value) {
  338. $this->currentPage = $value;
  339. }
  340. public function currentPage () {
  341. if ($this->currentPage < 1) {
  342. return 1;
  343. }
  344. $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
  345. if ($this->currentPage > $maxPage) {
  346. return $maxPage;
  347. }
  348. return $this->currentPage;
  349. }
  350. public function getPaginator ($entries) {
  351. $paginator = new Paginator ($entries);
  352. $paginator->_nbItems ($this->nbItems);
  353. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  354. $paginator->_currentPage ($this->currentPage ());
  355. return $paginator;
  356. }
  357. }
  358. class HelperEntry {
  359. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  360. $list = array ();
  361. if (!is_array ($listDAO)) {
  362. $listDAO = array ($listDAO);
  363. }
  364. foreach ($listDAO as $key => $dao) {
  365. $list[$key] = new Entry (
  366. $dao['id_feed'],
  367. $dao['guid'],
  368. $dao['title'],
  369. $dao['author'],
  370. unserialize (gzinflate (base64_decode ($dao['content']))),
  371. $dao['link'],
  372. $dao['date'],
  373. $dao['is_read'],
  374. $dao['is_favorite']
  375. );
  376. if (isset ($dao['id'])) {
  377. $list[$key]->_id ($dao['id']);
  378. }
  379. }
  380. return $list;
  381. }
  382. }