Skip to content

Commit

Permalink
Homepage: Enhanced Feed class adding callback arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 27, 2012
1 parent 972e5ec commit f0504dd
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions web/classes/feed.class.php
Expand Up @@ -42,8 +42,9 @@ class Feed implements Visual, Iterator, Countable
private $title = 'Untitled';
private $labelSpanClass = NULL;

// Callback to make when generating HTML for a feed item.
private $func_genElementHTML = NULL;
// Callback to make when generating Html for a feed item.
private $func_genElementHtml = NULL;
private $func_genElementHtmlParams = NULL;

public function __construct($feedURL, $maxItems=5)
{
Expand All @@ -62,12 +63,13 @@ public function setTitle($title, $labelSpanClass=NULL)
$this->_labelSpanClass = "$labelSpanClass";
}

public function setGenerateElementHTMLCallback($funcName)
public function setGenerateElementHtmlCallback($funcName, $params=NULL)
{
$funcName = "$funcName";
if(!is_callable($funcName)) return FALSE;

$this->func_genElementHTML = $funcName;
$this->func_genElementHtml = $funcName;
$this->func_genElementHtmlParams = $params;
return TRUE;
}

Expand All @@ -79,14 +81,14 @@ public function displayOptions()
return $this->_displayOptions;
}

private function generateElementHTML(&$item)
private function generateElementHtml(&$item)
{
if(!is_array($item))
throw new Exception('Received invalid item, array expected.');

if(!is_null($this->func_genElementHTML))
if(!is_null($this->func_genElementHtml))
{
return call_user_func($this->func_genElementHTML, $item);
return call_user_func_array($this->func_genElementHtml, array($item, $this->func_genElementHtmlParams));
}

$html = '<a href="'. preg_replace('/(&)/', '&amp;', $item['link'])
Expand All @@ -95,36 +97,37 @@ private function generateElementHTML(&$item)
return $html;
}

public function generateHTML()
public function generateHtml()
{
if(count($this) > 0)
{
if(count($this) <= 0) return;

$feedTitle = "$this->_title via $this->_feedFormat";

?><a href="<?php echo preg_replace('/(&)/', '&amp;', $this->_feedUri); ?>" class="link-rss" title="<?php echo htmlspecialchars($this->_title); ?>"><span class="hidden"><?php echo htmlspecialchars($this->_feedFormat); ?></span></a><?php
?><a href="<?php echo preg_replace('/(&)/', '&amp;', $this->_feedUri); ?>" class="link-rss" title="<?php echo htmlspecialchars($feedTitle); ?>"><span class="hidden"><?php echo htmlspecialchars($this->_feedFormat); ?></span></a><?php

if(!is_null($this->_labelSpanClass))
{
if(!is_null($this->_labelSpanClass))
{
?>&nbsp;<span class="<?php echo $this->_labelSpanClass; ?>"><?php
}
}

echo htmlspecialchars($this->_title);

if(!is_null($this->_labelSpanClass))
{
echo htmlspecialchars($feedTitle);

if(!is_null($this->_labelSpanClass))
{
?></span><?php
}
}

?><ul><?php

$n = (integer) 0;
foreach($this as $item)
{
$elementHtml = $this->generateElementHTML($item);
$n = (integer) 0;
foreach($this as $item)
{
$elementHtml = $this->generateElementHtml($item);

?><li><?php echo $elementHtml; ?></li><?php

if(++$n >= $this->_maxItems) break;
}
if(++$n >= $this->_maxItems) break;
}

?></ul><?php
Expand Down

0 comments on commit f0504dd

Please sign in to comment.