Entry.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 $is_public;
  13. private $feed;
  14. private $tags;
  15. private $notes;
  16. private $lastUpdate;
  17. public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
  18. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false,
  19. $is_public = false) {
  20. $this->_guid ($guid);
  21. $this->_title ($title);
  22. $this->_author ($author);
  23. $this->_content ($content);
  24. $this->_link ($link);
  25. $this->_date ($pubdate);
  26. $this->_isRead ($is_read);
  27. $this->_isFavorite ($is_favorite);
  28. $this->_isPublic ($is_public);
  29. $this->_feed ($feed);
  30. $this->_lastUpdate ($pubdate);
  31. $this->_notes ('');
  32. $this->_tags (array ());
  33. }
  34. public function id () {
  35. if(is_null($this->id)) {
  36. return small_hash ($this->guid . Configuration::selApplication ());
  37. } else {
  38. return $this->id;
  39. }
  40. }
  41. public function guid () {
  42. return $this->guid;
  43. }
  44. public function title () {
  45. return $this->title;
  46. }
  47. public function author () {
  48. return $this->author;
  49. }
  50. public function content () {
  51. return $this->content;
  52. }
  53. public function link () {
  54. return $this->link;
  55. }
  56. public function date ($raw = false) {
  57. if ($raw) {
  58. return $this->date;
  59. } else {
  60. return timestamptodate ($this->date);
  61. }
  62. }
  63. public function isRead () {
  64. return $this->is_read;
  65. }
  66. public function isFavorite () {
  67. return $this->is_favorite;
  68. }
  69. public function isPublic () {
  70. return $this->is_public;
  71. }
  72. public function feed ($object = false) {
  73. if ($object) {
  74. $feedDAO = new FeedDAO ();
  75. return $feedDAO->searchById ($this->feed);
  76. } else {
  77. return $this->feed;
  78. }
  79. }
  80. public function tags ($inString = false) {
  81. if ($inString) {
  82. if (!empty ($this->tags)) {
  83. return '#' . implode(' #', $this->tags);
  84. } else {
  85. return '';
  86. }
  87. } else {
  88. return $this->tags;
  89. }
  90. }
  91. public function notes ($raw = true, $parse_tags = true) {
  92. if ($raw) {
  93. return $this->notes;
  94. } else {
  95. if($parse_tags) {
  96. return parse_tags (bbdecode ($this->notes));
  97. } else {
  98. return bbdecode ($this->notes);
  99. }
  100. }
  101. }
  102. public function lastUpdate ($raw = false) {
  103. if ($raw) {
  104. return $this->lastUpdate;
  105. } else {
  106. return timestamptodate ($this->lastUpdate);
  107. }
  108. }
  109. public function _id ($value) {
  110. $this->id = $value;
  111. }
  112. public function _guid ($value) {
  113. $this->guid = $value;
  114. }
  115. public function _title ($value) {
  116. $this->title = $value;
  117. }
  118. public function _author ($value) {
  119. $this->author = $value;
  120. }
  121. public function _content ($value) {
  122. $this->content = $value;
  123. }
  124. public function _link ($value) {
  125. $this->link = $value;
  126. }
  127. public function _date ($value) {
  128. if (is_int (intval ($value))) {
  129. $this->date = $value;
  130. } else {
  131. $this->date = time ();
  132. }
  133. }
  134. public function _isRead ($value) {
  135. $this->is_read = $value;
  136. }
  137. public function _isFavorite ($value) {
  138. $this->is_favorite = $value;
  139. }
  140. public function _isPublic ($value) {
  141. $this->is_public = $value;
  142. }
  143. public function _feed ($value) {
  144. $this->feed = $value;
  145. }
  146. public function _tags ($value) {
  147. if (!is_array ($value)) {
  148. $value = array ($value);
  149. }
  150. foreach ($value as $key => $t) {
  151. if (!$t) {
  152. unset ($value[$key]);
  153. }
  154. }
  155. $this->tags = $value;
  156. }
  157. public function _notes ($value) {
  158. $this->notes = $value;
  159. }
  160. public function _lastUpdate ($value) {
  161. if (is_int (intval ($value))) {
  162. $this->lastUpdate = $value;
  163. } else {
  164. $this->lastUpdate = $this->date (true);
  165. }
  166. }
  167. public function isDay ($day) {
  168. $date = getdate ();
  169. $today = mktime (0, 0, 0, $date['mon'], $date['mday'], $date['year']);
  170. $yesterday = $today - 86400;
  171. if ($day == Days::TODAY &&
  172. $this->date >= $today && $this->date < $today + 86400) {
  173. return true;
  174. } elseif ($day == Days::YESTERDAY &&
  175. $this->date >= $yesterday && $this->date < $yesterday + 86400) {
  176. return true;
  177. } elseif ($day == Days::BEFORE_YESTERDAY && $this->date < $yesterday) {
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }
  183. public function toArray () {
  184. return array (
  185. 'id' => $this->id (),
  186. 'guid' => $this->guid (),
  187. 'title' => $this->title (),
  188. 'author' => $this->author (),
  189. 'content' => $this->content (),
  190. 'link' => $this->link (),
  191. 'date' => $this->date (true),
  192. 'is_read' => $this->isRead (),
  193. 'is_favorite' => $this->isFavorite (),
  194. 'is_public' => $this->isPublic (),
  195. 'id_feed' => $this->feed (),
  196. 'lastUpdate' => $this->lastUpdate (true),
  197. 'tags' => $this->tags (true),
  198. 'annotation' => $this->notes ()
  199. );
  200. }
  201. }
  202. class EntryDAO extends Model_pdo {
  203. public function addEntry ($valuesTmp) {
  204. $sql = 'INSERT INTO entry(id, guid, title, author, content, link, date, is_read, is_favorite, is_public, id_feed, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  205. $stm = $this->bd->prepare ($sql);
  206. $values = array (
  207. $valuesTmp['id'],
  208. $valuesTmp['guid'],
  209. $valuesTmp['title'],
  210. $valuesTmp['author'],
  211. base64_encode (gzdeflate (serialize ($valuesTmp['content']))),
  212. $valuesTmp['link'],
  213. $valuesTmp['date'],
  214. $valuesTmp['is_read'],
  215. $valuesTmp['is_favorite'],
  216. $valuesTmp['is_public'],
  217. $valuesTmp['id_feed'],
  218. $valuesTmp['lastUpdate'],
  219. );
  220. if ($stm && $stm->execute ($values)) {
  221. return true;
  222. } else {
  223. return false;
  224. }
  225. }
  226. public function updateEntry ($id, $valuesTmp) {
  227. if (isset ($valuesTmp['content'])) {
  228. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  229. }
  230. $set = '';
  231. foreach ($valuesTmp as $key => $v) {
  232. $set .= $key . '=?, ';
  233. }
  234. $set = substr ($set, 0, -2);
  235. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  236. $stm = $this->bd->prepare ($sql);
  237. foreach ($valuesTmp as $v) {
  238. $values[] = $v;
  239. }
  240. $values[] = $id;
  241. if ($stm && $stm->execute ($values)) {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. public function markReadEntries ($read, $dateMax) {
  248. $sql = 'UPDATE entry SET is_read = ? WHERE date < ?';
  249. $stm = $this->bd->prepare ($sql);
  250. $values = array ($read, $dateMax);
  251. if ($stm && $stm->execute ($values)) {
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. public function markReadCat ($id, $read, $dateMax) {
  258. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ? AND date < ?';
  259. $stm = $this->bd->prepare ($sql);
  260. $values = array ($read, $id, $dateMax);
  261. if ($stm && $stm->execute ($values)) {
  262. return true;
  263. } else {
  264. return false;
  265. }
  266. }
  267. public function markReadFeed ($id, $read, $dateMax) {
  268. $sql = 'UPDATE entry SET is_read = ? WHERE id_feed = ? AND date < ?';
  269. $stm = $this->bd->prepare ($sql);
  270. $values = array ($read, $id, $dateMax);
  271. if ($stm && $stm->execute ($values)) {
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. }
  277. public function updateEntries ($valuesTmp) {
  278. if (isset ($valuesTmp['content'])) {
  279. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  280. }
  281. $set = '';
  282. foreach ($valuesTmp as $key => $v) {
  283. $set .= $key . '=?, ';
  284. }
  285. $set = substr ($set, 0, -2);
  286. $sql = 'UPDATE entry SET ' . $set;
  287. $stm = $this->bd->prepare ($sql);
  288. foreach ($valuesTmp as $v) {
  289. $values[] = $v;
  290. }
  291. if ($stm && $stm->execute ($values)) {
  292. return true;
  293. } else {
  294. return false;
  295. }
  296. }
  297. public function cleanOldEntries ($nb_month) {
  298. $date = 60 * 60 * 24 * 30 * $nb_month;
  299. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0 AND notes != ""';
  300. $stm = $this->bd->prepare ($sql);
  301. $values = array (
  302. time () - $date
  303. );
  304. if ($stm && $stm->execute ($values)) {
  305. return true;
  306. } else {
  307. return false;
  308. }
  309. }
  310. public function searchById ($id) {
  311. $sql = 'SELECT * FROM entry WHERE id=?';
  312. $stm = $this->bd->prepare ($sql);
  313. $values = array ($id);
  314. $stm->execute ($values);
  315. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  316. $entry = HelperEntry::daoToEntry ($res);
  317. if (isset ($entry[0])) {
  318. return $entry[0];
  319. } else {
  320. return false;
  321. }
  322. }
  323. public function listEntries ($mode, $search = false, $order = 'high_to_low') {
  324. $where = '';
  325. if ($mode == 'not_read') {
  326. $where = ' WHERE is_read=0';
  327. }
  328. $values = array();
  329. if ($search) {
  330. $values[] = '%'.$search.'%';
  331. if ($mode == 'not_read') {
  332. $where = ' AND title LIKE ?';
  333. } else {
  334. $where = ' WHERE title LIKE ?';
  335. }
  336. }
  337. if ($order == 'low_to_high') {
  338. $order = ' DESC';
  339. } else {
  340. $order = '';
  341. }
  342. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  343. $stm = $this->bd->prepare ($sql);
  344. $stm->execute ($values);
  345. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  346. $this->nbItems = $res[0]['count'];
  347. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  348. $fin = $this->nbItemsPerPage;
  349. $sql = 'SELECT * FROM entry' . $where
  350. . ' ORDER BY date' . $order
  351. . ' LIMIT ' . $deb . ', ' . $fin;
  352. $stm = $this->bd->prepare ($sql);
  353. $stm->execute ($values);
  354. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  355. }
  356. public function listFavorites ($mode, $search = false, $order = 'high_to_low') {
  357. $where = ' WHERE is_favorite=1';
  358. if ($mode == 'not_read') {
  359. $where .= ' AND is_read=0';
  360. }
  361. $values = array();
  362. if ($search) {
  363. $values[] = '%'.$search.'%';
  364. $where = ' AND title LIKE ?';
  365. }
  366. if ($order == 'low_to_high') {
  367. $order = ' DESC';
  368. } else {
  369. $order = '';
  370. }
  371. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  372. $stm = $this->bd->prepare ($sql);
  373. $stm->execute ($values);
  374. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  375. $this->nbItems = $res[0]['count'];
  376. if($this->nbItemsPerPage < 0) {
  377. $sql = 'SELECT * FROM entry' . $where
  378. . ' ORDER BY date' . $order;
  379. } else {
  380. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  381. $fin = $this->nbItemsPerPage;
  382. $sql = 'SELECT * FROM entry' . $where
  383. . ' ORDER BY date' . $order
  384. . ' LIMIT ' . $deb . ', ' . $fin;
  385. }
  386. $stm = $this->bd->prepare ($sql);
  387. $stm->execute ($values);
  388. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  389. }
  390. public function listByCategory ($cat, $mode, $search = false, $order = 'high_to_low') {
  391. $where = ' WHERE category=?';
  392. if ($mode == 'not_read') {
  393. $where .= ' AND is_read=0';
  394. }
  395. $values = array ($cat);
  396. if ($search) {
  397. $values[] = '%'.$search.'%';
  398. $where = ' AND title LIKE ?';
  399. }
  400. if ($order == 'low_to_high') {
  401. $order = ' DESC';
  402. } else {
  403. $order = '';
  404. }
  405. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  406. $stm = $this->bd->prepare ($sql);
  407. $stm->execute ($values);
  408. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  409. $this->nbItems = $res[0]['count'];
  410. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  411. $fin = $this->nbItemsPerPage;
  412. $sql = 'SELECT e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  413. . ' ORDER BY date' . $order
  414. . ' LIMIT ' . $deb . ', ' . $fin;
  415. $stm = $this->bd->prepare ($sql);
  416. $stm->execute ($values);
  417. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  418. }
  419. public function listByFeed ($feed, $mode, $search = false, $order = 'high_to_low') {
  420. $where = ' WHERE id_feed=?';
  421. if ($mode == 'not_read') {
  422. $where .= ' AND is_read=0';
  423. }
  424. $values = array();
  425. if ($search) {
  426. $values[] = '%'.$search.'%';
  427. $where = ' AND title LIKE ?';
  428. }
  429. if ($order == 'low_to_high') {
  430. $order = ' DESC';
  431. } else {
  432. $order = '';
  433. }
  434. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  435. $stm = $this->bd->prepare ($sql);
  436. $values = array ($feed);
  437. $stm->execute ($values);
  438. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  439. $this->nbItems = $res[0]['count'];
  440. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  441. $fin = $this->nbItemsPerPage;
  442. $sql = 'SELECT * FROM entry e' . $where
  443. . ' ORDER BY date' . $order
  444. . ' LIMIT ' . $deb . ', ' . $fin;
  445. $stm = $this->bd->prepare ($sql);
  446. $values = array ($feed);
  447. $stm->execute ($values);
  448. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  449. }
  450. public function count () {
  451. $sql = 'SELECT COUNT(*) AS count FROM entry';
  452. $stm = $this->bd->prepare ($sql);
  453. $stm->execute ();
  454. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  455. return $res[0]['count'];
  456. }
  457. public function countNotRead () {
  458. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  459. $stm = $this->bd->prepare ($sql);
  460. $stm->execute ();
  461. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  462. return $res[0]['count'];
  463. }
  464. public function countNotReadByFeed ($id) {
  465. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read = 0 AND id_feed = ?';
  466. $stm = $this->bd->prepare ($sql);
  467. $stm->execute (array ($id));
  468. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  469. return $res[0]['count'];
  470. }
  471. public function countNotReadByCat ($id) {
  472. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND category = ?';
  473. $stm = $this->bd->prepare ($sql);
  474. $stm->execute (array ($id));
  475. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  476. return $res[0]['count'];
  477. }
  478. public function countNotReadFavorites () {
  479. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND is_favorite=1';
  480. $stm = $this->bd->prepare ($sql);
  481. $stm->execute ();
  482. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  483. return $res[0]['count'];
  484. }
  485. public function countFavorites () {
  486. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  487. $stm = $this->bd->prepare ($sql);
  488. $stm->execute ();
  489. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  490. return $res[0]['count'];
  491. }
  492. // gestion de la pagination directement via le DAO
  493. private $nbItemsPerPage = 1;
  494. private $currentPage = 1;
  495. private $nbItems = 0;
  496. public function _nbItemsPerPage ($value) {
  497. $this->nbItemsPerPage = $value;
  498. }
  499. public function _currentPage ($value) {
  500. $this->currentPage = $value;
  501. }
  502. public function currentPage () {
  503. if ($this->currentPage < 1) {
  504. return 1;
  505. }
  506. $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
  507. if ($this->currentPage > $maxPage) {
  508. return $maxPage;
  509. }
  510. return $this->currentPage;
  511. }
  512. public function getPaginator ($entries) {
  513. $paginator = new Paginator ($entries);
  514. $paginator->_nbItems ($this->nbItems);
  515. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  516. $paginator->_currentPage ($this->currentPage ());
  517. return $paginator;
  518. }
  519. }
  520. class HelperEntry {
  521. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  522. $list = array ();
  523. if (!is_array ($listDAO)) {
  524. $listDAO = array ($listDAO);
  525. }
  526. foreach ($listDAO as $key => $dao) {
  527. $list[$key] = new Entry (
  528. $dao['id_feed'],
  529. $dao['guid'],
  530. $dao['title'],
  531. $dao['author'],
  532. unserialize (gzinflate (base64_decode ($dao['content']))),
  533. $dao['link'],
  534. $dao['date'],
  535. $dao['is_read'],
  536. $dao['is_favorite'],
  537. $dao['is_public']
  538. );
  539. $tags = preg_split('/[\s#]/', $dao['tags']);
  540. $list[$key]->_notes ($dao['annotation']);
  541. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  542. $list[$key]->_tags ($tags);
  543. if (isset ($dao['id'])) {
  544. $list[$key]->_id ($dao['id']);
  545. }
  546. }
  547. return $list;
  548. }
  549. }