You are here

class SocialContentSoundCloud in Social Content 7.2

@file Social Content SoundCloud class.

Hierarchy

Expanded class hierarchy of SocialContentSoundCloud

1 string reference to 'SocialContentSoundCloud'
social_content_soundcloud_social_content_class_info in modules/soundcloud/social_content_soundcloud.module
Implements hook_social_content_class_info().

File

modules/soundcloud/social_content_soundcloud.class.inc, line 7
Social Content SoundCloud class.

View source
class SocialContentSoundCloud extends SocialContent {

  /**
   * The label for this global.
   *
   * @return string
   *   The label.
   */
  public function getLabel() {
    return t('SoundCloud');
  }

  /**
   * The machine name for the global.
   *
   * @return string
   *   The machine name.
   */
  public function getMachineName() {
    return 'soundcloud';
  }

  /**
   * Get the source being used to get the rows i.e. account / hashtag.
   *
   * @return string
   *   The account being used to fetch the rows.
   */
  public function getSource() {
    return $this->settings['instance']['account'];
  }

  /**
   * Do the uploads and attach expected fields to a row about to be imported.
   */
  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;
    }
  }

  /**
   * Fields to save from the row.
   *
   * Get fields to save.
   *
   * @return array
   *   List of fields to save.
   */
  public function fields() {
    return array(
      'id' => 'field_soundcloud_external_id',
      'created' => 'created',
      'user_id' => '',
      'user_name' => '',
      'user_link' => '',
      'caption' => 'body',
      'link' => 'field_soundcloud_link',
      'track' => 'field_soundcloud_track',
    ) + parent::fields();
  }

  /**
   * The shared global settings form from all SoundCloud instances.
   *
   * @return array
   *   Global settings form.
   */
  public function globalSettingsForm() {
    $settings = $this->settings['global'];
    $form = parent::globalSettingsForm($settings);
    $form['description'] = array(
      '#markup' => '<p>' . t('See !link for API information.', array(
        '!link' => l('developers.soundcloud.com', 'https://developers.soundcloud.com'),
      )) . '</p>',
    );
    $form['client_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Client ID'),
      '#default_value' => isset($settings['client_id']) ? $settings['client_id'] : NULL,
      '#required' => TRUE,
      '#description' => t('Register an application to get a Client ID.'),
    );
    return $form;
  }

  /**
   * Instance settings form.
   *
   * @return array
   *   Any instance settings that will be included on all
   *    instance forms for the current global.
   */
  public function instanceSettingsForm() {
    $settings = $this->settings['instance'];
    $form = parent::instanceSettingsForm($settings);
    $form['account_id'] = array(
      '#type' => 'textfield',
      '#title' => t('SoundCloud Account ID'),
      '#description' => t('The SoundCloud Account ID to pull the statuses from. Example: 3207'),
      '#suffix' => t("<strong>Note:</strong> To find the correct ID (As it's not the ID that may or may not be your user name), open soundcloud.com, load the /user/xxx landing page, view the page source and search for 'soundcloud://users:'. It is the number located after that string."),
      '#default_value' => isset($settings['account_id']) ? $settings['account_id'] : NULL,
    );
    return $form;
  }

  /**
   * Get the rows to import.
   *
   * @param mixed $last_id
   *   The id of the last import.
   *
   * @return array
   *   Array of rows.
   */
  public function getRows($last_id = NULL) {
    $settings = $this->settings['instance'];
    $global_settings = $this->settings['global'];
    $params = array(
      'client_id' => $global_settings['client_id'],
      'limit' => $settings['limit'],
      'linked_partitioning' => 1,
    );
    $url = 'http://api.soundcloud.com/users/' . $settings['account_id'] . '/tracks';
    $result = $this
      ->httpRequest(url($url, array(
      'query' => $params,
      'external' => TRUE,
    )));
    if ($result->code == 200) {
      $data = json_decode($result->data);
      return $data->collection;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialContent::$currentLanguageKey protected property
SocialContent::$settings protected property
SocialContent::applyGlobalTemplate protected function Apply global template to instance.
SocialContent::attachFields protected function Attach declare fields onto wrapper.
SocialContent::clearGlobals public function Delete global settings.
SocialContent::deleteHistory public static function Utility static function to delete log history of an internal id(nid).
SocialContent::deleteInstance public function Delete the current instance as well as it's history if required.
SocialContent::getAllInstances public static function Utility static function to get all instances.
SocialContent::getDynamicProperty protected function Get the value of a nested property, providing array of property names.
SocialContent::getFieldMappings public function Remote fields map to local fields.
SocialContent::getForm public function Get an internal form of the gievn type.
SocialContent::getFormRootElementKey public static function The root element to use in forms.
SocialContent::getImportCount public static function Utility static function to get a count number of imports for an instance.
SocialContent::getInstances public function Get all instances for the current global.
SocialContent::getInstanceTitle public function The title to use for the current instance.
SocialContent::getLastImportedExternalID public static function Utility static function to get last imported external id.
SocialContent::getObjectFromInstance public static function Utility static function to get a global object from instance id.
SocialContent::httpRequest protected static function Utility method make remote request.
SocialContent::import public function Do the import.
SocialContent::instanceSettingsFields protected function The default field keys which will be on all instance's forms.
SocialContent::isEnabled public function Whether the current instance is enabled for import.
SocialContent::loadGlobalSettings public function Load global settings.
SocialContent::loadInstanceSettings public function Load instance settings for the current global.
SocialContent::logHistory protected function Log import of row to history.
SocialContent::saveExternalFile protected function Utility static function to get and save a remote file.
SocialContent::saveForm public function Save the form values of a form that was request with the getForm method.
SocialContent::saveGlobalSettings public function Save global settings.
SocialContent::saveInstanceSettings public function Save instance settings. 3
SocialContent::setInstanceSettings public function Allow for overridding of intsnace settings.
SocialContent::translate public function Translate a node to a set given languages
SocialContent::validateText protected static function Strip non utf8 characters that can sometimes come through.
SocialContent::__construct public function Class constructor to instantiate a new object.
SocialContentSoundCloud::fields public function Fields to save from the row. Overrides SocialContent::fields
SocialContentSoundCloud::getLabel public function The label for this global. Overrides SocialContent::getLabel
SocialContentSoundCloud::getMachineName public function The machine name for the global. Overrides SocialContent::getMachineName
SocialContentSoundCloud::getRows public function Get the rows to import. Overrides SocialContent::getRows
SocialContentSoundCloud::getSource public function Get the source being used to get the rows i.e. account / hashtag. Overrides SocialContent::getSource
SocialContentSoundCloud::globalSettingsForm public function The shared global settings form from all SoundCloud instances. Overrides SocialContent::globalSettingsForm
SocialContentSoundCloud::instanceSettingsForm public function Instance settings form. Overrides SocialContent::instanceSettingsForm
SocialContentSoundCloud::prepareRow public function Do the uploads and attach expected fields to a row about to be imported. Overrides SocialContent::prepareRow