Entry.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 markReadEntries ($read, $dateMax) {
  159. $sql = 'UPDATE entry SET is_read = ? WHERE date < ?';
  160. $stm = $this->bd->prepare ($sql);
  161. $values = array ($read, $dateMax);
  162. if ($stm && $stm->execute ($values)) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. public function markReadCat ($id, $read, $dateMax) {
  169. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ? AND date < ?';
  170. $stm = $this->bd->prepare ($sql);
  171. $values = array ($read, $id, $dateMax);
  172. if ($stm && $stm->execute ($values)) {
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. public function markReadFeed ($id, $read, $dateMax) {
  179. $sql = 'UPDATE entry SET is_read = ? WHERE id_feed = ? AND date < ?';
  180. $stm = $this->bd->prepare ($sql);
  181. $values = array ($read, $id, $dateMax);
  182. if ($stm && $stm->execute ($values)) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }
  188. public function updateEntries ($valuesTmp) {
  189. if (isset ($valuesTmp['content'])) {
  190. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  191. }
  192. $set = '';
  193. foreach ($valuesTmp as $key => $v) {
  194. $set .= $key . '=?, ';
  195. }
  196. $set = substr ($set, 0, -2);
  197. $sql = 'UPDATE entry SET ' . $set;
  198. $stm = $this->bd->prepare ($sql);
  199. foreach ($valuesTmp as $v) {
  200. $values[] = $v;
  201. }
  202. if ($stm && $stm->execute ($values)) {
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. }
  208. public function cleanOldEntries ($nb_month) {
  209. $date = 60 * 60 * 24 * 30 * $nb_month;
  210. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0';
  211. $stm = $this->bd->prepare ($sql);
  212. $values = array (
  213. time () - $date
  214. );
  215. if ($stm && $stm->execute ($values)) {
  216. return true;
  217. } else {
  218. return false;
  219. }
  220. }
  221. public function searchById ($id) {
  222. $sql = 'SELECT * FROM entry WHERE id=?';
  223. $stm = $this->bd->prepare ($sql);
  224. $values = array ($id);
  225. $stm->execute ($values);
  226. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  227. $entry = HelperEntry::daoToEntry ($res);
  228. if (isset ($entry[0])) {
  229. return $entry[0];
  230. } else {
  231. return false;
  232. }
  233. }
  234. public function listEntries ($mode, $search = false, $order = 'high_to_low') {
  235. $where = '';
  236. if ($mode == 'not_read') {
  237. $where = ' WHERE is_read=0';
  238. }
  239. $values = array();
  240. if ($search) {
  241. $values[] = '%'.$search.'%';
  242. if ($mode == 'not_read') {
  243. $where = ' AND title LIKE ?';
  244. } else {
  245. $where = ' WHERE title LIKE ?';
  246. }
  247. }
  248. if ($order == 'low_to_high') {
  249. $order = ' DESC';
  250. } else {
  251. $order = '';
  252. }
  253. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  254. $stm = $this->bd->prepare ($sql);
  255. $stm->execute ($values);
  256. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  257. $this->nbItems = $res[0]['count'];
  258. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  259. $fin = $this->nbItemsPerPage;
  260. $sql = 'SELECT * FROM entry' . $where
  261. . ' ORDER BY date' . $order
  262. . ' LIMIT ' . $deb . ', ' . $fin;
  263. $stm = $this->bd->prepare ($sql);
  264. $stm->execute ($values);
  265. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  266. }
  267. public function listFavorites ($mode, $search = false, $order = 'high_to_low') {
  268. $where = ' WHERE is_favorite=1';
  269. if ($mode == 'not_read') {
  270. $where .= ' AND is_read=0';
  271. }
  272. $values = array();
  273. if ($search) {
  274. $values[] = '%'.$search.'%';
  275. $where = ' AND title LIKE ?';
  276. }
  277. if ($order == 'low_to_high') {
  278. $order = ' DESC';
  279. } else {
  280. $order = '';
  281. }
  282. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  283. $stm = $this->bd->prepare ($sql);
  284. $stm->execute ($values);
  285. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  286. $this->nbItems = $res[0]['count'];
  287. if($this->nbItemsPerPage < 0) {
  288. $sql = 'SELECT * FROM entry' . $where
  289. . ' ORDER BY date' . $order;
  290. } else {
  291. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  292. $fin = $this->nbItemsPerPage;
  293. $sql = 'SELECT * FROM entry' . $where
  294. . ' ORDER BY date' . $order
  295. . ' LIMIT ' . $deb . ', ' . $fin;
  296. }
  297. $stm = $this->bd->prepare ($sql);
  298. $stm->execute ($values);
  299. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  300. }
  301. public function listByCategory ($cat, $mode, $search = false, $order = 'high_to_low') {
  302. $where = ' WHERE category=?';
  303. if ($mode == 'not_read') {
  304. $where .= ' AND is_read=0';
  305. }
  306. $values = array ($cat);
  307. if ($search) {
  308. $values[] = '%'.$search.'%';
  309. $where = ' AND title LIKE ?';
  310. }
  311. if ($order == 'low_to_high') {
  312. $order = ' DESC';
  313. } else {
  314. $order = '';
  315. }
  316. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  317. $stm = $this->bd->prepare ($sql);
  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 e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  324. . ' ORDER BY date' . $order
  325. . ' LIMIT ' . $deb . ', ' . $fin;
  326. $stm = $this->bd->prepare ($sql);
  327. $stm->execute ($values);
  328. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  329. }
  330. public function listByFeed ($feed, $mode, $search = false, $order = 'high_to_low') {
  331. $where = ' WHERE id_feed=?';
  332. if ($mode == 'not_read') {
  333. $where .= ' AND is_read=0';
  334. }
  335. $values = array();
  336. if ($search) {
  337. $values[] = '%'.$search.'%';
  338. $where = ' AND title LIKE ?';
  339. }
  340. if ($order == 'low_to_high') {
  341. $order = ' DESC';
  342. } else {
  343. $order = '';
  344. }
  345. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  346. $stm = $this->bd->prepare ($sql);
  347. $values = array ($feed);
  348. $stm->execute ($values);
  349. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  350. $this->nbItems = $res[0]['count'];
  351. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  352. $fin = $this->nbItemsPerPage;
  353. $sql = 'SELECT * FROM entry e' . $where
  354. . ' ORDER BY date' . $order
  355. . ' LIMIT ' . $deb . ', ' . $fin;
  356. $stm = $this->bd->prepare ($sql);
  357. $values = array ($feed);
  358. $stm->execute ($values);
  359. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  360. }
  361. public function count () {
  362. $sql = 'SELECT COUNT(*) AS count FROM entry';
  363. $stm = $this->bd->prepare ($sql);
  364. $stm->execute ();
  365. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  366. return $res[0]['count'];
  367. }
  368. public function countNotRead () {
  369. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  370. $stm = $this->bd->prepare ($sql);
  371. $stm->execute ();
  372. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  373. return $res[0]['count'];
  374. }
  375. public function countFavorites () {
  376. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  377. $stm = $this->bd->prepare ($sql);
  378. $stm->execute ();
  379. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  380. return $res[0]['count'];
  381. }
  382. // gestion de la pagination directement via le DAO
  383. private $nbItemsPerPage = 1;
  384. private $currentPage = 1;
  385. private $nbItems = 0;
  386. public function _nbItemsPerPage ($value) {
  387. $this->nbItemsPerPage = $value;
  388. }
  389. public function _currentPage ($value) {
  390. $this->currentPage = $value;
  391. }
  392. public function currentPage () {
  393. if ($this->currentPage < 1) {
  394. return 1;
  395. }
  396. $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
  397. if ($this->currentPage > $maxPage) {
  398. return $maxPage;
  399. }
  400. return $this->currentPage;
  401. }
  402. public function getPaginator ($entries) {
  403. $paginator = new Paginator ($entries);
  404. $paginator->_nbItems ($this->nbItems);
  405. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  406. $paginator->_currentPage ($this->currentPage ());
  407. return $paginator;
  408. }
  409. }
  410. class HelperEntry {
  411. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  412. $list = array ();
  413. if (!is_array ($listDAO)) {
  414. $listDAO = array ($listDAO);
  415. }
  416. foreach ($listDAO as $key => $dao) {
  417. $list[$key] = new Entry (
  418. $dao['id_feed'],
  419. $dao['guid'],
  420. $dao['title'],
  421. $dao['author'],
  422. unserialize (gzinflate (base64_decode ($dao['content']))),
  423. $dao['link'],
  424. $dao['date'],
  425. $dao['is_read'],
  426. $dao['is_favorite']
  427. );
  428. if (isset ($dao['id'])) {
  429. $list[$key]->_id ($dao['id']);
  430. }
  431. }
  432. return $list;
  433. }
  434. }