View source
<?php
define('UR_BLOCK_ALL_TYPES', 'all');
define('UR_BLOCK_SEPARATOR', '-');
define('UR_BLOCK_MY', 'my');
define('UR_BLOCK_USER', 'user');
function user_relationship_blocks_user_relationships_type_delete($rtype) {
db_delete('user_relationship_blocks')
->condition('bid', '%-' . UR_BLOCK_SEPARATOR . $rtype->rtid . '%', 'LIKE')
->execute();
}
function user_relationship_blocks_theme() {
return array(
'user_relationship_block_subject' => array(
'variables' => array(
'account' => NULL,
'rtid' => NULL,
'extra' => NULL,
),
),
'user_relationships_block' => array(
'variables' => array(
'account' => NULL,
'settings' => NULL,
'extra' => NULL,
),
'path' => drupal_get_path('module', 'user_relationship_blocks') . '/templates',
'template' => 'user_relationships-block',
),
'user_relationships_pending_block' => array(
'variables' => array(
'account' => NULL,
'settings' => NULL,
),
'path' => drupal_get_path('module', 'user_relationship_blocks') . '/templates',
'template' => 'user_relationships-pending_block',
),
'user_relationships_actions_block' => array(
'variables' => array(
'account' => NULL,
'settings' => NULL,
),
'path' => drupal_get_path('module', 'user_relationship_blocks') . '/templates',
'template' => 'user_relationships-actions_block',
),
);
}
function theme_user_relationship_block_subject($variables) {
$bid = $variables['bid'];
$account = $variables['account'];
$rtid = $variables['rtid'];
$extra = $variables['extra'];
if ($bid == 'pending') {
return t('Relationship requests');
}
elseif ($bid == 'actions') {
return t('Relationship actions');
}
elseif ($extra == 'you_to_them') {
global $user;
$rtype = user_relationships_type_load($rtid);
if ($account->uid == $user->uid) {
return t('I am a @rel_name of', user_relationships_type_translations($rtype));
}
else {
return t('@username is a @rel_name of', array(
'@username' => format_username($account),
) + user_relationships_type_translations($rtype));
}
}
else {
global $user;
if ($rtid == UR_BLOCK_ALL_TYPES) {
$output = $account->uid == $user->uid ? t('My relationships') : t("@username's relationships", array(
'@username' => format_username($account),
));
}
else {
$rtype = user_relationships_type_load($rtid);
$output = $account->uid == $user->uid ? t('My @rel_name', user_relationships_type_translations($rtype)) : t("@username's @rel_name", array(
'@username' => format_username($account),
) + user_relationships_type_translations($rtype));
}
return $output;
}
}
function user_relationship_blocks_block_configure($delta) {
$exploded = explode(UR_BLOCK_SEPARATOR, $delta);
while (count($exploded) < 3) {
$exploded[] = NULL;
}
list($block, $rtid, $extra) = $exploded;
if ($block == 'pending') {
return;
}
$settings = user_relationship_blocks_block_save($delta);
if ($block != 'actions') {
if ($rtid == UR_BLOCK_ALL_TYPES) {
$relationship_name = t('All');
}
else {
$type = user_relationships_type_load($rtid);
$relationship_name = user_relationships_type_get_name($type);
}
$form['size'] = array(
'#type' => 'textfield',
'#size' => 4,
'#weight' => 1,
'#required' => TRUE,
'#title' => t('Number of relationships to display in block'),
'#description' => t('Enter the maximum number of relationships to display in this block.'),
'#default_value' => $settings->size,
'#validate' => array(
'user_relationships_ui_setting_validation' => array(
array(
'is_positive' => array(
'size' => t('Number of relationships to display must be an integer greater than 0.'),
),
),
),
),
);
$user_identifier = $block == UR_BLOCK_MY ? t('currently logged in user') : t('author whose node is being viewed');
$msg = t("NOTE: This block displays @rel_name relationships of the @user_identifier.", array(
'@rel_name' => $relationship_name,
'@user_identifier' => $user_identifier,
));
if ($extra) {
$relation = $extra == 'you_to_them' ? t('requester') : t('requestee');
$msg .= "\n" . t("Because this relationship is one-way this block will show relationships where the @user_identifier is the @relation", array(
'@user_identifier' => $user_identifier,
'@relation' => $relation,
));
}
$form['sort'] = array(
'#type' => 'radios',
'#title' => t('Which relationships should be displayed'),
'#options' => array(
'newest' => t('Newest'),
'oldest' => t('Oldest'),
'random' => t('Random'),
),
'#default_value' => $settings->sort,
'#required' => TRUE,
'#weight' => 3,
'#suffix' => $msg,
);
}
$form['bid'] = array(
'#type' => 'hidden',
'#value' => $delta,
);
return $form;
}
function user_relationship_blocks_block_view($delta) {
global $user;
$exploded = explode(UR_BLOCK_SEPARATOR, $delta);
while (count($exploded) < 3) {
$exploded[] = NULL;
}
list($block_type, $rtid, $extra) = $exploded;
$is_my_block = $block_type == UR_BLOCK_MY || in_array($block_type, array(
'pending',
));
if ($is_my_block && !$user->uid) {
return;
}
$settings = user_relationship_blocks_block_save($delta);
$settings->rtid = $rtid;
$settings->block_type = $block_type;
if ($is_my_block) {
if (user_relationships_ui_check_access('view', $user, user_relationships_type_load($rtid))) {
$account = $user;
}
}
elseif ($uid = _user_relationship_blocks_get_uid($delta)) {
$account_loaded = user_load($uid);
if ($account_loaded && user_relationships_ui_check_access('view', $account_loaded, user_relationships_type_load($rtid))) {
$account = $account_loaded;
}
}
if (!empty($account)) {
$add_to_string = in_array($block_type, array(
'pending',
'actions',
)) ? "_{$block_type}" : '';
return array(
'subject' => theme('user_relationship_block_subject', array(
'bid' => $delta,
'account' => $account,
'rtid' => $rtid,
'extra' => $extra,
)),
'content' => theme("user_relationships{$add_to_string}_block", array(
'account' => $account,
'settings' => $settings,
'extra' => $extra,
)),
);
}
}
function _user_relationship_blocks_get_uid($delta) {
foreach (module_implements('user_relationship_blocks_get_uid') as $module) {
$function = $module . '_user_relationship_blocks_get_uid';
if ($uid = $function($delta)) {
return $uid;
}
}
if ($node = menu_get_object()) {
return $node->uid;
}
if ($user = menu_get_object('user')) {
return $user->uid;
}
if (arg(0) == 'blog' && arg(1) > 0) {
return arg(1);
}
}
function user_relationship_blocks_block_info() {
$my = UR_BLOCK_MY . UR_BLOCK_SEPARATOR;
$usr = UR_BLOCK_USER . UR_BLOCK_SEPARATOR;
$blocks = array(
$my . UR_BLOCK_ALL_TYPES => array(
'info' => t('My Relationships: All relationships'),
'cache' => DRUPAL_NO_CACHE,
),
$usr . UR_BLOCK_ALL_TYPES => array(
'info' => t('User Relationships: All relationships'),
'cache' => DRUPAL_NO_CACHE,
),
'pending' => array(
'info' => t('My Pending Relationships'),
'cache' => DRUPAL_NO_CACHE,
),
'actions' => array(
'info' => t('User Relationships: Actions'),
'cache' => DRUPAL_NO_CACHE,
),
);
$types = user_relationships_types_load();
foreach ($types as $type) {
$my_delta = "{$my}{$type->rtid}";
$usr_delta = "{$usr}{$type->rtid}";
$extras = array(
'' => '',
);
if ($type->is_oneway) {
$extras = array(
UR_BLOCK_SEPARATOR . 'you_to_them' => t('(You to Them, backward direction)'),
UR_BLOCK_SEPARATOR . 'them_to_you' => t('(Them to You, normal direction)'),
);
}
foreach ($extras as $token => $extra) {
$block_types = array(
"{$my_delta}{$token}" => t('My Relationships: @rel_name_plural @extra', array(
'@extra' => $extra,
) + user_relationships_type_translations($type)),
"{$usr_delta}{$token}" => t('User Relationships: @rel_name_plural @extra', array(
'@extra' => $extra,
) + user_relationships_type_translations($type)),
);
foreach ($block_types as $bid => $title) {
$blocks[$bid] = array(
'info' => $title,
'cache' => DRUPAL_NO_CACHE,
);
}
}
}
return $blocks;
}
function user_relationship_blocks_block_save($delta = NULL, $edit = NULL) {
$settings = drupal_static(__FUNCTION__, array());
if (isset($edit)) {
if (empty($edit['bid'])) {
$edit['bid'] = $edit['delta'];
}
db_merge('user_relationship_blocks')
->key(array(
'bid' => isset($edit['bid']) ? $edit['bid'] : $edit['delta'],
))
->fields(array(
'size' => $edit['size'],
'sort' => $edit['sort'],
))
->execute();
$settings[$delta] = (object) $edit;
}
elseif ($delta && (!isset($settings[$delta]) || !$settings[$delta])) {
$settings[$delta] = db_query("SELECT * FROM {user_relationship_blocks} WHERE bid = :bid", array(
':bid' => $delta,
))
->fetchObject();
if (!$settings[$delta]) {
$settings[$delta] = (object) array(
'size' => 10,
'bid' => $delta,
'sort' => 'newest',
);
}
return $settings[$delta];
}
else {
$settings = db_query("SELECT * FROM {user_relationship_blocks}")
->fetchAllAssoc('bid');
return $settings;
}
}
function template_preprocess_user_relationships_block(&$variables) {
$account =& $variables['account'];
$settings =& $variables['settings'];
$extra =& $variables['extra'];
$query_opts = array(
'include_user_info' => TRUE,
);
if (empty($settings->sort)) {
$settings->sort = 'newest';
}
if (empty($settings->size)) {
$settings->size = 10;
}
switch ($settings->sort) {
case 'newest':
$query_opts['order'] = array(
'changed',
'DESC',
);
break;
case 'oldest':
$query_opts['order'] = array(
'changed',
'ASC',
);
break;
case 'random':
$query_opts['order'] = 'RAND()';
break;
}
$query_opts['limit'] = $settings->size;
$key = $extra ? $extra == 'you_to_them' ? 'requester_id' : 'requestee_id' : 'user';
$args = array(
$key => $account->uid,
'approved' => TRUE,
);
if ($settings->rtid != UR_BLOCK_ALL_TYPES) {
$args['rtid'] = $settings->rtid;
}
$variables['relationships'] = user_relationships_load($args, $query_opts);
}
function template_preprocess_user_relationships_pending_block(&$variables) {
$account =& $variables['account'];
$variables['relationships'] = user_relationships_load(array(
'user' => $account->uid,
'approved' => FALSE,
), array(
'include_user_info' => TRUE,
));
}
function template_preprocess_user_relationships_actions_block(&$variables) {
$user =& $variables['user'];
$account =& $variables['account'];
if ($user != $account) {
$variables['current_relationships'] = user_relationships_ui_actions_between($user, $account, array(
'remove' => 1,
));
}
$variables['actions'] = user_relationships_ui_actions_between($user, $account, array(
'add' => 1,
'requested' => 1,
'received' => 1,
));
}