You are here

public function SocialContentTwitter::prepareRow in Social Content 7.2

Do the uploads and attach expected fields to a row about to be imported.

Overrides SocialContent::prepareRow

File

modules/twitter/social_content_twitter.class.inc, line 50
Social Content Twitter class.

Class

SocialContentTwitter
@file Social Content Twitter class.

Code

public function prepareRow($row) {
  $settings = $this->settings['instance'];

  // Check if we are dealing with a retweet and whether we should import it.
  if (isset($row->retweeted_status) && !empty($settings['exclude_retweets'])) {
    return FALSE;
  }
  elseif (isset($row->retweeted_status)) {

    // If we do need import retweets then get the full tweet.
    $start = strpos($row->text, ':');
    if ($start) {
      $row->text = substr($row->text, 0, $start + 1) . ' ' . $row->retweeted_status->text;
    }
  }
  $mappings = $this
    ->getFieldMappings();
  $row->id = $row->id_str;
  $row->created = strtotime($row->created_at);
  $row->account = $row->user->screen_name;
  $row->name = $row->user->name;
  $row->link = 'http://twitter.com/' . $row->user->screen_name . '/status/' . $row->id;
  $row->account_link = 'http://twitter.com/' . $row->user->screen_name;
  $filter = filter_format_exists('tweet') ? 'tweet' : 'filtered_html';
  $row->text = array(
    'value' => $row->text,
    'format' => $filter,
  );
  if (isset($row->full_text)) {

    // Theoretically should respect 'display_text_range', which indicates [start, end]
    // of full_text to display. But that will throw off the mappings in 'entities',
    // since they seem to refer to 'full_text' as a whole (as of 2017-04-14).
    $row->text = $row->full_text;
  }
  if (isset($row->retweeted_status->full_text)) {
    $row->retweeted_status->text = $row->retweeted_status->full_text;
  }
  if (parent::prepareRow($row) === FALSE) {
    return FALSE;
  }
  if (isset($mappings['user_picture'])) {
    $picture = $this
      ->saveExternalFile($row->user->profile_image_url, $mappings['user_picture']);
    $row->user_picture = !empty($picture) ? $picture : NULL;
  }
  if (isset($mappings['media'])) {
    if (isset($row->entities->media)) {
      foreach ($row->entities->media as $item) {
        if (isset($item->media_url)) {
          $picture = $this
            ->saveExternalFile($item->media_url, $mappings['media']);
          $row->media = !empty($picture) ? $picture : NULL;
          if ($picture) {
            break;
          }
        }
      }
    }
  }

  // Get media from retweet:
  // Duplicate code in parent (social_content_twitter.class.inc) that gets first media
  // item from $row->entities, but apply it to $row->retweeted_status->entities.
  if (isset($mappings['media']) && isset($row->retweeted_status) && !isset($row->media)) {
    if (isset($row->retweeted_status->entities->media)) {
      foreach ($row->retweeted_status->entities->media as $item) {
        if (isset($item->media_url)) {
          $picture = $this
            ->saveExternalFile($item->media_url, $mappings['media']);
          $row->media = !empty($picture) ? $picture : NULL;
          if ($picture) {
            break;
          }
        }
      }
    }
  }
}