You are here

class SocialContentPinterest in Social Content 7.2

@file Social Content Pinterest class.

Hierarchy

Expanded class hierarchy of SocialContentPinterest

1 string reference to 'SocialContentPinterest'
social_content_pinterest_social_content_class_info in modules/pinterest/social_content_pinterest.module
Implements hook_social_content_class_info().

File

modules/pinterest/social_content_pinterest.class.inc, line 7
Social Content Pinterest class.

View source
class SocialContentPinterest extends SocialContent {

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

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

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

  /**
   * Fields to save from the row.
   *
   * Get fields to save.
   *
   * @return array
   *   List of fields to save.
   */
  public function fields() {
    return array(
      'id' => 'field_pinterest_guid',
      'created' => 'created',
      'description' => 'body',
      'link' => 'field_pinterest_link',
      'title' => 'title',
      'picture' => 'field_pinterest_picture',
    ) + parent::fields();
  }

  /**
   * The shared global settings form for all Pinterest instances.
   *
   * @return array
   *   Global settings form.
   */
  public function globalSettingsForm() {
    $settings = $this->settings['global'];
    $form = parent::globalSettingsForm();
    $form['api_url'] = array(
      '#type' => 'textfield',
      '#title' => 'API URL',
      '#description' => t('Use @user and @board tokens. Example: !url', array(
        '!url' => 'https://pinterest.com/@user/@board.rss',
      )),
      '#default_value' => isset($settings['api_url']) ? $settings['api_url'] : 'https://pinterest.com/@user/@board.rss',
      '#required' => TRUE,
    );
    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['user'] = array(
      '#type' => 'textfield',
      '#title' => t('Pinterest User'),
      '#description' => t('Enter the Pinterest user name.'),
      '#default_value' => isset($settings['user']) ? $settings['user'] : NULL,
      '#required' => TRUE,
    );
    $form['board'] = array(
      '#type' => 'textfield',
      '#title' => t('User\'s Board'),
      '#description' => t('Enter the specific board of the user.'),
      '#default_value' => isset($settings['board']) ? $settings['board'] : NULL,
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * Get the rows to import.
   *
   * @param mixed $last_id
   *   The id of the last import.
   *
   * @return mixed
   *   The array of the raws, or FALSE on error.
   */
  public function getRows($last_id = NULL) {
    $settings = $this->settings['instance'];
    $global_settings = $this->settings['global'];
    $url = $global_settings['api_url'];
    $url = str_replace('@user', $settings['user'], $url);
    $url = str_replace('@board', $settings['board'], $url);
    $response = drupal_http_request($url);
    $xml = simplexml_load_string($response->data);
    $json = json_encode($xml);
    $array = json_decode($json, TRUE);
    $rows = $array['channel']['item'];
    foreach ($rows as $key => $row) {
      $rows[$key] = (object) $row;
    }
    return $rows;
  }

  /**
   * Do the uploads and attach expected fields to a row about to be imported.
   */
  public function prepareRow($row) {
    $row->created = strtotime($row->pubDate);
    $filter = filter_format_exists('pinterest') ? 'pinterest' : 'filtered_html';
    $row->body = array(
      'value' => $row->description,
      'format' => $filter,
    );
    $id = str_replace('https://www.pinterest.com/pin/', '', $row->guid);
    $id = str_replace('/', '', $id);
    $row->id = $id;
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    $doc = new DOMDocument();
    @$doc
      ->loadHTML($row->description);
    $tags = $doc
      ->getElementsByTagName('img');
    $mappings = $this
      ->getFieldMappings();
    if ($img = $tags
      ->item(0)) {
      $src = $img
        ->getAttribute('src');
      $picture = $this
        ->saveExternalFile($src, $mappings['picture']);
      $row->picture = $picture ? $picture : NULL;
    }
    return TRUE;
  }

}

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.
SocialContentPinterest::fields public function Fields to save from the row. Overrides SocialContent::fields
SocialContentPinterest::getLabel public function The label for this global. Overrides SocialContent::getLabel
SocialContentPinterest::getMachineName public function The machine name for the global. Overrides SocialContent::getMachineName
SocialContentPinterest::getRows public function Get the rows to import. Overrides SocialContent::getRows
SocialContentPinterest::getSource public function Get the source being used to get the rows. Overrides SocialContent::getSource
SocialContentPinterest::globalSettingsForm public function The shared global settings form for all Pinterest instances. Overrides SocialContent::globalSettingsForm
SocialContentPinterest::instanceSettingsForm public function Instance settings form. Overrides SocialContent::instanceSettingsForm
SocialContentPinterest::prepareRow public function Do the uploads and attach expected fields to a row about to be imported. Overrides SocialContent::prepareRow