function twit_submit_block_submit_callback in Heartbeat 8
1 call to twit_submit_block_submit_callback()
- twit_submit_form_submit in modules/
statusmessage/ includes/ twit.php - Form submit handler
1 string reference to 'twit_submit_block_submit_callback'
- twit_submit_block_form in modules/
statusmessage/ includes/ twit.php - Provide simple form for visitor to submit tweets
File
- modules/
statusmessage/ includes/ twit.php, line 267
Code
function twit_submit_block_submit_callback($form, &$form_state) {
$form_state['rebuild'] = TRUE;
//define Tokens for REST API request
$settings = array(
'oauth_access_token' => "181287687-FNQLOpVXocD3gP46souGOj4FY1kMPlDzNw795MwQ",
'oauth_access_token_secret' => "w9KuI6T44w2HX6P8OCcOUDePHdyT6l0iGRCljKuZF9AE9",
'consumer_key' => "GULWdbfgUBicLUxEAD5a6KeE6",
'consumer_secret' => "6ereWWc9nkTZCS0TtgCDfW4tcTO3E5Wy5LW1kXk8qSrxL2YBAD",
);
//Acquire tweet ID
$tweetSubmit = $form_state['values']['tweet'];
$tweetIdArr = explode('status/', $tweetSubmit);
$tweetId = $tweetIdArr[1];
if ($tweetId == null) {
return null;
}
$url = 'https://api.twitter.com/1.1/statuses/show.json';
$getfield = '?id=' . $tweetId . '&tweet_mode=extended';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter
->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
//Decode request
$tresponse_decoded = json_decode($response);
dpm($tresponse_decoded);
//Create datetime object for title, media file path and content date field
$nowtime = new DateTime();
$unixtime = $nowtime
->getTimestamp();
$today = date("m.d.y");
//Get screen name for title, media file path and content screen name field
$twit_user = $tresponse_decoded->user->screen_name;
//Check access level of user submitting tweet
global $user;
$ip = $user->data['geoip_location']['ip_address'];
//get user's IP
$uid = $user->uid;
//Handler to optionally disable publishing of tweets based on access level
if (in_array('content administrator', $user->roles) || in_array('administrator', $user->roles)) {
$articleStatus = 1;
$articlePromote = 0;
}
else {
$articleStatus = 1;
$articlePromote = 0;
}
//Prepare node object
$node = new stdClass();
$node->type = 'tweet';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
//Define node title
$node->title = $twit_user . '_' . $nowtime
->format('Y.m.d.Hi');
//Set basic node data: language, status, promoted, uid, posted/created date and tweet ID
$node->body[$node->language][0]['value'] = $tresponse_decoded->full_text;
$node->body[$node->language][0]['format'] = 'filtered_html';
$node->status = $articleStatus;
$node->promote = $articlePromote;
$node->workbench_access = array(
'twitcher_tweet' => 'twitcher_tweet',
);
$node->uid = $uid;
$node->date = date('Y-m-d');
//***!This should be changed to previous date variable
$node->created = time();
$node->field_tweet_id[$node->language][0]['value'] = $tresponse_decoded->id;
//Get hashtags, populate taxonomy and set content type to tid
$hashArray = array();
$i = 0;
foreach ($tresponse_decoded->entities->hashtags as $key => $h) {
$hashArray[] = $h->text;
$tid = twit_submit_block_taxonomy_check($h);
$node->field_hashtag[$node->language][$i]['tid'] = $tid;
$i++;
}
$twithashUpdate = twithash_block_update($hashArray, $unixtime, $uid, $ip, $tresponse_decoded->id);
if (!empty($tresponse_decoded->entities->user_mentions)) {
$userArray = array();
foreach ($tresponse_decoded->entities->user_mentions as $userName) {
$userArray[] = $userName->screen_name;
}
$twitUserUpdate = twituser_block_update($userArray, $unixtime, $uid, $ip, $tresponse_decoded->id);
}
//Set content's link field to urls as displayed within the tweet
$i = 0;
if (!empty($tresponse_decoded->entities->urls)) {
foreach ($tresponse_decoded->entities->urls as $url) {
$node->field_tweet_links[$node->language][$i]['value'] = $tresponse_decoded->entities->urls[$i]->display_url;
$i++;
}
}
if (!empty($tresponse_decoded->user->profile_image_url_https)) {
$node->field_profile_pic[$node->language][0]['value'] = $tresponse_decoded->user->profile_image_url_https;
}
//Check for attached media and create a directory for saving
if (isset($tresponse_decoded->extended_entities->media)) {
if (!is_dir(DRUPAL_ROOT . '/sites/default/files/Tweet_Media/' . $today)) {
mkdir(DRUPAL_ROOT . '/sites/default/files/Tweet_Media/' . $today);
}
//Save each media entity with a unique filename within directory
$i = 0;
foreach ($tresponse_decoded->extended_entities->media as $media) {
$ext = substr($media->media_url, -3);
$filename = 'public://Tweet_Media/' . $today . '/' . $twit_user . $unixtime . $i . '.' . $ext;
file_put_contents($filename, file_get_contents($media->media_url));
//Download file and save to Drupal filesystem
$image = file_get_contents($media->media_url);
$file = file_save_data($image, $filename, FILE_EXISTS_REPLACE);
//Associate file with content image field
$node->field_tweet_images[$node->language][$i] = array(
'fid' => $file->fid,
'filename' => $file->filename,
'filemime' => $file->filemime,
'uid' => 1,
'uri' => $file->uri,
'status' => 1,
);
$i++;
}
if (!empty($tresponse_decoded->extended_entities->media[0]->video_info->variants)) {
$z = null;
$vidUrl = null;
$bitrate = new stdClass();
$bitrate->value = null;
$bitrate->index = null;
for ($z = 0; $z < $tresponse_decoded->extended_entities->media[0]->video_info->variants; $z++) {
if (!empty($tresponse_decoded->extended_entities->media[0]->video_info->variants[$z]->bitrate) && $tresponse_decoded->extended_entities->media[0]->video_info->variants[$z]->content_type === 'video/mp4') {
if ($tresponse_decoded->extended_entities->media[0]->video_info->variants[$z]->bitrate > $bitrate->value) {
$bitrate->value = $tresponse_decoded->extended_entities->media[0]->video_info->variants[$z]->bitrate;
$bitrate->index = $z;
}
}
}
if ($bitrate->index !== null) {
$vidUrl = $tresponse_decoded->extended_entities->media[0]->video_info->variants[$bitrate->index]->url;
}
$destination = 'public://Tweet_Media/' . $today;
if ($vFile = system_retrieve_file($vidUrl, $destination, TRUE, FILE_EXISTS_REPLACE)) {
$node->field_tweet_video[$node->language][0]['value'] = $vFile->uri;
}
}
}
//Set content screen name, date and tweet url
$node->field_screen_name[$node->language][0]['value'] = $twit_user;
$node->field_tweet_date[$node->language][0]['value'] = $unixtime;
$node->field_tweet_date[$node->language][0]['timezone'] = 'America/New_York';
$node->field_tweet_date[$node->language][0]['data_type'] = 'datestamp';
$node->field_tweet_url[$node->language][0]['value'] = $tweetSubmit;
//Set the node path and save
$path = 'tweet/' . $tweetId;
$node->path = array(
'alias' => $path,
);
// if(node_save($node)) {
// $commands[] = ajax_command_html('#twit-notification', 'Tweet submitted to Twitcher!');
// }else{
//
// $commands[] = ajax_command_html('#twit-notification', 'Tweet could not be saved.');
// }
try {
node_save($node);
$commands[] = ajax_command_css('#twit-notification', array(
"display" => "block",
));
$commands[] = ajax_command_html('#twit-notification', 'Tweet submitted to Twitcher!');
} catch (Exception $e) {
dpm($e
->getMessage());
$commands[] = ajax_command_css('#twit-notification', array(
"display" => "block",
));
$commands[] = ajax_command_html('#twit-notification', 'Tweet could not be saved.');
}
$commands[] = ajax_command_invoke('#edit-tweet', 'val', array(
'',
));
// $commands[] = ajax_command_invoke('#twit-notification', 'addClass', array('twit-active'));
$commands[] = ajax_command_invoke('#twit-notification', 'delay', array(
3000,
));
$commands[] = ajax_command_invoke('#twit-notification', 'fadeOut', array(
'slow',
));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}