class SocialContentFacebook in Social Content 7.2
@file Social Content Facebook class.
Hierarchy
- class \SocialContent
- class \SocialContentFacebook
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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SocialContent:: |
protected | property | ||
SocialContent:: |
protected | property | ||
SocialContent:: |
protected | function | Apply global template to instance. | |
SocialContent:: |
protected | function | Attach declare fields onto wrapper. | |
SocialContent:: |
public | function | Delete global settings. | |
SocialContent:: |
public static | function | Utility static function to delete log history of an internal id(nid). | |
SocialContent:: |
public | function | Delete the current instance as well as it's history if required. | |
SocialContent:: |
public static | function | Utility static function to get all instances. | |
SocialContent:: |
protected | function | Get the value of a nested property, providing array of property names. | |
SocialContent:: |
public | function | Remote fields map to local fields. | |
SocialContent:: |
public | function | Get an internal form of the gievn type. | |
SocialContent:: |
public static | function | The root element to use in forms. | |
SocialContent:: |
public static | function | Utility static function to get a count number of imports for an instance. | |
SocialContent:: |
public | function | Get all instances for the current global. | |
SocialContent:: |
public | function | The title to use for the current instance. | |
SocialContent:: |
public static | function | Utility static function to get last imported external id. | |
SocialContent:: |
public static | function | Utility static function to get a global object from instance id. | |
SocialContent:: |
protected static | function | Utility method make remote request. | |
SocialContent:: |
public | function | Do the import. | |
SocialContent:: |
protected | function | The default field keys which will be on all instance's forms. | |
SocialContent:: |
public | function | Whether the current instance is enabled for import. | |
SocialContent:: |
public | function | Load global settings. | |
SocialContent:: |
public | function | Load instance settings for the current global. | |
SocialContent:: |
protected | function | Log import of row to history. | |
SocialContent:: |
protected | function | Utility static function to get and save a remote file. | |
SocialContent:: |
public | function | Save the form values of a form that was request with the getForm method. | |
SocialContent:: |
public | function | Save global settings. | |
SocialContent:: |
public | function | Allow for overridding of intsnace settings. | |
SocialContent:: |
public | function | Translate a node to a set given languages | |
SocialContent:: |
protected static | function | Strip non utf8 characters that can sometimes come through. | |
SocialContent:: |
public | function | Class constructor to instantiate a new object. | |
SocialContentFacebook:: |
public | function |
Fields to save from the row. Overrides SocialContent:: |
|
SocialContentFacebook:: |
protected | function | Get the image url from a given row. | |
SocialContentFacebook:: |
public | function |
The label for this global. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
The machine name for the global. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
Get the rows to import. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
Get the source being used to get the rows i.e. account / hashtag. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
The shared global settings form for all Instagram instances. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
Instance settings form. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
Do the uploads and attach expected fields to a row about to be imported. Overrides SocialContent:: |
|
SocialContentFacebook:: |
public | function |
Save instance settings. Overrides SocialContent:: |