Entry.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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, $search = false, $order = 'high_to_low') {
  205. $where = '';
  206. if ($mode == 'not_read') {
  207. $where = ' WHERE is_read=0';
  208. }
  209. $values = array();
  210. if ($search) {
  211. $values[] = '%'.$search.'%';
  212. if ($mode == 'not_read') {
  213. $where = ' AND title LIKE ?';
  214. } else {
  215. $where = ' WHERE title LIKE ?';
  216. }
  217. }
  218. if ($order == 'low_to_high') {
  219. $order = ' DESC';
  220. } else {
  221. $order = '';
  222. }
  223. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  224. $stm = $this->bd->prepare ($sql);
  225. $stm->execute ($values);
  226. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  227. $this->nbItems = $res[0]['count'];
  228. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  229. $fin = $this->nbItemsPerPage;
  230. $sql = 'SELECT * FROM entry' . $where
  231. . ' ORDER BY date' . $order
  232. . ' LIMIT ' . $deb . ', ' . $fin;
  233. $stm = $this->bd->prepare ($sql);
  234. $stm->execute ($values);
  235. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  236. }
  237. public function listFavorites ($mode, $search = false, $order = 'high_to_low') {
  238. $where = ' WHERE is_favorite=1';
  239. if ($mode == 'not_read') {
  240. $where .= ' AND is_read=0';
  241. }
  242. $values = array();
  243. if ($search) {
  244. $values[] = '%'.$search.'%';
  245. $where = ' AND title LIKE ?';
  246. }
  247. if ($order == 'low_to_high') {
  248. $order = ' DESC';
  249. } else {
  250. $order = '';
  251. }
  252. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  253. $stm = $this->bd->prepare ($sql);
  254. $stm->execute ($values);
  255. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  256. $this->nbItems = $res[0]['count'];
  257. if($this->nbItemsPerPage < 0) {
  258. $sql = 'SELECT * FROM entry' . $where
  259. . ' ORDER BY date' . $order;
  260. } else {
  261. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  262. $fin = $this->nbItemsPerPage;
  263. $sql = 'SELECT * FROM entry' . $where
  264. . ' ORDER BY date' . $order
  265. . ' LIMIT ' . $deb . ', ' . $fin;
  266. }
  267. $stm = $this->bd->prepare ($sql);
  268. $stm->execute ($values);
  269. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  270. }
  271. public function listByCategory ($cat, $mode, $search = false, $order = 'high_to_low') {
  272. $where = ' WHERE category=?';
  273. if ($mode == 'not_read') {
  274. $where .= ' AND is_read=0';
  275. }
  276. $values = array ($cat);
  277. if ($search) {
  278. $values[] = '%'.$search.'%';
  279. $where = ' AND title LIKE ?';
  280. }
  281. if ($order == 'low_to_high') {
  282. $order = ' DESC';
  283. } else {
  284. $order = '';
  285. }
  286. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  287. $stm = $this->bd->prepare ($sql);
  288. $stm->execute ($values);
  289. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  290. $this->nbItems = $res[0]['count'];
  291. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  292. $fin = $this->nbItemsPerPage;
  293. $sql = 'SELECT e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  294. . ' ORDER BY date' . $order
  295. . ' LIMIT ' . $deb . ', ' . $fin;
  296. $stm = $this->bd->prepare ($sql);
  297. $stm->execute ($values);
  298. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  299. }
  300. public function listByFeed ($feed, $mode, $search = false, $order = 'high_to_low') {
  301. $where = ' WHERE id_feed=?';
  302. if ($mode == 'not_read') {
  303. $where .= ' AND is_read=0';
  304. }
  305. $values = array();
  306. if ($search) {
  307. $values[] = '%'.$search.'%';
  308. $where = ' AND title LIKE ?';
  309. }
  310. if ($order == 'low_to_high') {
  311. $order = ' DESC';
  312. } else {
  313. $order = '';
  314. }
  315. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  316. $stm = $this->bd->prepare ($sql);
  317. $values = array ($feed);
  318. $stm->execute ($values);
  319. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  320. $this->nbItems = $res[0]['count'];
  321. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  322. $fin = $this->nbItemsPerPage;
  323. $sql = 'SELECT * FROM entry e' . $where
  324. . ' ORDER BY date' . $order
  325. . ' LIMIT ' . $deb . ', ' . $fin;
  326. $stm = $this->bd->prepare ($sql);
  327. $values = array ($feed);
  328. $stm->execute ($values);
  329. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  330. }
  331. public function count () {
  332. $sql = 'SELECT COUNT(*) AS count FROM entry';
  333. $stm = $this->bd->prepare ($sql);
  334. $stm->execute ();
  335. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  336. return $res[0]['count'];
  337. }
  338. public function countNotRead () {
  339. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  340. $stm = $this->bd->prepare ($sql);
  341. $stm->execute ();
  342. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  343. return $res[0]['count'];
  344. }
  345. public function countFavorites () {
  346. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  347. $stm = $this->bd->prepare ($sql);
  348. $stm->execute ();
  349. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  350. return $res[0]['count'];
  351. }
  352. // gestion de la pagination directement via le DAO
  353. private $nbItemsPerPage = 1;
  354. private $currentPage = 1;
  355. private $nbItems = 0;
  356. public function _nbItemsPerPage ($value) {
  357. $this->nbItemsPerPage = $value;
  358. }
  359. public function _currentPage ($value) {
  360. $this->currentPage = $value;
  361. }
  362. public function currentPage () {
  363. if ($this->currentPage < 1) {
  364. return 1;
  365. }
  366. $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
  367. if ($this->currentPage > $maxPage) {
  368. return $maxPage;
  369. }
  370. return $this->currentPage;
  371. }
  372. public function getPaginator ($entries) {
  373. $paginator = new Paginator ($entries);
  374. $paginator->_nbItems ($this->nbItems);
  375. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  376. $paginator->_currentPage ($this->currentPage ());
  377. return $paginator;
  378. }
  379. }
  380. class HelperEntry {
  381. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  382. $list = array ();
  383. if (!is_array ($listDAO)) {
  384. $listDAO = array ($listDAO);
  385. }
  386. foreach ($listDAO as $key => $dao) {
  387. $list[$key] = new Entry (
  388. $dao['id_feed'],
  389. $dao['guid'],
  390. $dao['title'],
  391. $dao['author'],
  392. unserialize (gzinflate (base64_decode ($dao['content']))),
  393. $dao['link'],
  394. $dao['date'],
  395. $dao['is_read'],
  396. $dao['is_favorite']
  397. );
  398. if (isset ($dao['id'])) {
  399. $list[$key]->_id ($dao['id']);
  400. }
  401. }
  402. return $list;
  403. }
  404. }