Created
June 23, 2011 08:31
-
-
Save rande/1042156 to your computer and use it in GitHub Desktop.
Rss Block Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Sonata project. | |
* | |
* (c) Thomas Rabaix <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Sonata\PageBundle\Block; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Form\Form; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Sonata\PageBundle\Model\BlockInterface; | |
use Sonata\PageBundle\Model\PageInterface; | |
/** | |
* PageExtension | |
* | |
* @author Thomas Rabaix <[email protected]> | |
*/ | |
class RssBlockService extends BaseBlockService | |
{ | |
public function getName() | |
{ | |
return 'Rss Reader'; | |
} | |
function getDefaultSettings() | |
{ | |
return array( | |
'url' => false, | |
'title' => 'Insert the rss title' | |
); | |
} | |
public function buildCreateForm(FormMapper $formMapper, BlockInterface $block) | |
{ | |
$formMapper->addType('settings', 'sonata_type_immutable_array', array( | |
'keys' => array( | |
array('url', 'url', array('required' => false)), | |
array('title', 'text', array('required' => false)), | |
) | |
)); | |
} | |
public function buildEditForm(FormMapper $formMapper, BlockInterface $block) | |
{ | |
return $this->buildCreateForm($formMapper, $block); | |
} | |
public function execute(BlockInterface $block, PageInterface $page, Response $response = null) | |
{ | |
// merge settings | |
$settings = array_merge($this->getDefaultSettings(), $block->getSettings()); | |
$feeds = false; | |
if ($settings['url']) { | |
$options = array( | |
'http' => array( | |
'user_agent' => 'Sonata/RSS Reader', | |
'timeout' => 2, | |
) | |
); | |
// retrieve contents with a specific stream context to avoid php errors | |
$content = @file_get_contents($settings['url'], false, stream_context_create($options)); | |
if ($content) { | |
// generate a simple xml element | |
try { | |
$feeds = new \SimpleXMLElement($content); | |
$feeds = $feeds->channel->item; | |
} catch(\Exception $e) { | |
// silently fail error | |
} | |
} | |
} | |
return $this->renderResponse('SonataPageBundle:Block:block_core_rss.html.twig', array( | |
'feeds' => $feeds, | |
'block' => $block, | |
'settings' => $settings | |
), $response); | |
} | |
} | |
# Template | |
{% extends 'SonataPageBundle:Block:block_base.html.twig' %} | |
{% block block %} | |
<h3 class="sonata-feed-title">{{ settings.title }}</h3> | |
<div class="sonata-feeds-container"> | |
{% for feed in feeds %} | |
<div> | |
<strong><a href="{{ feed.link}}" rel="nofollow" title="{{ feed.title }}">{{ feed.title }}</a></strong> | |
<div>{{ feed.description|raw }}</div> | |
</div> | |
{% else %} | |
No feeds available. | |
{% endfor %} | |
</div> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment