date_format = $format; return TRUE; } /** * Get date format * * @return string */ public function getDateFormat() { return $this->date_format; } /** * Set path for RSS feed * * @param string $url */ public function addPath($path = "") { if (is_string($path)) { $this->path[] = $path; $this->_setFeeds($path); return TRUE; } return FALSE; } /** * Get path for RSS feed * * @return string */ public function getPath() { return $this->path; } /** * Set RSS link * * @param string $url */ public function setRssLink($url = "") { if (is_string($url)) { $this->rss_link = $url; return TRUE; } return FALSE; } /** * Get RSS link * * @param string $html * * @return string */ public function getRssLink() { return $this->rss_link; } /** * Get RSS link * * @param string $html * * @return string */ public function getRssLinkHTML() { $url = $this->getRssLink(); if (empty($url)) { return ""; } return ' Subscribe to our RSS-feed'; } /** * Set Press Release Flag * * @param string $flag */ public function setPressRelease($flag = FALSE) { if (is_bool($flag)) { $this->press_release = $flag; return TRUE; } return FALSE; } /** * Get Press Release Flag * * @return string */ public function getPressRelease() { return $this->press_release; } /** * Set item count * * @param number $count */ public function setCount($count = 4) { if (is_numeric($count)) { $this->count = $count; return TRUE; } return FALSE; } /** * Get item count * * @return number $count */ public function getCount() { return $this->count; } /** * Get page number * * @return string */ public function getPage() { if (!empty($_GET['page']) && is_numeric($_GET['page'])) { return $_GET['page']; } return 1; } /** * Set description limit * * @param number $limit */ public function setLimit($limit = 200) { if (is_numeric($limit)) { $this->limit = $limit; return TRUE; } return FALSE; } /** * Get description limit * * @return number $limit */ public function getLimit() { return $this->limit; } /** * Set view_more link * * @param string $url * @param string $caption * @param string $prefix */ public function setViewMoreLink($url = "", $caption = 'View all', $prefix = '> ') { if (is_string($url) && is_string($caption) && is_string($prefix)) { $this->view_more = array( 'url' => $url, 'caption' => $caption, 'prefix' => $prefix ); return TRUE; } return FALSE; } /** * Set Match Height * * @param bool $match_height */ public function setMatchHeight($match_height) { if (is_bool($match_height)) { $this->match_height = $match_height; } } /** * Get Match Height * * @return bool */ public function getMatchHeight() { return $this->match_height; } /** * Get ViewMore link * * @return string */ public function getViewMoreLink() { $view_more = $this->view_more; if (empty($view_more['url']) || empty($view_more['caption'])) { return array(); } if (!isset($view_more['prefix'])) { $view_more['prefix'] = ""; } return $view_more; } /** * Get view_more link (HTML) * * @return string */ public function getViewMoreLinkHTML() { $view_more = $this->getViewMoreLink(); if (empty($view_more)) { return ""; } return $view_more['prefix'] . '' . $view_more['caption'] . ''; } /** * Html Output * * @return string */ public function output() { if (!$this->_parseFeeds()) { return '

This news feed is currently empty. Please try again later.

'; } $output = ''; if (!empty($this->items)) { $output .= '
'; foreach ($this->items as $item) { $output .= '
'; $output .= '

' . $item['date'] . '

'; $output .= '

' . $item['title'] . '

'; if ($this->getLimit() > 0) { $output .= '

' . $item['description'] . '

'; } $output .= '
'; } $output .= '
'; } return $output; } /** * Get the Next page link * * @return string */ public function getPagination() { $feeds = $this->_getFeeds(); if (empty($feeds)) { return ""; } // Count all the items of all feeds $feed_items = 0; foreach ($feeds as $feed) { $feed_items += count($feed->channel->item); } // If the feed contains less items than the maximum allowed, // we don't need pagination if ($feed_items < $this->getCount()) { return ""; } $current_page = $this->getPage(); if (empty($current_page)) { $current_page = 1; } $url_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . '?'; $url_queries = array( 'page' => 1 ); $links = array(); // Add the previous page link if needed $previous_page = $current_page - 1; if ($previous_page > 1) { $url_queries['page'] = $previous_page; $links[] = '
  • '; } // Get the number of pages $number_of_pages = ceil($feed_items / $this->count); // Put the current page at the center of the pagination items $start_pagination = $current_page - 5; // Or make it the first item if its within the first 5 items if ($start_pagination <= 0) { $start_pagination = 1; } // Add the numerical links for ($i = $start_pagination; $i <= $start_pagination + 9; $i++) { $active = ""; if ($current_page == $i) { $active = ' class="active"'; } $url_queries['page'] = $i; $links[] = '' . $i . ''; if ($i >= $number_of_pages) { break; } } // Add the next page link if needed $next_page = $current_page + 1; if (!empty($feed_items) && $next_page <= $number_of_pages) { $url_queries['page'] = $next_page; $links[] = '
  • '; } return ''; } /** * Parse the Feed * * @return boolean */ private function _parseFeeds() { $feeds = $this->_getFeeds(); if (empty($feeds)) { return FALSE; } $count = 0; foreach ($feeds as $feed) { $start_range = 0; $page = $this->getPage(); if (!empty($page) && $page > 1) { $start_range = ($page - 1) * $this->count; } if (isset($feed) && $feed != FALSE) { foreach ($feed->channel->item as $item) { // Skip the items that are part of the previous page if ($count < $start_range) { $count++; continue; } // Break if we have enough feed items in the array if (count($this->items) >= $this->count) { break; } if ($this->getPressRelease() && $item->pressrelease != 1) { continue; } $date = strtotime((string) $item->pubDate); $date = date($this->getDateFormat(), $date); $description = (string) strip_tags($item->description); if (strlen($description) > $this->getLimit()) { $description = substr($description, 0, $this->limit); $description .= "..."; } $item_array = array( 'title' => (string) $item->title, 'description' => $description, 'link' => (string) $item->link, 'date' => $date ); $this->items[] = $item_array; } } } if (!empty($this->items)) { return TRUE; } return FALSE; } /** * Set the feeds based on the path */ private function _setFeeds($path = "") { if (empty($path)) { return FALSE; } $feed = simplexml_load_file($path); if (empty($feed)) { return FALSE; } $this->feeds[] = $feed; } /** * Get the feeds * * @return array */ private function _getFeeds() { return $this->feeds; } }