innerJoin('user_relationship_types', 'urt', 'urt.rtid = ur.rtid'); foreach ($param as $key => $value) { if (!isset($value)) { continue; } switch ($key) { case 'between': $or = db_or()->condition(db_and() ->condition('ur.requester_id', $value[0]) ->condition('ur.requestee_id', $value[1]) ); $and = db_and() ->condition('ur.requestee_id', $value[0]) ->condition('ur.requester_id', $value[1]); //#479486 do not include reverse records of two-way relationships by default if (!isset($include_twoway_reverse)) { $and->condition(db_or() ->condition('ur.approved', 1, '<>') ->condition('urt.is_oneway', 0, '<>')); $twoway_reverse_clause = TRUE; } $or->condition($and); $query->condition($or); break; case 'user': //#479486 when showing all user's relationships, do not include reverse records of two-way relationships if (!isset($include_twoway_reverse)) { $query->condition(db_or() ->condition('ur.requester_id', $value) ->condition(db_and() ->condition(db_or() ->condition('ur.approved', 1, '<>') ->condition('urt.is_oneway', 0, '<>') ) ->condition('ur.requestee_id', $value) ) ); $twoway_reverse_clause = TRUE; } else { $query->condition(db_or() ->condition('ur.requester_id', $value) ->condition('ur.requestee_id', $value) ); } $arguments[] = $value; break; case 'requester_id': case 'requestee_id': $twoway_reverse_clause = TRUE;//#479486 these columns automatically should exclude duplicates default: $types_cols = array('name', 'plural_name', 'is_oneway', 'is_reciprocal', 'requires_approval', 'expires_val'); $prefix = !in_array($key, $types_cols) ? 'ur' : 'urt'; $query->condition($prefix . '.' . $key, $value); } } //#479486 add a general clause that removed reverse direction from two-way relationship results, unless it's been addressed above if (!$twoway_reverse_clause && !isset($include_twoway_reverse)) { $query->condition(db_or() ->condition('urt.is_oneway', 0, '<>') ->condition('ur.approved', 1, '<>') ->where('ur.requester_id < ur.requestee_id') ); $twoway_reverse_clause = TRUE; } if (!empty($only_count) || !empty($paging)) { $count = clone $query; $count->addExpression('COUNT(DISTINCT rid)', 'count'); } $query ->distinct() ->fields('ur') ->fields('urt'); if (isset($include_user_info) && $include_user_info) { $query->addField('requesters', 'name', 'requester_name'); $query->addField('requesters', 'mail', 'requester_mail'); $query->addField('requesters', 'data', 'requester_data'); $query->addField('requesters', 'picture', 'requester_picture'); $query->addField('requestees', 'name', 'requestee_name'); $query->addField('requestees', 'mail', 'requestee_mail'); $query->addField('requestees', 'data', 'requestee_data'); $query->addField('requestees', 'picture', 'requestee_picture'); $query->innerJoin('users', 'requesters', 'ur.requester_id = requesters.uid'); $query->innerJoin('users', 'requestees', 'ur.requestee_id = requestees.uid'); } if (!empty($order)) { if (is_array($order)) { $query->orderBy($order[0], $order[1]); } else { $query->orderBy($order); } } if (!empty($limit)) { $query->range(0, $limit); } if (!empty($only_count)) { return $count; } if (!empty($paging)) { $query = $query ->extend('PagerDefault') ->limit($paging); $query->setCountQuery($count); } return $query; } /** * hook_perm() */ function user_relationships_api_permission() { return array('can have relationships' => array( 'title' => t('Can have relationships'), 'description' => t('The user is allowed to have and accept relationships.'), )); } /** * hook_cron() */ function user_relationships_api_cron() { $now = REQUEST_TIME; // only expire relationships once a day $last_cron = variable_get('user_relationships_last_expire', 0); if ($now < $last_cron + 86400) { return FALSE; } $result = db_query( " SELECT ur.rid FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt ON ur.rtid = urt.rtid WHERE ur.approved = 0 AND urt.expires_val > 0 AND ur.updated_at < (:now - (urt.expires_val * 86400)) GROUP BY ur.rid", array(':now' => $now) ); $expired_requests = $result->fetchCol(); if (count($expired_requests)) { db_delete('user_relationships') ->condition('rid', $expired_requests) ->execute(); //FIXME: this will trigger various hook_user_relationships() but will not pass a valid relationship object. E.g. in mailer. module_invoke_all('user_relationships_delete', $expired_requests); } // remember when we last expired relationships variable_set('user_relationships_last_expire', $now); return TRUE; } /** * Implements hook_user_cancel(). */ function user_relationships_api_user_cancel($edit, $account, $method) { db_delete('user_relationships') ->condition(db_or() ->condition('requester_id', $account->uid) ->condition('requestee_id', $account->uid) ) ->execute(); } /** * Wrapper function for tt() if i18nstrings enabled. */ function ur_tt($name, $string, $langcode = NULL, $update = FALSE) { if (module_exists('i18nstrings')) { return tt($name, $string, $langcode, $update); } else { return $string; } } /** * Implements hook_activity_info(). */ function user_relationships_api_activity_info() { $info = new stdClass(); $info->api = 2; $info->name = 'user_relationships_api'; $info->object_type = 'user_relationships_api'; $info->objects = array('Requester' => 'requester', 'Requestee' => 'requestee', 'Relationship' => 'relationship'); $info->hooks = array('user_relationships_api' => array('approve', 'request')); foreach (user_relationships_types_load() as $type_obj) { $info->realms["user_relationships_" . $type_obj->rtid] = $type_obj->plural_name; } return $info; } /** * Implements hook_token_list(). */ function user_relationships_api_token_list($type = 'all') { if ($type == 'requester') { $tokens['user_relationships_api'] = array( 'requester' => t('The user who issued the connection request.'), ); } elseif ($type == 'relationship') { $tokens['user_relationships_api'] = array( 'requestee' => t('The user who approved the connnection request.'), 'relationship-name' => t('The relationship name (singular form)'), ); } return $tokens; } /** * Implements hook_token_values(). */ function user_relationships_api_token_values($type, $data = NULL, $options = array()) { if ($type == 'requester') { $r = $data; $token_values = array( 'requester' => theme('username', array('account' => user_load($r->uid))), ); } elseif ($type == 'requestee') { $r = $data; $token_values = array( 'requestee' => theme('username', array('account' => user_load($r->uid))), ); } elseif ($type == 'relationship') { // http://drupal.org/node/811222 need to unwrap the relationship object from an array sometimes if (is_array($data)) { foreach ($data as $object) { if (is_object($object)) { $r = $object; break; } } } else { $r = $data; } $r_type = user_relationships_type_load((int) $r->rtid); $token_values = array( 'requestee' => theme('username', array('account' => user_load($r->requestee_id))), 'requester' => theme('username', array('account' => user_load($r->requester_id))), 'relationship-name' => drupal_placeholder($r_type->name), ); } return isset($token_values) ? $token_values : NULL; }