Entry.php 9.6 KB

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