summaryrefslogtreecommitdiff
path: root/app/models/Entry.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-05-05 13:20:13 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-05-05 13:20:13 +0200
commit2e7afb7d340eb367d65ac042ae24a10fa021c073 (patch)
tree27208483aac8262cdc42ee492a0f222991a0b4fa /app/models/Entry.php
parentae7c9787cd8afd4313d356c6525e40d4ce79f99b (diff)
parentd4e6176a1ae210c011b14839023f91b4014f2881 (diff)
Merge branch 'releases'0.3.0
Diffstat (limited to 'app/models/Entry.php')
-rwxr-xr-xapp/models/Entry.php333
1 files changed, 102 insertions, 231 deletions
diff --git a/app/models/Entry.php b/app/models/Entry.php
index 230b457bf..f49e74239 100755
--- a/app/models/Entry.php
+++ b/app/models/Entry.php
@@ -216,7 +216,7 @@ class Entry extends Model {
class EntryDAO extends Model_pdo {
public function addEntry ($valuesTmp) {
- $sql = 'INSERT INTO entry(id, guid, title, author, content, link, date, is_read, is_favorite, is_public, id_feed, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
+ $sql = 'INSERT INTO entry(id, guid, title, author, content, link, date, is_read, is_favorite, is_public, id_feed, lastUpdate, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
$stm = $this->bd->prepare ($sql);
$values = array (
@@ -232,6 +232,7 @@ class EntryDAO extends Model_pdo {
$valuesTmp['is_public'],
$valuesTmp['id_feed'],
$valuesTmp['lastUpdate'],
+ $valuesTmp['tags'],
);
if ($stm && $stm->execute ($values)) {
@@ -367,7 +368,7 @@ class EntryDAO extends Model_pdo {
$stm->execute ($values);
$res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $entry = HelperEntry::daoToEntry ($res);
+ list ($entry, $next) = HelperEntry::daoToEntry ($res);
if (isset ($entry[0])) {
return $entry[0];
@@ -376,16 +377,11 @@ class EntryDAO extends Model_pdo {
}
}
- public function listEntries ($mode, $search = false, $order = 'high_to_low') {
- $where = ' WHERE priority > 0';
- if ($mode == 'not_read') {
- $where .= ' AND is_read=0';
- }
-
- $values = array();
- if ($search) {
- $values[] = '%'.$search.'%';
- $where .= ' AND title LIKE ?';
+ public function listWhere ($where, $state, $order, $values = array ()) {
+ if ($state == 'not_read') {
+ $where .= ' AND is_read = 0';
+ } elseif ($state == 'read') {
+ $where .= ' AND is_read = 1';
}
if ($order == 'low_to_high') {
@@ -394,181 +390,28 @@ class EntryDAO extends Model_pdo {
$order = '';
}
- $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
- $stm = $this->bd->prepare ($sql);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $this->nbItems = $res[0]['count'];
-
- $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
- $fin = $this->nbItemsPerPage;
-
$sql = 'SELECT e.* FROM entry e'
. ' INNER JOIN feed f ON e.id_feed = f.id' . $where
- . ' ORDER BY date' . $order
- . ' LIMIT ' . $deb . ', ' . $fin;
+ . ' ORDER BY date' . $order;
$stm = $this->bd->prepare ($sql);
$stm->execute ($values);
return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
}
-
- public function listFavorites ($mode, $search = false, $order = 'high_to_low') {
- $where = ' WHERE is_favorite=1';
- if ($mode == 'not_read') {
- $where .= ' AND is_read=0';
- }
-
- $values = array();
- if ($search) {
- $values[] = '%'.$search.'%';
- $where .= ' AND title LIKE ?';
- }
-
- if ($order == 'low_to_high') {
- $order = ' DESC';
- } else {
- $order = '';
- }
-
- $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
- $stm = $this->bd->prepare ($sql);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $this->nbItems = $res[0]['count'];
-
- if($this->nbItemsPerPage < 0) {
- $sql = 'SELECT * FROM entry' . $where
- . ' ORDER BY date' . $order;
- } else {
- $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
- $fin = $this->nbItemsPerPage;
-
- $sql = 'SELECT * FROM entry' . $where
- . ' ORDER BY date' . $order
- . ' LIMIT ' . $deb . ', ' . $fin;
- }
- $stm = $this->bd->prepare ($sql);
-
- $stm->execute ($values);
-
- return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
+ public function listEntries ($state, $order = 'high_to_low') {
+ return $this->listWhere (' WHERE priority > 0', $state, $order);
}
-
- public function listPublic ($mode, $search = false, $order = 'high_to_low') {
- $where = ' WHERE is_public=1';
- if ($mode == 'not_read') {
- $where .= ' AND is_read=0';
- }
-
- $values = array();
- if ($search) {
- $values[] = '%'.$search.'%';
- $where .= ' AND title LIKE ?';
- }
-
- if ($order == 'low_to_high') {
- $order = ' DESC';
- } else {
- $order = '';
- }
-
- $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
- $stm = $this->bd->prepare ($sql);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $this->nbItems = $res[0]['count'];
-
- if($this->nbItemsPerPage < 0) {
- $sql = 'SELECT * FROM entry' . $where
- . ' ORDER BY date' . $order;
- } else {
- $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
- $fin = $this->nbItemsPerPage;
-
- $sql = 'SELECT * FROM entry' . $where
- . ' ORDER BY date' . $order
- . ' LIMIT ' . $deb . ', ' . $fin;
- }
- $stm = $this->bd->prepare ($sql);
-
- $stm->execute ($values);
-
- return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
+ public function listFavorites ($state, $order = 'high_to_low') {
+ return $this->listWhere (' WHERE is_favorite = 1', $state, $order);
}
-
- public function listByCategory ($cat, $mode, $search = false, $order = 'high_to_low') {
- $where = ' WHERE category=?';
- if ($mode == 'not_read') {
- $where .= ' AND is_read=0';
- }
-
- $values = array ($cat);
- if ($search) {
- $values[] = '%'.$search.'%';
- $where .= ' AND title LIKE ?';
- }
-
- if ($order == 'low_to_high') {
- $order = ' DESC';
- } else {
- $order = '';
- }
-
- $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where;
- $stm = $this->bd->prepare ($sql);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $this->nbItems = $res[0]['count'];
-
- $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
- $fin = $this->nbItemsPerPage;
- $sql = 'SELECT e.* FROM entry e INNER JOIN feed f ON e.id_feed = f.id' . $where
- . ' ORDER BY date' . $order
- . ' LIMIT ' . $deb . ', ' . $fin;
-
- $stm = $this->bd->prepare ($sql);
-
- $stm->execute ($values);
-
- return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
+ public function listPublic ($state, $order = 'high_to_low') {
+ return $this->listWhere (' WHERE is_public = 1', $state, $order);
}
-
- public function listByFeed ($feed, $mode, $search = false, $order = 'high_to_low') {
- $where = ' WHERE id_feed=?';
- if ($mode == 'not_read') {
- $where .= ' AND is_read=0';
- }
-
- $values = array($feed);
- if ($search) {
- $values[] = '%'.$search.'%';
- $where .= ' AND title LIKE ?';
- }
-
- if ($order == 'low_to_high') {
- $order = ' DESC';
- } else {
- $order = '';
- }
-
- $sql = 'SELECT COUNT(*) AS count FROM entry' . $where;
- $stm = $this->bd->prepare ($sql);
- $stm->execute ($values);
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
- $this->nbItems = $res[0]['count'];
-
- $deb = ($this->currentPage () - 1) * $this->nbItemsPerPage;
- $fin = $this->nbItemsPerPage;
- $sql = 'SELECT * FROM entry e' . $where
- . ' ORDER BY date' . $order
- . ' LIMIT ' . $deb . ', ' . $fin;
-
- $stm = $this->bd->prepare ($sql);
-
- $stm->execute ($values);
-
- return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
+ public function listByCategory ($cat, $state, $order = 'high_to_low') {
+ return $this->listWhere (' WHERE category = ?', $state, $order, array ($cat));
+ }
+ public function listByFeed ($feed, $state, $order = 'high_to_low') {
+ return $this->listWhere (' WHERE id_feed = ?', $state, $order, array ($feed));
}
public function count () {
@@ -579,7 +422,6 @@ class EntryDAO extends Model_pdo {
return $res[0]['count'];
}
-
public function countNotRead () {
$sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE is_read=0 AND priority > 0';
$stm = $this->bd->prepare ($sql);
@@ -615,7 +457,6 @@ class EntryDAO extends Model_pdo {
return $res[0]['count'];
}
-
public function countFavorites () {
$sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_favorite=1';
$stm = $this->bd->prepare ($sql);
@@ -624,73 +465,103 @@ class EntryDAO extends Model_pdo {
return $res[0]['count'];
}
+}
- // gestion de la pagination directement via le DAO
- private $nbItemsPerPage = 1;
- private $currentPage = 1;
- private $nbItems = 0;
- public function _nbItemsPerPage ($value) {
- $this->nbItemsPerPage = $value;
- }
- public function _currentPage ($value) {
- $this->currentPage = $value;
- }
- public function currentPage () {
- if ($this->currentPage < 1) {
- return 1;
+class HelperEntry {
+ public static $nb = 1;
+ public static $first = '';
+
+ public static $filter = array (
+ 'words' => array (),
+ 'tags' => array (),
+ );
+
+ public static function daoToEntry ($listDAO) {
+ $list = array ();
+
+ if (!is_array ($listDAO)) {
+ $listDAO = array ($listDAO);
}
- $maxPage = ceil ($this->nbItems / $this->nbItemsPerPage);
- if ($this->currentPage > $maxPage) {
- return $maxPage;
+ $count = 0;
+ $first_is_found = false;
+ $break_after = false;
+ $next = '';
+ foreach ($listDAO as $key => $dao) {
+ $dao['content'] = unserialize (gzinflate (base64_decode ($dao['content'])));
+ $dao['tags'] = preg_split('/[\s#]/', $dao['tags']);
+
+ if (self::tagsMatchEntry ($dao) &&
+ self::searchMatchEntry ($dao)) {
+ if ($break_after) {
+ $next = $dao['id'];
+ break;
+ }
+ if ($first_is_found || $dao['id'] == self::$first || self::$first == '') {
+ $list[$key] = self::createEntry ($dao);
+
+ $count++;
+ $first_is_found = true;
+ }
+ if ($count >= self::$nb) {
+ $break_after = true;
+ }
+ }
}
- return $this->currentPage;
+ unset ($listDAO);
+ return array ($list, $next);
}
- public function getPaginator ($entries) {
- $paginator = new Paginator ($entries);
- $paginator->_nbItems ($this->nbItems);
- $paginator->_nbItemsPerPage ($this->nbItemsPerPage);
- $paginator->_currentPage ($this->currentPage ());
+ private static function createEntry ($dao) {
+ $entry = new Entry (
+ $dao['id_feed'],
+ $dao['guid'],
+ $dao['title'],
+ $dao['author'],
+ $dao['content'],
+ $dao['link'],
+ $dao['date'],
+ $dao['is_read'],
+ $dao['is_favorite'],
+ $dao['is_public']
+ );
- return $paginator;
- }
-}
+ $entry->_notes ($dao['annotation']);
+ $entry->_lastUpdate ($dao['lastUpdate']);
+ $entry->_tags ($dao['tags']);
-class HelperEntry {
- public static function daoToEntry ($listDAO, $mode = 'all', $favorite = false) {
- $list = array ();
+ if (isset ($dao['id'])) {
+ $entry->_id ($dao['id']);
+ }
- if (!is_array ($listDAO)) {
- $listDAO = array ($listDAO);
+ return $entry;
+ }
+
+ private static function tagsMatchEntry ($dao) {
+ $tags = self::$filter['tags'];
+ foreach ($tags as $tag) {
+ if (!in_array ($tag, $dao['tags'])) {
+ return false;
+ }
}
- foreach ($listDAO as $key => $dao) {
- $list[$key] = new Entry (
- $dao['id_feed'],
- $dao['guid'],
- $dao['title'],
- $dao['author'],
- unserialize (gzinflate (base64_decode ($dao['content']))),
- $dao['link'],
- $dao['date'],
- $dao['is_read'],
- $dao['is_favorite'],
- $dao['is_public']
- );
-
- $tags = preg_split('/[\s#]/', $dao['tags']);
- $list[$key]->_notes ($dao['annotation']);
- $list[$key]->_lastUpdate ($dao['lastUpdate']);
- $list[$key]->_tags ($tags);
-
- if (isset ($dao['id'])) {
- $list[$key]->_id ($dao['id']);
+ return true;
+ }
+ private static function searchMatchEntry ($dao) {
+ $words = self::$filter['words'];
+
+ foreach ($words as $word) {
+ $word = strtolower ($word);
+ if (strpos (strtolower ($dao['title']), $word) === false &&
+ strpos (strtolower ($dao['content']), $word) === false &&
+ strpos (strtolower ($dao['link']), $word) === false &&
+ strpos (strtolower ($dao['annotation']), $word) === false) {
+ return false;
}
}
- return $list;
+ return true;
}
}