-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_tweet.module
executable file
·244 lines (228 loc) · 6.89 KB
/
simple_tweet.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
// $Id$
/**
* Simple Tweet Module
*/
/**
* Constants
*/
define('SIMPLE_TWEET_PAGE', variable_get('simple_tweet_tweets_path', ''));
define('SIMPLE_TWEET_PAGENAME', variable_get('simple_tweet_tweets_page_title', 'Our Tweets From Twitter'));
/**
* Implementation of hook_menu()
*/
function simple_tweet_menu() {
$items = array();
$items['admin/settings/simple-tweet'] = array(
'title' => t('Simple Tweet Settings'),
'access arguments' => array('administer simple tweet'),
'description' => t('Simple Tweet Settings and Configuration Page'),
'page callback' => drupal_get_form,
'page arguments' => array('simple_tweet_admin_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'simple_tweet.admin.inc',
);
$items[SIMPLE_TWEET_PAGE] = array(
'title' => SIMPLE_TWEET_PAGENAME,
'access arguments' => array('see tweets'),
'description' => 'Twitter Tweets Page',
'page callback' => simple_tweet_show_tweets,
);
return $items;
}
/**
* Implementation of hook_perm
*/
function simple_tweet_perm() {
return array('administer simple tweet', 'see tweets');
}
/**
* Implementation of hook_block
* This has 2 different blocks. One for only one tweet
* and one for multiple tweets.
*/
function simple_tweet_block($op = 'list', $delta = 0, $edit = array()) {
$block_tweets = variable_get('simple_tweet_block_number', "1");
switch ($op) {
//list
case 'list':
$blocks[0] = array(
'info' => t('Latest Tweets - Multiple Tweets Block'),
'status' => FALSE,
);
$blocks[1] = array(
'info' => t('Latest Tweet - Single Tweet Block'),
'status' => FALSE,
);
return $blocks;
break;
//configure
case 'configure':
return $form;
break;
//view & default
case 'view': default:
switch ($delta) {
case 0:
$block['subject'] = t('Latest Tweets');
$twitters = _simple_tweet_get_twitters();
$block['content'] = theme('simple_tweet_list', $twitters, $block_tweets);
break;
case 1:
$block['subject'] = t('Latest Tweet');
$twitters = _simple_tweet_get_twitters();
//the variable here is different on the end because there is only one tweet.
$block['content'] = theme('simple_tweet_list', $twitters, 1);
break;
} //end switch
return $block;
}
}
/**
* Callback Functions
*/
function simple_tweet_show_tweets() {
$twitters = _simple_tweet_get_twitters();
$op .= theme('simple_tweet_list', $twitters, NULL);
return $op;
}
/**
* Implementation of hook_theme
*
* @return array of theme functions
*/
function simple_tweet_theme() {
return array(
'simple_tweet_list' => array('arguments' => array('twitters' => NULL, 'number' => NULL)),
'simple_tweet_tweet' => array('arguments' => array('twit' => NULL)),
);
}
/**
* theme_simple_tweet_list - format a list of tweets
*
* @param object $twitters
* @param int $number
* @return string
*/
function theme_simple_tweet_list($twitters, $number = NULL) {
$op = '';
//if number of tweets is not 1
if ($number != '1') {
$op .= '<div class="simple-tweet-timeline">';
}
$count = 0;
//if this is the tweets page
if ($number == NULL) {
$count = count($twitters->channel->item);
for ($i=0; $i<$count; $i++) {
//assign zebra
$zebra = ($i % 2) ? ' odd' : ' even';
$op .= theme('simple_tweet_tweet', $twitters->channel->item[$i], $zebra);
//coder module says this below needs to be like this!!! ??
}
}
else{
//coder module says above has to be this way
//if this is the tweets block
$count = $number;
for ($i=0; $i<$count; $i++) {
//extra code
unset($zebra);
if($i == 0){$zebra = ' first';}
$op .= theme('simple_tweet_tweet', $twitters->channel->item[$i], $zebra);
}
}
//closing div
if ($number != '1') {
$op .= '</div>';
}
//this is to show the link to the users twitter account on twitter.com
if ($number == NULL) {
$op .= '<div class="simple-bottom">';
$op .= '<a id="follow-twitter" href="http://twitter.com/' . variable_get('simple_tweet_username', '') .'">' . variable_get('simple_tweet_page_link', '') . '</a>';
$op .= '</div>';
}
else{
//this is to show a link to the page of tweets
$op .= '<div class="simple-bottom">';
$op .= '<a class="follow-twitter" href="/' . SIMPLE_TWEET_PAGE .'">' . variable_get('simple_tweet_block_link', '') . '</a>';
$op .= '</div>';
}
return $op;
}
/**
* theme_simple_tweet_tweet - format an individual tweet
*
* @param object $twit
* @return string
*/
function theme_simple_tweet_tweet($twit, $zebra) {
$date = _simple_tweet_format_date($twit->pubDate);
$text = _simple_tweet_process_links($twit->description);
//get the twitter username and colon of the tweet
$twitter_username = variable_get('simple_tweet_username', '') . ":";
$text = str_ireplace($twitter_username, "", $text);
$text = trim($text);
//add themeing html
$op = '<div class="simple-tweet' . $zebra . '">';
$op .= '<div class="simple-tweet-msg">'. $text .'</div>';
$op .= '<div class="simple-tweet-created">'. $date .'</div>';
$op .= '</div>';
return $op;
}
/**
* Get Tweets
*/
function _simple_tweet_get_twitters() {
//get rss url
$rss_url = variable_get('simple_tweet_rss_url', '');
if (!$rss_url) {
drupal_set_message(t('Please set your Twitter User RSS Feed.'), 'error');
//get out
return;
}else{
//if the tweets rss feed has an error
if (preg_match('/^http?:\/\/.+/', $rss_url)) {
$twitter_xml = file_get_contents($rss_url);
//parse the data
if(simplexml_load_string($twitter_xml)){
$twitters = simplexml_load_string($twitter_xml);
//return tweets
return $twitters;
}else{
watchdog('simple_tweet', 'There is an issue with the RSS twitter Feed');
}
}else{
//log error
watchdog('simple_tweet', 'There is an issue with the RSS twitter Feed');
//get out
return;
}
}
}
/**
* Process Links in Tweets
*/
function _simple_tweet_process_links($text) {
$text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $text);
$text = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $text);
return $text;
}
/**
* Format Dates in Tweets
*/
function _simple_tweet_format_date($date) {
$date = strtotime($date);
$type = variable_get('simple_tweet_dateformat', 'medium');
$system = array('small', 'medium', 'large');
if (!in_array($type, $system)) {
$format = variable_get('date_format_' . $type, 'medium');
$type = 'custom';
return format_date($date, $type, $format);
}
else {
return format_date($date, $type);
}
}