class SocialContentYoutube in Social Content 7.2
@file Social Content Youtube class.
Hierarchy
- class \SocialContent
- class \SocialContentYoutube
Expanded class hierarchy of SocialContentYoutube
1 string reference to 'SocialContentYoutube'
- social_content_youtube_social_content_class_info in modules/
youtube/ social_content_youtube.module - Implements hook_social_content_class_info().
File
- modules/
youtube/ social_content_youtube.class.inc, line 7 - Social Content Youtube class.
View source
class SocialContentYoutube extends SocialContent {
/**
* The label for this global.
*
* @return string
* The label.
*/
public function getLabel() {
return t('Youtube');
}
/**
* The machine name for the global.
*
* @return string
* The machine name.
*/
public function getMachineName() {
return 'youtube';
}
/**
* Get the source being used to get the rows i.e. account / hashtag.
*
* @return string
* The Youtube Channel ID used to fetch the rows.
*/
public function getSource() {
return $this->settings['instance']['youtube_id'];
}
/**
* Fields to save from the row.
*
* Get fields to save.
*
* @return array
* List of fields to save.
*/
public function fields() {
return array(
'id' => 'field_youtube_external_id',
'created' => 'created',
'account_id' => '',
'account_name' => '',
'account_link' => '',
'caption' => 'body',
'link' => 'field_youtube_link',
'embed_link' => 'field_youtube_embed',
'picture' => 'field_youtube_picture',
) + parent::fields();
}
/**
* The shared global settings form for all Youtube instances.
*
* @return array
* Global settings form.
*/
public function globalSettingsForm() {
$settings = $this->settings['global'];
$form = parent::globalSettingsForm();
$form['description'] = array(
'#markup' => '<p>' . t('See !link', array(
'!link' => l('developers.google.com/youtube', 'https://developers.google.com/youtube/registering_an_application'),
)) . '</p>',
);
$form['api_key'] = array(
'#type' => 'textfield',
'#title' => t('Google API Key'),
'#default_value' => isset($settings['api_key']) ? $settings['api_key'] : NULL,
'#required' => TRUE,
);
$form['api_url'] = array(
'#type' => 'textfield',
'#title' => t('API URL'),
'#description' => t('Do not include a trailing slash. If not sure, use %url', array(
'%url' => 'https://www.googleapis.com/youtube/v3',
)),
'#default_value' => isset($settings['api_url']) ? $settings['api_url'] : 'https://www.googleapis.com/youtube/v3',
'#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['youtube_username'] = array(
'#type' => 'textfield',
'#title' => t('Youtube User Name'),
'#description' => t("E.g. 'NationalGeographic' for http://youtube.com/user/NationalGeographic"),
'#default_value' => isset($settings['youtube_username']) ? $settings['youtube_username'] : NULL,
'#required' => TRUE,
);
$form['youtube_id'] = array(
'#type' => 'textfield',
'#title' => t('Youtube Channel ID'),
'#description' => t('This field is generated automatically if you enter the Youtube User Name.<br>You can also capture it, see <a href="@url" target="_blank">@url</a> to find the channel ID.', array(
'@url' => 'https://stackoverflow.com/a/16326307/872050',
)),
'#default_value' => isset($settings['youtube_id']) ? $settings['youtube_id'] : NULL,
);
return $form;
}
/**
* Save instance settings.
*
* @param array $settings
* The settings to save.
*/
public function saveInstanceSettings($settings) {
$global_settings = $this->settings['global'];
// Look for the Youtube Channel ID.
if (!empty($settings['youtube_username'])) {
$url = $global_settings['api_url'] . '/channels';
$params = array(
'key' => $global_settings['api_key'],
'forUsername' => $settings['youtube_username'],
'part' => 'id',
);
$result = $this
->httpRequest(url($url, array(
'query' => $params,
'external' => TRUE,
)));
if ($result->code == 200) {
$data = json_decode($result->data);
if (isset($data->items) && is_array($data->items)) {
foreach ($data->items as $item) {
$settings['youtube_id'] = $item->id;
}
}
}
else {
watchdog('social_content_youtube', 'Error fetching feed, data: %data', array(
'%data' => $result->data,
), WATCHDOG_WARNING);
return FALSE;
}
}
return parent::saveInstanceSettings($settings);
}
/**
* Get the rows to import.
*
* @param mixed $last_id
* The id of the last import.
*
* @return array
* Array with the rows.
*/
public function getRows($last_id = NULL) {
$settings = $this->settings['instance'];
$global_settings = $this->settings['global'];
if (!empty($settings['youtube_id'])) {
$params = array(
'order' => 'date',
'part' => 'snippet,id',
'key' => $global_settings['api_key'],
'channelId' => $settings['youtube_id'],
'type' => 'video',
);
if (!empty($settings['limit'])) {
$params['maxResults'] = $settings['limit'];
}
$url = $global_settings['api_url'] . '/search';
$result = $this
->httpRequest(url($url, array(
'query' => $params,
'external' => TRUE,
)));
if ($result->code == 200) {
$data = json_decode($result->data);
// $items->id is an object causes conflicts.
foreach ($data->items as &$item) {
$id_number = $item->id->videoId;
unset($item->id);
$item->id = $id_number;
}
return $data->items;
}
else {
watchdog('social_content_youtube', 'Error fetching feed, data: %data', array(
'%data' => $result->data,
), WATCHDOG_WARNING);
return FALSE;
}
}
return array();
}
/**
* Do the uploads and attach expected fields to a row about to be imported.
*/
public function prepareRow($row) {
$settings = $this->settings['instance'];
$mappings = $this
->getFieldMappings();
if (parent::prepareRow($row) === FALSE) {
return FALSE;
}
$row->title = $row->snippet->title;
$row->caption = $row->snippet->description;
$row->account_id = $settings['youtube_id'];
$row->account_name = $settings['youtube_username'];
$row->account_link = 'https://www.youtube.com/user/' . $settings['youtube_username'];
$date = new DateTime($row->snippet->publishedAt);
$row->created = $date
->getTimestamp();
$row->link = 'https://www.youtube.com/watch?v=' . $row->id;
$row->embed_link = 'https://www.youtube.com/embed/' . $row->id;
// @todo: Make sure saving file to disk is optional.
$image_url = 'http://i.ytimg.com/vi/' . $row->id . '/hqdefault.jpg';
$picture = $this
->saveExternalFile($image_url, $mappings['picture']);
$row->picture = $picture ? $picture : NULL;
return TRUE;
}
}
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. | |
SocialContentYoutube:: |
public | function |
Fields to save from the row. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
The label for this global. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
The machine name for the global. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
Get the rows to import. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
Get the source being used to get the rows i.e. account / hashtag. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
The shared global settings form for all Youtube instances. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
Instance settings form. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
Do the uploads and attach expected fields to a row about to be imported. Overrides SocialContent:: |
|
SocialContentYoutube:: |
public | function |
Save instance settings. Overrides SocialContent:: |