View source
<?php
function tweet_feed_accounts_table() {
$breadcrumbs = array();
$breadcrumbs[] = l('Home', '<front>');
$breadcrumbs[] = l('Administration', 'admin');
$breadcrumbs[] = l('Configuration', 'admin/config');
$breadcrumbs[] = l('Web Services', 'admin/config/services');
$breadcrumbs[] = l('Tweet Feed Accounts', 'admin/config/services/tweet_feed/accounts');
drupal_set_breadcrumb($breadcrumbs);
$rows = array();
$header = array(
'account_name' => array(
'data' => t('Account name'),
'style' => 'text-align: center;',
),
'uses' => array(
'data' => t('# Uses'),
'style' => 'text-align: center;',
),
'consumer_key' => array(
'data' => t('Consumer Key'),
'style' => 'text-align: center;',
),
'oauth_token' => array(
'data' => t('Oauth Token'),
'style' => 'text-align: center;',
),
'edit' => array(
'data' => t('Edit'),
'style' => 'text-align: center;',
),
'delete' => array(
'data' => t('Delete'),
'style' => 'text-align: center;',
),
);
$result = db_select('tweet_accounts', 'a')
->fields('a')
->orderBy('account_name', 'ASC')
->execute();
while ($data = $result
->fetchObject()) {
$cresult = db_select('tweet_feeds', 'f')
->fields('f')
->condition('f.aid', $data->aid)
->execute();
$count = $cresult
->rowCount();
$row = array();
$row[] = $data->account_name;
$row[] = array(
'data' => $count,
'align' => 'center',
);
$row[] = array(
'data' => $data->consumer_key,
'align' => 'center',
);
$row[] = array(
'data' => $data->oauth_token,
'align' => 'center',
);
$row[] = array(
'data' => l(t('Edit'), 'admin/config/services/tweet_feed/accounts/edit/' . $data->aid),
'align' => 'center',
);
$row[] = array(
'data' => l(t('Delete'), 'admin/config/services/tweet_feed/accounts/delete/' . $data->aid),
'align' => 'center',
);
$rows[] = $row;
}
if (count($rows) == 0) {
$rows = array(
array(
'data' => array(
array(
'align' => 'center',
'colspan' => 6,
'data' => t('THERE ARE CURRENTLY NO CONFIGURED TWITTER ACCOUNTS.'),
),
),
),
);
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
));
return $output;
}
function tweet_feed_feeds_table() {
$breadcrumbs = array();
$breadcrumbs[] = l('Home', '<front>');
$breadcrumbs[] = l('Administration', 'admin');
$breadcrumbs[] = l('Configuration', 'admin/config');
$breadcrumbs[] = l('Web Services', 'admin/config/services');
$breadcrumbs[] = l('Tweet Feed Feeds', 'admin/config/services/tweet_feed/feeds');
drupal_set_breadcrumb($breadcrumbs);
$rows = array();
$header = array(
'feed_name' => array(
'data' => t('Feed name'),
'style' => 'text-align: center;',
),
'feed_type' => array(
'data' => t('Type'),
'style' => 'text-align: center;',
),
'feed_criteria' => array(
'data' => t('Feed Criteria'),
'style' => 'text-align: center;',
),
'number_per_pull' => array(
'data' => t('# Per Pull'),
'style' => 'text-align: center;',
),
'new_window' => array(
'data' => t('New Window'),
'style' => 'text-align: center;',
),
'truncate' => array(
'data' => t('Truncate'),
'style' => 'text-align: center;',
),
'edit' => array(
'data' => t('Edit'),
'style' => 'text-align: center;',
),
'delete' => array(
'data' => t('Delete'),
'style' => 'text-align: center;',
),
'import' => array(
'data' => t('Import'),
'style' => 'text-align: center;',
),
);
$result = db_select('tweet_feeds', 'f')
->fields('f')
->orderBy('feed_name', 'ASC')
->execute();
while ($data = $result
->fetchObject()) {
switch ($data->query_type) {
case QUERY_SEARCH:
$query_type = 'Timeline Search';
$feed_criteria = $data->search_term;
break;
case QUERY_TIMELINE:
$query_type = 'User Timeline';
$feed_criteria = $data->timeline_id;
break;
case QUERY_LIST:
$query_type = 'User List';
$feed_criteria = $data->timeline_id . '/' . $data->list_name;
break;
default:
$query_type = t('Unknown');
$feed_criteria = t('Unknown');
}
$row = array();
$row[] = $data->feed_name;
$row[] = array(
'data' => $query_type,
'align' => 'center',
);
$row[] = array(
'data' => $feed_criteria,
'align' => 'center',
);
$row[] = array(
'data' => $data->pull_count,
'align' => 'center',
);
$row[] = array(
'data' => $data->new_window,
'align' => 'center',
);
$row[] = array(
'data' => $data->clear_prior,
'align' => 'center',
);
$row[] = array(
'data' => l(t('Edit'), 'admin/config/services/tweet_feed/feeds/edit/' . $data->fid),
'align' => 'center',
);
$row[] = array(
'data' => l(t('Delete'), 'admin/config/services/tweet_feed/feeds/delete/' . $data->fid),
'align' => 'center',
);
$row[] = array(
'data' => l(t('Import'), 'admin/config/services/tweet_feed/feeds/run/' . $data->fid),
'align' => 'center',
);
$rows[] = $row;
}
if (count($rows) == 0) {
$rows = array(
array(
'data' => array(
array(
'align' => 'center',
'colspan' => 9,
'data' => t('THERE ARE CURRENTLY NO CONFIGURED TWITTER FEEDS.'),
),
),
),
);
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
));
return $output;
}
function tweet_feed_account_form($form, &$form_state, $aid = 0) {
$breadcrumbs = array();
$breadcrumbs[] = l('Home', '<front>');
$breadcrumbs[] = l('Administration', 'admin');
$breadcrumbs[] = l('Configuration', 'admin/config');
$breadcrumbs[] = l('Web Services', 'admin/config/services');
$breadcrumbs[] = l('Tweet Feed Accounts', 'admin/config/services/tweet_feed/accounts');
$breadcrumbs[] = l('Tweet Feed Account Form', substr(request_uri(), 1));
drupal_set_breadcrumb($breadcrumbs);
if (!empty($aid)) {
$result = db_select('tweet_accounts', 'a')
->fields('a')
->condition('a.aid', $aid)
->execute()
->fetchObject();
$account_name = $result->account_name;
$consumer_key = $result->consumer_key;
$consumer_secret = $result->consumer_secret;
$oauth_token = $result->oauth_token;
$oauth_token_secret = $result->oauth_token_secret;
}
else {
$aid = $account_name = $consumer_key = $consumer_secret = NULL;
$oauth_token = $oauth_token_secret = NULL;
}
if (!empty($aid)) {
$form['aid'] = array(
'#type' => 'hidden',
'#value' => $aid,
);
}
$form['tweet_feed_account'] = array(
'#type' => 'fieldset',
'#title' => t('Tweet Feed Account Information Form'),
'#description' => t('Provide information about the Twitter account you wish to add. These can be used to get the feeds for any of our configurable options.'),
);
$form['tweet_feed_account']['account_name'] = array(
'#type' => 'textfield',
'#title' => t('Account Name'),
'#max_length' => 128,
'#required' => TRUE,
'#default_value' => $account_name,
);
$form['tweet_feed_account']['consumer_key'] = array(
'#type' => 'textfield',
'#title' => t('Consumer Key'),
'#max_length' => 255,
'#required' => TRUE,
'#default_value' => $consumer_key,
);
$form['tweet_feed_account']['consumer_secret'] = array(
'#type' => 'textfield',
'#title' => t('Consumer Secret'),
'#max_length' => 255,
'#required' => TRUE,
'#default_value' => $consumer_secret,
);
$form['tweet_feed_account']['oauth_token'] = array(
'#type' => 'textfield',
'#title' => t('Oauth Token'),
'#max_length' => 255,
'#required' => TRUE,
'#default_value' => $oauth_token,
);
$form['tweet_feed_account']['oauth_token_secret'] = array(
'#type' => 'textfield',
'#title' => t('Oauth Token Secret'),
'#max_length' => 255,
'#required' => TRUE,
'#default_value' => $oauth_token_secret,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Form'),
);
return $form;
}
function tweet_feed_account_form_submit($form, &$form_state) {
$values = $form_state['values'];
$data = array(
'account_name' => $values['account_name'],
'consumer_key' => $values['consumer_key'],
'consumer_secret' => $values['consumer_secret'],
'oauth_token' => $values['oauth_token'],
'oauth_token_secret' => $values['oauth_token_secret'],
);
if (!empty($values['aid'])) {
$data['aid'] = $values['aid'];
$status = drupal_write_record('tweet_accounts', $data, array(
'aid',
));
}
else {
$status = drupal_write_record('tweet_accounts', $data);
}
$form_state['redirect'] = 'admin/config/services/tweet_feed/accounts';
switch ($status) {
case SAVED_NEW:
drupal_set_message('New Twitter account has been successfully added.', 'status');
break;
case SAVED_UPDATED:
drupal_set_message('Twitter account has been successfully updated.', 'status');
break;
case FALSE:
default:
drupal_set_message('The Twitter details provided could not be properly saved to the database.', 'error');
break;
}
}
function tweet_feed_feeds_form($form, &$form_state, $fid = 0) {
$breadcrumbs = array();
$breadcrumbs[] = l('Home', '<front>');
$breadcrumbs[] = l('Administration', 'admin');
$breadcrumbs[] = l('Configuration', 'admin/config');
$breadcrumbs[] = l('Web Services', 'admin/config/services');
$breadcrumbs[] = l('Tweet Feed Feeds', 'admin/config/services/tweet_feed/feeds');
$breadcrumbs[] = l('Tweet Feed Feeds Form', substr(request_uri(), 1));
drupal_set_breadcrumb($breadcrumbs);
if (!empty($fid)) {
$result = db_select('tweet_feeds', 'f')
->fields('f')
->condition('f.fid', $fid)
->execute()
->fetchObject();
$fid = $result->fid;
$aid = $result->aid;
$feed_name = $result->feed_name;
$query_type = $result->query_type;
$timeline_id = $result->timeline_id;
$search_term = $result->search_term;
$list_name = $result->list_name;
$pull_count = $result->pull_count;
$clear_prior = $result->clear_prior;
$new_window = $result->new_window;
}
else {
$fid = $aid = $query_type = $search_term = $list_name = $feed_name = NULL;
$twitter_user_id = $pull_count = $new_window = $clear_prior = $timeline_id = NULL;
}
if (!empty($fid)) {
$form['fid'] = array(
'#type' => 'hidden',
'#value' => $fid,
);
}
$form['tweet_feed_query_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Twitter Query Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 2,
);
$form['tweet_feed_query_settings']['feed_name'] = array(
'#type' => 'textfield',
'#title' => t('Feed Name'),
'#description' => t('The name of the feed as it will appear on administrative forms'),
'#default_value' => $feed_name,
'#required' => TRUE,
);
$accounts = array();
$result = db_select('tweet_accounts', 'a')
->fields('a', array(
'aid',
'account_name',
))
->orderBy('account_name', 'ASC')
->execute();
while ($adata = $result
->fetchObject()) {
$accounts[$adata->aid] = $adata->account_name;
}
if (count($accounts) < 1) {
drupal_set_message('You cannot create a feed until you have added an account. Please add an account here before proceeding to add a feed.', 'error');
drupal_goto('admin/config/services/tweet_feed/accounts');
}
$form['tweet_feed_query_settings']['aid'] = array(
'#type' => 'select',
'#title' => t('API Account To Use For Pulling This Feed'),
'#options' => $accounts,
'#default_value' => $aid,
'#required' => TRUE,
);
$form['tweet_feed_query_settings']['query_type'] = array(
'#type' => 'radios',
'#title' => t('Type of Twitter Query'),
'#options' => array(
QUERY_SEARCH => t('Twitter Search'),
QUERY_TIMELINE => t('User Timeline Display'),
QUERY_LIST => t('User List Display'),
),
'#required' => TRUE,
'#default_value' => $query_type,
);
$form['tweet_feed_query_settings']['search_term'] = array(
'#type' => 'textfield',
'#title' => t('Twitter Search Term'),
'#max_length' => 64,
'#default_value' => $search_term,
'#states' => array(
'visible' => array(
':input[name="query_type"]' => array(
'value' => QUERY_SEARCH,
),
),
),
);
$form['tweet_feed_query_settings']['timeline_id'] = array(
'#type' => 'textfield',
'#title' => t('Exact Twitter User ID For Timline Query'),
'#description' => t('You can get this by going to mytwitterid.com'),
'#max_length' => 64,
'#default_value' => $timeline_id,
'#states' => array(
'visible' => array(
':input[name="query_type"]' => array(
array(
'value' => QUERY_TIMELINE,
),
array(
'value' => QUERY_LIST,
),
),
),
),
);
$form['tweet_feed_query_settings']['list_name'] = array(
'#type' => 'textfield',
'#title' => t('List name'),
'#description' => t('Enter the list name exactly as it appears on twitter.com'),
'#max_length' => 64,
'#default_value' => $list_name,
'#states' => array(
'visible' => array(
':input[name="query_type"]' => array(
'value' => QUERY_LIST,
),
),
),
);
$form['tweet_feed_query_settings']['pull_count'] = array(
'#type' => 'select',
'#title' => t('Number of Items to Pull'),
'#max_length' => 2,
'#options' => array(
'100' => '100',
'200' => '200',
'300' => '300',
'400' => '400',
'500' => '500',
'600' => '600',
'700' => '700',
'800' => '800',
'900' => '900',
'1000' => '1000',
),
'#description' => t('Twitter limits tweet pulling to 1500 every 15 minutes. Keep this in mind when setting the pull count in conjunction with the frequency of cron/drush runs.'),
'#required' => TRUE,
'#default_value' => $pull_count,
);
$form['tweet_feed_query_settings']['new_window'] = array(
'#type' => 'checkbox',
'#title' => t('Open tweeted links, hashtags and mentions in a new window.'),
'#default_value' => $new_window,
);
$form['tweet_feed_query_settings']['clear_prior'] = array(
'#type' => 'checkbox',
'#title' => t('Remove all tweets in this feed prior to import.'),
'#default_value' => $clear_prior,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Settings Form'),
'#weight' => 3,
);
return $form;
}
function tweet_feed_feeds_form_submit($form, &$form_state) {
$values = $form_state['values'];
$data = array(
'aid' => $values['aid'],
'feed_name' => $values['feed_name'],
'query_type' => $values['query_type'],
'timeline_id' => $values['timeline_id'],
'search_term' => $values['search_term'],
'list_name' => $values['list_name'],
'pull_count' => $values['pull_count'],
'clear_prior' => $values['clear_prior'],
'new_window' => $values['new_window'],
);
if (!empty($values['fid'])) {
$data['fid'] = $values['fid'];
$status = drupal_write_record('tweet_feeds', $data, array(
'fid',
));
}
else {
$status = drupal_write_record('tweet_feeds', $data);
}
$form_state['redirect'] = 'admin/config/services/tweet_feed/feeds';
switch ($status) {
case SAVED_NEW:
drupal_set_message('New Twitter feed has been successfully added.', 'status');
break;
case SAVED_UPDATED:
drupal_set_message('Twitter feed has been successfully updated.', 'status');
break;
case FALSE:
default:
drupal_set_message('The Twitter feed details provided could not be properly saved to the database.', 'error');
break;
}
}
function tweet_feed_settings_form($form, &$form_state) {
$form['tweet_feed_get_tweeter_profiles'] = array(
'#type' => 'checkbox',
'#title' => t('Save user profile information for each unique tweeter in feeds.'),
'#description' => t('This will create a node record for every person who tweets in your feed.'),
'#default_value' => variable_get('tweet_feed_get_tweeter_profiles', 0),
);
return system_settings_form($form);
}
function tweet_feed_delete_feed_form($form, &$form_state, $fid) {
$form['fid'] = array(
'#type' => 'hidden',
'#value' => $fid,
);
return confirm_form($form, t('Are you sure that you wish to delete this feed?'), 'admin/config/services/tweet_feed/feeds', t('There is no way to undo this action once it has been taken. Consider that carefully before proceeding.'), t('Delete Feed'), t('Cancel'));
}
function tweet_feed_delete_feed_form_submit($form, &$form_state) {
$fid = $form_state['values']['fid'];
if (!empty($fid)) {
$number_deleted = db_delete('tweet_feeds')
->condition('fid', $fid)
->execute();
if ($number_deleted > 0) {
drupal_set_message('You have successfully deleted the selected feed.', 'status');
$form_state['redirect'] = 'admin/config/services/tweet_feed/feeds';
}
else {
drupal_set_message('The selected feed could not be deleted.', 'error');
$form_state['redirect'] = 'admin/config/services/tweet_feed/feeds';
}
}
else {
drupal_set_message('There was an internal error and the request could not be processed.', 'error');
$form_state['redirect'] = 'admin/config/services/tweet_feed/feeds';
}
}
function tweet_feed_delete_account_form($form, &$form_state, $aid) {
$form['aid'] = array(
'#type' => 'hidden',
'#value' => $aid,
);
return confirm_form($form, t('Are you sure that you wish to delete this account?'), 'admin/config/services/tweet_feed/accounts', t('There is no way to undo this action once it has been taken. Consider that carefully before proceeding.'), t('Delete Account'), t('Cancel'));
}
function tweet_feed_delete_account_form_submit($form, &$form_state) {
$aid = $form_state['values']['aid'];
if (!empty($aid)) {
$number_deleted = db_delete('tweet_accounts')
->condition('aid', $aid)
->execute();
if ($number_deleted > 0) {
drupal_set_message('You have successfully deleted the selected account.', 'status');
$form_state['redirect'] = 'admin/config/services/tweet_feed/accounts';
}
else {
drupal_set_message('The selected account could not be deleted.', 'error');
$form_state['redirect'] = 'admin/config/services/tweet_feed/accounts';
}
}
else {
drupal_set_message('There was an internal error and the request could not be processed.', 'error');
$form_state['redirect'] = 'admin/config/services/tweet_feed/accounts';
}
}
function tweet_feed_run_import($fid) {
$tweets = tweet_feed_pull_data_from_feed($fid, TRUE);
$feed = tweet_feed_get_feed_object($fid);
switch ($feed->query_type) {
case QUERY_SEARCH:
$query_type = 'Timeline Search';
$feed_criteria = $feed->search_term;
break;
case QUERY_TIMELINE:
$query_type = 'User Timeline';
$feed_criteria = $feed->timeline_id;
break;
case QUERY_LIST:
$query_type = 'User List';
$feed_criteria = $feed->timeline_id . '/' . $feed->list_name;
break;
default:
$query_type = t('Unknown');
$feed_criteria = t('Unknown');
}
$batch = array(
'title' => t('Tweet Feed Import Batch: Feed: ' . $query_type . ': ' . $feed_criteria),
'init_message' => t('Loading tweets...'),
'operations' => array(),
'finished' => 'tweet_feed_run_import_finish',
);
foreach ($tweets as $key => $tweet) {
$update_node_id = 0;
$hash = NULL;
$result = db_select('tweet_hashes', 't')
->fields('t', array(
'nid',
'tid',
'hash',
))
->condition('t.tid', $tweet->id)
->execute();
if ($result
->rowCount() > 0) {
$tdata = $result
->fetchObject();
$hash = md5(serialize($tweet));
if ($hash == $tdata->hash) {
continue;
}
else {
$update_node_id = $tdata->nid;
}
}
$batch['operations'][] = array(
'tweet_feed_save_tweet',
array(
$tweet,
$feed,
$update_node_id,
$hash,
),
);
}
batch_set($batch);
batch_process('admin/config/services/tweet_feed/feeds');
}