You are here

class SocialContentFacebook in Social Content 7.2

@file Social Content Facebook class.

Hierarchy

Expanded class hierarchy of SocialContentFacebook

1 string reference to 'SocialContentFacebook'
social_content_facebook_social_content_class_info in modules/facebook/social_content_facebook.module
Implements hook_social_content_class_info().

File

modules/facebook/social_content_facebook.class.inc, line 7
Social Content Facebook class.

View source
class SocialContentFacebook extends SocialContent {

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

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

  /**
   * 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'];
  }

  /**
   * Fields to save from the row.
   *
   * Get fields to save.
   *
   * @return array
   *   List of fields to save.
   */
  public function fields() {
    return array(
      'id' => 'field_facebook_external_id',
      'created' => 'created',
      'account' => '',
      'account_link' => '',
      'message' => 'body',
      'link' => 'field_facebook_link',
      'picture' => 'field_facebook_picture',
    ) + parent::fields();
  }

  /**
   * The shared global settings form for all Instagram 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', array(
        '!link' => l('developers.facebook.com', 'https://developers.facebook.com/apps'),
      )) . '</p>',
    );
    $form['graph_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Graph URL'),
      '#description' => t('Do not include a trailing slash. If not sure, use %url', array(
        '%url' => 'https://graph.facebook.com',
      )),
      '#default_value' => isset($settings['graph_url']) ? $settings['graph_url'] : 'https://graph.facebook.com',
      '#required' => TRUE,
    );
    $form['access_token'] = array(
      '#type' => 'textfield',
      '#title' => t('Access Token'),
      '#description' => t('This is required to interact with Facebook. Use %url to generate one.', array(
        '%url' => 'https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&&grant_type=client_credentials',
      )),
      '#default_value' => isset($settings['access_token']) ? $settings['access_token'] : NULL,
      '#maxlength' => 300,
      '#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['account'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Page User ID'),
      '#description' => t('You can use !link to get this. Or leave empty and it will be generated using the Page Name.', array(
        '!link' => l('findmyfacebookid.com', 'http://findmyfacebookid.com/'),
      )),
      '#default_value' => isset($settings['account']) ? $settings['account'] : NULL,
    );
    $form['page_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Page Name'),
      '#description' => t("E.g. 'cocacola' for http://facebook.com/cocacola page."),
      '#default_value' => isset($settings['page_name']) ? $settings['page_name'] : NULL,
      '#required' => TRUE,
    );
    $form['min_resolution'] = array(
      '#type' => 'textfield',
      '#title' => t('Minimum image resolution'),
      '#description' => t('Only posts that have images that meet the minimum image resolution (in {width}x{height} format) will be imported.'),
      '#default_value' => isset($settings['min_resolution']) ? $settings['min_resolution'] : NULL,
      '#required' => FALSE,
    );
    return $form;
  }

  /**
   * Save instance settings.
   *
   * @param array $settings
   *   The settings to save.
   */
  public function saveInstanceSettings($settings) {
    $global_settings = $this->settings['global'];

    // Look for the Facebook Page id.
    if (!empty($settings['page_name'])) {
      $url = $global_settings['graph_url'] . '/' . $settings['page_name'];
      $result = $this
        ->httpRequest(url($url, array(
        'query' => array(
          'access_token' => $global_settings['access_token'],
        ),
        'external' => TRUE,
      )));
      if ($result->code == 200) {
        $data = json_decode($result->data);
        if (!empty($data->id)) {
          $settings['account'] = $data->id;
        }
      }
    }
    return parent::saveInstanceSettings($settings);
  }

  /**
   * Get the rows to import.
   *
   * @param mixed $last_id
   *   The id of the last import.
   *
   * @return bool|object
   *   The data object, or FALSE on error.
   */
  public function getRows($last_id = NULL) {
    $settings = $this->settings['instance'];
    $global_settings = $this->settings['global'];
    $params = array(
      'access_token' => $global_settings['access_token'],
      'fields' => 'message,id,created_time,from,full_picture',
    );
    if (!empty($settings['limit'])) {
      $params['limit'] = $settings['limit'];
    }
    $url = url($global_settings['graph_url'] . '/' . $settings['account'] . '/posts', array(
      'query' => $params,
      'external' => TRUE,
    ));
    $result = $this
      ->httpRequest($url);
    if ($result->code == 200) {
      $posts = json_decode($result->data);
      return $posts->data;
    }
    else {
      watchdog('social_content_facebook', 'Error fetching feed, data: %data', array(
        '%data' => $result->data,
      ), WATCHDOG_WARNING);
      return FALSE;
    }
  }

  /**
   * Do the uploads and attach expected fields to a row about to be imported.
   */
  public function prepareRow($row) {

    // Only add direct wall posts.
    if (isset($row->to)) {
      return FALSE;
    }

    // Discard updates without content.
    if (!isset($row->message)) {
      return FALSE;
    }
    $global_settings = $this->settings['global'];
    $settings = $this->settings['instance'];
    $id_parts = explode('_', $row->id);
    $row->id = $id_parts[1];
    $row->link = 'http://www.facebook.com/' . $row->from->id . '/posts/' . $row->id;
    $row->created = strtotime($row->created_time);
    $row->account = $row->from->name;
    $row->account_link = 'http://www.facebook.com/' . $row->from->id;
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    if (!empty($row->full_picture)) {
      $validators = array();
      if (!empty($settings['min_resolution'])) {
        $validators = array(
          'file_validate_image_resolution' => array(
            0,
            $settings['min_resolution'],
          ),
        );
      }
      $image_url = $this
        ->getImageUrl($row, $global_settings);
      if ($image_url) {
        $mappings = $this
          ->getFieldMappings();
        $picture = $this
          ->saveExternalFile($image_url, $mappings['picture'], $validators);
      }
    }

    // For legacy reasons we store this in $row->picture.
    // Facebook changed where they stored this field.
    // See https://www.drupal.org/node/2559873
    $row->picture = !empty($picture) ? $picture : NULL;
    return TRUE;
  }

  /**
   * Get the image url from a given row.
   *
   * @param object $row
   *   The url of the image.
   * @param string $settings
   *   The settings for this instance.
   *
   * @return bool|string
   *   The URL, or FALSE on error.
   */
  protected function getImageUrl($row, $settings) {
    if (isset($row->full_picture)) {
      $picture_url = $row->full_picture;
    }
    if (isset($row->object_id)) {
      $picture_url = $settings['graph_url'] . '/' . $row->object_id . '/picture';
    }
    else {
      $picture_url_parts = drupal_parse_url($row->full_picture);
      if (isset($picture_url_parts['query']['url'])) {
        $picture_url = $picture_url_parts['query']['url'];
      }
      if (isset($picture_url_parts['query']['src'])) {
        $picture_url = $picture_url_parts['query']['src'];
      }
    }
    if (!isset($picture_url)) {
      return FALSE;
    }
    return $picture_url;
  }

}

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::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.
SocialContentFacebook::fields public function Fields to save from the row. Overrides SocialContent::fields
SocialContentFacebook::getImageUrl protected function Get the image url from a given row.
SocialContentFacebook::getLabel public function The label for this global. Overrides SocialContent::getLabel
SocialContentFacebook::getMachineName public function The machine name for the global. Overrides SocialContent::getMachineName
SocialContentFacebook::getRows public function Get the rows to import. Overrides SocialContent::getRows
SocialContentFacebook::getSource public function Get the source being used to get the rows i.e. account / hashtag. Overrides SocialContent::getSource
SocialContentFacebook::globalSettingsForm public function The shared global settings form for all Instagram instances. Overrides SocialContent::globalSettingsForm
SocialContentFacebook::instanceSettingsForm public function Instance settings form. Overrides SocialContent::instanceSettingsForm
SocialContentFacebook::prepareRow public function Do the uploads and attach expected fields to a row about to be imported. Overrides SocialContent::prepareRow
SocialContentFacebook::saveInstanceSettings public function Save instance settings. Overrides SocialContent::saveInstanceSettings