View source
<?php
include_once 'message_subscribe_ui.features.inc';
function message_subscribe_ui_menu() {
$items = array();
$items['user/%user/message-subscribe'] = array(
'title' => 'Subscriptions',
'type' => MENU_LOCAL_TASK,
'page callback' => 'message_subscribe_ui_tab',
'page arguments' => array(
1,
),
'access callback' => 'message_subscribe_ui_tab_access',
'access arguments' => array(
1,
),
);
foreach (array_keys(message_subscribe_flag_get_flags()) as $flag_name) {
$items['user/%user/message-subscribe/' . $flag_name] = array(
'title callback' => 'message_subscribe_ui_tab_title',
'title arguments' => array(
1,
$flag_name,
),
'page callback' => 'message_subscribe_ui_tab',
'page arguments' => array(
1,
$flag_name,
),
'access callback' => 'message_subscribe_ui_tab_access',
'access arguments' => array(
1,
$flag_name,
),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function message_subscribe_permission() {
return array(
'administer message subscribe' => array(
'title' => t('Administer message subscribe'),
'description' => t('Administer user subscriptions.'),
),
);
}
function message_subscribe_ui_tab_access($account, $flag_name = NULL) {
global $user;
if (!$flag_name) {
$flag_name = key(message_subscribe_flag_get_flags());
}
if (!($flag = flag_get_flag($flag_name))) {
return;
}
if (isset($rel_flag->status) && $rel_flag->status === FALSE) {
return;
}
if (user_access('administer message subscribe')) {
return TRUE;
}
if (!$flag
->user_access('unflag', $account) || $account->uid != $user->uid) {
return;
}
return TRUE;
}
function message_subscribe_ui_tab_title($account, $flag_name = NULL) {
global $user;
if (!$flag_name) {
$flag_name = key(message_subscribe_flag_get_flags());
}
$flag = flag_get_flag($flag_name);
$count = 0;
if ($view = message_subscribe_ui_get_view($account, $flag_name)) {
$view->get_total_rows = TRUE;
$view
->execute();
$count = $view->total_rows;
}
$params = array(
'@flag' => $flag->title,
'@count' => $count,
'@name' => format_username($account),
);
return $account->uid == $user->uid ? t('@flag you follow (@count)', $params) : t('@flag @name follows (@count)', $params);
}
function message_subscribe_ui_tab($account, $flag_name = NULL) {
if (!$flag_name) {
$flag_name = key(message_subscribe_flag_get_flags());
}
$view = message_subscribe_ui_get_view($account, $flag_name);
return $view ? $view
->preview() : '';
}
function message_subscribe_ui_get_view($account, $flag_name) {
$flag = flag_get_flag($flag_name);
$entity_type = FLAG_API_VERSION == 3 ? $flag->entity_type : $flag->content_type;
$prefix = variable_get('message_subscribe_flag_prefix', 'subscribe') . '_';
list($view_name, $display_id) = explode(':', variable_get('message_' . $flag->name, $prefix . $entity_type . ':default'));
if (!($view = views_get_view($view_name))) {
$params = array(
'%name' => $view_name,
);
watchdog('message_subscribe', 'View %name does not exist.', $params);
drupal_set_message(t('There View %name does not exist.', $params), 'error');
return FALSE;
}
$view
->set_display($display_id);
$view
->set_arguments(array(
$account->uid,
));
$relationships = $view->display_handler
->get_option('relationships');
foreach ($relationships as $key => $relationship) {
if (strpos($key, 'flag_') !== 0) {
continue;
}
$rel_flag = flag_get_flag($relationship['flag']);
if (empty($rel_flag) || isset($rel_flag->status) && $rel_flag->status === FALSE) {
$params = array(
'%name' => $relationship['flag'],
);
watchdog('message_subscribe', 'Flag %name is not setup correctly. It is probably disabled or have no bundles configured.', $params);
drupal_set_message(t('Flag %name is not setup correctly. It is probably disabled or have no bundles configured.', $params), 'error');
return FALSE;
}
$rel_set = FALSE;
if (strpos($relationship['flag'], $prefix) === 0) {
$rel_set = TRUE;
}
elseif (strpos($relationship['flag'], 'email') === 0) {
$rel_set = TRUE;
$flag_name = 'email_' . str_replace($prefix, '', $flag_name);
}
if ($rel_set) {
$relationships[$key]['flag'] = $flag_name;
$view->display_handler
->set_option('relationships', $relationships);
}
}
return $view;
}