+
+ public function search($term, $user_id, $limit = '')
+ {
+ $search = '%'.$term.'%';
+ $sql_action = "SELECT * FROM entries WHERE user_id=? AND (content LIKE ? OR title LIKE ? OR url LIKE ?) "; //searches in content, title and URL
+ $sql_action .= $this->getEntriesOrder().' ' . $limit;
+ $params_action = array($user_id, $search, $search, $search);
+ $query = $this->executeQuery($sql_action, $params_action);
+ return $query->fetchAll();
+ }
+
+ public function retrieveAllTags($user_id, $term = NULL)
+ {
+ $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags
+ LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
+ LEFT JOIN entries ON tags_entries.entry_id=entries.id
+ WHERE entries.user_id=?
+ ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ."
+ GROUP BY tags.id, tags.value
+ ORDER BY tags.value";
+ $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) ));
+ $tags = $query->fetchAll();
+
+ return $tags;
+ }
+
+ public function retrieveTag($id, $user_id)
+ {
+ $tag = NULL;
+ $sql = "SELECT DISTINCT tags.* FROM tags
+ LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
+ LEFT JOIN entries ON tags_entries.entry_id=entries.id
+ WHERE tags.id=? AND entries.user_id=?";
+ $params = array(intval($id), $user_id);
+ $query = $this->executeQuery($sql, $params);
+ $tag = $query->fetchAll();
+
+ return isset($tag[0]) ? $tag[0] : NULL;
+ }
+
+ public function retrieveEntriesByTag($tag_id, $user_id)
+ {
+ $sql =
+ "SELECT entries.* FROM entries
+ LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
+ WHERE tags_entries.tag_id = ? AND entries.user_id=? ORDER by entries.id DESC";
+ $query = $this->executeQuery($sql, array($tag_id, $user_id));
+ $entries = $query->fetchAll();
+
+ return $entries;
+ }
+
+ public function retrieveTagsByEntry($entry_id)
+ {
+ $sql =
+ "SELECT tags.* FROM tags
+ LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
+ WHERE tags_entries.entry_id = ?";
+ $query = $this->executeQuery($sql, array($entry_id));
+ $tags = $query->fetchAll();
+
+ return $tags;
+ }
+
+ public function removeTagForEntry($entry_id, $tag_id)
+ {
+ $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
+ $params_action = array($tag_id, $entry_id);
+ $query = $this->executeQuery($sql_action, $params_action);
+ return $query;
+ }
+
+ public function cleanUnusedTag($tag_id)
+ {
+ $sql_action = "SELECT tags.* FROM tags JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
+ $query = $this->executeQuery($sql_action,array($tag_id));
+ $tagstokeep = $query->fetchAll();
+ $sql_action = "SELECT tags.* FROM tags LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id WHERE tags.id=?";
+ $query = $this->executeQuery($sql_action,array($tag_id));
+ $alltags = $query->fetchAll();
+
+ foreach ($alltags as $tag) {
+ if ($tag && !in_array($tag,$tagstokeep)) {
+ $sql_action = "DELETE FROM tags WHERE id=?";
+ $params_action = array($tag[0]);
+ $this->executeQuery($sql_action, $params_action);
+ return true;
+ }
+ }
+
+ }
+
+ public function retrieveTagByValue($value)
+ {
+ $tag = NULL;
+ $sql = "SELECT * FROM tags WHERE value=?";
+ $params = array($value);
+ $query = $this->executeQuery($sql, $params);
+ $tag = $query->fetchAll();
+
+ return isset($tag[0]) ? $tag[0] : null;
+ }
+
+ public function createTag($value)
+ {
+ $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
+ $params_action = array($value);
+ $query = $this->executeQuery($sql_action, $params_action);
+ return $query;
+ }
+
+ public function setTagToEntry($tag_id, $entry_id)
+ {
+ $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
+ $params_action = array($tag_id, $entry_id);
+ $query = $this->executeQuery($sql_action, $params_action);
+ return $query;
+ }
+
+ private function getEntriesOrder()
+ {
+ if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) {
+ return $this->order[$_SESSION['sort']];
+ }
+ else {
+ return $this->order['default'];
+ }
+ }