1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Jonah_Block_Latest extends Horde_Core_Block
14: {
15: 16:
17: protected $_story = null;
18:
19: 20:
21: public function __construct($app, $params = array())
22: {
23: parent::__construct($app, $params);
24:
25: $this->_name = _("Latest News");
26: }
27:
28: 29:
30: protected function _params()
31: {
32: $params['source'] = array('name' => _("News Source"),
33: 'type' => 'enum',
34: 'values' => array());
35:
36: $channels = $GLOBALS['injector']->getInstance('Jonah_Driver')->getChannels();
37: foreach ($channels as $channel) {
38: $params['source']['values'][$channel['channel_id']] = $channel['channel_name'];
39: }
40: natcasesort($params['source']['values']);
41:
42:
43: $channel = reset($channels);
44: $params['source']['default'] = $channel['channel_id'];
45:
46: $params['countReads'] = array(
47: 'name' => _("Count reads of the latest story when this block is displayed"),
48: 'type' => 'boolean',
49: 'default' => false);
50:
51: return $params;
52: }
53:
54: 55:
56: protected function _title()
57: {
58: if (empty($this->_params['source'])) {
59: return $this->getName();
60: }
61:
62: try {
63: $story = $this->_fetch();
64: } catch (Exception $e) {
65: return htmlspecialchars($e->getMessage());
66: }
67:
68: return '<span class="storyDate">'
69: . htmlspecialchars($story['updated_date'])
70: . '</span> '
71: . htmlspecialchars($story['title']);
72: }
73:
74: 75:
76: protected function _content()
77: {
78: if (empty($this->_params['source'])) {
79: return _("No channel specified.");
80: }
81:
82: try {
83: $story = $this->_fetch();
84: } catch (Exception $e) {
85: return sprintf(_("Error fetching story: %s"), $e->getMessage());
86: }
87:
88: if (empty($story['body_type']) || $story['body_type'] == 'text') {
89: $story['body'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($story['body'], 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
90: }
91:
92: return '<p class="storySubtitle">' . htmlspecialchars($story['description']) . '</p><div class="storyBody">' . $story['body'] . '</div>';
93: }
94:
95: 96: 97:
98: private function _fetch()
99: {
100: if (empty($this->_params['source'])) {
101: return;
102: }
103:
104: if (is_null($this->_story)) {
105: $driver = $GLOBALS['injector']->getInstance('Jonah_Driver');
106: $this->_story = $driver->getStory($this->_params['source'],
107: $driver->getLatestStoryId($this->_params['source']),
108: !empty($this->_params['countReads']));
109: }
110:
111: return $this->_story;
112: }
113:
114: }
115: