1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class extends Horde_Core_Block
21: {
22: 23: 24:
25: private ;
26:
27: 28: 29: 30: 31:
32: private $_profile;
33:
34: 35:
36: public function __construct($app, $params = array())
37: {
38: parent::__construct($app, $params);
39:
40: $this->enabled = !empty($GLOBALS['conf']['twitter']['enabled']);
41: $this->_name = _("Twitter Timeline");
42: }
43:
44: 45:
46: protected function _title()
47: {
48: try {
49: $twitter = $this->_getTwitterObject();
50: } catch (Horde_Exception $e) {
51: return $this->getName();
52: }
53: try {
54: $this->_profile = Horde_Serialize::unserialize($twitter->account->verifyCredentials(), Horde_Serialize::JSON);
55: if (!empty($this->_profile)) {
56: $username = $this->_profile->screen_name;
57: return sprintf(_("Twitter Timeline for %s"), $username);
58: }
59: } catch (Horde_Service_Twitter_Exception $e) {}
60:
61: return $this->getName();
62: }
63:
64: 65:
66: protected function _params()
67: {
68: return array(
69: 'height' => array(
70: 'name' => _("Height of stream content (width automatically adjusts to block)"),
71: 'type' => 'int',
72: 'default' => 350
73: ),
74: 'refresh_rate' => array(
75: 'name' => _("Number of seconds to wait to refresh"),
76: 'type' => 'int',
77: 'default' => 300
78: )
79: );
80: }
81:
82: 83:
84: protected function _content()
85: {
86: global $conf;
87:
88:
89: try {
90: $twitter = $this->_getTwitterObject();
91: } catch (Horde_Exception $e) {
92: throw new Horde_Exception(sprintf(_("There was an error contacting Twitter: %s"), $e->getMessage()));
93: }
94:
95:
96: $instance = (string)new Horde_Support_Randomid();
97:
98:
99: if (empty($this->_profile->status)) {
100:
101: try {
102: $this->_profile = Horde_Serialize::unserialize($twitter->account->verifyCredentials(), Horde_Serialize::JSON);
103: if (empty($this->_profile)) {
104: return _("Temporarily unable to contact Twitter. Please try again later.");
105: }
106: } catch (Horde_Service_Twitter_Exception $e) {
107: $msg = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON);
108: return sprintf(_("There was an error contacting Twitter: %s"), $msg);
109: }
110: }
111:
112:
113: $defaultText = addslashes(_("What are you working on now?"));
114: $endpoint = Horde::url('services/twitter/', true);
115: $spinner = $instance . '_loading';
116: $inputNode = $instance . '_newStatus';
117: $inReplyToNode = $instance . '_inReplyTo';
118: $inReplyToText = addslashes(_("In reply to:"));
119: $contentNode = 'twitter_body' . $instance;
120: $justNowText = addslashes(_("Just now..."));
121: $refresh = empty($this->_params['refresh_rate']) ? 300 : $this->_params['refresh_rate'];
122:
123:
124: Horde::addScriptFile('twitterclient.js');
125: Horde::addScriptFile('effects.js');
126: $script = <<<EOT
127: var Horde = window.Horde || {};
128: Horde['twitter{$instance}'] = new Horde_Twitter({
129: instanceid: '{$instance}',
130: getmore: '{$instance}_getmore',
131: input: '{$instance}_newStatus',
132: spinner: '{$instance}_loading',
133: content: '{$instance}_stream',
134: contenttab: '{$instance}_contenttab',
135: mentiontab: '{$instance}_mentiontab',
136: mentions: '{$instance}_mentions',
137: endpoint: '{$endpoint}',
138: inreplyto: '{$inReplyToNode}',
139: refreshrate: {$refresh},
140: counter: '{$instance}_counter',
141: strings: { inreplyto: '{$inReplyToText}', defaultText: '{$defaultText}', justnow: '{$justNowText}' }
142: });
143: EOT;
144: Horde::addInlineScript($script, 'dom');
145:
146:
147:
148:
149:
150: $view = new Horde_View(array('templatePath' => HORDE_TEMPLATES . '/block'));
151: $view->addHelper('Tag');
152: $view->instance = $instance;
153: $view->defaultText = $defaultText;
154: $view->loadingImg = Horde::img('loading.gif', '', array('id' => $instance . '_loading', 'style' => 'display:none;'));
155: $view->latestStatus = !empty($this->_profile->status) ? htmlspecialchars($this->_profile->status->text) : '';
156: $view->latestDate = !empty($this->_profile->status) ? Horde_Date_Utils::relativeDateTime(strtotime($this->_profile->status->created_at), $GLOBALS['prefs']->getValue('date_format'), ($GLOBALS['prefs']->getValue('twentyFour') ? "%H:%M" : "%I:%M %P")) : '';
157: $view->bodyHeight = empty($this->_params['height']) ? 350 : $this->_params['height'];
158:
159: return $view->render('twitter-layout');
160: }
161:
162: 163:
164: private function ()
165: {
166: $token = unserialize($GLOBALS['prefs']->getValue('twitter'));
167: if (empty($token['key']) && empty($token['secret'])) {
168: $pref_link = Horde::getServiceLink('prefs', 'horde')->add('group', 'twitter')->link();
169: throw new Horde_Exception(sprintf(_("You have not properly connected your Twitter account with Horde. You should check your Twitter settings in your %s."), $pref_link . _("preferences") . '</a>'));
170: }
171:
172: $this->_twitter = $GLOBALS['injector']->getInstance('Horde_Service_Twitter');
173: $auth_token = new Horde_Oauth_Token($token['key'], $token['secret']);
174: $this->_twitter->auth->setToken($auth_token);
175:
176: return $this->_twitter;
177: }
178:
179: }
180: