You are here

public function SocialContentSoundCloud::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/soundcloud/social_content_soundcloud.class.inc, line 42
Social Content SoundCloud class.

Class

SocialContentSoundCloud
@file Social Content SoundCloud class.

Code

public function prepareRow($row) {
  $global_settings = $this->settings['global'];
  $settings = $this->settings['instance'];
  $mappings = $this
    ->getFieldMappings();
  $row->created = strtotime($row->created_at);
  $row->caption = $row->title;
  $row->link = $row->permalink_url;

  // Which URL is available for the track.
  if (isset($row->download_url)) {
    $string = $row->download_url;
  }
  elseif (isset($row->stream_url)) {
    $string = $row->stream_url;
  }

  // If there is a track we are getting.
  if (isset($string)) {
    $result = $this
      ->httpRequest(url($string, array(
      'query' => array(
        'client_id' => $global_settings['client_id'],
      ),
      'external' => TRUE,
    )));

    // The field we are mapping into and its settings.
    $field_info = field_info_field($mappings['track']);
    $field_uri_scheme = $field_info['settings']['uri_scheme'];
    $instance_info = field_info_instance('node', $mappings['track'], $settings['content_type']);
    if ($instance_info && !empty($instance_info['settings']) && !empty($instance_info['settings']['file_directory'])) {
      $directory = $instance_info['settings']['file_directory'];
      $dir_path = $field_uri_scheme . '://' . $directory;
      if (file_prepare_directory($dir_path, FILE_CREATE_DIRECTORY)) {
        $filename = $directory . '/' . $row->title . '.' . $row->original_format;
      }
    }
    $row->title = 'SoundCloud : ' . $row->title;
    $file = NULL;
    try {
      $file = file_save_data($result->data, $field_uri_scheme . '://' . $filename, FILE_EXISTS_RENAME);
      $file->status = 1;
      $file->display = 1;
      $row->track = get_object_vars($file);
    } catch (Exception $e) {
      watchdog('social_content', 'Error saving file: %message', array(
        '%message' => $e
          ->getMessage(),
      ));
      return FALSE;
    }
  }
  if (parent::prepareRow($row) === FALSE) {
    return FALSE;
  }
}