diff --git a/twitter.inc b/twitter.inc index be2993c..c6300a9 100644 --- a/twitter.inc +++ b/twitter.inc @@ -138,8 +138,14 @@ function twitter_load_authenticated_accounts() { * An instance of stdClass object with the Tweet data or FALSE. */ function twitter_status_load($status_id) { - return db_query("SELECT * FROM {twitter} WHERE twitter_id = :status_id", - array(':status_id' => $status_id))->fetchObject(); + $row = db_query("SELECT * FROM {twitter} WHERE twitter_id = :status_id", + array(':status_id' => $status_id))->fetchObject(); + + if ($row && isset($row->entities) && is_string($row->entities)) { + $row->entities = unserialize($row->entities); + } + + return $row; } /** @@ -156,6 +162,7 @@ function twitter_status_save($status) { 'in_reply_to_user_id' => (int) $status->in_reply_to_user_id, 'in_reply_to_screen_name' => $status->in_reply_to_screen_name, 'truncated' => (int) $status->truncated, + 'entities' => isset($status->entities) ? serialize($status->entities) : NULL, ); db_merge('twitter') ->key(array('twitter_id' => $row['twitter_id'])) @@ -240,6 +247,9 @@ function twitter_tweets($screen_name = NULL) { $tweets = array(); foreach ($result as $row) { + if (isset($row->entities) && is_string($row->entities)) { + $row->entities = unserialize($row->entities); + } $tweets[] = $row; } return $tweets; diff --git a/twitter.install b/twitter.install index f80be36..06e5699 100644 --- a/twitter.install +++ b/twitter.install @@ -109,6 +109,13 @@ function twitter_schema() { 'not null' => TRUE, 'default' => 0, ), + 'entities' => array( + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + 'serialize' => TRUE, + 'description' => 'A serialized array of {twitter} post entities.', + ), ), 'indexes' => array('screen_name' => array('screen_name')), 'primary key' => array('twitter_id'), @@ -434,3 +441,18 @@ function twitter_update_7500() { function twitter_update_7501(&$sandbox) { variable_set('twitter_search', 'http://twitter.com'); } + +/** + * Add entities field to the twitter post table. + */ +function twitter_update_7502(&$sandbox) { + if (!db_field_exists('twitter', 'entities')) { + db_add_field('twitter', 'entities', array( + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + 'serialize' => TRUE, + 'description' => 'A serialized array of {twitter} post entities.', + )); + } +} diff --git a/twitter.views.inc b/twitter.views.inc index 6e319f1..7097956 100644 --- a/twitter.views.inc +++ b/twitter.views.inc @@ -157,6 +157,16 @@ function twitter_views_data() { ), ); + // Tweet first image. + $data['twitter']['tweet_first_image'] = array( + 'title' => t('First image'), + 'help' => t('The first image attached to the tweet.'), + 'field' => array( + 'real field' => 'entities', + 'handler' => 'twitter_views_handler_field_first_image', + ), + ); + // Renders a formatted tweet. $data['twitter']['formatted_tweet'] = array( 'title' => t('Formatted tweet'), diff --git a/twitter_views_field_handlers.inc b/twitter_views_field_handlers.inc index 567dd37..af54bde 100644 --- a/twitter_views_field_handlers.inc +++ b/twitter_views_field_handlers.inc @@ -163,3 +163,26 @@ class twitter_views_handler_field_formatted_tweet extends views_handler_field { )); } } + +/** + * Renders the first image attached to a tweet. + */ +class twitter_views_handler_field_first_image extends views_handler_field { + function render($values) { + if (!empty($values->twitter_entities) && is_string($values->twitter_entities)) { + $entities = unserialize($values->twitter_entities); + + if (!empty($entities->media) && is_array($entities->media)) { + foreach ($entities->media as $media) { + if ($media->type == 'photo' && !empty($media->media_url)) { + $image_path = $GLOBALS['is_https'] ? $media->media_url_https : $media->media_url; + return theme('image', array( + 'path' => $image_path, + 'attributes' => array('class' => 'tweet-image'), + )); + } + } + } + } + } +}