| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Display help and module information
|
| 6 |
* @param section which section of the site we're displaying help
|
| 7 |
* @return help text for section
|
| 8 |
*/
|
| 9 |
function facebook_status_help($section='') {
|
| 10 |
|
| 11 |
$output = '';
|
| 12 |
|
| 13 |
switch ($section) {
|
| 14 |
case "admin/help#facebook_status":
|
| 15 |
$output = '<p>'. t("This module adds a Facebook-style status block. Please see <a href=\"http://drupal.org/project/facebook_status\" title=\"http://drupal.org/project/facebook_status\" rel=\"nofollow\">http://drupal.org/project/facebook_status</a> for more information.") .'</p>';
|
| 16 |
break;
|
| 17 |
}
|
| 18 |
|
| 19 |
return $output;
|
| 20 |
} // function facebook_status_help
|
| 21 |
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Valid permissions for this module
|
| 25 |
* @return array An array of valid permissions for the facebook_status module
|
| 26 |
*/
|
| 27 |
|
| 28 |
function facebook_status_perm() {
|
| 29 |
return array('edit own facebook_status', 'edit all facebook_status'); //note that, since all this module does is provide a block, there is no need for an 'access facebook_status' permission because access can be set in the block's settings
|
| 30 |
} // function facebook_status_perm()
|
| 31 |
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Generate HTML for the facebook_status block
|
| 35 |
* @param op the operation from the URL
|
| 36 |
* @param delta offset
|
| 37 |
* @returns block HTML
|
| 38 |
*/
|
| 39 |
function facebook_status_block($op='list', $delta=0) {
|
| 40 |
// listing of blocks, such as on the admin/block page
|
| 41 |
if ($op == "list") {
|
| 42 |
$block[0]["info"] = t("Facebook Status");
|
| 43 |
$block[1]["info"] = t("Facebook Status Recent Updates");
|
| 44 |
if (module_exists('user_relationships')) {
|
| 45 |
$block[2]["info"] = t("Facebook Status UR Recent Updates");
|
| 46 |
}
|
| 47 |
return $block;
|
| 48 |
}
|
| 49 |
else if ($op == 'view') {
|
| 50 |
switch ($delta) {
|
| 51 |
case 0:
|
| 52 |
global $user;
|
| 53 |
$x = FALSE; //helps with theming and brevity, see later comments when this is used
|
| 54 |
$path = drupal_get_path('module', 'facebook_status');
|
| 55 |
if (variable_get('facebook_status_ajax', 1)) {
|
| 56 |
drupal_add_js($path .'/facebook_status.js');
|
| 57 |
}
|
| 58 |
$block_content = '<div class="facebook_status_block">';
|
| 59 |
if ( (((arg(0) == 'user' && is_numeric(arg(1)) && arg(1) == $user->uid) //if the user is viewing her own profile (i.e. if the form is going to update the user's own status)
|
| 60 |
|| (arg(0) == 'node' && is_numeric(arg(1)) && db_result("SELECT uid FROM node WHERE nid = %d", arg(1)) == $user->uid)) //or if the user is viewing a node she created
|
| 61 |
&& user_access('edit own facebook_status')) //and the user has permission to edit her status
|
| 62 |
|| user_access('edit all facebook_status') ) { //or if the user has permission to edit anyone's status
|
| 63 |
$block_content .= '<div class="facebook_status_form">'. facebook_status_form_display() .'</div>'; //print the form which allows the user to update her status
|
| 64 |
$x = TRUE; //used later to check if the form will show up so we don't have to repeat this gigantic 'if' statement
|
| 65 |
}
|
| 66 |
if (arg(0) == 'user' && is_numeric(arg(1))) { //if we're viewing a profile page
|
| 67 |
$uname = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", arg(1)));
|
| 68 |
$fbs_name = htmlspecialchars($uname, ENT_NOQUOTES); //htmlspecialchars instead of the usual check_plain because names sometimes have quotes in them; however, this means that you have to be careful when writing database queries.
|
| 69 |
$sm = db_fetch_array(db_query_range("SELECT status_fb, status_time FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", arg(1), 0, 1)); //grabs the status of the user who's profile is being viewed
|
| 70 |
}
|
| 71 |
else if (arg(0) == 'node' && is_numeric(arg(1))) { //if we're viewing a node
|
| 72 |
$fbs_user = db_result(db_query("SELECT uid, name FROM {users} LEFT JOIN node ON node.uid = users.uid WHERE node.nid = %d", arg(1)));
|
| 73 |
$fbs_name = htmlspecialchars($fbs_user['name'], ENT_NOQUOTES);
|
| 74 |
$sm = db_fetch_array(db_query_range("SELECT status_fb, status_time FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", $fbs_user['uid'], 0, 1)); //grabs the status of the currently viewed node's author
|
| 75 |
}
|
| 76 |
else {
|
| 77 |
$fbs_name = htmlspecialchars($user->name, ENT_NOQUOTES);
|
| 78 |
$sm = db_fetch_array(db_query_range("SELECT status_fb, status_time FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", $user->uid, 0, 1)); //grabs the status of the current user if the block is not on a user profile page
|
| 79 |
}
|
| 80 |
if (variable_get('facebook_status_mode', 1)) { //if Facebook Mode is on prepend the username before the status
|
| 81 |
$block_content .= '<div class="facebook_status_status">';
|
| 82 |
if ($x) {
|
| 83 |
$block_content .= '<a>';
|
| 84 |
} //if the form is going to show up (i.e. if a user can edit their status) then apply 'a' formatting to the status so it looks clickable so users are aware that the form will appear using AJAX. Of course, if JS is turned off, the formatting looks rather unsatisfactory anyway.
|
| 85 |
$block_content .= $fbs_name;
|
| 86 |
}
|
| 87 |
if ($sm['status_time']) { //if the user has posted her status before
|
| 88 |
$block_content .= " ". htmlspecialchars($sm['status_fb'], ENT_NOQUOTES) ." <span class='submitted'>". format_interval(time() - $sm['status_time'], 1) ."</span>"; //print the status with the time it was posted
|
| 89 |
}
|
| 90 |
else { //if the user has not posted a status
|
| 91 |
if (!variable_get('facebook_status_mode', 1)) { //if Facebook Mode is off prepend the username anyway
|
| 92 |
$block_content .= $fbs_name;
|
| 93 |
}
|
| 94 |
$block_content .= t(" does not yet have a status."); //with Facebook Mode off, a suggestion might be " has not yet posted thoughts" or similar
|
| 95 |
}
|
| 96 |
if ($x) {
|
| 97 |
$block_content .= '</a>';
|
| 98 |
}
|
| 99 |
$block_content .= '</div></div>';
|
| 100 |
// set up the block
|
| 101 |
$block['subject'] = 'Status';
|
| 102 |
$block['content'] = $block_content;
|
| 103 |
break;
|
| 104 |
case 1:
|
| 105 |
$c_array = facebook_status_get_status(-1, variable_get('facebook_status_max_num_block_stats_all', 5));
|
| 106 |
$block_content = "<div class='facebook_status_block_all'><ul>";
|
| 107 |
foreach ($c_array as $row) {
|
| 108 |
$block_content .= "<li>". l(htmlspecialchars($row['fbs_name'], ENT_NOQUOTES), "user/". $row['fbs_uid']) . t(" ") . htmlspecialchars($row['status_fb'], ENT_NOQUOTES) ." <div class=". t("submitted") .">". format_interval(time() - $row['status_time'], 1) ."</div></li>";
|
| 109 |
}
|
| 110 |
$block_content .= "</ul></div>";
|
| 111 |
$block['subject'] = "Recent Status Updates";
|
| 112 |
$block['content'] = $block_content;
|
| 113 |
break;
|
| 114 |
case 2:
|
| 115 |
if (module_exists('user_relationships')) { //this may not be necessary
|
| 116 |
$subject = db_result(db_query("SELECT plural_name FROM {user_relationship_types} WHERE rtid = %d", variable_get('facebook_status_ur_type', 1)));
|
| 117 |
$block['subject'] = $subject . t("' Recent Status Updates");
|
| 118 |
$block['content'] = facebook_status_get_ur_status();
|
| 119 |
}
|
| 120 |
break;
|
| 121 |
}
|
| 122 |
return $block;
|
| 123 |
}
|
| 124 |
|
| 125 |
} // end facebook_status_block
|
| 126 |
|
| 127 |
|
| 128 |
//builds the status submission form
|
| 129 |
function facebook_status_update_form() {
|
| 130 |
global $user;
|
| 131 |
if (variable_get('facebook_status_mode', 1)) { //if Facebook Mode is on, add the username to the title of the textfield so we get the "User is ___" effect.
|
| 132 |
if (arg(0) == 'user' && is_numeric(arg(1))) { //if we're viewing a profile page
|
| 133 |
$uname = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", arg(1)));
|
| 134 |
$xname = htmlspecialchars($uname, ENT_NOQUOTES); //goes in title of the textfield
|
| 135 |
}
|
| 136 |
else if (arg(0) == 'node' && is_numeric(arg(1))) { //if we're viewing a node
|
| 137 |
$fbs_user = db_result(db_query("SELECT uid, name FROM {users} LEFT JOIN node ON node.uid = users.uid WHERE node.nid = %d", arg(1)));
|
| 138 |
$xname = htmlspecialchars($fbs_user['name'], ENT_NOQUOTES); //goes in title of the textfield
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
$xname = htmlspecialchars($user->name, ENT_NOQUOTES); //goes in title of the textfield
|
| 142 |
$sm = db_fetch_array(db_query_range("SELECT status_fb, status_time FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", $user->uid, 0, 1)); //grabs the status of the current user if the block is not on a user profile page
|
| 143 |
}
|
| 144 |
$xname = ": " . $xname; //this needs to be done here instead of in the actual #title so it can be turned off if Facebook Mode is off
|
| 145 |
}
|
| 146 |
else {
|
| 147 |
$xname = '';
|
| 148 |
} //if Facebook Mode is off, just set $xname to blank so it doesn't interfere.
|
| 149 |
$status = facebook_status_get_status();
|
| 150 |
$form['name'] = array(
|
| 151 |
'#type' => 'textfield',
|
| 152 |
'#title' => t("Status") . $xname,
|
| 153 |
'#size' => variable_get('facebook_status_field_length', 30),
|
| 154 |
'#maxlength' => variable_get('facebook_status_length', 192), //warning: the status column in the {facebook_status} table only holds 255 bytes. In strict SQL mode, statuses larger than this will not be saved; in other modes, they will be truncated.
|
| 155 |
'#description' => t("Please enter your status. Remember that it will not update unless you click 'Save.'"),
|
| 156 |
'#default_value' => ((!empty( $status[0]['status_fb'] ) ) ? htmlspecialchars($status[0]['status_fb'], ENT_NOQUOTES) : t("is ")),
|
| 157 |
);
|
| 158 |
$form['submit'] = array('#type' => 'submit', '#value' => t("Save") );
|
| 159 |
return $form;
|
| 160 |
}
|
| 161 |
|
| 162 |
//renders the status submission form; call this if you want to display it arbitrarily
|
| 163 |
//remember to use user_access appropriately before calling this function
|
| 164 |
function facebook_status_form_display() {
|
| 165 |
return drupal_get_form('facebook_status_update_form');
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
| 169 |
* In _submit and _validate I decided to make separate sections for the different permission types because it's easier to read and runs a little faster
|
| 170 |
* than if I had only used the 'edit all' section and just checked the permissions each time before setting $uid
|
| 171 |
* the result is that the code is slightly longer and so more is loaded on bootstrap.
|
| 172 |
* This is all technically unnecessary in terms of this module because the form doesn't render if it can't be used,
|
| 173 |
* but it saves a lot of work for anyone who decides to render the form arbitrarily.
|
| 174 |
*/
|
| 175 |
|
| 176 |
//the submit function, tells Drupal what to do when the status is submitted
|
| 177 |
//indentation is a little "off" here because permission checking was added "after the fact"
|
| 178 |
function facebook_status_update_form_submit($form, $form_values) {
|
| 179 |
global $user;
|
| 180 |
//note that this first part is only for people who can edit their own status, so we don't need to check whose database records to update.
|
| 181 |
if ( user_access('edit own facebook_status') && !user_access('edit all facebook_status') ) {
|
| 182 |
if ( db_query("INSERT INTO {facebook_status} (status_fb, status_time, uid) VALUES('%s', %d, %d)", $form_values['name'], time(), $user->uid) ) {
|
| 183 |
//only save last $num_status updates; delete one if there are too many
|
| 184 |
if ( variable_get('facebook_status_number', 10) ) { //zero is the equivalent of infinite; that is, if var(facebook_status_number) == 0 then no rows will ever be deleted.
|
| 185 |
while ( db_num_rows(db_query("SELECT * FROM {facebook_status} WHERE uid = %d", $user->uid)) > variable_get('facebook_status_number', 10) ) {
|
| 186 |
db_query_range("DELETE FROM {facebook_status} WHERE uid = %d ORDER BY status_time ASC", $user->uid, 0, 1);
|
| 187 |
}
|
| 188 |
}
|
| 189 |
drupal_set_message(t('Your status has been updated.')); }
|
| 190 |
else {
|
| 191 |
drupal_set_message(t('An error has occurred while updating your status. Please try again. If the problem persists, please contact an administrator.'), 'error'); }
|
| 192 |
return '';
|
| 193 |
//note that it doesn't matter whether this user is updating someone else's status or not because the system finds out whose status the form should update no matter what
|
| 194 |
}
|
| 195 |
else if (user_access('edit all facebook_status')) {
|
| 196 |
//determine the uid of the user whose status will be updated
|
| 197 |
if ( arg(0) == 'user' && is_numeric(arg(1)) ) {
|
| 198 |
$uid = arg(1);
|
| 199 |
} //the relevant user is the owner of the currently viewed profile
|
| 200 |
else if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
|
| 201 |
$uid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", arg(1)));
|
| 202 |
} //the relevant user is the author of the currently viewed node
|
| 203 |
else {
|
| 204 |
$uid = $user->uid;
|
| 205 |
} //on any other page (i.e. not a user page or a node) just use the current user
|
| 206 |
if ( db_query("INSERT INTO {facebook_status} (status_fb, status_time, uid) VALUES('%s', %d, %d)", $form_values['name'], time(), $uid) ) {
|
| 207 |
//only save last $num_status updates; delete one if there are too many
|
| 208 |
if ( variable_get('facebook_status_number', 10) ) { //zero is the equivalent of infinite; that is, if var(facebook_status_number) == 0 then no rows will ever be deleted.
|
| 209 |
while ( db_result(db_query("SELECT COUNT(sid) FROM {facebook_status} WHERE uid = %d", $uid)) > variable_get('facebook_status_number', 10) ) { //only continue if there are too many status records from one user; note that we have to use 'while' instead of 'if' so it still works if 'facebook_status_number' is lowered
|
| 210 |
db_query_range("DELETE FROM {facebook_status} WHERE uid = %d ORDER BY status_time ASC", $uid, 0, 1); //deletes excess records to clean up database
|
| 211 |
}
|
| 212 |
}
|
| 213 |
drupal_set_message(t('The status has been updated.')); }
|
| 214 |
else {
|
| 215 |
drupal_set_message(t('An error has occurred while updating the status. Please try again.'), 'error'); }
|
| 216 |
return '';
|
| 217 |
}
|
| 218 |
else { //this technically shouldn't happen, but we need a catch in case someone without permission manages to submit the form.
|
| 219 |
drupal_set_message(t('An error has occurred while updating the status: you do not have permission to perform this action. Please contact an administrator.'), 'error'); }
|
| 220 |
return '';
|
| 221 |
}
|
| 222 |
|
| 223 |
function facebook_status_update_form_validate($form_id, $form_values) {
|
| 224 |
global $user;
|
| 225 |
if ( user_access('edit own facebook_status') && !user_access('edit all facebook_status') ) {
|
| 226 |
if ($form_values['name'] == '' || $form_values['name'] == ' ') { //if the user didn't enter anything in the textfield or entered whitespace,
|
| 227 |
$last_status = db_result(db_query_range("SELECT status_fb FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", $user->uid, 0, 1)); //get the last status
|
| 228 |
if ( $last_status ) { //if the user has posted her status before, use that so we don't have to have a blank status
|
| 229 |
$form_values['name'] = $last_status;
|
| 230 |
}
|
| 231 |
else {
|
| 232 |
form_set_error('', t('You must enter a status in order to save it.')); //give an error as opposed to saving a default so we can distinguish more easily who hasn't set their status
|
| 233 |
}
|
| 234 |
} //else just keep going
|
| 235 |
}
|
| 236 |
else if (user_access('edit all facebook_status')) { //the only difference in this section is that $uid is used instead of $user->uid
|
| 237 |
if ($form_values['name'] == '' || $form_values['name'] == ' ') { //if the user didn't enter anything in the textfield or entered whitespace,
|
| 238 |
//determine the uid of the user whose status will be updated
|
| 239 |
if ( arg(0) == 'user' && is_numeric(arg(1)) ) {
|
| 240 |
$uid = arg(1);
|
| 241 |
} //the relevant user is the owner of the currently viewed profile
|
| 242 |
else if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
|
| 243 |
$uid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", arg(1)));
|
| 244 |
} //the relevant user is the author of the currently viewed node
|
| 245 |
else {
|
| 246 |
$uid = $user->uid;
|
| 247 |
} //on any other page (i.e. not a user page or a node) just use the current user
|
| 248 |
$last_status = db_result(db_query_range("SELECT status_fb FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC", $uid, 0, 1)); //get the last status
|
| 249 |
if ( $last_status ) { //if the user has posted her status before, use that so we don't have to have a blank status
|
| 250 |
$form_values['name'] = $last_status;
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
form_set_error('', t('You must enter a status in order to save it.')); //give an error as opposed to saving a default so we can distinguish more easily who hasn't set their status
|
| 254 |
}
|
| 255 |
} //else just keep going
|
| 256 |
} //there's no reason to add a catch for users who don't have permission to submit the form, because there's no reason to validate anything; the necessary catch is in _submit.
|
| 257 |
}
|
| 258 |
|
| 259 |
/**
|
| 260 |
* Returns an array with the status, status-posted-time, uid, and username of the user whose uid is passed to the function as $fbs_uid.
|
| 261 |
* If no uid is passed and the current page is a user profile, then we use the uid of the user whose profile is being viewed.
|
| 262 |
* If no uid is passed and the current page is a node, then we use the uid of the author of that node.
|
| 263 |
* If no uid is passed and the current page is neither a node nor a user profile, we use the current user's uid.
|
| 264 |
* This is the same process used in _submit and _validate above, although here it's written much more clearly.
|
| 265 |
* If the uid passed is -1, the latest status updates for all users are returned, but only one status update is returned per user.
|
| 266 |
* If the uid passed is less than -1, the all the latest status updates (for any user) are returned.
|
| 267 |
* $num_results controls the number of status updates to return for the relevant user.
|
| 268 |
* If $num_results is zero, all results are returned.
|
| 269 |
* The uid and username (fbs_uid and fbs_name, respectively) of the user who owns the status are also passed back as part of the array in case they're needed,
|
| 270 |
* for example when $fbs_uid is not specified when the function is called but the developer wants the username to be themed differently than the status itself.
|
| 271 |
* Warning: the status is not escaped, so you still need to do validation to make sure there are no XSS attacks.
|
| 272 |
*/
|
| 273 |
function facebook_status_get_status($fbs_uid = 0, $num_results = 1) {
|
| 274 |
$result = array();
|
| 275 |
if ( !$fbs_uid || !is_numeric($fbs_uid) ) { //we do some extra work with is_numeric to prevent errors when non-numeric values get passed in
|
| 276 |
if ( arg(0) == 'user' && is_numeric(arg(1)) ) {
|
| 277 |
$fbs_uid = arg(1);
|
| 278 |
}
|
| 279 |
else if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
|
| 280 |
$fbs_uid = db_result(db_query("SELECT uid FROM {node} WHERE nid = %d", arg(1)));
|
| 281 |
}
|
| 282 |
else {
|
| 283 |
global $user;
|
| 284 |
$fbs_uid = $user->uid;
|
| 285 |
}
|
| 286 |
}
|
| 287 |
if ( !is_numeric($num_results) ) {
|
| 288 |
$num_results = 1;
|
| 289 |
} //if $num_results is invalid, set it to the default (1).
|
| 290 |
else if (is_numeric($num_results) && $num_results == 0) {
|
| 291 |
$nr = ""; } // if $num_results is zero, return *all* status updates. This can be slow, but it can also be useful if the developer doesn't want to include status updates from a certain user (including the current user).
|
| 292 |
else {
|
| 293 |
$nr = "LIMIT 0," . $num_results;
|
| 294 |
} //if $num_results is a valid number, limit the number of statuses returned to it.
|
| 295 |
if ( $fbs_uid > 0 ) {
|
| 296 |
$query = db_query("SELECT status_fb, status_time FROM {facebook_status} WHERE uid = %d ORDER BY status_time DESC %s", $fbs_uid, $nr); //grabs user's status
|
| 297 |
$result = array();
|
| 298 |
$i = 0;
|
| 299 |
while ($row = db_fetch_array($query)) {
|
| 300 |
$result[$i] = $row;
|
| 301 |
$result[$i]['fbs_name'] = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $fbs_uid)); //just a convenience, but don't forget to add a space between it and the status
|
| 302 |
$result[$i]['fbs_uid'] = $fbs_uid;
|
| 303 |
$i++;
|
| 304 |
}
|
| 305 |
}
|
| 306 |
else if ( $fbs_uid < 0 ) { //if a negative number is passed as $fbs_uid, return values for all users.
|
| 307 |
if ($fbs_uid < -1) {
|
| 308 |
$duid = "";
|
| 309 |
}
|
| 310 |
else {
|
| 311 |
$duid = "GROUP BY fbs_uid";
|
| 312 |
} //if $fbs_uid is -1, return only the most recent status for each user; otherwise, return the most recent status updates regardless of who created them.
|
| 313 |
$query = db_query("SELECT uid as fbs_uid, status_fb, status_time FROM {facebook_status} %s ORDER BY status_time DESC %s", $duid, $nr); //grabs statuses
|
| 314 |
$result = array();
|
| 315 |
$i = 0;
|
| 316 |
while ($row = db_fetch_array($query)) {
|
| 317 |
$row['status_fb'] = drupal_substr($row['status_fb'], 0, variable_get('facebook_status_max_block_len_all', variable_get('facebook_status_length', 192)));
|
| 318 |
if ( drupal_strlen($row['status_fb']) == variable_get('facebook_status_max_block_len_all', variable_get('facebook_status_length', 192)) ) {
|
| 319 |
$row['status_fb'] .= "...";
|
| 320 |
}
|
| 321 |
$result[$i] = $row;
|
| 322 |
$result[$i]['fbs_name'] = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $result[$i]['fbs_uid'])); //just a convenience, but don't forget to add a space between it and the status
|
| 323 |
$i++;
|
| 324 |
}
|
| 325 |
}
|
| 326 |
else { //should technically never happen
|
| 327 |
$result = t("Error: anonymous users have no status.");
|
| 328 |
}
|
| 329 |
return $result;
|
| 330 |
}
|
| 331 |
|
| 332 |
|
| 333 |
//configuration settings
|
| 334 |
|
| 335 |
function facebook_status_admin() {
|
| 336 |
|
| 337 |
$form['facebook_status_field_length'] = array(
|
| 338 |
'#type' => 'textfield',
|
| 339 |
'#title' => t('Length of the Status textfield'),
|
| 340 |
'#default_value' => variable_get('facebook_status_field_length', 30),
|
| 341 |
'#size' => 3,
|
| 342 |
'#maxlength' => 3,
|
| 343 |
'#description' => t("30 is standard length for a sidebar block (and the default); 72 is standard for most content and footer blocks.")
|
| 344 |
);
|
| 345 |
|
| 346 |
$form['facebook_status_length'] = array(
|
| 347 |
'#type' => 'textfield',
|
| 348 |
'#title' => t('Maximum length of a status'),
|
| 349 |
'#default_value' => variable_get('facebook_status_length', 192),
|
| 350 |
'#size' => 3,
|
| 351 |
'#maxlength' => 3,
|
| 352 |
'#description' => t("192 is the default; 255 is the maximum; no higher than 240 is recommended. This is the length that shows up on the Facebook Status block.")
|
| 353 |
);
|
| 354 |
|
| 355 |
if (module_exists('user_relationships')) {
|
| 356 |
$form['facebook_status_max_block_len'] = array(
|
| 357 |
'#type' => 'textfield',
|
| 358 |
'#title' => t('Shorten statuses in the Facebook Status UR Recent Updates to this length'),
|
| 359 |
'#default_value' => variable_get('facebook_status_max_block_len', variable_get('facebook_status_length', 192)),
|
| 360 |
'#size' => 3,
|
| 361 |
'#maxlength' => 3,
|
| 362 |
'#description' => t("The maximum length of a status is the default and maximum; 25 is the minimum. You may want to lower this if you have more than one status showing on the block.")
|
| 363 |
);
|
| 364 |
|
| 365 |
$form['facebook_status_max_num_block_stats'] = array(
|
| 366 |
'#type' => 'textfield',
|
| 367 |
'#title' => t('The maximum number of statuses to show in the Facebook Status UR Recent Updates block'),
|
| 368 |
'#default_value' => variable_get('facebook_status_max_num_block_stats', 5),
|
| 369 |
'#size' => 3,
|
| 370 |
'#maxlength' => 3,
|
| 371 |
'#description' => t("5 is the default, 1 is the minimum, and the maximum number of status updates you keep in the database for each user is the maximum.")
|
| 372 |
);
|
| 373 |
|
| 374 |
$fbsur_result = db_query("SELECT plural_name, rtid FROM {user_relationship_types}");
|
| 375 |
while ($row = db_fetch_array($fbsur_result)) {
|
| 376 |
$i = $row['rtid'];
|
| 377 |
$fbsur[$i] = $row['plural_name'];
|
| 378 |
}
|
| 379 |
$form['facebook_status_ur_type'] = array(
|
| 380 |
'#type' => 'select',
|
| 381 |
'#title' => t('User Relationship type to use for the Facebook Status UR Recent Updates block'),
|
| 382 |
'#default_value' => variable_get('facebook_status_ur_type', 1),
|
| 383 |
'#options' => $fbsur,
|
| 384 |
);
|
| 385 |
}
|
| 386 |
|
| 387 |
$form['facebook_status_max_block_len_all'] = array(
|
| 388 |
'#type' => 'textfield',
|
| 389 |
'#title' => t('Shorten statuses in the Facebook Status Recent Updates block to this length'),
|
| 390 |
'#default_value' => variable_get('facebook_status_max_block_len_all', variable_get('facebook_status_length', 192)),
|
| 391 |
'#size' => 3,
|
| 392 |
'#maxlength' => 3,
|
| 393 |
'#description' => t("The maximum length of a status is the default and maximum; 25 is the minimum. You may want to lower this if you have more than one status showing on the block.")
|
| 394 |
);
|
| 395 |
|
| 396 |
$form['facebook_status_max_num_block_stats_all'] = array(
|
| 397 |
'#type' => 'textfield',
|
| 398 |
'#title' => t('The maximum number of statuses to show in the Facebook Status Recent Updates block'),
|
| 399 |
'#default_value' => variable_get('facebook_status_max_num_block_stats_all', 5),
|
| 400 |
'#size' => 3,
|
| 401 |
'#maxlength' => 3,
|
| 402 |
'#description' => t("5 is the default, 1 is the minimum.")
|
| 403 |
);
|
| 404 |
|
| 405 |
$form['facebook_status_number'] = array(
|
| 406 |
'#type' => 'textfield',
|
| 407 |
'#title' => t('Maximum number of status updates to keep in the database for each user'),
|
| 408 |
'#default_value' => variable_get('facebook_status_number', 10),
|
| 409 |
'#size' => 3,
|
| 410 |
'#maxlength' => 3,
|
| 411 |
'#description' => t("10 is the default; enter 0 for unlimited. This is just to help keep your database small(er).")
|
| 412 |
);
|
| 413 |
|
| 414 |
$form['facebook_status_mode'] = array(
|
| 415 |
'#type' => 'checkbox',
|
| 416 |
'#title' => t('Facebook Mode'),
|
| 417 |
'#default_value' => variable_get('facebook_status_mode', 1),
|
| 418 |
'#description' => t("Facebook Mode makes this module work like Facebook, where the user's username is appended to the front of the status. Default is On."),
|
| 419 |
);
|
| 420 |
|
| 421 |
$form['facebook_status_ajax'] = array(
|
| 422 |
'#type' => 'checkbox',
|
| 423 |
'#title' => t('Slide Effect'),
|
| 424 |
'#default_value' => variable_get('facebook_status_ajax', 1),
|
| 425 |
'#description' => t("With this enabled, users who have permission to edit statuses can click on the status to see the edit box drop down. This minimizes the space that Facebook Status takes up. Default is On."),
|
| 426 |
);
|
| 427 |
|
| 428 |
return system_settings_form($form);
|
| 429 |
}
|
| 430 |
|
| 431 |
function facebook_status_admin_validate($form_id, $form_values) {
|
| 432 |
if (!(is_numeric($form_values['facebook_status_field_length']) && $form_values['facebook_status_field_length'] <= variable_get('facebook_status_length', 192) && $form_values['facebook_status_field_length'] > 1)) {
|
| 433 |
form_set_error('', t('Please enter a number between 1 and the maximum status length, inclusive, for the length of the status textfield.'));
|
| 434 |
}
|
| 435 |
if (!(is_numeric($form_values['facebook_status_length']) && $form_values['facebook_status_length'] <= 255 && $form_values['facebook_status_length'] > 1)) {
|
| 436 |
form_set_error('', t('Please enter a number between 1 and 255, inclusive, for the maximum status length.'));
|
| 437 |
}
|
| 438 |
if (!(is_numeric($form_values['facebook_status_number']) && $form_values['facebook_status_number'] <= 999 && $form_values['facebook_status_number'] > 1)) {
|
| 439 |
form_set_error('', t('Please enter a number between 0 and 999, inclusive, for the maximum number of status updates to keep in the database for each user; use zero to keep all updates.'));
|
| 440 |
}
|
| 441 |
if (module_exists('user_relationships')) {
|
| 442 |
if (!(is_numeric($form_values['facebook_status_max_num_block_stats']) && $form_values['facebook_status_max_num_block_stats'] >= 1)) {
|
| 443 |
form_set_error('', t('Please enter a number between 1 and the maximum number of status updates kept in the database for each user, inclusive, for the maximum number of statuses to show in the Facebook Status UR Recent Updates block.'));
|
| 444 |
}
|
| 445 |
if (!(is_numeric($form_values['facebook_status_max_block_len']) && $form_values['facebook_status_max_block_len'] >= 25)) {
|
| 446 |
form_set_error('', t('Please enter a number between 25 and the maximum status length, inclusive, for the maximum length of statuses in the Facebook Status UR Recent Updates block.'));
|
| 447 |
}
|
| 448 |
}
|
| 449 |
if (!(is_numeric($form_values['facebook_status_max_block_len_all']) && $form_values['facebook_status_max_block_len_all'] >= 25)) {
|
| 450 |
form_set_error('', t('Please enter a number between 25 and the maximum status length, inclusive, for the maximum length of statuses in the Facebook Status Recent Updates block.'));
|
| 451 |
}
|
| 452 |
if (!(is_numeric($form_values['facebook_status_max_num_block_stats_all']) && $form_values['facebook_status_max_num_block_stats_all'] >= 1)) {
|
| 453 |
form_set_error('', t('Please enter a number above zero for the maximum number of statuses to show in the Facebook Status Recent Updates block.'));
|
| 454 |
}
|
| 455 |
}
|
| 456 |
|
| 457 |
/**
|
| 458 |
* Implementation of hook_menu().
|
| 459 |
*/
|
| 460 |
function facebook_status_menu() {
|
| 461 |
|
| 462 |
$items = array();
|
| 463 |
|
| 464 |
$items[] = array(
|
| 465 |
'path' => 'admin/settings/facebook_status',
|
| 466 |
'title' => t('Facebook Status settings'),
|
| 467 |
'description' => t('Allows administrators to adjust certain display settings for Facebook Status.'),
|
| 468 |
'callback' => 'drupal_get_form',
|
| 469 |
'callback arguments' => 'facebook_status_admin',
|
| 470 |
'access' => user_access('access administration pages'),
|
| 471 |
'type' => MENU_NORMAL_ITEM,
|
| 472 |
);
|
| 473 |
|
| 474 |
return $items;
|
| 475 |
}
|
| 476 |
|
| 477 |
/*
|
| 478 |
* Returns the latest status updates from users with whom the current user has a relationship. Only one status update is returned per user.
|
| 479 |
* If a rtid is not passed, this function uses the default relationship specified on the Facebook Status settings page.
|
| 480 |
* Returns FALSE if user_relationships is not available or if there are no relationship types yet.
|
| 481 |
* $number controls the number of statuses returned; if it is not passed, it defaults to the setting on the Facebook Status settings page.
|
| 482 |
*/
|
| 483 |
function facebook_status_get_ur_status($ur_rtid = -1, $number = 0) {
|
| 484 |
if (module_exists('user_relationships')) { //don't bother using this function at all if UR isn't installed
|
| 485 |
if (!is_numeric($number) || $number < 0) {
|
| 486 |
$number = 0;
|
| 487 |
}
|
| 488 |
if (!$number) {
|
| 489 |
$number = variable_get('facebook_status_max_num_block_stats', 5);
|
| 490 |
}
|
| 491 |
if ($ur_rtid = -1) {
|
| 492 |
$ur_rtid = variable_get('facebook_status_ur_type', 1);
|
| 493 |
}
|
| 494 |
if (db_num_rows(db_query("SELECT * FROM {user_relationship_types}"))) { //don't bother with any of this if there aren't any relationship types yet
|
| 495 |
global $user;
|
| 496 |
$ur_object = db_fetch_array(db_query("SELECT is_oneway, requires_approval FROM {user_relationship_types} WHERE rtid = %d", $ur_rtid));
|
| 497 |
if ($ur_object['is_oneway'] == 1) {
|
| 498 |
$ur_oneway_urjoin = "u.uid = ur.requester_id";
|
| 499 |
$ur_oneway_fbjoin = "ur.requester_id = fb.uid";
|
| 500 |
$ur_oneway_urwhere = "ur.requester_id = ". $user->uid;
|
| 501 |
}
|
| 502 |
else {
|
| 503 |
$ur_oneway_urjoin = "(u.uid = ur.requester_id OR u.uid = ur.requestee_id)";
|
| 504 |
$ur_oneway_fbjoin = "(ur.requester_id = fb.uid OR ur.requestee_id = fb.uid)";
|
| 505 |
$ur_oneway_urwhere = "(ur.requester_id = ". $user->uid ." OR ur.requestee_id = ". $user->uid .")";
|
| 506 |
}
|
| 507 |
if ($ur_object['requires_approval'] = 1) {
|
| 508 |
$ur_requires_approval = "AND (ur.approved = 1)";
|
| 509 |
}
|
| 510 |
else {
|
| 511 |
$ur_requires_approval = "";
|
| 512 |
}
|
| 513 |
$user_rel = db_query("
|
| 514 |
SELECT u.uid, u.name, fb.status_fb, fb.status_time
|
| 515 |
FROM {users} AS u
|
| 516 |
LEFT JOIN {user_relationships} AS ur
|
| 517 |
ON %s
|
| 518 |
LEFT JOIN {facebook_status} AS fb
|
| 519 |
ON %s
|
| 520 |
WHERE %s
|
| 521 |
AND %s
|
| 522 |
%s
|
| 523 |
AND (ur.rtid = %d)
|
| 524 |
AND (u.uid = fb.uid)
|
| 525 |
GROUP BY (u.name)
|
| 526 |
ORDER BY fb.status_time DESC
|
| 527 |
", $ur_oneway_urjoin, $ur_oneway_fbjoin, $ur_oneway_urjoin, $ur_oneway_urwhere, $ur_requires_approval, $ur_rtid);
|
| 528 |
|
| 529 |
$output = "<div class='facebook_status_ur_block'>";
|
| 530 |
$output .= "<ul>";
|
| 531 |
$i = 0; //count by $i instead of a limit in the query so we can exclude the current user and still end up with the right number of results
|
| 532 |
while ($row = db_fetch_array($user_rel)) {
|
| 533 |
if ($row['uid'] != $user->uid) {
|
| 534 |
$row['status_fb'] = drupal_substr($row['status_fb'], 0, variable_get('facebook_status_max_block_len', variable_get('facebook_status_length', 192)));
|
| 535 |
if ( drupal_strlen($row['status_fb']) == variable_get('facebook_status_max_block_len', variable_get('facebook_status_length', 192)) ) {
|
| 536 |
$row['status_fb'] .= "...";
|
| 537 |
}
|
| 538 |
$output .= "<li>". l(htmlspecialchars($row['name'], ENT_NOQUOTES), "user/". $row['uid']) . t(" ") . htmlspecialchars($row['status_fb'], ENT_NOQUOTES) ." <div class=" . t("submitted") .">". format_interval(time() - $row['status_time'], 1) ."</div></li>";
|
| 539 |
$i++;
|
| 540 |
if ($i >= $number) {
|
| 541 |
break;
|
| 542 |
}
|
| 543 |
}
|
| 544 |
}
|
| 545 |
$output .= "</ul></div>";
|
| 546 |
return $output;
|
| 547 |
}
|
| 548 |
else {
|
| 549 |
return FALSE;
|
| 550 |
} //no relationship types
|
| 551 |
}
|
| 552 |
else {
|
| 553 |
return FALSE;
|
| 554 |
} //UR is not installed
|
| 555 |
}
|