Overview

Packages

  • Horde
  • None

Classes

  • Horde_Ajax_Application
  • Horde_Api
  • Horde_Block_Account
  • Horde_Block_Account_Base
  • Horde_Block_Account_Finger
  • Horde_Block_Account_Ldap
  • Horde_Block_Account_Localhost
  • Horde_Block_Cloud
  • Horde_Block_FbStream
  • Horde_Block_Feed
  • Horde_Block_Fortune
  • Horde_Block_Google
  • Horde_Block_Iframe
  • Horde_Block_Metar
  • Horde_Block_Moon
  • Horde_Block_Sunrise
  • Horde_Block_Time
  • Horde_Block_TwitterTimeline
  • Horde_Block_Vatid
  • Horde_Block_Weather
  • Horde_LoginTasks_SystemTask_GarbageCollection
  • Horde_LoginTasks_SystemTask_Upgrade
  • Horde_LoginTasks_Task_AdminCheck
  • Horde_LoginTasks_Task_LastLogin
  • Horde_LoginTasks_Task_TosAgreement
  • Horde_Prefs_Ui
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * A bare-bones twitter client in a Horde block.
  4:  *
  5:  * Still @TODO:
  6:  *  - configure block to show friendTimeline, specific user, public timeline,
  7:  *    'mentions' for current user etc..
  8:  *  - keep track of call limits and either dynamically alter update time or
  9:  *    at least provide feedback to user.
 10:  *
 11:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
 12:  *
 13:  * See the enclosed file COPYING for license information (LGPL). If you
 14:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 15:  *
 16:  * @author  Ben Klang <ben@alkaloid.net>
 17:  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
 18:  * @package Horde
 19:  */
 20: class Horde_Block_TwitterTimeline extends Horde_Core_Block
 21: {
 22:     /**
 23:      * @var Horde_Service_Twitter
 24:      */
 25:     private $_twitter;
 26: 
 27:     /**
 28:      * Twitter profile information returned from verify_credentials
 29:      *
 30:      * @var Object
 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:         /* Get the twitter driver */
 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:         /* Get a unique ID in case we have multiple Twitter blocks. */
 96:         $instance = (string)new Horde_Support_Randomid();
 97: 
 98:         /* Latest status */
 99:         if (empty($this->_profile->status)) {
100:             // status might not be set if only updating the block via ajax
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:         /* Build values to pass to the javascript twitter client */
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:         /* Add the client javascript / initialize it */
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:         /* Get the user's most recent tweet */
147: 
148: 
149:         /* Build the UI */
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 _getTwitterObject()
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: 
API documentation generated by ApiGen