diff --git a/flag_friend.info b/flag_friend.info index 56f966b..a194bad 100644 --- a/flag_friend.info +++ b/flag_friend.info @@ -9,8 +9,12 @@ files[] = flag_friend.test files[] = includes/flag_friend.views.inc files[] = includes/flag_friend.views_default.inc files[] = includes/flag_friend_handler_argument_apachesolr_friend.inc -files[] = includes/flag_friend_handler_argument_numeric.inc -files[] = includes/flag_friend_handler_field_links.inc +files[] = includes/flag_friend_handler_argument_numeric.incĀ¬ +files[] = includes/flag_friend_handler_field_links.incĀ¬ +files[] = includes/flag_friend_views_handler_field_links.inc +files[] = includes/flag_friend_views_handler_field_status.inc +files[] = includes/flag_friend_views_handler_filter_status.inc +files[] = includes/flag_friend_plugin_access_pending_requested.inc files[] = access/flag_friend_is_friend.inc files[] = content_types/flag_friend_count.inc files[] = content_types/flag_friend_links.inc diff --git a/flag_friend.install b/flag_friend.install index 8800c71..ecf6f46 100644 --- a/flag_friend.install +++ b/flag_friend.install @@ -14,6 +14,8 @@ function flag_friend_install() { /** * Implements hook_uninstall(). + * + * @TODO: We should consider removing all friend flags when uninstalling... */ function flag_friend_uninstall() { // Remove default flag if it's enabled. @@ -29,13 +31,13 @@ function flag_friend_uninstall() { function flag_friend_schema() { $schema['flag_friend'] = array( 'fields' => array( - 'uid' => array( + 'fcid' => array( + 'description' => 'The unique ID for this particular flag.', 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, - 'disp-width' => '10' ), - 'friend_uid' => array( + 'status' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, @@ -47,10 +49,7 @@ function flag_friend_schema() { 'disp-width' => '11' ) ), - 'primary key' => array('uid', 'friend_uid'), - 'indexes' => array( - 'friend_uid' => array('friend_uid'), - ), + 'primary key' => array('fcid'), ); $schema['flag_friend_message'] = array( @@ -110,3 +109,92 @@ function flag_friend_update_7001() { db_query("DELETE ff FROM {flag_friend} ff WHERE ff.uid = :uid AND ff.friend_uid = :friend_uid", array(':uid' => $uid, ':friend_uid' => $friend_uid)); } } + +/** + * Change the execution weight of the module. + */ +function flag_friend_update_7002() { + db_update('system') + ->fields(array('weight' => -10)) + ->condition('type', 'module') + ->condition('name', 'flag_friend') + ->execute(); +} + +/** + * Update the flag_friend table to assume flags persist. + */ +function flag_friend_update_7003() { + + // Rename the the flag_friend table to flag_friend_old. + db_rename_table('flag_friend', 'flag_friend_old'); + + // Create the new flag_friend table. + $schema = flag_friend_schema(); + db_create_table('flag_friend', $schema['flag_friend']); +} + +/** + * Record friend status based on existing flags. + */ +function flag_friend_update_7004() { + // Get the friend flag. + $flag = flag_get_flag('friend'); + + // Query the database directly, as there's no API call in Flag 2.x. + $result = db_select('flag_content', 'fc') + ->fields('fc', array('fcid', 'content_id', 'uid', 'timestamp')) + ->condition('fid', $flag->fid) + ->execute(); + + // Insert all pending relationships into the table. + foreach ($result as $row) { + db_insert('flag_friend') + ->fields(array( + 'fcid' => $row->fcid, + 'status' => FLAG_FRIEND_PENDING, + 'created' => $row->timestamp, + )) + ->execute(); + } +} + +/** + * Re-flag friended user accounts. + */ +function flag_friend_update_7005(&$sandbox) { + $max_entries_per_round = 100; + + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['max'] = db_query('SELECT COUNT(*) FROM flag_friend_old')->fetchField(); + } + + // Query 100 items from the flag_friend_old table. + $result = db_select('flag_friend_old', 'ffo') + ->fields('ffo', array('uid', 'friend_uid', 'created')) + ->range(0, $max_entries_per_round) + ->execute(); + + // Reflag friended users. + foreach ($result as $row) { + flag('flag', 'friend', $row->friend_uid, user_load($row->uid)); + flag('flag', 'friend', $row->uid, user_load($row->friend_uid)); + db_delete('flag_friend_old') + ->condition('uid', $row->uid) + ->condition('friend_uid', $row->friend_uid) + ->execute(); + $sandbox['progress']++; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); + + if ($sandbox['#finished'] >= 1) { + // Delete the old friend table. + db_drop_table('flag_friend_old'); + + // Finally clear the cache. + cache_clear_all(); + } +} + diff --git a/flag_friend.module b/flag_friend.module index 092fa8a..e0a9ece 100644 --- a/flag_friend.module +++ b/flag_friend.module @@ -6,67 +6,231 @@ */ // define our statuses -define('FLAG_FRIEND_BOTH', 0); -define('FLAG_FRIEND_FLAGGED', 1); -define('FLAG_FRIEND_UNFLAGGED', 2); -define('FLAG_FRIEND_APPROVAL', 3); -define('FLAG_FRIEND_PENDING', 4); +define('FLAG_FRIEND_BOTH', 0); //Users are friends +define('FLAG_FRIEND_FLAGGED', 1); //Users are friends +define('FLAG_FRIEND_UNFLAGGED', 2); //Neither user is flagged as a friend +define('FLAG_FRIEND_APPROVAL', 3); //User has been flagged, pending their approval +define('FLAG_FRIEND_PENDING', 4); //1 user has flagged the other user /** - * Implements hook_flag(). + * Implements hook_views_access(). */ -function flag_friend_flag($event, $flag, $content_id, $account) { +function flag_friend_views_access() { + global $user; + if ($user->uid == arg(1)) { + return TRUE; + } + else { + return FALSE; + } +} + +/** + * Implements hook_menu_alter. + * + * Todo: Figure out why we have this. + */ +function flag_friend_menu_alter(&$items) { + $items['flag/%/%flag/%']['page callback'] = 'flag_friend_page'; +} + +/** + * Alters the friend flagging confirmation page. + * + * @param $action + * A string of the action to perform. + * @param $flag + * The flag object. + * @param $content_id + * The entity ID being flagged. + */ +function flag_friend_page ($action, $flag, $content_id) { + if ($action != 'unflag') { + //we only want to alter unflagging + flag_page($action, $flag, $content_id); + } else { + global $user; + $fcid = db_query("SELECT fcid FROM {flag_content} WHERE content_id = :content_id AND uid = :uid", array(':content_id' => $content_id, ':uid' => $user->uid))->fetchField(); + if ($fcid) { + //if we are unflagging a reciprocal relationship, proceed as usual with the flag module + flag_page($action, $flag, $content_id); + } else { + //if we are acting on a deny link, we need the FID and original flagging user account + //to pass to into hook_flag + $fcid = db_query("SELECT fcid FROM {flag_content} WHERE content_id = :content_id AND uid = :uid", array(':content_id' => $user->uid, ':uid' => $content_id))->fetchField(); + $account = user_load($content_id); + flag_friend_flag('unflag', $flag, $user->uid, $account, $fcid, $_REQUEST['token']); + } + } +} + +/** + * Implements hook_flag() + * + * We've added a $token param which is passed in if the function is called from flag_friend_page, + * our override for flag_page in case a deny function is clicked. + * + * @see flag_friend_flag_flag(), flag_friend_flag_unflag() + * + */ +function flag_friend_flag($op, $flag, $content_id, $account, $fcid, $token = NULL) { if ($flag->name == 'friend') { - if ($event == 'flag') { - - // See the status of the friendship. - $status = flag_friend_determine_friend_status($flag, $account->uid, $content_id, TRUE); - $friend_account = user_load($content_id); - - // If both are now flagged, we record the relationship and remove the flags. - if ($status === FLAG_FRIEND_BOTH) { - // Ensure no duplicates are made. - $exists = db_query("SELECT COUNT(1) FROM {flag_friend} WHERE (uid = :uid AND friend_uid = :friend_uid) OR (uid = :friend_uid AND friend_uid = :uid)", array(':uid' => $account->uid, ':friend_uid' => $content_id))->fetchField(); - if (!$exists) { - $id = db_insert('flag_friend') - ->fields(array( - 'uid' => $account->uid, - 'friend_uid' => $content_id, - 'created' => $_SERVER['REQUEST_TIME'], - )) - ->execute(); - } + if ($op == 'flag') { + flag_friend_flag_flag($flag, $content_id, $account, $fcid, $token); + } + elseif ($op == 'unflag') { + flag_friend_flag_unflag($flag, $content_id, $account, $fcid, $token); + } + } +} - // Remove any message entries for either user. - flag_friend_message('unflag', $flag, $account->uid, $content_id); - flag_friend_message('unflag', $flag, $content_id, $account->uid); +/** + * Flags a user as a friend. + * + * @param $flag + * The flag object. + * @param $content_id + * The ID of the user to flag. + * @param $account + * @param $fcid + * @param null $token + */ +function flag_friend_flag_flag($flag, $content_id, $account, $fcid, $token = NULL) { + // See the status of the friendship. + $status = flag_friend_determine_friend_status($flag, $content_id, $account->uid, TRUE); + $friend_account = user_load($content_id); + + //FLAG_FRIEND_UNFLAGGED -- need to add a pending relationship + if ($status === FLAG_FRIEND_UNFLAGGED) { + // Insert new entry into the flag friend table. + $result = db_insert('flag_friend') + ->fields(array( + 'fcid' => $fcid, + 'status' => FLAG_FRIEND_PENDING, + 'created' => REQUEST_TIME,)) + ->execute(); + + // Invoke rules for the new friend relationship. + if (function_exists('rules_invoke_event_by_args')) { + rules_invoke_event_by_args('flag_friend_request', array( + 'receiving_user' => $content_id, + 'requesting_user' => $account, + )); + } - // Then remove the flags. - $flag->flag('unflag', $content_id, $account); - $flag->flag('unflag', $account->uid, $friend_account); + // Invoke triggers. + module_invoke_all('flag_friend', 'request', $content_id, $account, $flag); + } + elseif ($status === FLAG_FRIEND_APPROVAL) { + // FLAG_FRIEND_APPROVAL -- Need to approve an existing friendship. + // User B has already flagged the current user A. Now A is approving the + // relationship by flagging B back. + + // Insert the current user's flagging into the flag friend table. + $result = db_insert('flag_friend') + ->fields(array( + 'fcid' => $fcid, + 'status' => FLAG_FRIEND_FLAGGED, + 'created' => REQUEST_TIME, + ))->execute(); + + // Query the database for user B's flagging. + $fcid2 = db_query("SELECT fcid FROM {flag_content} WHERE content_id = :account AND uid = :content_id", array(':account' => $account->uid, ':content_id' => $content_id))->fetchField(); + + // Update user B's flagging in the flag friend table. + $query = db_update('flag_friend') + ->fields(array('status' => FLAG_FRIEND_FLAGGED)) + ->condition(db_and() + ->condition('fcid', $fcid2) + ->condition('status', FLAG_FRIEND_PENDING)) + ->execute(); + + // Remove any message entries for either user. + flag_friend_message('unflag', $flag, $account->uid, $content_id); + flag_friend_message('unflag', $flag, $content_id, $account->uid); + + // Invoke rules for the approved relationship. + if (function_exists('rules_invoke_event_by_args')) { + rules_invoke_event_by_args('flag_friend_approve', array( + 'approving_user' => $account, + 'requesting_user' => $content_id, + )); + } + } - // fire trigger - module_invoke_all('flag_friend', 'approve', $friend_account, $account, $flag); + // fire btrigger + module_invoke_all('flag_friend', 'approve', $content_id, $account, $flag); +} - if (function_exists('rules_invoke_event_by_args')) { - rules_invoke_event_by_args('flag_friend_approve', array( - 'approving_user' => $account, - 'requesting_user' => $friend_account, - )); - } +function flag_friend_flag_unflag($flag, $content_id, $account, $fcid, $token = NULL) { + // Get the friend status. + $status = flag_friend_determine_friend_status($flag, $content_id, $account->uid, TRUE); + + $fids = array($fcid); + $fcid2 = flag_friend_get_flags($flag, $content_id, $account->uid, $reset = NULL); + if (!empty($fcid2)) { + $fids[] = $fcid2[0]->fcid; + + //we need to delete the reciprocal flag in the flag_content table. + //This should be done via the $flag object to ensure the hooks are fired + //but it didn't seem to work + $friend_delete = db_delete('flag_content') + ->condition('fcid', $fcid2[0]->fcid) + ->execute(); + } + $num_deleted = db_delete('flag_friend') + ->condition('fcid', $fids, 'IN') + ->execute(); + + //if this function is being called by flag_friend_page, our deny link + //we need to mimic the flag module behaviour. This is taken from the + //flag module flag_page function + if ($token) { + global $user; + // Shorten up the variables that affect the behavior of this page. + $js = isset($_REQUEST['js']); + + // Specifically $_GET to avoid getting the $_COOKIE variable by the same key. + $has_js = isset($_GET['has_js']); + + // Check the flag token, then perform the flagging. + // This is different from flag module in that we are passing the account uid + // as the content_id. This is because the situation is reversed since the user + // acting on the flag is the actual content_id and not the owner of the flag + if (!flag_check_token($token, $account->uid)) { + $error = t('Bad token. You seem to have followed an invalid link.') . ' ' . $token; + } + elseif ($user->uid == 0 && !$has_js) { + $error = t('You must have JavaScript and cookies enabled in your browser to flag content.'); + } + // If an error was received, set a message and exit. + if (isset($error)) { + if ($js) { + drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8'); + print drupal_json_encode(array( + 'status' => FALSE, + 'errorMessage' => $error, + )); + drupal_exit(); } else { - // fire trigger - module_invoke_all('flag_friend', 'request', $friend_account, $account, $flag); - - if (function_exists('rules_invoke_event_by_args')) { - rules_invoke_event_by_args('flag_friend_request', array( - 'receiving_user' => $friend_account, - 'requesting_user' => $account, - )); - } + drupal_set_message($error); + drupal_access_denied(); + return; } } + + // If successful, return data according to the request type. + if ($js) { + drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8'); + $flag->link_type = 'toggle'; + print drupal_json_encode(flag_build_javascript_info($flag, $content_id)); + drupal_exit(); + } + else { + drupal_set_message($flag->get_label('unflag_message', $content_id)); + drupal_goto(); + } } } @@ -82,6 +246,9 @@ function flag_friend_init() { /** * Implements hook_preprocess_flag(). + * + * We preprocess the flag for two reasons: To modify the text of the flagging, + * and to change the link URL for removing a friend flagging. */ function flag_friend_preprocess_flag(&$vars) { // this hook preprocesses ALL flag links, so make sure we have ours @@ -89,8 +256,10 @@ function flag_friend_preprocess_flag(&$vars) { global $user; // Determine what the status in the friend process is. - $status = flag_friend_determine_friend_status($vars['flag'], $user->uid, $vars['content_id']); + $status = flag_friend_determine_friend_status($vars['flag'], $vars['content_id'], $user->uid); + switch ($status) { + // Relationship has yet to be approved, change link text. case FLAG_FRIEND_PENDING: $vars['link_text'] = t('Friend Requested. Cancel?'); break; @@ -135,6 +304,8 @@ function flag_friend_views_api() { /** * Implements hook_form_alter(). + * + * Todo: Figure out why we have this. */ function flag_friend_form_flag_form_alter(&$form, &$form_state, $form_id) { if ($form['#flag']->name == 'friend') { @@ -182,40 +353,83 @@ function flag_friend_rules_event_info() { } /** - * Retrieve pending friend flags. + * Function to get the fcids for a given user / content id * * @param $flag * The flag object. * @param $content_id - * The content we're operating on. - * @param $reset - * Boolean trigger to reset the static cache. + * The account id of the user being acted on. + * @param $uid + * The account id of the logged in user. * @return - * Array of pending friend flags. + * An array containing information for each flag content id found */ -function flag_friend_get_flags($flag, $content_id, $reset = NULL) { - static $flagged_content; - $uid = $content_id; - $content_type = $flag->content_type; - - if (!isset($flagged_content[$uid][$content_type][$content_id]) || $reset) { - $flags = flag_get_flags($flag->content_type); - $flagged_content[$uid][$content_type][$content_id] = array(); - - // get flags with messages - $results = db_query("SELECT fc.*, ffm.message FROM {flag_content} fc LEFT JOIN {flag_friend_message} ffm ON ffm.fcid = fc.fcid WHERE fc.fid = :fc.fid AND fc.content_type = :fc.content_type AND fc.content_id = :fc.content_id", array(':fc.fid' => $flag->fid, ':fc.content_type' => $content_type, ':fc.content_id' => $content_id))->fetchAll(); - foreach ($result as $new_flag) { - $fcid = flag_friend_get_fcid($flag, $content_id, $new_flag->uid); - $flagged_content[$uid][$content_type][$content_id][$fcid] = $new_flag; - $flagged_content[$uid][$content_type][$content_id][$fcid]->user = user_load($new_flag->uid); + +function flag_friend_get_flags($flag, $content_id, $uid, $reset = FALSE) { + //TODO: Need to come back to the static cache +/* $records = &drupal_static(__FUNCTION__, array(), $reset); + if (!empty($records)) { + return $records; + }*/ + + $query = db_select('flag_content', 'fc'); + $query->join('flag_friend', 'ff', 'ff.fcid = fc.fcid'); + $query + ->fields('fc', array('fcid', 'content_id', 'uid')) + ->fields('ff', array('status')) + ->condition(db_or() + ->condition(db_and() + ->condition('content_id', $content_id) + ->condition('fc.uid', $uid)) + ->condition(db_and() + ->condition('content_id', $uid) + ->condition('fc.uid', $content_id))); + $result = $query->execute(); + + $records = array(); + + foreach ($result as $record) { + $records[] = $record; + } + return $records; +} + +/** + * Retrieve a list of friends for the given user. + * + * @param $uid + * The user id. + * @return + * Array of UIDs that are friends of the given user. + */ +function flag_friend_get_friends_query($uid) { + $query = db_select('flag_content', 'fc'); + $query->join('flag_friend', 'ff', 'ff.fcid = fc.fcid'); + $query->condition(db_or() + ->condition('fc.uid', $uid) + ->condition('fc.content_id', $uid)) + ->condition('ff.status', FLAG_FRIEND_FLAGGED) + ->fields('fc', array('uid', 'content_id')); + $results = $query->execute(); + + $friends = array(); + + foreach ($results as $friend) { + //we'll get two results for each friendship. Need to make sure we only assign one to + //the return array + if ($friend->uid == $uid) { + $friends[$friend->content_id] = $friend->content_id; + } + elseif ($friend->content_id == $uid) { + $friends[$friend->uid] = $friend->uid; } } - return $flagged_content[$uid][$content_type][$content_id]; + return $friends; } /** - * Retrieve a list of friends for the given user. + * Retrieve each loaded friend for the given user. * * @param $uid * The user id. @@ -225,25 +439,16 @@ function flag_friend_get_flags($flag, $content_id, $reset = NULL) { * Array of user objects. */ function flag_friend_get_friends($uid, $reset = NULL) { - static $friends; + static $cache = array(); - if (!isset($friends[$uid]) || $reset) { - $friends[$uid] = array(); - $results = db_query("SELECT * FROM {flag_friend} WHERE uid = :uid OR friend_uid = :friend_uid", array(':uid' => $uid, ':friend_uid' => $uid))->fetchAll(); - foreach ($results as $friend) { - // if the current user is in the uid column - if ($friend->uid == $uid) { - // load the friend_uid - $friends[$uid][$friend->friend_uid] = user_load($friend->friend_uid); - } - else { // the current user is the friend_uid - // load the uid column as the friend - $friends[$uid][$friend->uid] = user_load($friend->uid); - } + if (!isset($cache[$uid]) || $reset) { + $cache[$uid] = array(); + $friends = flag_friend_get_friends_query($uid); + foreach ($friends as $friend) { + $cache[$uid][$friend] = (array) user_load($friend); } } - - return $friends[$uid]; + return $cache[$uid]; } /** @@ -259,16 +464,19 @@ function flag_friend_get_friends($uid, $reset = NULL) { function flag_friend_get_friend_count($uid, $reset = NULL) { static $friend_count; - if (!isset($friend_count[$uid]) || $reset) { - $sql = "SELECT COUNT(1) FROM {flag_friend} WHERE uid = %d OR friend_uid = %d"; - $friend_count[$uid] = db_query("SELECT COUNT(1) FROM {flag_friend} WHERE uid = :uid OR friend_uid = :uid", array(':uid' => $uid, ':friend_uid' => $uid))->fetchField(); + if (!isset($friends[$uid]) || $reset) { + $friends = flag_friend_get_friends_query($uid); + $friend_count = count($friends); } - return $friend_count[$uid]; + return $friend_count; } /** * Implements hook_form_alter(). + * + * Change the user registration and user profile form to include + * setting for flag friend. */ function flag_friend_form_alter(&$form, &$form_state, $form_id) { if (($form_id == 'user_register_form' || $form_id == 'user_profile_form') && $form['#user_category'] == 'account') { @@ -315,6 +523,8 @@ function flag_friend_account_fieldset_remove_if_empty($element) { /** * Implements hook_user_presave(). + * + * Stash the flag friend settings in the user account data array. */ function flag_friend_user_presave(&$edit, $account, $category) { if (isset($edit['flag_friend_request_notify'])) { @@ -326,16 +536,18 @@ function flag_friend_user_presave(&$edit, $account, $category) { } /** - * Implements hook_user_cancel(). + * Implements hook_user_delete() + * + * Delete all the user's friend flaggings when deleting their account. */ -function flag_friend_user_cancel($edit, $account, $method) { - // remove any friend relationships if an account is removed +function flag_friend_user_delete($account) { + $result = db_query("SELECT * FROM {flag_content} WHERE uid = :uid OR content_id = :uid", array(':uid' => $account->uid)); + $fcids = array(); + foreach ($result as $record) { + $fcids[] = $record->fcid; + } db_delete('flag_friend') - ->condition( - db_or() - ->condition('uid', $account->uid) - ->condition('friend_uid', $account->uid) - ) + ->condition('fcid', $fcids, 'IN') ->execute(); } @@ -349,7 +561,7 @@ function flag_friend_user_view($account, $view_mode) { $account->content['flags'][$flag->name]['#access'] = FALSE; } else { - $status = flag_friend_determine_friend_status($flag, $GLOBALS['user']->uid, $account->uid); + $status = flag_friend_determine_friend_status($flag, $account->uid, $GLOBALS['user']->uid); if ($status == FLAG_FRIEND_APPROVAL) { $account->content['flags'][$flag->name]['#markup'] .= flag_friend_create_link('unflag', $account->uid); } @@ -367,14 +579,8 @@ function flag_friend_user_view($account, $view_mode) { */ function flag_friend_create_link($type, $uid) { $flag = flag_get_flag('friend'); - if ($type == 'unfriend') { - $link = str_replace(t('Approve'), t('Deny'), str_replace('unflag', 'unfriend', $flag->theme('unflag', $uid))); - return $link; - } - else { - $link = str_replace(t('Approve'), t('Deny'), $flag->theme('unflag', $uid)); - return $link; - } + $link = str_replace(t('Approve'), t('Deny'), $flag->theme('unflag', $uid)); + return $link; } /** @@ -382,67 +588,39 @@ function flag_friend_create_link($type, $uid) { * * @param $flag * The flag object. - * @param $uid1 - * The account id of one of the users. - * @param $uid2 - * The account id of the other user. + * @param $content_id + * The account id of the user being acted on. + * @param $uid + * The account id of the logged in user. * @return * A string describing the status of the relationship. * @todo: this could possibly go into hook_flag_access once available. */ -function flag_friend_determine_friend_status($flag, $uid1, $uid2, $reset = NULL) { - static $status_cache = array(); - if ($reset) { - unset($status_cache); - } - // always keep these in the same order - if ($uid1 > $uid2) { - $key1 = $uid1; - $key2 = $uid2; - } - else { - $key1 = $uid2; - $key2 = $uid1; - } +function flag_friend_determine_friend_status($flag, $content_id, $uid, $reset = NULL) { + if(isset($flag)) { + $records = flag_friend_get_flags($flag, $content_id, $uid, TRUE); - if (isset($flag)) { - if (!isset($status_cache[$key1][$key2])) { - $you_are_flagged = $flag->is_flagged($uid1, $uid2); - $they_are_flagged = $flag->is_flagged($uid2, $uid1); - - $friends = db_select('flag_friend', 'ff') - ->fields('ff', array('created')) - ->condition(db_or() - ->condition(db_and() - ->condition('uid', $uid1) - ->condition('friend_uid', $uid2) - ) - ->condition(db_and() - ->condition('uid', $uid2) - ->condition('friend_uid', $uid1) - ) - ) - ->execute()->fetchField(); + if (empty($records)) { + //neither user has every flagged eachother + return FLAG_FRIEND_UNFLAGGED; + } - // see if these users have flagged eachother - if ($you_are_flagged && $they_are_flagged) { - $status_cache[$key1][$key2] = FLAG_FRIEND_BOTH; - } - elseif ($friends) { - $status_cache[$key1][$key2] = FLAG_FRIEND_FLAGGED; - } - elseif (!$you_are_flagged && !$they_are_flagged) { - $status_cache[$key1][$key2] = FLAG_FRIEND_UNFLAGGED; - } - elseif ($you_are_flagged && !$they_are_flagged) { - $status_cache[$key1][$key2] = FLAG_FRIEND_APPROVAL; - } - elseif (!$you_are_flagged && $they_are_flagged) { - $status_cache[$key1][$key2] = FLAG_FRIEND_PENDING; - } + //status will be the same for whatever flags we get back and if it is a pending relationship + //we'll only have 1 record returned + $relationship = array_shift($records); + + if ($relationship->status == 0 || $relationship->status == 1) { + //the users are friends + return FLAG_FRIEND_BOTH; + } elseif ($relationship->status == 4 && $relationship->content_id == $uid) { + //the logged in user was flagged by another user + return FLAG_FRIEND_APPROVAL; + } elseif ($relationship->status == 4 && $relationship->content_id == $content_id) { + //the logged in user flagged another user + return FLAG_FRIEND_PENDING; } } - return $status_cache[$key1][$key2]; + } /** @@ -505,7 +683,7 @@ function flag_friend_unfriend_form($action, $flag, $content_id, $token) { global $user; $form['current'] = array('#type' => 'value', '#value' => func_get_args()); - $status = flag_friend_determine_friend_status($flag, $user->uid, $content_id); + $status = flag_friend_determine_friend_status($flag, $content_id, $user->uid); switch ($status) { case FLAG_FRIEND_PENDING: // pending @@ -537,7 +715,7 @@ function flag_friend_form_submit($form, &$form_state) { $flag = $form_state['values']['current'][1]; $content_id = $form_state['values']['current'][2]; $token = $form_state['values']['current'][3]; - $status = flag_friend_determine_friend_status($flag, $user->uid, $content_id, TRUE); + $status = flag_friend_determine_friend_status($flag, $content_id, $user->uid, TRUE); if (isset($form_state['values']['flag_friend_message'])) { $flag->friend_message = $form_state['values']['flag_friend_message']; @@ -547,14 +725,15 @@ function flag_friend_form_submit($form, &$form_state) { } flag_friend_message_email($status, $flag, $content_id, $user); } - // the only time we want to unfriend, is if $status = FLAGGED or APPROVAL - elseif ($status === FLAG_FRIEND_FLAGGED || $status === FLAG_FRIEND_APPROVAL) { - flag_friend_unfriend($action, $flag, $content_id, $user, $status); - } elseif ($status === FLAG_FRIEND_PENDING) { // Clean message when user cancel own request. flag_friend_message($action, $flag, $content_id, $user->uid); } + elseif ($status === FLAG_FRIEND_APPROVAL) { + $flaggingUser = user_load($content_id); + flag($action, $flag->name, $user->uid, $flaggingUser); + } + //flag.module doesn't understand 'unfriend' $form_state['values']['action'] = 'unflag'; } @@ -997,7 +1176,7 @@ function flag_friend_preprocess_author_pane(&$variables) { // flag out-of-the-box so I feel it's pretty safe to use this permisssion. if ($account_id != 0 && user_access('access user profiles') && $user->uid != $account_id) { $flag = flag_get_flag('friend'); - $flag_status = flag_friend_determine_friend_status($flag, $user->uid, $account_id); + $flag_status = flag_friend_determine_friend_status($flag, $account_id, $user->uid); switch ($flag_status) { case (FLAG_FRIEND_FLAGGED): diff --git a/flag_friend.test b/flag_friend.test index 647b252..2023256 100644 --- a/flag_friend.test +++ b/flag_friend.test @@ -179,20 +179,22 @@ class FlagFriendViewsTestCase extends FlagFriendUserTestCase { $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); // Show awaiting approvals. Should only be User C. - $this->drupalGet('user/' . $this->user_a->uid . '/friends/flagged'); + $this->drupalGet('user/' . $this->user_a->uid . '/friends/pending'); $this->assertText($this->user_c->name); $this->assertText(t('Friend Requested. Cancel?')); $this->assertNoText($this->user_b->name); // Show friend requests. Should be nobody. - $this->drupalGet('user/' . $this->user_a->uid . '/friends/pending'); + $this->drupalGet('user/' . $this->user_a->uid . '/friends/flagged'); $this->assertNoText($this->user_b->name); $this->assertNoText($this->user_c->name); // Show User B's friend list. Should only be User A. + $this->drupalLogin($this->user_b); $this->drupalGet('user/' . $this->user_b->uid . '/friends'); $this->assertText($this->user_a->name); - $this->assertNoText(t('Remove friend')); + $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); // Show User C's friend list. Should be nobody. + $this->drupalLogin($this->user_c); $this->drupalGet('user/' . $this->user_c->uid . '/friends'); $this->assertNoText($this->user_a->name); $this->assertNoText($this->user_b->name); @@ -205,21 +207,23 @@ class FlagFriendViewsTestCase extends FlagFriendUserTestCase { $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); // Show awaiting approvals. Should be nobody. - $this->drupalGet('user/' . $this->user_b->uid . '/friends/flagged'); + $this->drupalGet('user/' . $this->user_b->uid . '/friends/pending'); $this->assertNoText($this->user_c->name); $this->assertNoText($this->user_a->name); // Show friend requests. Should only be User C. - $this->drupalGet('user/' . $this->user_b->uid . '/friends/pending'); + $this->drupalGet('user/' . $this->user_b->uid . '/friends/flagged'); $this->assertText($this->user_c->name); $this->assertText(t('Approve')); $this->assertText(t('Deny')); $this->assertNoText($this->user_a->name); // Show User A's friend list. Should only be User B. + $this->drupalLogin($this->user_a); $this->drupalGet('user/' . $this->user_a->uid . '/friends'); $this->assertText($this->user_b->name); - $this->assertNoText(t('Remove friend')); + $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); // Show User C's friend list. Should be nobody. + $this->drupalLogin($this->user_c); $this->drupalGet('user/' . $this->user_c->uid . '/friends'); $this->assertNoText($this->user_a->name); $this->assertNoText($this->user_b->name); @@ -231,25 +235,27 @@ class FlagFriendViewsTestCase extends FlagFriendUserTestCase { $this->assertNoText($this->user_a->name); $this->assertNoText($this->user_b->name); // Show awaiting approvals. Should only be User B. - $this->drupalGet('user/' . $this->user_c->uid . '/friends/flagged'); + $this->drupalGet('user/' . $this->user_c->uid . '/friends/pending'); $this->assertText($this->user_b->name); $this->assertText(t('Friend Requested. Cancel?')); $this->assertNoText($this->user_a->name); // Show friend requests. Should only be User A. - $this->drupalGet('user/' . $this->user_c->uid . '/friends/pending'); + $this->drupalGet('user/' . $this->user_c->uid . '/friends/flagged'); $this->assertText($this->user_a->name); $this->assertText(t('Approve')); $this->assertText(t('Deny')); $this->assertNoText($this->user_b->name); // Show User A's friend list. Should only be User B. + $this->drupalLogin($this->user_a); $this->drupalGet('user/' . $this->user_a->uid . '/friends'); $this->assertText($this->user_b->name); - $this->assertNoText(t('Remove friend')); + $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); // Show User B's friend list. Should only be User A. + $this->drupalLogin($this->user_b); $this->drupalGet('user/' . $this->user_b->uid . '/friends'); $this->assertText($this->user_a->name); - $this->assertNoText(t('Remove friend')); + $this->assertText(t('Remove friend')); $this->assertNoText($this->user_c->name); } } diff --git a/includes/flag_friend.views.inc b/includes/flag_friend.views.inc index 90c61ba..f85d6a5 100644 --- a/includes/flag_friend.views.inc +++ b/includes/flag_friend.views.inc @@ -6,28 +6,6 @@ */ /** - * Implements hook_views_handlers(). - */ -function flag_friend_views_handlers() { - return array( - 'info' => array( - 'path' => drupal_get_path('module', 'flag_friend') . '/includes', - ), - 'handlers' => array( - 'flag_friend_handler_argument_numeric' => array( - 'parent' => 'views_handler_argument_numeric', - ), - 'flag_friend_handler_argument_apachesolr_friend' => array( - 'parent' => 'apachesolr_views_handler_argument', - ), - 'flag_friend_handler_field_links' => array( - 'parent' => 'views_handler_field', - ), - ), - ); -} - -/** * Implements hook_views_data(). */ function flag_friend_views_data() { @@ -35,29 +13,33 @@ function flag_friend_views_data() { // flag_friend table $data['flag_friend']['table']['group'] = t('Flag friend'); - $data['flag_friend']['table']['join'] = array( - 'users' => array( - 'left_field' => 'uid', - 'field' => 'uid', - ), + + $data['flag_friend']['table']['join']['flag_content'] = array( + 'left_field' => 'fcid', + 'field' => 'fcid', + 'type' => 'INNER', ); - $data['flag_friend']['uid'] = array( - 'group' => t('Flag friend'), - 'title' => t('Flag friend links'), - 'help' => t('Remove/Deny links'), - 'real field' => 'uid', + $data['flag_friend']['status'] = array( + 'title' => t('Relationship Status'), + 'help' => t('Display the status of the relationship, whether pending or approved'), 'field' => array( - 'handler' => 'flag_friend_handler_field_links', + 'handler' => 'flag_friend_views_handler_field_status', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'flag_friend_views_handler_filter_status', ), ); - // argument - $data['flag_friend']['friend_uid'] = array( - 'title' => t('Friends of'), - 'help' => t('Retrieve the friends of the user id given.'), - 'argument' => array( - 'handler' => 'flag_friend_handler_argument_numeric', + $data['flag_friend']['friend_link'] = array( + 'title' => t('Friend Link'), + 'help' => t('Flag Friend Link. Only used for creating links for users who have been flagged as by a friend'), + 'field' => array( + 'handler' => 'flag_friend_views_handler_field_links', ), ); @@ -79,7 +61,7 @@ function flag_friend_views_data() { 'handler' => 'views_handler_argument_date', ), ); - +/* // flag_friend_message table $data['flag_friend_message']['table']['group'] = t('Flag friend'); $data['flag_friend_message']['table']['join'] = array( @@ -97,7 +79,7 @@ function flag_friend_views_data() { 'handler' => 'views_handler_field', 'click sortable' => FALSE, ), - ); + );*/ // Add the flag relationship. // This basically just changes the 'base field' while reusing Flag's @@ -112,7 +94,7 @@ function flag_friend_views_data() { 'handler' => 'flag_handler_relationship_content', 'label' => t('flag friend'), 'base' => 'flag_content', - 'base field' => 'uid', + 'base field' => 'content_id', 'relationship field' => 'uid', ), ); @@ -135,3 +117,18 @@ function flag_friend_views_data_alter(&$data) { ); } } + +/** + * Implements hook_views_plugins + */ +function flag_friend_views_plugins() { + return array( + 'access' => array( + 'flag_friend' => array( + 'title' => t('Restrict Flag Friend Pending and Requests Access'), + 'help' => t('Creates a custom access check to see if a user is looking at their own account.'), + 'handler' => 'flag_friend_plugin_access_pending_requested', + ), + ), + ); +} \ No newline at end of file diff --git a/includes/flag_friend.views_default.inc b/includes/flag_friend.views_default.inc index fe783a8..342ddb2 100644 --- a/includes/flag_friend.views_default.inc +++ b/includes/flag_friend.views_default.inc @@ -10,336 +10,297 @@ * Implements hook_views_default_views(). */ function flag_friend_views_default_views() { - $view = new view; + $view = new view(); $view->name = 'friends'; - $view->description = 'Various page displays for flag_friend.'; - $view->tag = 'flag.friend'; + $view->description = 'Flag Friend Default Views'; + $view->tag = 'default'; $view->base_table = 'users'; - $view->human_name = ''; - $view->api_version = '3.0-alpha1'; + $view->human_name = 'Friends'; + $view->core = 7; + $view->api_version = '3.0'; $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ - /* Display: Defaults */ - $handler = $view->new_display('default', 'Defaults', 'default'); - $handler->display->display_options['access']['type'] = 'perm'; + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['title'] = 'Friends'; + $handler->display->display_options['use_more_always'] = FALSE; + $handler->display->display_options['access']['type'] = 'flag_friend'; $handler->display->display_options['cache']['type'] = 'none'; $handler->display->display_options['query']['type'] = 'views_query'; - $handler->display->display_options['query']['options']['distinct'] = TRUE; $handler->display->display_options['exposed_form']['type'] = 'basic'; $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '10'; $handler->display->display_options['style_plugin'] = 'table'; $handler->display->display_options['style_options']['columns'] = array( + 'name' => 'name', 'picture' => 'picture', - 'name' => 'picture', - 'message' => 'message', - 'ops' => 'ops', - 'uid' => 'ops', ); $handler->display->display_options['style_options']['default'] = '-1'; $handler->display->display_options['style_options']['info'] = array( - 'picture' => array( - 'sortable' => 0, - 'separator' => '', - ), 'name' => array( 'sortable' => 0, + 'default_sort_order' => 'asc', + 'align' => '', 'separator' => '', + 'empty_column' => 0, ), - 'message' => array( - 'separator' => '', - ), - 'ops' => array( - 'separator' => '', - ), - 'uid' => array( + 'picture' => array( + 'sortable' => 0, + 'default_sort_order' => 'asc', + 'align' => '', 'separator' => '', + 'empty_column' => 0, ), ); - $handler->display->display_options['style_options']['override'] = 1; - $handler->display->display_options['style_options']['sticky'] = 0; - /* Empty text: Global: Text area */ - $handler->display->display_options['empty']['text']['id'] = 'area'; - $handler->display->display_options['empty']['text']['table'] = 'views'; - $handler->display->display_options['empty']['text']['field'] = 'area'; - $handler->display->display_options['empty']['text']['empty'] = FALSE; - $handler->display->display_options['empty']['text']['content'] = 'No Friends have been added.'; - $handler->display->display_options['empty']['text']['format'] = '1'; - /* Relationship: Flag Friend: friend */ - $handler->display->display_options['relationships']['flag_friend_content_rel']['id'] = 'flag_friend_content_rel'; - $handler->display->display_options['relationships']['flag_friend_content_rel']['table'] = 'users'; - $handler->display->display_options['relationships']['flag_friend_content_rel']['field'] = 'flag_friend_content_rel'; - $handler->display->display_options['relationships']['flag_friend_content_rel']['flag'] = 'friend'; - $handler->display->display_options['relationships']['flag_friend_content_rel']['user_scope'] = 'any'; + /* No results behavior: Global: Text area */ + $handler->display->display_options['empty']['area']['id'] = 'area'; + $handler->display->display_options['empty']['area']['table'] = 'views'; + $handler->display->display_options['empty']['area']['field'] = 'area'; + $handler->display->display_options['empty']['area']['empty'] = TRUE; + $handler->display->display_options['empty']['area']['content'] = 'No friends have been added.'; + $handler->display->display_options['empty']['area']['format'] = 'filtered_html'; + /* Relationship: Flags: friend */ + $handler->display->display_options['relationships']['flag_content_rel']['id'] = 'flag_content_rel'; + $handler->display->display_options['relationships']['flag_content_rel']['table'] = 'users'; + $handler->display->display_options['relationships']['flag_content_rel']['field'] = 'flag_content_rel'; + $handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'friend'; + /* Field: User: Picture */ + $handler->display->display_options['fields']['picture']['id'] = 'picture'; + $handler->display->display_options['fields']['picture']['table'] = 'users'; + $handler->display->display_options['fields']['picture']['field'] = 'picture'; + $handler->display->display_options['fields']['picture']['label'] = ''; + $handler->display->display_options['fields']['picture']['element_label_colon'] = FALSE; /* Field: User: Name */ $handler->display->display_options['fields']['name']['id'] = 'name'; $handler->display->display_options['fields']['name']['table'] = 'users'; $handler->display->display_options['fields']['name']['field'] = 'name'; - $handler->display->display_options['fields']['name']['link_to_user'] = 1; + $handler->display->display_options['fields']['name']['relationship'] = 'uid'; + $handler->display->display_options['fields']['name']['label'] = 'Requester'; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE; + $handler->display->display_options['fields']['name']['element_label_colon'] = FALSE; /* Field: Flags: Flag link */ $handler->display->display_options['fields']['ops']['id'] = 'ops'; $handler->display->display_options['fields']['ops']['table'] = 'flag_content'; $handler->display->display_options['fields']['ops']['field'] = 'ops'; - $handler->display->display_options['fields']['ops']['relationship'] = 'flag_friend_content_rel'; - /* Field: Flag friend: Flag friend links */ - $handler->display->display_options['fields']['uid']['id'] = 'uid'; - $handler->display->display_options['fields']['uid']['table'] = 'flag_friend'; - $handler->display->display_options['fields']['uid']['field'] = 'uid'; - /* Argument: Flag friend: Friends of */ - $handler->display->display_options['arguments']['friend_uid']['id'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['table'] = 'flag_friend'; - $handler->display->display_options['arguments']['friend_uid']['field'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['style_plugin'] = 'default_summary'; - $handler->display->display_options['arguments']['friend_uid']['default_argument_type'] = 'fixed'; - /* Filter: User: Active */ + $handler->display->display_options['fields']['ops']['relationship'] = 'flag_content_rel'; + /* Sort criterion: User: Created date */ + $handler->display->display_options['sorts']['created']['id'] = 'created'; + $handler->display->display_options['sorts']['created']['table'] = 'users'; + $handler->display->display_options['sorts']['created']['field'] = 'created'; + $handler->display->display_options['sorts']['created']['order'] = 'DESC'; + /* Filter criterion: User: Active */ $handler->display->display_options['filters']['status']['id'] = 'status'; $handler->display->display_options['filters']['status']['table'] = 'users'; $handler->display->display_options['filters']['status']['field'] = 'status'; $handler->display->display_options['filters']['status']['value'] = '1'; + $handler->display->display_options['filters']['status']['group'] = 1; $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + /* Filter criterion: Flag friend: Relationship Status */ + $handler->display->display_options['filters']['status_1']['id'] = 'status_1'; + $handler->display->display_options['filters']['status_1']['table'] = 'flag_friend'; + $handler->display->display_options['filters']['status_1']['field'] = 'status'; + $handler->display->display_options['filters']['status_1']['relationship'] = 'flag_content_rel'; + $handler->display->display_options['filters']['status_1']['value'] = '1'; - /* Display: Page (friends) */ - $handler = $view->new_display('page', 'Page (friends)', 'page_1'); - $handler->display->display_options['defaults']['title'] = FALSE; - $handler->display->display_options['title'] = 'Friends'; - $handler->display->display_options['defaults']['relationships'] = FALSE; + /* Display: Page: All Friends */ + $handler = $view->new_display('page', 'Page: All Friends', 'page'); + $handler->display->display_options['display_description'] = 'Listing of all approved friends'; $handler->display->display_options['defaults']['fields'] = FALSE; /* Field: User: Picture */ $handler->display->display_options['fields']['picture']['id'] = 'picture'; $handler->display->display_options['fields']['picture']['table'] = 'users'; $handler->display->display_options['fields']['picture']['field'] = 'picture'; - $handler->display->display_options['fields']['picture']['label'] = 'Friend'; + $handler->display->display_options['fields']['picture']['label'] = ''; + $handler->display->display_options['fields']['picture']['element_label_colon'] = FALSE; /* Field: User: Name */ $handler->display->display_options['fields']['name']['id'] = 'name'; $handler->display->display_options['fields']['name']['table'] = 'users'; $handler->display->display_options['fields']['name']['field'] = 'name'; - $handler->display->display_options['fields']['name']['link_to_user'] = 1; - /* Field: Flag friend: Flag friend links */ - $handler->display->display_options['fields']['uid']['id'] = 'uid'; - $handler->display->display_options['fields']['uid']['table'] = 'flag_friend'; - $handler->display->display_options['fields']['uid']['field'] = 'uid'; - $handler->display->display_options['fields']['uid']['label'] = 'Actions'; - $handler->display->display_options['defaults']['arguments'] = FALSE; - /* Argument: Flag friend: Friends of */ - $handler->display->display_options['arguments']['friend_uid']['id'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['table'] = 'flag_friend'; - $handler->display->display_options['arguments']['friend_uid']['field'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['default_action'] = 'default'; - $handler->display->display_options['arguments']['friend_uid']['style_plugin'] = 'default_summary'; - $handler->display->display_options['arguments']['friend_uid']['default_argument_type'] = 'user'; - $handler->display->display_options['arguments']['friend_uid']['default_argument_options']['user'] = FALSE; - $handler->display->display_options['arguments']['friend_uid']['break_phrase'] = 0; - $handler->display->display_options['arguments']['friend_uid']['not'] = 0; + $handler->display->display_options['fields']['name']['label'] = 'User'; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE; + /* Field: Flags: Flag link */ + $handler->display->display_options['fields']['ops']['id'] = 'ops'; + $handler->display->display_options['fields']['ops']['table'] = 'flag_content'; + $handler->display->display_options['fields']['ops']['field'] = 'ops'; + $handler->display->display_options['fields']['ops']['relationship'] = 'flag_content_rel'; $handler->display->display_options['path'] = 'user/%/friends/all'; $handler->display->display_options['menu']['type'] = 'default tab'; $handler->display->display_options['menu']['title'] = 'View All Friends'; + $handler->display->display_options['menu']['description'] = 'View all of your friends'; $handler->display->display_options['menu']['weight'] = '-10'; + $handler->display->display_options['menu']['name'] = 'user-menu'; + $handler->display->display_options['menu']['context'] = 0; + $handler->display->display_options['menu']['context_only_inline'] = 0; $handler->display->display_options['tab_options']['type'] = 'tab'; $handler->display->display_options['tab_options']['title'] = 'Friends'; $handler->display->display_options['tab_options']['weight'] = '0'; - /* Display: Page (pending) */ - $handler = $view->new_display('page', 'Page (pending)', 'page_2'); + /* Display: Page: Pending Friends */ + $handler = $view->new_display('page', 'Page: Pending Friends', 'page_1'); $handler->display->display_options['defaults']['title'] = FALSE; - $handler->display->display_options['title'] = 'Friend Requests'; - $handler->display->display_options['defaults']['header'] = FALSE; - /* Header: Global: Text area */ - $handler->display->display_options['header']['text']['id'] = 'area'; - $handler->display->display_options['header']['text']['table'] = 'views'; - $handler->display->display_options['header']['text']['field'] = 'area'; - $handler->display->display_options['header']['text']['empty'] = FALSE; - $handler->display->display_options['header']['text']['content'] = 'These are users who would like to be your friend.'; - $handler->display->display_options['header']['text']['format'] = '1'; + $handler->display->display_options['title'] = 'Pending Friends'; + $handler->display->display_options['display_description'] = 'List of all pending friend relationships'; + $handler->display->display_options['defaults']['access'] = FALSE; + $handler->display->display_options['access']['type'] = 'flag_friend'; $handler->display->display_options['defaults']['empty'] = FALSE; - /* Empty text: Global: Text area */ - $handler->display->display_options['empty']['text']['id'] = 'area'; - $handler->display->display_options['empty']['text']['table'] = 'views'; - $handler->display->display_options['empty']['text']['field'] = 'area'; - $handler->display->display_options['empty']['text']['empty'] = FALSE; - $handler->display->display_options['empty']['text']['content'] = 'No Friend Requests.'; + /* No results behavior: Global: Text area */ + $handler->display->display_options['empty']['area']['id'] = 'area'; + $handler->display->display_options['empty']['area']['table'] = 'views'; + $handler->display->display_options['empty']['area']['field'] = 'area'; + $handler->display->display_options['empty']['area']['empty'] = TRUE; + $handler->display->display_options['empty']['area']['content'] = 'No pending friends.'; + $handler->display->display_options['empty']['area']['format'] = 'filtered_html'; $handler->display->display_options['defaults']['fields'] = FALSE; /* Field: User: Picture */ $handler->display->display_options['fields']['picture']['id'] = 'picture'; $handler->display->display_options['fields']['picture']['table'] = 'users'; $handler->display->display_options['fields']['picture']['field'] = 'picture'; - $handler->display->display_options['fields']['picture']['label'] = 'User'; + $handler->display->display_options['fields']['picture']['label'] = ''; + $handler->display->display_options['fields']['picture']['element_label_colon'] = FALSE; /* Field: User: Name */ $handler->display->display_options['fields']['name']['id'] = 'name'; $handler->display->display_options['fields']['name']['table'] = 'users'; $handler->display->display_options['fields']['name']['field'] = 'name'; - $handler->display->display_options['fields']['name']['link_to_user'] = 1; - /* Field: Flag friend: Message */ - $handler->display->display_options['fields']['message']['id'] = 'message'; - $handler->display->display_options['fields']['message']['table'] = 'flag_friend_message'; - $handler->display->display_options['fields']['message']['field'] = 'message'; - $handler->display->display_options['fields']['message']['relationship'] = 'flag_friend_content_rel'; + $handler->display->display_options['fields']['name']['label'] = 'User'; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE; /* Field: Flags: Flag link */ $handler->display->display_options['fields']['ops']['id'] = 'ops'; $handler->display->display_options['fields']['ops']['table'] = 'flag_content'; $handler->display->display_options['fields']['ops']['field'] = 'ops'; - $handler->display->display_options['fields']['ops']['relationship'] = 'flag_friend_content_rel'; - $handler->display->display_options['fields']['ops']['label'] = 'Actions'; - /* Field: Flag friend: Flag friend links */ - $handler->display->display_options['fields']['uid']['id'] = 'uid'; - $handler->display->display_options['fields']['uid']['table'] = 'flag_friend'; - $handler->display->display_options['fields']['uid']['field'] = 'uid'; - $handler->display->display_options['defaults']['arguments'] = FALSE; - /* Argument: Flags: Content ID */ - $handler->display->display_options['arguments']['content_id']['id'] = 'content_id'; - $handler->display->display_options['arguments']['content_id']['table'] = 'flag_content'; - $handler->display->display_options['arguments']['content_id']['field'] = 'content_id'; - $handler->display->display_options['arguments']['content_id']['relationship'] = 'flag_friend_content_rel'; - $handler->display->display_options['arguments']['content_id']['default_action'] = 'default'; - $handler->display->display_options['arguments']['content_id']['style_plugin'] = 'default_summary'; - $handler->display->display_options['arguments']['content_id']['default_argument_type'] = 'user'; - $handler->display->display_options['arguments']['content_id']['default_argument_options']['user'] = FALSE; - $handler->display->display_options['arguments']['content_id']['validate_type'] = 'php'; - $handler->display->display_options['arguments']['content_id']['validate_options']['code'] = 'if ($argument !== $GLOBALS[\'user\']->uid && !user_access(\'administer users\')) { - return FALSE; - } - return TRUE;'; - $handler->display->display_options['arguments']['content_id']['break_phrase'] = 0; - $handler->display->display_options['arguments']['content_id']['not'] = 0; + $handler->display->display_options['fields']['ops']['relationship'] = 'flag_content_rel'; + $handler->display->display_options['defaults']['filter_groups'] = FALSE; + $handler->display->display_options['defaults']['filters'] = FALSE; + /* Filter criterion: User: Active */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'users'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = '1'; + $handler->display->display_options['filters']['status']['group'] = 1; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + /* Filter criterion: Flag friend: Relationship Status */ + $handler->display->display_options['filters']['status_1']['id'] = 'status_1'; + $handler->display->display_options['filters']['status_1']['table'] = 'flag_friend'; + $handler->display->display_options['filters']['status_1']['field'] = 'status'; + $handler->display->display_options['filters']['status_1']['relationship'] = 'flag_content_rel'; + $handler->display->display_options['filters']['status_1']['value'] = '4'; $handler->display->display_options['path'] = 'user/%/friends/pending'; $handler->display->display_options['menu']['type'] = 'tab'; - $handler->display->display_options['menu']['title'] = 'Friend Requests'; + $handler->display->display_options['menu']['title'] = 'Pending Friends'; + $handler->display->display_options['menu']['description'] = 'Pending Friend Requests'; $handler->display->display_options['menu']['weight'] = '0'; + $handler->display->display_options['menu']['context'] = 0; + $handler->display->display_options['menu']['context_only_inline'] = 0; - /* Display: Page (flagged) */ - $handler = $view->new_display('page', 'Page (flagged)', 'page_3'); + /* Display: Page: Friend Requests */ + $handler = $view->new_display('page', 'Page: Friend Requests', 'page_2'); $handler->display->display_options['defaults']['title'] = FALSE; - $handler->display->display_options['title'] = 'Awaiting Friend Approvals'; - $handler->display->display_options['defaults']['header'] = FALSE; - /* Header: Global: Text area */ - $handler->display->display_options['header']['text']['id'] = 'area'; - $handler->display->display_options['header']['text']['table'] = 'views'; - $handler->display->display_options['header']['text']['field'] = 'area'; - $handler->display->display_options['header']['text']['empty'] = TRUE; - $handler->display->display_options['header']['text']['content'] = 'These are users who you have requested to be friends with.'; - $handler->display->display_options['header']['text']['format'] = '1'; + $handler->display->display_options['title'] = 'Friend Requests'; + $handler->display->display_options['display_description'] = 'Friends flagged by the current user'; + $handler->display->display_options['defaults']['access'] = FALSE; + $handler->display->display_options['access']['type'] = 'flag_friend'; $handler->display->display_options['defaults']['empty'] = FALSE; - /* Empty text: Global: Text area */ - $handler->display->display_options['empty']['text']['id'] = 'area'; - $handler->display->display_options['empty']['text']['table'] = 'views'; - $handler->display->display_options['empty']['text']['field'] = 'area'; - $handler->display->display_options['empty']['text']['empty'] = FALSE; - $handler->display->display_options['empty']['text']['content'] = 'No Friend Requests.'; + /* No results behavior: Global: Text area */ + $handler->display->display_options['empty']['area']['id'] = 'area'; + $handler->display->display_options['empty']['area']['table'] = 'views'; + $handler->display->display_options['empty']['area']['field'] = 'area'; + $handler->display->display_options['empty']['area']['empty'] = TRUE; + $handler->display->display_options['empty']['area']['content'] = 'No friend requests.'; + $handler->display->display_options['empty']['area']['format'] = 'filtered_html'; $handler->display->display_options['defaults']['relationships'] = FALSE; - /* Relationship: Flags: friend */ - $handler->display->display_options['relationships']['flag_content_rel']['id'] = 'flag_content_rel'; - $handler->display->display_options['relationships']['flag_content_rel']['table'] = 'users'; - $handler->display->display_options['relationships']['flag_content_rel']['field'] = 'flag_content_rel'; - $handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'friend'; - $handler->display->display_options['relationships']['flag_content_rel']['user_scope'] = 'any'; + /* Relationship: Flags: User's flagged content */ + $handler->display->display_options['relationships']['flag_user_content_rel']['id'] = 'flag_user_content_rel'; + $handler->display->display_options['relationships']['flag_user_content_rel']['table'] = 'users'; + $handler->display->display_options['relationships']['flag_user_content_rel']['field'] = 'flag_user_content_rel'; + $handler->display->display_options['relationships']['flag_user_content_rel']['flag'] = 'friend'; /* Relationship: Flags: User */ $handler->display->display_options['relationships']['uid']['id'] = 'uid'; $handler->display->display_options['relationships']['uid']['table'] = 'flag_content'; $handler->display->display_options['relationships']['uid']['field'] = 'uid'; - $handler->display->display_options['relationships']['uid']['relationship'] = 'flag_content_rel'; - $handler->display->display_options['relationships']['uid']['required'] = 0; + $handler->display->display_options['relationships']['uid']['relationship'] = 'flag_user_content_rel'; $handler->display->display_options['defaults']['fields'] = FALSE; - /* Field: User: Picture */ - $handler->display->display_options['fields']['picture']['id'] = 'picture'; - $handler->display->display_options['fields']['picture']['table'] = 'users'; - $handler->display->display_options['fields']['picture']['field'] = 'picture'; - $handler->display->display_options['fields']['picture']['label'] = 'User'; /* Field: User: Name */ $handler->display->display_options['fields']['name']['id'] = 'name'; $handler->display->display_options['fields']['name']['table'] = 'users'; $handler->display->display_options['fields']['name']['field'] = 'name'; - $handler->display->display_options['fields']['name']['link_to_user'] = 1; - /* Field: Flag friend: Message */ - $handler->display->display_options['fields']['message']['id'] = 'message'; - $handler->display->display_options['fields']['message']['table'] = 'flag_friend_message'; - $handler->display->display_options['fields']['message']['field'] = 'message'; - $handler->display->display_options['fields']['message']['relationship'] = 'flag_content_rel'; - /* Field: Flags: Flag link */ - $handler->display->display_options['fields']['ops']['id'] = 'ops'; - $handler->display->display_options['fields']['ops']['table'] = 'flag_content'; - $handler->display->display_options['fields']['ops']['field'] = 'ops'; - $handler->display->display_options['fields']['ops']['relationship'] = 'flag_content_rel'; - $handler->display->display_options['fields']['ops']['label'] = 'Actions'; + /* Field: User: Picture */ + $handler->display->display_options['fields']['picture']['id'] = 'picture'; + $handler->display->display_options['fields']['picture']['table'] = 'users'; + $handler->display->display_options['fields']['picture']['field'] = 'picture'; + /* Field: Flag friend: Friend Link */ + $handler->display->display_options['fields']['friend_link']['id'] = 'friend_link'; + $handler->display->display_options['fields']['friend_link']['table'] = 'flag_friend'; + $handler->display->display_options['fields']['friend_link']['field'] = 'friend_link'; + $handler->display->display_options['fields']['friend_link']['relationship'] = 'flag_user_content_rel'; $handler->display->display_options['defaults']['arguments'] = FALSE; - /* Argument: User: Uid */ - $handler->display->display_options['arguments']['uid']['id'] = 'uid'; - $handler->display->display_options['arguments']['uid']['table'] = 'users'; - $handler->display->display_options['arguments']['uid']['field'] = 'uid'; - $handler->display->display_options['arguments']['uid']['relationship'] = 'uid'; - $handler->display->display_options['arguments']['uid']['default_action'] = 'default'; - $handler->display->display_options['arguments']['uid']['style_plugin'] = 'default_summary'; - $handler->display->display_options['arguments']['uid']['default_argument_type'] = 'user'; - $handler->display->display_options['arguments']['uid']['default_argument_options']['user'] = FALSE; - $handler->display->display_options['arguments']['uid']['validate_type'] = 'php'; - $handler->display->display_options['arguments']['uid']['validate_options']['code'] = 'if ($argument !== $GLOBALS[\'user\']->uid && !user_access(\'administer users\')) { - return FALSE; - } - return TRUE;'; - $handler->display->display_options['arguments']['uid']['break_phrase'] = 0; - $handler->display->display_options['arguments']['uid']['not'] = 0; + /* Contextual filter: Flags: Content ID */ + $handler->display->display_options['arguments']['content_id']['id'] = 'content_id'; + $handler->display->display_options['arguments']['content_id']['table'] = 'flag_content'; + $handler->display->display_options['arguments']['content_id']['field'] = 'content_id'; + $handler->display->display_options['arguments']['content_id']['relationship'] = 'flag_user_content_rel'; + $handler->display->display_options['arguments']['content_id']['default_action'] = 'not found'; + $handler->display->display_options['arguments']['content_id']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['content_id']['summary']['number_of_records'] = '0'; + $handler->display->display_options['arguments']['content_id']['summary']['format'] = 'default_summary'; + $handler->display->display_options['arguments']['content_id']['summary_options']['items_per_page'] = '25'; + $handler->display->display_options['arguments']['content_id']['validate']['type'] = 'php'; + $handler->display->display_options['arguments']['content_id']['validate_options']['code'] = 'global $user; + if ($user->uid == $handler->argument) { + return TRUE; + } else { + return FALSE; + }'; + $handler->display->display_options['arguments']['content_id']['validate']['fail'] = 'access denied'; + $handler->display->display_options['defaults']['filter_groups'] = FALSE; + $handler->display->display_options['defaults']['filters'] = FALSE; + /* Filter criterion: User: Active */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'users'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = '1'; + $handler->display->display_options['filters']['status']['group'] = 1; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + /* Filter criterion: Flag friend: Relationship Status */ + $handler->display->display_options['filters']['status_1']['id'] = 'status_1'; + $handler->display->display_options['filters']['status_1']['table'] = 'flag_friend'; + $handler->display->display_options['filters']['status_1']['field'] = 'status'; + $handler->display->display_options['filters']['status_1']['relationship'] = 'flag_user_content_rel'; + $handler->display->display_options['filters']['status_1']['value'] = '4'; $handler->display->display_options['path'] = 'user/%/friends/flagged'; $handler->display->display_options['menu']['type'] = 'tab'; - $handler->display->display_options['menu']['title'] = 'Awaiting Friend Approvals'; - $handler->display->display_options['menu']['weight'] = '0'; + $handler->display->display_options['menu']['title'] = 'Friend Requests'; + $handler->display->display_options['menu']['description'] = 'Friends you have flagged'; + $handler->display->display_options['menu']['weight'] = '10'; + $handler->display->display_options['menu']['context'] = 0; + $handler->display->display_options['menu']['context_only_inline'] = 0; - /* Display: Current user's Friends block */ - $handler = $view->new_display('block', 'Current user\'s Friends block', 'block_1'); - $handler->display->display_options['defaults']['items_per_page'] = FALSE; - $handler->display->display_options['defaults']['use_more'] = FALSE; - $handler->display->display_options['use_more'] = TRUE; - $handler->display->display_options['defaults']['relationships'] = FALSE; + /* Display: Block: Friends */ + $handler = $view->new_display('block', 'Block: Friends', 'block_1'); + $handler->display->display_options['defaults']['pager'] = FALSE; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '5'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['pager']['options']['quantity'] = '9'; $handler->display->display_options['defaults']['fields'] = FALSE; + /* Field: User: Picture */ + $handler->display->display_options['fields']['picture']['id'] = 'picture'; + $handler->display->display_options['fields']['picture']['table'] = 'users'; + $handler->display->display_options['fields']['picture']['field'] = 'picture'; + $handler->display->display_options['fields']['picture']['label'] = ''; + $handler->display->display_options['fields']['picture']['element_label_colon'] = FALSE; /* Field: User: Name */ $handler->display->display_options['fields']['name']['id'] = 'name'; $handler->display->display_options['fields']['name']['table'] = 'users'; $handler->display->display_options['fields']['name']['field'] = 'name'; - $handler->display->display_options['fields']['name']['link_to_user'] = 1; - /* Field: Flag friend: Flag friend links */ - $handler->display->display_options['fields']['uid']['id'] = 'uid'; - $handler->display->display_options['fields']['uid']['table'] = 'flag_friend'; - $handler->display->display_options['fields']['uid']['field'] = 'uid'; - $handler->display->display_options['defaults']['arguments'] = FALSE; - /* Argument: Flag friend: Friends of */ - $handler->display->display_options['arguments']['friend_uid']['id'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['table'] = 'flag_friend'; - $handler->display->display_options['arguments']['friend_uid']['field'] = 'friend_uid'; - $handler->display->display_options['arguments']['friend_uid']['default_action'] = 'default'; - $handler->display->display_options['arguments']['friend_uid']['style_plugin'] = 'default_summary'; - $handler->display->display_options['arguments']['friend_uid']['default_argument_type'] = 'current_user'; - $handler->display->display_options['arguments']['friend_uid']['break_phrase'] = 0; - $handler->display->display_options['arguments']['friend_uid']['not'] = 0; - $translatables['friends'] = array( - t('Defaults'), - t('more'), - t('Apply'), - t('Reset'), - t('Sort By'), - t('Asc'), - t('Desc'), - t('Items per page'), - t('- All -'), - t('Offset'), - t('No Friends have been added.'), - t('flag friend'), - t('Name'), - t('Flag link'), - t('Flag friend links'), - t('All'), - t('Page (friends)'), - t('Friends'), - t('Friend'), - t('Actions'), - t('Page (pending)'), - t('Friend Requests'), - t('These are users who would like to be your friend.'), - t('No Friend Requests.'), - t('User'), - t('Message'), - t('Page (flagged)'), - t('Awaiting Friend Approvals'), - t('These are users who you have requested to be friends with.'), - t('flag'), - t('Flag user'), - t('Current user\'s Friends block'), - ); + $handler->display->display_options['fields']['name']['relationship'] = 'uid'; + $handler->display->display_options['fields']['name']['label'] = 'Requester'; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE; + $handler->display->display_options['fields']['name']['element_label_colon'] = FALSE; + $handler->display->display_options['block_description'] = 'Flag Friend Friends'; $views[$view->name] = $view; return $views; } diff --git a/includes/flag_friend_plugin_access_pending_requested.inc b/includes/flag_friend_plugin_access_pending_requested.inc new file mode 100644 index 0000000..9d3c4ba --- /dev/null +++ b/includes/flag_friend_plugin_access_pending_requested.inc @@ -0,0 +1,20 @@ +uid; + // what's the status? + $status = flag_friend_determine_friend_status($flag, $user->uid, $content_id); + if ($status == FLAG_FRIEND_PENDING) { + $link = flag_create_link('friend', $content_id); + $link .= ' ' . flag_friend_create_link('friend', $content_id); + return $link; + } + } +} \ No newline at end of file diff --git a/includes/flag_friend_views_handler_field_status.inc b/includes/flag_friend_views_handler_field_status.inc new file mode 100644 index 0000000..d049693 --- /dev/null +++ b/includes/flag_friend_views_handler_field_status.inc @@ -0,0 +1,70 @@ + 'Friends'); + $options['not_friends'] = array('default' => 'Not friends'); + $options['pending_friends'] = array('default' => 'Pending'); + + return $options; + } + + function options_form(&$form, &$form_state) { + $form['friends'] = array ( + '#type' => 'textfield', + '#title' => 'Approved relationship name', + '#default_value' => $this->options['friends'], + ); + + $form['not_friends'] = array ( + '#type' => 'textfield', + '#title' => 'No relationship name', + '#default_value' => $this->options['not_friends'], + ); + + $form['pending_friends'] = array ( + '#type' => 'textfield', + '#title' => 'Pending relationship name', + '#default_value' => $this->options['pending_friends'], + ); + + parent::options_form($form, $form_state); + } + + function render($values) { + $alias = $this->field_alias; + switch ($values->$alias) { + case '0' : + case '1' : + return check_plain($this->options['friends']); + break; + case '2' : + return check_plain($this->options['not_friends']); + break; + case '3' : + case '4' : + return check_plain($this->options['pending_friends']); + } + } +} \ No newline at end of file diff --git a/includes/flag_friend_views_handler_filter_status.inc b/includes/flag_friend_views_handler_filter_status.inc new file mode 100644 index 0000000..32f6468 --- /dev/null +++ b/includes/flag_friend_views_handler_filter_status.inc @@ -0,0 +1,41 @@ + 'radios', + '#title' => t('Relationship Status'), + '#options' => array( + '1' => t('Friends'), + '4' => t('Pending'), + ), + '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 1, + ); + } +} \ No newline at end of file