diff --git a/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php new file mode 100644 index 0000000..f72d4d9 --- /dev/null +++ b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php @@ -0,0 +1,93 @@ +status = new TwitterStatus($values['status']); + unset($values['status']); + } + parent::__construct($values, 'twitter_account'); + } + + /** + * Returns an array with the authentication tokens. + * + * @return array with the oauth token key and secret. + */ + public function getAuth() { + return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret); + } + + /** + * Sets the authentication tokens to a user. + * + * @param array $values + * Array with 'oauth_token' and 'oauth_token_secret' keys. + */ + public function setAuth($values) { + $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL; + $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL; + } + + /** + * Checks whether the account is authenticated or not. + * + * @return + * boolean TRUE when the account is authenticated. + */ + public function isAuth() { + return !empty($this->oauth_token) && !empty($this->oauth_token_secret); + } +} diff --git a/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php new file mode 100644 index 0000000..323977b --- /dev/null +++ b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php @@ -0,0 +1,77 @@ +user = new TwitterAccount($values['user']); + unset($values['user']); + } + parent::__construct($values, 'twitter_status'); + } +} diff --git a/lib/Drupal/twitter/TwitterAccountStorageController.php b/lib/Drupal/twitter/TwitterAccountStorageController.php new file mode 100644 index 0000000..81d92b6 --- /dev/null +++ b/lib/Drupal/twitter/TwitterAccountStorageController.php @@ -0,0 +1,21 @@ +get_auth(); + $auth = $account->getAuth(); if (isset($auth['oauth_token']) && isset($auth['oauth_token_secret'])) { return new Twitter(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''), $auth['oauth_token'], $auth['oauth_token_secret']); @@ -93,7 +93,7 @@ function twitter_load_authenticated_accounts() { $accounts = twitter_load_accounts(); $auth_accounts = array(); foreach ($accounts as $index => $account) { - if ($account->is_auth()) { + if ($account->isAuth()) { $auth_accounts[] = $account; } } @@ -107,12 +107,12 @@ function twitter_status_save($status) { $twitter_status = new TwitterStatus(); $twitter_status->twitter_id = $status->id; $twitter_status->screen_name = $status->user->screen_name; - $twitter_status->created_time = strtotime($status->created_at); + $twitter_status->created_time = strtotime($status->createdAt); $twitter_status->text = $status->text; $twitter_status->source = $status->source; - $twitter_status->in_reply_to_status_id = ($status->in_reply_to_status_id > 0) ? (string) $status->in_reply_to_status_id : NULL; - $twitter_status->in_reply_to_user_id = (int) $status->in_reply_to_user_id; - $twitter_status->in_reply_to_screen_name = $status->in_reply_to_screen_name; + $twitter_status->inReplyToStatusId = ($status->inReplyToStatusId > 0) ? (string) $status->inReplyToStatusId : NULL; + $twitter_status->inReplyToUserId = (int) $status->inReplyToUserId; + $twitter_status->inReplyToScreenName = $status->inReplyToScreenName; $twitter_status->truncated = (int) $status->truncated; $twitter_status->is_new = entity_load_single('twitter_status', $twitter_status->twitter_id) ? FALSE : TRUE; entity_save('twitter_status', $twitter_status); diff --git a/twitter.module b/twitter.module index c6310cc..aa067a4 100644 --- a/twitter.module +++ b/twitter.module @@ -12,38 +12,6 @@ define ('TWITTER_SEARCH', 'http://search.twitter.com'); define ('TWITTER_TINYURL', 'http://tinyurl.com'); /** - * Implements hook_entity_info(). - */ -function twitter_entity_info() { - return array( - 'twitter_status' => array( - 'label' => t('Twitter Status'), - 'module' => 'twitter', - 'entity class' => 'TwitterStatus', - 'controller class' => 'EntityAPIController', - 'base table' => 'twitter', - 'entity keys' => array( - 'id' => 'twitter_id', - ), - 'label callback' => 'entity_class_label', - 'uri callback' => 'entity_class_uri', - ), - 'twitter_account' => array( - 'label' => t('Twitter Account'), - 'module' => 'twitter', - 'entity class' => 'TwitterAccount', - 'controller class' => 'EntityAPIController', - 'base table' => 'twitter_account', - 'entity keys' => array( - 'id' => 'twitter_uid', - ), - 'label callback' => 'entity_class_label', - 'uri callback' => 'entity_class_uri', - ), - ); -} - -/** * Implements hook_menu(). */ function twitter_menu() { @@ -419,99 +387,3 @@ function _twitter_user_profile($screen_name) { function _twitter_status_url($status) { return TWITTER_HOST . '/' . $status->user->screen_name . '/status/' . $status->id; } - -/** - * Class for containing an individual twitter status. - */ -class TwitterStatus extends Entity { - /** - * @var created_at - */ - public $created_at; - - public $id; - - public $text; - - public $source; - - public $truncated; - - public $favorited; - - public $in_reply_to_status_id; - - public $in_reply_to_user_id; - - public $in_reply_to_screen_name; - - public $user; - - /** - * Constructor for TwitterStatus - */ - public function __construct($values = array()) { - if (isset($values['user'])) { - $this->user = new TwitterAccount($values['user']); - unset($values['user']); - } - parent::__construct($values, 'twitter_status'); - } -} - -/** - * Class to define the structure of a Twitter account. - */ -class TwitterAccount extends Entity { - /** - * Constructor - */ - public function __construct($values = array()) { - // Prepare values to match twitter_account table fields. - if (!empty($values['id'])) { - $values['twitter_uid'] = $values['id']; - unset($values['id']); - } - if (!empty($values['created_at']) && $created_time = strtotime($values['created_at'])) { - $values['created_time'] = $created_time; - } - - $values['utc_offset'] = isset($values['utc_offset']) ? $values['utc_offset'] : 0; - if (isset($values['status'])) { - $this->status = new TwitterStatus($values['status']); - unset($values['status']); - } - parent::__construct($values, 'twitter_account'); - } - - /** - * Returns an array with the authentication tokens. - * - * @return - * array with the oauth token key and secret. - */ - public function get_auth() { - return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret); - } - - /** - * Sets the authentication tokens to a user. - * - * @param array $values - * Array with 'oauth_token' and 'oauth_token_secret' keys. - */ - public function set_auth($values) { - $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL; - $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL; - } - - /** - * Checks whether the account is authenticated or not. - * - * @return - * boolean TRUE when the account is authenticated. - */ - public function is_auth() { - return !empty($this->oauth_token) && !empty($this->oauth_token_secret); - } -} diff --git a/twitter.pages.inc b/twitter.pages.inc index 3840a0c..55e2e56 100644 --- a/twitter.pages.inc +++ b/twitter.pages.inc @@ -455,7 +455,7 @@ function twitter_oauth_callback_form_submit($form, &$form_state) { return; } // Save the new Twitter account and set the user's uid who added it. - $twitter_account->set_auth($response); + $twitter_account->setAuth($response); global $user; $twitter_account->uid = $user->uid; twitter_account_save($twitter_account);