title = 'AGGREGGATE'; $this->subtitle = 'AGREGGATE SUBTITLE'; $this->created_on = new DateTime(); $this->updated_on = new DateTime(); $this->app = $app; $tmp_feeds = array(); foreach ($feeds as $feed) { $tmp_feeds[$feed->get_id()] = $feed; } $this->feeds = $tmp_feeds; return $this; } public function get_id() { throw new LogicException('Aggregate feed does not have an id'); } /** * * @return string */ public function get_icon_url() { $url = '/skins/icons/rss32.gif'; return $url; } /** * * @return boolean */ public function is_aggregated() { return true; } /** * * @param int $offset_start * @param int $how_many * @return Feed_Entry_Collection */ public function get_entries($offset_start, $how_many) { $result = new Feed_Entry_Collection(); if (count($this->feeds) === 0) { return $result; } $offset_start = (int) $offset_start; $how_many = $how_many > 20 ? 20 : (int) $how_many; $sql = 'SELECT id, feed_id FROM feed_entries WHERE feed_id IN (' . implode(', ', array_keys($this->feeds)) . ') ORDER BY id DESC LIMIT ' . $offset_start . ', ' . $how_many; $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); foreach ($rs as $row) { $entry = new Feed_Entry_Adapter($this->app, $this->feeds[$row['feed_id']], $row['id']); $result->add_entry($entry); } return $result; } /** * * @return int */ public function get_count_total_entries() { if (count($this->feeds) === 0) { return 0; } $sql = 'SELECT count(id) as number FROM feed_entries WHERE feed_id IN (' . implode(', ', array_keys($this->feeds)) . ') '; $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $number = $row ? (int) $row['number'] : 0; $stmt->closeCursor(); return $number; } /** * * @param registryInterface $registry * @param string $format * @param int $page * @return Feed_Link */ public function get_homepage_link(registryInterface $registry, $format, $page = null) { switch ($format) { case self::FORMAT_ATOM: return new Feed_Link( sprintf('%s/feeds/aggregated/atom/%s' , rtrim($registry->get('GV_ServerName'), '/') , ($page ? '?page=' . $page : '') ) , sprintf('%s - %s', $this->get_title(), 'Atom') , 'application/atom+xml' ); break; case self::FORMAT_COOLIRIS: return new Feed_Link( sprintf('%s/feeds/cooliris/%s' , rtrim($registry->get('GV_ServerName'), '/') , ($page ? '?page=' . $page : '') ) , sprintf('%s - %s', $this->get_title(), 'RSS') , 'application/rss+xml' ); break; default: case self::FORMAT_RSS: return new Feed_Link( sprintf('%s/feeds/aggregated/rss/%s' , rtrim($registry->get('GV_ServerName'), '/') , ($page ? '?page=' . $page : '') ) , sprintf('%s - %s', $this->get_title(), 'RSS') , 'application/rss+xml' ); break; } } /** * * @param User_Adapter $user * @param boolean $renew * @return string */ protected function get_token(User_Adapter $user, $renew = false) { $sql = 'SELECT token FROM feed_tokens WHERE usr_id = :usr_id AND aggregated = "1"'; $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute(array(':usr_id' => $user->get_id())); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); if (!$row || $renew === true) { $token = random::generatePassword(12, random::LETTERS_AND_NUMBERS); $sql = 'REPLACE INTO feed_tokens (id, token, feed_id, usr_id, aggregated) VALUES (null, :token, :feed_id, :usr_id, :aggregated)'; $params = array( ':token' => $token , ':feed_id' => null , ':usr_id' => $user->get_id() , ':aggregated' => '1' ); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt->execute($params); } else { $token = $row['token']; } return $token; } /** * * @param Application $app * @param User_Adapter $user * @param array $reduce * @return Feed_Aggregate */ public static function load_with_user(Application $app, User_Adapter $user, $reduce = array()) { $feeds = Feed_Collection::load($app, $user, $reduce); return new self($app, $feeds->get_feeds()); } /** * * @param registryInterface $registry * @param User_Adapter $user * @param string $format * @param int $page * @param boolean $renew_token * @return Feed_Link */ public function get_user_link(registryInterface $registry, User_Adapter $user, $format, $page = null, $renew_token = false) { switch ($format) { case self::FORMAT_ATOM: return new Feed_Link( sprintf('%s/feeds/userfeed/aggregated/%s/atom/%s' , rtrim($registry->get('GV_ServerName'), '/') , $this->get_token($user, $renew_token) , ($page ? '?page=' . $page : '') ) , sprintf('%s - %s', $this->get_title(), 'Atom') , 'application/atom+xml' ); break; case self::FORMAT_RSS: return new Feed_Link( sprintf('%s/feeds/userfeed/aggregated/%s/rss/%s' , rtrim($registry->get('GV_ServerName'), '/') , $this->get_token($user, $renew_token) , ($page ? '?page=' . $page : '') ) , sprintf('%s - %s', $this->get_title(), 'RSS') , 'application/rss+xml' ); break; } } }