1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
<?php
/**
* A bare-bones twitter client in a Horde block.
*
* Copyright 2009-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL-2). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl.
*
* @author Ben Klang <ben@alkaloid.net>
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package Horde
*/
class Horde_Block_TwitterTimeline extends Horde_Core_Block
{
/**
* @var Horde_Service_Twitter
*/
protected $_twitter;
/**
* Twitter profile information returned from verify_credentials
*
* @var Object
*/
protected $_profile;
/**
*/
public function __construct($app, $params = array())
{
parent::__construct($app, $params);
$this->enabled = !empty($GLOBALS['conf']['twitter']['enabled']);
$this->_name = _("Twitter Timeline");
}
/**
*/
protected function _title()
{
try {
$twitter = $this->_getTwitterObject();
} catch (Horde_Exception $e) {
return $this->getName();
}
try {
$this->_profile = Horde_Serialize::unserialize($twitter->account->verifyCredentials(), Horde_Serialize::JSON);
if (!empty($this->_profile)) {
$username = $this->_profile->screen_name;
return sprintf(_("Twitter Timeline for %s"), $username);
}
} catch (Horde_Service_Twitter_Exception $e) {}
return $this->getName();
}
/**
*/
protected function _params()
{
return array(
'height' => array(
'name' => _("Height of stream content (width automatically adjusts to block)"),
'type' => 'int',
'default' => 350
),
'refresh_rate' => array(
'name' => _("Number of seconds to wait to refresh"),
'type' => 'int',
'default' => 300
)
);
}
/**
*/
protected function _content()
{
global $page_output;
/* Get the twitter driver */
try {
$twitter = $this->_getTwitterObject();
} catch (Horde_Exception $e) {
throw new Horde_Exception(sprintf(_("There was an error contacting Twitter: %s"), $e->getMessage()));
}
/* Get a unique ID in case we have multiple Twitter blocks. */
$instance = (string)new Horde_Support_Randomid();
/* Latest status */
if (empty($this->_profile->status)) {
// status might not be set if only updating the block via ajax
try {
$this->_profile = Horde_Serialize::unserialize($twitter->account->verifyCredentials(), Horde_Serialize::JSON);
if (empty($this->_profile)) {
return _("Temporarily unable to contact Twitter. Please try again later.");
}
} catch (Horde_Service_Twitter_Exception $e) {
$msg = Horde_Serialize::unserialize($e->getMessage(), Horde_Serialize::JSON);
if (is_object($msg)) {
$msg = $msg->errors[0]->message;
}
return sprintf(_("There was an error contacting Twitter: %s"), $msg);
}
}
/* Build values to pass to the javascript twitter client */
$defaultText = addslashes(_("What are you working on now?"));
$endpoint = Horde::url('services/twitter/', true);
$inReplyToNode = $instance . '_inReplyTo';
$inReplyToText = addslashes(_("In reply to:"));
$justNowText = addslashes(_("Just now..."));
$refresh = empty($this->_params['refresh_rate']) ? 300 : $this->_params['refresh_rate'];
/* Add the client javascript / initialize it */
$page_output->addScriptFile('twitterclient.js', 'horde');
$page_output->addScriptFile('scriptaculous/effects.js', 'horde');
$favorite = _("Favorite");
$unfavorite = _("Unfavorite");
$script = <<<EOT
Horde = window.Horde = window.Horde || {};
Horde['twitter{$instance}'] = new Horde_Twitter({
instanceid: '{$instance}',
getmore: '{$instance}_getmore',
input: '{$instance}_newStatus',
spinner: '{$instance}_loading',
content: '{$instance}_stream',
contenttab: '{$instance}_contenttab',
mentiontab: '{$instance}_mentiontab',
mentions: '{$instance}_mentions',
endpoint: '{$endpoint}',
inreplyto: '{$inReplyToNode}',
refreshrate: {$refresh},
counter: '{$instance}_counter',
strings: { inreplyto: '{$inReplyToText}', defaultText: '{$defaultText}', justnow: '{$justNowText}', favorite: '{$favorite}', unfavorite: '{$unfavorite}' }
});
EOT;
$page_output->addInlineScript($script, true);
/* Build the UI */
$view = new Horde_View(array('templatePath' => HORDE_TEMPLATES . '/block'));
$view->addHelper('Tag');
$view->instance = $instance;
$view->defaultText = $defaultText;
$view->loadingImg = Horde_Themes_Image::tag('loading.gif', array(
'attr' => array(
'id' => $instance . '_loading',
'style' => 'display:none;'
)
));
$view->latestStatus = !empty($this->_profile->status) ? htmlspecialchars($this->_profile->status->text) : '';
$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")) : '';
$view->bodyHeight = empty($this->_params['height']) ? 350 : $this->_params['height'];
return $view->render('twitter-layout');
}
/**
*/
protected function _getTwitterObject()
{
$token = unserialize($GLOBALS['prefs']->getValue('twitter'));
if (empty($token['key']) && empty($token['secret'])) {
$pref_link = $GLOBALS['registry']->getServiceLink('prefs', 'horde')->add('group', 'twitter')->link();
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>'));
}
$this->_twitter = $GLOBALS['injector']->getInstance('Horde_Service_Twitter');
$auth_token = new Horde_Oauth_Token($token['key'], $token['secret']);
$this->_twitter->auth->setToken($auth_token);
return $this->_twitter;
}
}
|