Entry.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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, annotation, tags, 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['annotation'],
  219. $valuesTmp['tags'],
  220. $valuesTmp['lastUpdate'],
  221. );
  222. if ($stm && $stm->execute ($values)) {
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. public function updateEntry ($id, $valuesTmp) {
  229. if (isset ($valuesTmp['content'])) {
  230. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  231. }
  232. $set = '';
  233. foreach ($valuesTmp as $key => $v) {
  234. $set .= $key . '=?, ';
  235. }
  236. $set = substr ($set, 0, -2);
  237. $sql = 'UPDATE entry SET ' . $set . ' WHERE id=?';
  238. $stm = $this->bd->prepare ($sql);
  239. foreach ($valuesTmp as $v) {
  240. $values[] = $v;
  241. }
  242. $values[] = $id;
  243. if ($stm && $stm->execute ($values)) {
  244. return true;
  245. } else {
  246. return false;
  247. }
  248. }
  249. public function markReadEntries ($read, $dateMax) {
  250. $sql = 'UPDATE entry SET is_read = ? WHERE date < ?';
  251. $stm = $this->bd->prepare ($sql);
  252. $values = array ($read, $dateMax);
  253. if ($stm && $stm->execute ($values)) {
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. }
  259. public function markReadCat ($id, $read, $dateMax) {
  260. $sql = 'UPDATE entry e INNER JOIN feed f ON e.id_feed = f.id SET is_read = ? WHERE category = ? AND date < ?';
  261. $stm = $this->bd->prepare ($sql);
  262. $values = array ($read, $id, $dateMax);
  263. if ($stm && $stm->execute ($values)) {
  264. return true;
  265. } else {
  266. return false;
  267. }
  268. }
  269. public function markReadFeed ($id, $read, $dateMax) {
  270. $sql = 'UPDATE entry SET is_read = ? WHERE id_feed = ? AND date < ?';
  271. $stm = $this->bd->prepare ($sql);
  272. $values = array ($read, $id, $dateMax);
  273. if ($stm && $stm->execute ($values)) {
  274. return true;
  275. } else {
  276. return false;
  277. }
  278. }
  279. public function updateEntries ($valuesTmp) {
  280. if (isset ($valuesTmp['content'])) {
  281. $valuesTmp['content'] = base64_encode (gzdeflate (serialize ($valuesTmp['content'])));
  282. }
  283. $set = '';
  284. foreach ($valuesTmp as $key => $v) {
  285. $set .= $key . '=?, ';
  286. }
  287. $set = substr ($set, 0, -2);
  288. $sql = 'UPDATE entry SET ' . $set;
  289. $stm = $this->bd->prepare ($sql);
  290. foreach ($valuesTmp as $v) {
  291. $values[] = $v;
  292. }
  293. if ($stm && $stm->execute ($values)) {
  294. return true;
  295. } else {
  296. return false;
  297. }
  298. }
  299. public function cleanOldEntries ($nb_month) {
  300. $date = 60 * 60 * 24 * 30 * $nb_month;
  301. $sql = 'DELETE FROM entry WHERE date <= ? AND is_favorite = 0 AND notes != ""';
  302. $stm = $this->bd->prepare ($sql);
  303. $values = array (
  304. time () - $date
  305. );
  306. if ($stm && $stm->execute ($values)) {
  307. return true;
  308. } else {
  309. return false;
  310. }
  311. }
  312. public function searchById ($id) {
  313. $sql = 'SELECT * FROM entry WHERE id=?';
  314. $stm = $this->bd->prepare ($sql);
  315. $values = array ($id);
  316. $stm->execute ($values);
  317. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  318. $entry = HelperEntry::daoToEntry ($res);
  319. if (isset ($entry[0])) {
  320. return $entry[0];
  321. } else {
  322. return false;
  323. }
  324. }
  325. public function listEntries ($mode, $search = false, $order = 'high_to_low') {
  326. $where = '';
  327. if ($mode == 'not_read') {
  328. $where = ' WHERE is_read=0';
  329. }
  330. $values = array();
  331. if ($search) {
  332. $values[] = '%'.$search.'%';
  333. if ($mode == 'not_read') {
  334. $where = ' AND title LIKE ?';
  335. } else {
  336. $where = ' WHERE title LIKE ?';
  337. }
  338. }
  339. if ($order == 'low_to_high') {
  340. $order = ' DESC';
  341. } else {
  342. $order = '';
  343. }
  344. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  345. $stm = $this->bd->prepare ($sql);
  346. $stm->execute ($values);
  347. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  348. $this->nbItems = $res[0]['count'];
  349. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  350. $fin = $this->nbItemsPerPage;
  351. $sql = 'SELECT * FROM entry' . $where
  352. . ' ORDER BY date' . $order
  353. . ' LIMIT ' . $deb . ', ' . $fin;
  354. $stm = $this->bd->prepare ($sql);
  355. $stm->execute ($values);
  356. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  357. }
  358. public function listFavorites ($mode, $search = false, $order = 'high_to_low') {
  359. $where = ' WHERE is_favorite=1';
  360. if ($mode == 'not_read') {
  361. $where .= ' AND is_read=0';
  362. }
  363. $values = array();
  364. if ($search) {
  365. $values[] = '%'.$search.'%';
  366. $where = ' AND title LIKE ?';
  367. }
  368. if ($order == 'low_to_high') {
  369. $order = ' DESC';
  370. } else {
  371. $order = '';
  372. }
  373. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  374. $stm = $this->bd->prepare ($sql);
  375. $stm->execute ($values);
  376. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  377. $this->nbItems = $res[0]['count'];
  378. if($this->nbItemsPerPage < 0) {
  379. $sql = 'SELECT * FROM entry' . $where
  380. . ' ORDER BY date' . $order;
  381. } else {
  382. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  383. $fin = $this->nbItemsPerPage;
  384. $sql = 'SELECT * FROM entry' . $where
  385. . ' ORDER BY date' . $order
  386. . ' LIMIT ' . $deb . ', ' . $fin;
  387. }
  388. $stm = $this->bd->prepare ($sql);
  389. $stm->execute ($values);
  390. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  391. }
  392. public function listPublic ($order = 'high_to_low') {
  393. $where = ' WHERE is_public=1';
  394. if ($order == 'low_to_high') {
  395. $order = ' DESC';
  396. } else {
  397. $order = '';
  398. }
  399. $sql = 'SELECT * FROM entry' . $where . ' ORDER BY date' . $order;
  400. $stm = $this->bd->prepare ($sql);
  401. $stm->execute ();
  402. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  403. }
  404. public function listByCategory ($cat, $mode, $search = false, $order = 'high_to_low') {
  405. $where = ' WHERE category=?';
  406. if ($mode == 'not_read') {
  407. $where .= ' AND is_read=0';
  408. }
  409. $values = array ($cat);
  410. if ($search) {
  411. $values[] = '%'.$search.'%';
  412. $where = ' AND title LIKE ?';
  413. }
  414. if ($order == 'low_to_high') {
  415. $order = ' DESC';
  416. } else {
  417. $order = '';
  418. }
  419. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
  420. $stm = $this->bd->prepare ($sql);
  421. $stm->execute ($values);
  422. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  423. $this->nbItems = $res[0]['count'];
  424. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  425. $fin = $this->nbItemsPerPage;
  426. $sql = 'SELECT e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
  427. . ' ORDER BY date' . $order
  428. . ' LIMIT ' . $deb . ', ' . $fin;
  429. $stm = $this->bd->prepare ($sql);
  430. $stm->execute ($values);
  431. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  432. }
  433. public function listByFeed ($feed, $mode, $search = false, $order = 'high_to_low') {
  434. $where = ' WHERE id_feed=?';
  435. if ($mode == 'not_read') {
  436. $where .= ' AND is_read=0';
  437. }
  438. $values = array();
  439. if ($search) {
  440. $values[] = '%'.$search.'%';
  441. $where = ' AND title LIKE ?';
  442. }
  443. if ($order == 'low_to_high') {
  444. $order = ' DESC';
  445. } else {
  446. $order = '';
  447. }
  448. $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
  449. $stm = $this->bd->prepare ($sql);
  450. $values = array ($feed);
  451. $stm->execute ($values);
  452. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  453. $this->nbItems = $res[0]['count'];
  454. $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
  455. $fin = $this->nbItemsPerPage;
  456. $sql = 'SELECT * FROM entry e' . $where
  457. . ' ORDER BY date' . $order
  458. . ' LIMIT ' . $deb . ', ' . $fin;
  459. $stm = $this->bd->prepare ($sql);
  460. $values = array ($feed);
  461. $stm->execute ($values);
  462. return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
  463. }
  464. public function count () {
  465. $sql = 'SELECT COUNT(*) AS count FROM entry';
  466. $stm = $this->bd->prepare ($sql);
  467. $stm->execute ();
  468. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  469. return $res[0]['count'];
  470. }
  471. public function countNotRead () {
  472. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0';
  473. $stm = $this->bd->prepare ($sql);
  474. $stm->execute ();
  475. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  476. return $res[0]['count'];
  477. }
  478. public function countNotReadByFeed ($id) {
  479. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read = 0 AND id_feed = ?';
  480. $stm = $this->bd->prepare ($sql);
  481. $stm->execute (array ($id));
  482. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  483. return $res[0]['count'];
  484. }
  485. public function countNotReadByCat ($id) {
  486. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND category = ?';
  487. $stm = $this->bd->prepare ($sql);
  488. $stm->execute (array ($id));
  489. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  490. return $res[0]['count'];
  491. }
  492. public function countNotReadFavorites () {
  493. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND is_favorite=1';
  494. $stm = $this->bd->prepare ($sql);
  495. $stm->execute ();
  496. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  497. return $res[0]['count'];
  498. }
  499. public function countFavorites () {
  500. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
  501. $stm = $this->bd->prepare ($sql);
  502. $stm->execute ();
  503. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  504. return $res[0]['count'];
  505. }
  506. // gestion de la pagination directement via le DAO
  507. private $nbItemsPerPage = 1;
  508. private $currentPage = 1;
  509. private $nbItems = 0;
  510. public function _nbItemsPerPage ($value) {
  511. $this->nbItemsPerPage = $value;
  512. }
  513. public function _currentPage ($value) {
  514. $this->currentPage = $value;
  515. }
  516. public function currentPage () {
  517. if ($this->currentPage < 1) {
  518. return 1;
  519. }
  520. $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
  521. if ($this->currentPage > $maxPage) {
  522. return $maxPage;
  523. }
  524. return $this->currentPage;
  525. }
  526. public function getPaginator ($entries) {
  527. $paginator = new Paginator ($entries);
  528. $paginator->_nbItems ($this->nbItems);
  529. $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
  530. $paginator->_currentPage ($this->currentPage ());
  531. return $paginator;
  532. }
  533. }
  534. class HelperEntry {
  535. public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
  536. $list = array ();
  537. if (!is_array ($listDAO)) {
  538. $listDAO = array ($listDAO);
  539. }
  540. foreach ($listDAO as $key => $dao) {
  541. $list[$key] = new Entry (
  542. $dao['id_feed'],
  543. $dao['guid'],
  544. $dao['title'],
  545. $dao['author'],
  546. unserialize (gzinflate (base64_decode ($dao['content']))),
  547. $dao['link'],
  548. $dao['date'],
  549. $dao['is_read'],
  550. $dao['is_favorite'],
  551. $dao['is_public']
  552. );
  553. $tags = preg_split('/[\s#]/', $dao['tags']);
  554. $list[$key]->_notes ($dao['annotation']);
  555. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  556. $list[$key]->_tags ($tags);
  557. if (isset ($dao['id'])) {
  558. $list[$key]->_id ($dao['id']);
  559. }
  560. }
  561. return $list;
  562. }
  563. }