| 1 |
<?php
|
| 2 |
//$Id: flag_friend.module,v 1.7 2008/11/21 23:25:08 sirkitree Exp $
|
| 3 |
|
| 4 |
// define our statuses
|
| 5 |
define('FLAG_FRIEND_BOTH', 0);
|
| 6 |
define('FLAG_FRIEND_FLAGGED', 1);
|
| 7 |
define('FLAG_FRIEND_UNFLAGGED', 2);
|
| 8 |
define('FLAG_FRIEND_APPROVAL', 3);
|
| 9 |
define('FLAG_FRIEND_PENDING', 4);
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_flag().
|
| 13 |
*/
|
| 14 |
function flag_friend_flag($event, $flag, $content_id, $account) {
|
| 15 |
if ($flag->name == 'friend') {
|
| 16 |
switch ($event) {
|
| 17 |
case 'flag':
|
| 18 |
// See the status of the friendship.
|
| 19 |
$status = flag_friend_determine_friend_status($flag, $account->uid, $content_id);
|
| 20 |
|
| 21 |
// If both are now flagged, we record the relationship and remove the flags.
|
| 22 |
if ($status == FLAG_FRIEND_BOTH) {
|
| 23 |
// Remove any message entries for either user.
|
| 24 |
flag_friend_message('unflag', $flag, $account->uid, $content_id);
|
| 25 |
flag_friend_message('unflag', $flag, $content_id, $account->uid);
|
| 26 |
|
| 27 |
// Since these users have flagged eachother, we create the relationship in the flag_friend table.
|
| 28 |
db_query("INSERT INTO {flag_friend} VALUES(%d, %d, %d)", $account->uid, $content_id, $_SERVER['REQUEST_TIME']);
|
| 29 |
|
| 30 |
// Then remove the flags.
|
| 31 |
$flag->flag('unflag', $content_id, $account);
|
| 32 |
$flag->flag('unflag', $account->uid, user_load(array('uid' => $content_id)));
|
| 33 |
}
|
| 34 |
break;
|
| 35 |
case 'unflag':
|
| 36 |
// Remove message.
|
| 37 |
flag_friend_message($event, $flag, $content_id, $account->uid);
|
| 38 |
break;
|
| 39 |
}
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_preprocess_flag().
|
| 45 |
*/
|
| 46 |
function flag_friend_preprocess_flag(&$vars) {
|
| 47 |
// this hook preprocesses ALL flag links, so make sure we have ours
|
| 48 |
if ($vars['flag']->name == 'friend') {
|
| 49 |
global $user;
|
| 50 |
|
| 51 |
// Determine what the status in the friend process is.
|
| 52 |
$status = flag_friend_determine_friend_status($vars['flag'], $user->uid, $vars['content_id']);
|
| 53 |
|
| 54 |
|
| 55 |
// Depending on the status, we need to manipulate the vars.
|
| 56 |
if ($status == FLAG_FRIEND_PENDING) {
|
| 57 |
$vars['link_text'] = t('Pending - Cancel?');
|
| 58 |
$vars['flag_name_css'] = 'pending friend';
|
| 59 |
$vars['link_href'] = str_replace('flag/confirm', 'flag-friend/confirm', $vars['link_href']);
|
| 60 |
// TODO: some js to detect pending links and handle accordingly
|
| 61 |
}
|
| 62 |
|
| 63 |
if ($status == FLAG_FRIEND_FLAGGED) {
|
| 64 |
// Make this link into a remove link with
|
| 65 |
$vars['action'] = 'unflag';
|
| 66 |
$vars['link_href'] = str_replace('flag/confirm/flag', 'flag-friend/confirm/unfriend', $vars['link_href']);
|
| 67 |
$vars['link_text'] = $vars['flag']->unflag_short;
|
| 68 |
}
|
| 69 |
|
| 70 |
if ($status == FLAG_FRIEND_APPROVAL) {
|
| 71 |
$vars['link_text'] = t('Approve');
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Implementation of hook_perm().
|
| 78 |
*/
|
| 79 |
function flag_friend_perm() {
|
| 80 |
return array('receive friend email notification');
|
| 81 |
}
|
| 82 |
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Implementation of hook_menu().
|
| 86 |
*/
|
| 87 |
function flag_friend_menu($may_cache) {
|
| 88 |
global $user;
|
| 89 |
$items = array();
|
| 90 |
|
| 91 |
if (!$may_cache) {
|
| 92 |
if ($user->uid) {
|
| 93 |
if (arg(0) == 'user' && is_numeric(arg(1))) {
|
| 94 |
// menu item to see message and respond
|
| 95 |
$items[] = array(
|
| 96 |
'path' => 'user/'. arg(1) .'/friends',
|
| 97 |
'callback' => 'flag_friend_page',
|
| 98 |
'callback arguments' => array(user_load(array('uid' => arg(1)))),
|
| 99 |
'access' => user_access('access content'),
|
| 100 |
'type' => MENU_CALLBACK,
|
| 101 |
);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
$items[] = array(
|
| 105 |
'path' => 'flag-friend/confirm',
|
| 106 |
'callback' => 'drupal_get_form',
|
| 107 |
'callback arguments' => array('flag_friend_unfriend_confirm'),
|
| 108 |
'access' => user_access('access content'),
|
| 109 |
'type' => MENU_CALLBACK,
|
| 110 |
);
|
| 111 |
|
| 112 |
// post means that there isn't a friend
|
| 113 |
// message already
|
| 114 |
$post = TRUE;
|
| 115 |
if (isset($_SESSION['messages']['notice'])) {
|
| 116 |
foreach ($_SESSION['messages']['notice'] as $message) {
|
| 117 |
if (strpos($message, 'pending friend') !== FALSE) {
|
| 118 |
$post = FALSE;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
if ($post) {
|
| 124 |
// grab the count of the pending friends and output them
|
| 125 |
$flag = flag_get_flag('friend');
|
| 126 |
$flags = flag_friend_get_flags($flag, $user->uid);
|
| 127 |
if ($count = count($flags)) {
|
| 128 |
drupal_set_message(l(t('You have !count.', array('!count' => format_plural($count, '1 pending friend request', '@count pending friend requests'))), 'user/' . $user->uid . '/friends'), 'notice');
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
return $items;
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Menu callback for displaying friends
|
| 138 |
*/
|
| 139 |
function flag_friend_page($account) {
|
| 140 |
global $user;
|
| 141 |
$output = '';
|
| 142 |
|
| 143 |
// make sure that only you can see your own pending friends
|
| 144 |
if ($user->uid == $account->uid) {
|
| 145 |
drupal_set_title(t('My friends'));
|
| 146 |
$flag = flag_get_flag('friend');
|
| 147 |
// display a list of any pending friend requests
|
| 148 |
$flags = flag_friend_get_flags($flag, $account->uid);
|
| 149 |
if (!empty($flags)) {
|
| 150 |
$output .= theme('flag_friend_pending_flags', $account, $flags);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
drupal_set_title(t("@name's friends", array('@name' => $account->name)));
|
| 155 |
}
|
| 156 |
|
| 157 |
// display a list of this user's friends
|
| 158 |
$friends = flag_friend_get_friends($account->uid);
|
| 159 |
$output .= theme('flag_friend_friend_list', $account, $friends);
|
| 160 |
|
| 161 |
return $output;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Callback function to retrieve pending friend flags for theming.
|
| 166 |
*/
|
| 167 |
function flag_friend_get_flags($flag, $content_id, $reset = NULL) {
|
| 168 |
static $flagged_content;
|
| 169 |
$uid = $content_id;
|
| 170 |
$content_type = $flag->content_type;
|
| 171 |
|
| 172 |
if (!isset($flagged_content[$uid][$content_type][$content_id]) || $reset) {
|
| 173 |
$flags = flag_get_flags($flag->content_type);
|
| 174 |
$flagged_content[$uid][$content_type][$content_id] = array();
|
| 175 |
// get flags with messages
|
| 176 |
$result = db_query("SELECT fc.*, ffm.message FROM {flag_content} fc LEFT JOIN {flag_friend_message} ffm ON ffm.fcid = fc.fcid WHERE content_type = '%s' AND content_id = %d", $content_type, $content_id);
|
| 177 |
while ($new_flag = db_fetch_object($result)) {
|
| 178 |
$fcid = flag_friend_get_fcid($flag, $content_id, $new_flag->uid);
|
| 179 |
$flagged_content[$uid][$content_type][$content_id][$fcid] = $new_flag;
|
| 180 |
$flagged_content[$uid][$content_type][$content_id][$fcid]->user = user_load(array('uid' => $new_flag->uid));
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
return $flagged_content[$uid][$content_type][$content_id];
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Callback function to retrieve a list of friends for the given user.
|
| 189 |
*/
|
| 190 |
function flag_friend_get_friends($uid, $reset = NULL) {
|
| 191 |
static $friends;
|
| 192 |
|
| 193 |
if (!isset($friends[$uid]) || $reset) {
|
| 194 |
$result = db_query("SELECT * FROM {flag_friend} WHERE uid = %d OR friend_uid = %d", $uid, $uid);
|
| 195 |
while ($friend = db_fetch_object($result)) {
|
| 196 |
// if the current user is in the uid column
|
| 197 |
if ($friend->uid == $uid) {
|
| 198 |
// load the friend_uid
|
| 199 |
$friends[$uid][$friend->friend_uid] = user_load(array('uid' => $friend->friend_uid));
|
| 200 |
}
|
| 201 |
else { // the current user is the friend_uid
|
| 202 |
// load the uid column as the friend
|
| 203 |
$friends[$uid][$friend->uid] = user_load(array('uid' => $friend->uid));
|
| 204 |
}
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
return $friends[$uid];
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Theme function for the list of pending flags.
|
| 213 |
*/
|
| 214 |
function theme_flag_friend_pending_flags($account, $flags) {
|
| 215 |
// $account is the user of the page we're looking at
|
| 216 |
// $flag->user is the user requesting the relationship
|
| 217 |
$output = '';
|
| 218 |
$output .= t('<h3>You have !friend_request.</h3>', array('!friend_request' => format_plural(count($flags), 'a friend request', count($flags) .' friend requests')));
|
| 219 |
$output .= '<dl id="requests" class="accordion">';
|
| 220 |
$count = count($flags);
|
| 221 |
$output .= '<dt>'. $count .' Pending Friend'. format_plural($count, '', 's') .'</dt>';
|
| 222 |
$output .= '<dd><ul>';
|
| 223 |
foreach ($flags as $flag) {
|
| 224 |
$output .= '<li><div class="user-list"><div class="user clear-block">';
|
| 225 |
$output .= '<div class="picture-name"><div class="picture">'. theme('user_picture', $flag->user) .'</div>';
|
| 226 |
$output .= '<span class="breakwords">'. theme('username', $flag->user) .'</span></div>';
|
| 227 |
$output .= '<div class="info">'. $flag->message .'<br />'. flag_create_link('friend', $flag->user->uid) . flag_friend_create_link('unfriend', $flag->user->uid) .'</div>';
|
| 228 |
$output .= '</div></div></li>';
|
| 229 |
}
|
| 230 |
$output .= '</ul></dd></dl>';
|
| 231 |
|
| 232 |
return $output;
|
| 233 |
}
|
| 234 |
|
| 235 |
/**
|
| 236 |
* Form for confirming the (un)flagging of a piece of content.
|
| 237 |
* NOTE: this duplicates flag.module's flag/confirm but not sure
|
| 238 |
* what else to do in order to circumvent the flagging
|
| 239 |
* process in the case of a friend removal where no flag
|
| 240 |
* is actually set.
|
| 241 |
*/
|
| 242 |
function flag_friend_unfriend_confirm($action = 'flag', $flag_name, $content_id = '') {
|
| 243 |
global $user;
|
| 244 |
|
| 245 |
$flag = flag_get_flag($flag_name);
|
| 246 |
$status = flag_friend_determine_friend_status($flag, $user->uid, $content_id);
|
| 247 |
$form = array();
|
| 248 |
|
| 249 |
$form['action'] = array(
|
| 250 |
'#type' => 'value',
|
| 251 |
'#value' => $action,
|
| 252 |
);
|
| 253 |
$form['flag_name'] = array(
|
| 254 |
'#type' => 'value',
|
| 255 |
'#value' => $flag_name,
|
| 256 |
);
|
| 257 |
$form['content_id'] = array(
|
| 258 |
'#type' => 'value',
|
| 259 |
'#value' => $content_id,
|
| 260 |
);
|
| 261 |
$form['status'] = array(
|
| 262 |
'#type' => 'value',
|
| 263 |
'#value' => $status,
|
| 264 |
);
|
| 265 |
$form['#theme'] = 'flag_friend_unfriend_form';
|
| 266 |
|
| 267 |
switch ($status) {
|
| 268 |
case FLAG_FRIEND_FLAGGED:
|
| 269 |
$question = t('Are you sure you want to remove this user from your list of friends?');
|
| 270 |
break;
|
| 271 |
case FLAG_FRIEND_APPROVAL:
|
| 272 |
$question = t('Are you sure you don\'t want to be friends with this user?');
|
| 273 |
break;
|
| 274 |
default:
|
| 275 |
$question = $flag->get_label('unflag_confirmation', $content_id);
|
| 276 |
break;
|
| 277 |
}
|
| 278 |
$path = isset($_GET['destination']) ? $_GET['destination'] : '<front>';
|
| 279 |
$yes = $flag->get_label('unflag_short', $content_id);
|
| 280 |
|
| 281 |
return confirm_form($form, $question, $path, '', $yes);
|
| 282 |
}
|
| 283 |
|
| 284 |
function flag_friend_unfriend_confirm_submit($form_id, $form_values) {
|
| 285 |
$action = $form_values['action'];
|
| 286 |
$flag_name = $form_values['flag_name'];
|
| 287 |
$content_id = $form_values['content_id'];
|
| 288 |
$status = $form_values['status'];
|
| 289 |
$flag = flag_get_flag($flag_name);
|
| 290 |
flag_friend_unfriend($action, $flag_name, $content_id, $status);
|
| 291 |
drupal_set_message($flag->get_label($action . '_message', $content_id));
|
| 292 |
}
|
| 293 |
|
| 294 |
/**
|
| 295 |
* Menu callback to either unflag yourself, or remove the relationship record.
|
| 296 |
*/
|
| 297 |
function flag_friend_unfriend($event, $flag_name, $content_id, $status) {
|
| 298 |
global $user;
|
| 299 |
|
| 300 |
// 'Denial' and 'Pending - Cancel?'
|
| 301 |
if ($event == 'unflag') {
|
| 302 |
$flag = flag_get_flag($flag_name);
|
| 303 |
if ($status == FLAG_FRIEND_APPROVAL) {
|
| 304 |
// Denial
|
| 305 |
// the content_id is actually the account param in this case
|
| 306 |
$account = user_load(array('uid' => $content_id));
|
| 307 |
|
| 308 |
// and the $user->uid is actually the content(_id) we're unflagging
|
| 309 |
$content_id = $user->uid;
|
| 310 |
}
|
| 311 |
// else we're cancelling out own flag
|
| 312 |
$flag->flag($event, $content_id, $account);
|
| 313 |
}
|
| 314 |
else { // event = unfriend
|
| 315 |
// remove the friend relationship
|
| 316 |
db_query('DELETE FROM {flag_friend} WHERE (uid = %d AND friend_uid = %d) OR (uid = %d AND friend_uid = %d)', $user->uid, $content_id, $content_id, $user->uid);
|
| 317 |
}
|
| 318 |
drupal_goto();
|
| 319 |
}
|
| 320 |
|
| 321 |
/**
|
| 322 |
* Implementation of hook_user().
|
| 323 |
*/
|
| 324 |
function flag_friend_user($op, &$edit, &$account, $category = NULL) {
|
| 325 |
switch ($op) {
|
| 326 |
case 'form':
|
| 327 |
// The user account edit form is about to be displayed. The module should present the form elements it wishes to inject into the form.
|
| 328 |
$form = array();
|
| 329 |
$form['friend_notification'] = array(
|
| 330 |
'#type' => 'select',
|
| 331 |
'#title' => t('I would like to be notified when someone wants to be friends with me'),
|
| 332 |
'#multiple' => FALSE,
|
| 333 |
'#options' => array(0 => 'Yes', -1 => 'No'),
|
| 334 |
'#default_value' => isset($account->friend_notification) ? $account->friend_notification : FLAG_FRIEND_NOTIFICATION,
|
| 335 |
'#weight' => -10,
|
| 336 |
);
|
| 337 |
return $form;
|
| 338 |
break;
|
| 339 |
}
|
| 340 |
}
|
| 341 |
|
| 342 |
/**
|
| 343 |
* Implementation of hook_user_link().
|
| 344 |
*/
|
| 345 |
function flag_friend_user_link($account) {
|
| 346 |
global $user;
|
| 347 |
|
| 348 |
// do not supply a link if the account and user are the same
|
| 349 |
if ($user->uid != $account->uid) {
|
| 350 |
$links = array();
|
| 351 |
$links['flag-friend'] = array(
|
| 352 |
'title' => flag_create_link('friend', $account->uid),
|
| 353 |
'html' => TRUE,
|
| 354 |
);
|
| 355 |
return $links;
|
| 356 |
}
|
| 357 |
}
|
| 358 |
|
| 359 |
/**
|
| 360 |
* Create a denial link.
|
| 361 |
*/
|
| 362 |
function flag_friend_create_link($type, $uid) {
|
| 363 |
if ($type == 'unfriend') {
|
| 364 |
$flag = flag_get_flag('friend');
|
| 365 |
$link = str_replace('Approve', 'Deny', str_replace('/flag/confirm', '/flag-friend/confirm', $flag->theme('unflag', $uid)));
|
| 366 |
return $link;
|
| 367 |
}
|
| 368 |
}
|
| 369 |
|
| 370 |
/**
|
| 371 |
* Theme function for the list of friends for the given user.
|
| 372 |
*/
|
| 373 |
function theme_flag_friend_friend_list($account, $friends) {
|
| 374 |
$output = '';
|
| 375 |
if (!empty($friends)) {
|
| 376 |
$output .= '<ul>';
|
| 377 |
foreach ($friends as $friend) {
|
| 378 |
$output .= '<li>';
|
| 379 |
$output .= theme('user_picture', $friend);
|
| 380 |
$output .= '</li>';
|
| 381 |
}
|
| 382 |
$output .= '</ul>';
|
| 383 |
}
|
| 384 |
else {
|
| 385 |
$output .= 'You have no friends, loser.';
|
| 386 |
}
|
| 387 |
return $output;
|
| 388 |
}
|
| 389 |
|
| 390 |
/**
|
| 391 |
* Determines the status of the friendship by testing various conditions.
|
| 392 |
*
|
| 393 |
* @param object $flag
|
| 394 |
* The flag object.
|
| 395 |
*
|
| 396 |
* @param int $uid1
|
| 397 |
* The account id of one of the users.
|
| 398 |
*
|
| 399 |
* @param int $uid2
|
| 400 |
* The account id of the other user.
|
| 401 |
*
|
| 402 |
* @return
|
| 403 |
* A string describing the status of the relationship.
|
| 404 |
*
|
| 405 |
* NOTE: this could possibly go into hook_flag_access? once available.
|
| 406 |
*/
|
| 407 |
function flag_friend_determine_friend_status($flag, $uid1, $uid2) {
|
| 408 |
static $status_cache = array();
|
| 409 |
// always keep these in the same order
|
| 410 |
if ($uid1 > $uid2) {
|
| 411 |
$key1 = $uid1;
|
| 412 |
$key2 = $uid2;
|
| 413 |
}
|
| 414 |
else {
|
| 415 |
$key1 = $uid2;
|
| 416 |
$key2 = $uid1;
|
| 417 |
}
|
| 418 |
|
| 419 |
if (!isset($status_cache[$key1][$key2])) {
|
| 420 |
$you_are_flagged = $flag->is_flagged($uid1, $uid2);
|
| 421 |
$they_are_flagged = $flag->is_flagged($uid2, $uid1);
|
| 422 |
$friends = db_result(db_query("SELECT * FROM {flag_friend} WHERE (uid = %d AND friend_uid = %d) OR (uid = %d AND friend_uid = %d)", $uid1, $uid2, $uid2, $uid1));
|
| 423 |
// see if these users have flagged eachother
|
| 424 |
if ($you_are_flagged && $they_are_flagged) {
|
| 425 |
$status_cache[$key1][$key2] = FLAG_FRIEND_BOTH;
|
| 426 |
}
|
| 427 |
else if ($friends) {
|
| 428 |
$status_cache[$key1][$key2] = FLAG_FRIEND_FLAGGED;
|
| 429 |
}
|
| 430 |
else if (!$you_are_flagged && !$they_are_flagged) {
|
| 431 |
$status_cache[$key1][$key2] = FLAG_FRIEND_UNFLAGGED;
|
| 432 |
}
|
| 433 |
else if ($you_are_flagged && !$they_are_flagged) {
|
| 434 |
$status_cache[$key1][$key2] = FLAG_FRIEND_APPROVAL;
|
| 435 |
}
|
| 436 |
else if (!$you_are_flagged && $they_are_flagged) {
|
| 437 |
$status_cache[$key1][$key2] = FLAG_FRIEND_PENDING;
|
| 438 |
}
|
| 439 |
}
|
| 440 |
return $status_cache[$key1][$key2];
|
| 441 |
}
|
| 442 |
|
| 443 |
function flag_friend_form_alter($form_id, &$form) {
|
| 444 |
if ($form_id == 'flag_confirm' && $form['flag_name']['#value'] == 'friend') {
|
| 445 |
$action = $form['action']['#value'];
|
| 446 |
$flag = flag_get_flag('friend');
|
| 447 |
$content_id = $form['content_id']['#value'];
|
| 448 |
$token = $_REQUEST['token'];
|
| 449 |
|
| 450 |
switch ($action) {
|
| 451 |
case 'flag':
|
| 452 |
$flag_form = flag_friend_message_form($action, $flag, $content_id, $token);
|
| 453 |
$form = array_merge($flag_form, $form);
|
| 454 |
unset($form['actions']['submit']);
|
| 455 |
unset($form['actions']['cancel']);
|
| 456 |
$form['#submit']['flag_friend_form_submit'] = array();
|
| 457 |
break;
|
| 458 |
case 'unflag':
|
| 459 |
$unflag_form = flag_friend_unfriend_form($action, $flag, $content_id, $token);
|
| 460 |
$form = array_merge($form, $unflag_form);
|
| 461 |
$form['#submit']['flag_friend_form_submit'] = array();
|
| 462 |
break;
|
| 463 |
}
|
| 464 |
}
|
| 465 |
}
|
| 466 |
|
| 467 |
/**
|
| 468 |
* Form to send a message to a user before friend flagging.
|
| 469 |
*/
|
| 470 |
function flag_friend_message_form($action, $flag, $content_id, $token) {
|
| 471 |
$form['current'] = array('#type' => 'value', '#value' => func_get_args());
|
| 472 |
|
| 473 |
$form['flag_friend_message'] = array(
|
| 474 |
'#type' => 'textarea',
|
| 475 |
'#title' => t('Friend message (optional)'),
|
| 476 |
'#description' => t('Enter a message to send to this user.'),
|
| 477 |
'#cols' => 60,
|
| 478 |
'#rows' => 5,
|
| 479 |
);
|
| 480 |
$form['flag_friend_submit'] = array(
|
| 481 |
'#type' => 'submit',
|
| 482 |
'#value' => t('Send'),
|
| 483 |
'#suffix' => l('Cancel', $_GET['destination']),
|
| 484 |
);
|
| 485 |
$form['#theme'] = 'flag_friend_message_form';
|
| 486 |
return $form;
|
| 487 |
}
|
| 488 |
|
| 489 |
/**
|
| 490 |
* Form to confirm an unfriend flagging.
|
| 491 |
*/
|
| 492 |
function flag_friend_unfriend_form($action, $flag, $content_id, $token) {
|
| 493 |
$form['current'] = array('#type' => 'value', '#value' => func_get_args());
|
| 494 |
$question = t('Are you sure you want to !action?', array('!action' => $action));
|
| 495 |
$path = $_REQUEST['destination'];
|
| 496 |
$form = confirm_form($form, $question, $path);
|
| 497 |
$form['#redirect'] = 'flag/confirm/'. $action .'/'. $flag->name .'/'. $content_id .'/'. $token .'?'. drupal_get_destination();
|
| 498 |
$form['#theme'] = 'flag_friend_unfriend_form';
|
| 499 |
return $form;
|
| 500 |
}
|
| 501 |
|
| 502 |
/**
|
| 503 |
* Submit handler for message_form() and unfriend_form().
|
| 504 |
*/
|
| 505 |
function flag_friend_form_submit($form_id, $form_values) {
|
| 506 |
global $user;
|
| 507 |
$action = $form_values['current'][0];
|
| 508 |
$flag = $form_values['current'][1];
|
| 509 |
$content_id = $form_values['current'][2];
|
| 510 |
$account = $user;
|
| 511 |
$token = $form_values['current'][3];
|
| 512 |
|
| 513 |
if ($form_values['flag_friend_message']) {
|
| 514 |
$flag->friend_message = $form_values['flag_friend_message'];
|
| 515 |
}
|
| 516 |
|
| 517 |
flag_friend_message($action, $flag, $content_id, $account->uid);
|
| 518 |
$status = flag_friend_determine_friend_status($flag, $account->uid, $content_id);
|
| 519 |
flag_friend_message_email($status, $flag, $content_id, $account);
|
| 520 |
}
|
| 521 |
|
| 522 |
/**
|
| 523 |
* API callback function to update our new field.
|
| 524 |
*/
|
| 525 |
function flag_friend_message($action, $flag, $content_id, $account_uid) {
|
| 526 |
// see if the flag has an fcid
|
| 527 |
if (!isset($flag->fcid)) {
|
| 528 |
$flag->fcid = flag_friend_get_fcid($flag, $content_id, $account_uid);
|
| 529 |
}
|
| 530 |
if ($action == 'flag' && $flag->friend_message) {
|
| 531 |
db_query("INSERT INTO {flag_friend_message} VALUES(%d, '%s')", $flag->fcid, $flag->friend_message);
|
| 532 |
}
|
| 533 |
else if ($action == 'unflag') {
|
| 534 |
db_query("DELETE FROM {flag_friend_message} WHERE fcid = %d", $flag->fcid);
|
| 535 |
}
|
| 536 |
}
|
| 537 |
|
| 538 |
function flag_friend_message_email($status, $flag, $recipient_uid, $sender) {
|
| 539 |
$recipient = user_load(array('uid' => $recipient_uid));
|
| 540 |
// if the user can receive notifications
|
| 541 |
if (user_access('receive friend email notification')) {
|
| 542 |
// and they've expressed they want them
|
| 543 |
if ((isset($recipient->friend_notification) && $recipient->friend_notification !== -1) || !isset($recipient->friend_notification)) {
|
| 544 |
$email = theme('flag_friend_message_email', $status, $flag, $recipient, $sender);
|
| 545 |
if (isset($email['body'])) {
|
| 546 |
if (function_exists('messaging_message_send_user')) {
|
| 547 |
messaging_message_send_user($recipient, $email, NULL, 1);
|
| 548 |
}
|
| 549 |
else {
|
| 550 |
drupal_mail($email['type'], $recipient->mail, $email['subject'], $email['body']);
|
| 551 |
}
|
| 552 |
}
|
| 553 |
}
|
| 554 |
}
|
| 555 |
}
|
| 556 |
|
| 557 |
/**
|
| 558 |
* Theme the outgoing email message.
|
| 559 |
*
|
| 560 |
* @param string $status
|
| 561 |
* Status of the friendship.
|
| 562 |
*
|
| 563 |
* @param object $flag
|
| 564 |
* The flag object.
|
| 565 |
*
|
| 566 |
* @param object $recipient
|
| 567 |
* The user object of the person receiving the email.
|
| 568 |
*
|
| 569 |
* @param object $sender
|
| 570 |
* The user object of the person sending the email.
|
| 571 |
*
|
| 572 |
* @return
|
| 573 |
* An array containing the email [type] (mailkey), [subject] and [body].
|
| 574 |
*/
|
| 575 |
function theme_flag_friend_message_email($status, $flag, $recipient, $sender) {
|
| 576 |
$email = array();
|
| 577 |
$email['type'] = 'flag-friend';
|
| 578 |
|
| 579 |
switch ($status) {
|
| 580 |
case FLAG_FRIEND_BOTH:
|
| 581 |
// sender confirmed you as a friend
|
| 582 |
$email['subject'] = t('!username confirmed you as a friend !site', array(
|
| 583 |
'!username' => $sender->name,
|
| 584 |
'!site' => 'on '. variable_get('site_name', ''),
|
| 585 |
));
|
| 586 |
$email['body'] = t('!firstname confirmed you as a friend on !site.
|
| 587 |
|
| 588 |
To view !firstname\'s profile, follow this link,
|
| 589 |
!link
|
| 590 |
|
| 591 |
!message
|
| 592 |
|
| 593 |
Thanks,
|
| 594 |
|
| 595 |
The !site Team', array(
|
| 596 |
'!firstname' => $sender->firstname ? $sender->firstname : $sender->name,
|
| 597 |
'!site' => variable_get('site_name', ''),
|
| 598 |
'!message' => $flag->friend_message ? 'Message:
|
| 599 |
'. $flag->friend_message : '',
|
| 600 |
'!link' => url('user/'. $sender->uid, NULL, NULL, TRUE),
|
| 601 |
));
|
| 602 |
break;
|
| 603 |
|
| 604 |
case FLAG_FRIEND_PENDING:
|
| 605 |
// sender added you as a friend
|
| 606 |
$email['subject'] = t('!username added you as a friend !site', array('!username' => $sender->name, '!site' => 'on '. variable_get('site_name', '')));
|
| 607 |
$email['body'] = t('!firstname added you as a friend on !site. We need to confirm that you know !firstname in order for you to be friends on !site.
|
| 608 |
|
| 609 |
To confirm this friend request, follow the link below:
|
| 610 |
!link
|
| 611 |
|
| 612 |
!message
|
| 613 |
|
| 614 |
Thanks,
|
| 615 |
The !site Team', array(
|
| 616 |
'!firstname' => $sender->firstname ? $sender->firstname : $sender->name,
|
| 617 |
'!site' => variable_get('site_name', ''),
|
| 618 |
'!message' => $flag->friend_message ? 'Message:
|
| 619 |
'. $flag->friend_message : '',
|
| 620 |
'!link' => url('user/'. $recipient->uid .'/friends', NULL, NULL, TRUE),
|
| 621 |
));
|
| 622 |
break;
|
| 623 |
}
|
| 624 |
return $email;
|
| 625 |
}
|
| 626 |
|
| 627 |
/**
|
| 628 |
* Retrieves the fcid of a flag.
|
| 629 |
*
|
| 630 |
* NOTE: hopefully fcid will be passed into the hook_flag() at some point
|
| 631 |
* at which time will render this function unnecessary
|
| 632 |
*/
|
| 633 |
function flag_friend_get_fcid($flag, $content_id, $account_uid) {
|
| 634 |
return db_result(db_query("SELECT fcid FROM {flag_content} WHERE fid = %d AND content_type = '%s' AND content_id = %d AND uid = %d", $flag->fid, $flag->content_type, $content_id, $account_uid));
|
| 635 |
}
|
| 636 |
|
| 637 |
/**
|
| 638 |
* Retrieve our flag's message.
|
| 639 |
*/
|
| 640 |
function flag_friend_get_message($fcid) {
|
| 641 |
$flag_friend = FALSE;
|
| 642 |
$result = db_result(db_query("SELECT message FROM {flag_friend_message} WHERE fcid = %d", $fcid));
|
| 643 |
if ($result) {
|
| 644 |
$flag_friend = $result;
|
| 645 |
}
|
| 646 |
return $flag_friend;
|
| 647 |
}
|
| 648 |
|
| 649 |
/**
|
| 650 |
* Theme function for the message form.
|
| 651 |
*/
|
| 652 |
function theme_flag_friend_message_form($form) {
|
| 653 |
drupal_set_title('Send a friend request.');
|
| 654 |
return drupal_render($form);
|
| 655 |
}
|
| 656 |
|
| 657 |
/**
|
| 658 |
* Theme function for the unfriending action.
|
| 659 |
*/
|
| 660 |
function theme_flag_friend_unfriend_form($form) {
|
| 661 |
return drupal_render($form);
|
| 662 |
}
|
| 663 |
|
| 664 |
/**
|
| 665 |
* Implementation of hook_flag_default_flags().
|
| 666 |
*/
|
| 667 |
function flag_friend_flag_default_flags() {
|
| 668 |
$flags = array();
|
| 669 |
$flags[] = array(
|
| 670 |
'content_type' => 'user',
|
| 671 |
'name' => 'friend',
|
| 672 |
'title' => 'Friend',
|
| 673 |
'roles' => array(
|
| 674 |
0 => '2',
|
| 675 |
),
|
| 676 |
'global' => FALSE,
|
| 677 |
'flag_short' => 'Add friend',
|
| 678 |
'flag_long' => 'Add this user to your list of friends.',
|
| 679 |
'flag_confirmation' => 'Are you sure you want to add this user to your list of friends?',
|
| 680 |
'unflag_short' => 'Remove friend',
|
| 681 |
'unflag_long' => 'Remove this user from your list of friends.',
|
| 682 |
'unflag_confirmation' => 'Are you sure you want to cancel your pending friend request?',
|
| 683 |
'status' => FALSE,
|
| 684 |
'link_type' => 'confirm',
|
| 685 |
'locked' => array('name', 'global', 'link_type'),
|
| 686 |
);
|
| 687 |
return $flags;
|
| 688 |
}
|