View source
<?php
include_once dirname(__FILE__) . '/flag.inc';
function flag_menu() {
$items = array();
$items['admin/build/flags'] = array(
'title' => 'Flags',
'page callback' => 'flag_admin_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'description' => 'Configure flags for marking content with arbitary information (such as <em>offensive</em> or <em>bookmarked</em>).',
'file' => 'includes/flag.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/flags/edit'] = array(
'title' => 'Edit flags',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/build/flags/delete'] = array(
'title' => 'Delete flags',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_delete_confirm',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/build/flags/list'] = array(
'title' => 'List',
'page callback' => 'flag_admin_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/build/flags/add'] = array(
'title' => 'Add',
'page callback' => 'flag_add_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['flag'] = array(
'title' => 'Flag',
'page callback' => 'flag_page',
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['flag/confirm'] = array(
'title' => 'Flag confirm',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_confirm',
),
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function flag_init() {
$path = drupal_get_path('module', 'flag');
if (module_exists('trigger')) {
include_once $path . '/includes/flag.actions.inc';
}
if (module_exists('token')) {
include_once $path . '/includes/flag.token.inc';
}
}
function flag_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'flag') . '/includes',
);
}
function flag_perm() {
return array(
'administer flags',
);
}
function flag_access($flag, $account = NULL) {
return $flag
->user_access($account);
}
function flag_content_enabled($flag, $content_type, $content_subtype = NULL) {
$return = $flag->content_type == $content_type && (!isset($content_subtype) || in_array($content_subtype, $flag->types));
return $return;
}
function flag_link($type, $object = NULL, $teaser = FALSE) {
if (!isset($object) || !flag_fetch_definition($type)) {
return;
}
global $user;
if (!$user->uid) {
return;
}
$flags = flag_get_flags($type);
foreach ($flags as $flag) {
if (!$flag
->user_access($user)) {
continue;
}
if (!$flag
->uses_hook_link($teaser)) {
continue;
}
if (!$flag
->applies_to_content_object($object)) {
continue;
}
$content_id = $flag
->get_content_id($object);
$links['flag-' . $flag->name] = array(
'title' => $flag
->theme($flag
->is_flagged($content_id) ? 'unflag' : 'flag', $content_id),
'html' => TRUE,
);
}
if (isset($links)) {
return $links;
}
}
function flag_flag_link($flag, $action, $content_id) {
$token = flag_get_token($content_id);
return array(
'href' => "flag/" . ($flag->link_type == 'confirm' ? 'confirm/' : '') . "{$action}/{$flag->name}/{$content_id}",
'query' => drupal_get_destination() . ($flag->link_type == 'confirm' ? '' : '&token=' . $token),
);
}
function flag_flag_link_types() {
return array(
'toggle' => t('JavaScript toggle'),
'normal' => t('Normal link'),
'confirm' => t('Confirmation form'),
);
}
function flag_content_extra_fields($type_name) {
$extra = array();
$flags = flag_get_flags('node', $type_name);
foreach ($flags as $name => $flag) {
if (!$flag->show_on_form) {
unset($flags[$name]);
}
}
if (!empty($flags)) {
$extra['flag'] = array(
'label' => t('Flags'),
'description' => t('Flags fieldset.'),
'weight' => 0,
);
}
return $extra;
}
function flag_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$flags = flag_get_flags('node', $form['#node_type']->type, $user);
foreach ($flags as $flag) {
if ($flag->show_on_form) {
$var = 'flag_' . $flag->name . '_default';
$form['workflow']['flag'][$var] = array(
'#type' => 'checkbox',
'#title' => $flag
->get_label('flag_short'),
'#default_value' => variable_get($var . '_' . $form['#node_type']->type, 0),
'#return_value' => 1,
);
}
}
if (isset($form['workflow']['flag'])) {
$form['workflow']['flag'] += array(
'#type' => 'item',
'#title' => t('Default flags'),
'#description' => t('Above are the <a href="@flag-url">flags</a> you elected to show on the node editing form. You may specify their initial state here.', array(
'@flag-url' => url('admin/build/flags'),
)),
'#prefix' => '<div class="form-checkboxes">',
'#suffix' => '</div>',
);
}
}
elseif (isset($form['type']) && isset($form['#node']) && $form_id == $form['type']['#value'] . '_node_form') {
if (!$user->uid) {
return;
}
$nid = !empty($form['nid']['#value']) ? $form['nid']['#value'] : NULL;
$flags = flag_get_flags('node', $form['type']['#value'], $user);
foreach ($flags as $name => $flag) {
if (!$flag->show_on_form) {
unset($flags[$name]);
}
}
if (count($flags)) {
$form['flag'] = array(
'#type' => 'fieldset',
'#weight' => module_exists('content') ? content_extra_field_weight($form['#node']->type, 'flag') : 1,
'#tree' => TRUE,
'#title' => t('Flags'),
'#collapsible' => TRUE,
);
}
foreach ($flags as $flag) {
if (isset($form['#node']->flag[$flag->name])) {
$flag_status = $form['#node']->flag[$flag->name];
}
else {
$flag_status_default = variable_get('flag_' . $flag->name . '_default_' . $form['type']['#value'], 0);
$flag_status = $nid ? $flag
->is_flagged($nid) : $flag_status_default;
}
$form['flag'][$flag->name] = array(
'#type' => 'checkbox',
'#title' => $flag
->get_label('flag_short', $nid),
'#description' => $flag
->get_label('flag_long', $nid),
'#default_value' => $flag_status,
'#return_value' => 1,
);
}
}
}
function flag_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
global $user;
switch ($op) {
case 'update':
case 'insert':
$remembered = FALSE;
if (isset($node->flag)) {
foreach ($node->flag as $name => $state) {
$flag = flag_get_flag($name);
if (!$remembered) {
$flag
->remember_content($node->nid, $node);
$remembered = TRUE;
}
flag($state ? 'flag' : 'unflag', $name, $node->nid);
}
}
break;
case 'delete':
foreach (flag_get_flags('node') as $flag) {
if (!$flag->i18n || empty($node->tnid)) {
db_query("DELETE FROM {flag_content} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
db_query("DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d", $flag->fid, $node->nid);
}
}
break;
case 'translation_change':
if (isset($node->translation_change)) {
$content_id = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
foreach (flag_get_flags('node') as $flag) {
if ($flag->i18n) {
db_query("UPDATE {flag_content} SET content_id = %d WHERE fid = %d AND content_id = %d", $content_id, $flag->fid, $node->translation_change['old_tnid']);
db_query("UPDATE {flag_counts} SET content_id = %d WHERE fid = %d AND content_id = %d", $content_id, $flag->fid, $node->translation_change['old_tnid']);
}
}
}
break;
}
}
function flag_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'delete':
$result = db_query("SELECT fc.fid, fc.content_id, c.count FROM {flag_content} fc LEFT JOIN {flag_counts} c ON fc.content_id = c.content_id AND fc.content_type = c.content_type WHERE fc.uid = %d", $account->uid);
while ($flag_data = db_fetch_object($result)) {
$flag_data->count--;
if ($flag_data->count > 0) {
db_query("UPDATE {flag_counts} SET count = %d WHERE fid = %d AND content_id = %d", $flag_data->count, $flag_data->fid, $flag_data->content_id);
}
elseif ($flag_data->count == 0) {
db_query("DELETE FROM {flag_counts} WHERE fid = %d AND content_id = %d", $flag_data->fid, $flag_data->content_id);
}
}
db_query("DELETE FROM {flag_content} WHERE uid = %d", $account->uid);
break;
case 'view':
$flags = flag_get_flags('user');
$flag_items = array();
foreach ($flags as $flag) {
if (!$flag
->user_access()) {
continue;
}
if (!$flag
->applies_to_content_object($account)) {
continue;
}
if (!$flag
->uses_hook_link(array())) {
continue;
}
$flag_items[$flag->name] = array(
'#type' => 'user_profile_item',
'#title' => $flag
->get_title($account->uid),
'#value' => $flag
->theme($flag
->is_flagged($account->uid) ? 'unflag' : 'flag', $account->uid),
'#attributes' => array(
'class' => 'flag-profile-' . $flag->name,
),
);
}
if (!empty($flag_items)) {
$account->content['flags'] = $flag_items;
$account->content['flags'] += array(
'#type' => 'user_profile_category',
'#title' => t('Actions'),
'#attributes' => array(
'class' => 'flag-profile',
),
);
}
break;
}
}
function flag_node_type($op, $info) {
switch ($op) {
case 'delete':
db_query("DELETE FROM {flag_types} WHERE type = '%s'", $info->type);
break;
}
}
function flag_page($action = NULL, $flag_name = NULL, $content_id = NULL) {
$js = isset($_REQUEST['js']);
$token = $_REQUEST['token'];
if (!flag_check_token($token, $content_id)) {
$error = t('Bad token. You seem to have followed an invalid link.');
}
else {
$result = flag($action, $flag_name, $content_id);
if (!$result) {
$error = t('You are not allowed to flag, or unflag, this content.');
}
}
if (isset($error)) {
if ($js) {
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
print drupal_to_js(array(
'status' => FALSE,
'errorMessage' => $error,
));
exit;
}
else {
drupal_set_message($error);
drupal_access_denied();
return;
}
}
if ($js) {
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
$flag = flag_get_flag($flag_name);
$flag->link_type = 'toggle';
print drupal_to_js(array(
'status' => TRUE,
'newLink' => $flag
->theme($flag
->is_flagged($content_id) ? 'unflag' : 'flag', $content_id, TRUE),
'contentId' => $content_id,
'contentType' => $flag->content_type,
'flagName' => $flag->name,
'flagStatus' => $flag
->is_flagged($content_id) ? 'flagged' : 'unflagged',
));
exit;
}
else {
$flag = flag_get_flag($flag_name);
drupal_set_message($flag
->get_label($action . '_message', $content_id));
drupal_goto();
}
}
function flag_confirm(&$form_state, $action = 'flag', $flag_name, $content_id = '') {
$form = array();
$form['action'] = array(
'#type' => 'value',
'#value' => $action,
);
$form['flag_name'] = array(
'#type' => 'value',
'#value' => $flag_name,
);
$form['content_id'] = array(
'#type' => 'value',
'#value' => $content_id,
);
$flag = flag_get_flag($flag_name);
$question = $flag
->get_label($action . '_confirmation', $content_id);
$path = isset($_GET['destination']) ? $_GET['destination'] : '<front>';
$yes = $flag
->get_label($action . '_short', $content_id);
return confirm_form($form, $question, $path, '', $yes);
}
function flag_confirm_submit(&$form, &$form_state) {
$action = $form_state['values']['action'];
$flag_name = $form_state['values']['flag_name'];
$content_id = $form_state['values']['content_id'];
$result = flag($action, $flag_name, $content_id);
if (!$result) {
drupal_set_message(t('You are not allowed to flag, or unflag, this content.'));
}
else {
$flag = flag_get_flag($flag_name);
drupal_set_message($flag
->get_label($action . '_message', $content_id));
}
}
function flag($action, $flag_name, $content_id, $account = NULL) {
if (!($flag = flag_get_flag($flag_name))) {
return FALSE;
}
return $flag
->flag($action, $content_id, $account);
}
function flag_flag($action, $flag, $content_id, $account) {
if (module_exists('trigger')) {
$flag_action = $flag
->get_flag_action($content_id);
$flag_action->action = $action;
$context = (array) $flag_action;
$aids = _trigger_get_hook_aids($action, $action);
foreach ($aids as $aid => $action_info) {
if ($aid) {
actions_do($aid, $flag, $context);
}
}
}
if (module_exists('rules')) {
$event_name = ($action == 'flag' ? 'flag_flagged_' : 'flag_unflagged_') . $flag->name;
$arguments = array(
'flag' => $flag,
'flag_content_id' => $content_id,
'flagging_user' => $account,
);
rules_invoke_event($event_name, $arguments);
}
}
function flag_node_operations() {
global $user;
$flags = flag_get_flags('node', NULL, $user);
$operations = array();
foreach ($flags as $flag) {
$operations['flag_' . $flag->name] = array(
'label' => $flag
->get_label('flag_short'),
'callback' => 'flag_nodes',
'callback arguments' => array(
'flag',
$flag->name,
),
'behavior' => array(),
);
$operations['unflag_' . $flag->name] = array(
'label' => $flag
->get_label('unflag_short'),
'callback' => 'flag_nodes',
'callback arguments' => array(
'unflag',
$flag->name,
),
'behavior' => array(),
);
}
return $operations;
}
function flag_nodes($nodes, $action, $flag_name) {
$performed = FALSE;
foreach ($nodes as $nid) {
$performed |= flag($action, $flag_name, $nid);
}
if ($performed) {
drupal_set_message(t('The update has been performed.'));
}
}
function flag_mail($key, &$message, $params) {
switch ($key) {
case 'over_threshold':
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
break;
}
}
function flag_theme() {
$path = drupal_get_path('module', 'flag') . '/theme';
return array(
'flag' => array(
'arguments' => array(
'flag' => NULL,
'action' => NULL,
'content_id' => NULL,
'after_flagging' => FALSE,
),
'template' => 'flag',
'pattern' => 'flag__',
'path' => $path,
),
'flag_token_help' => array(
'arguments' => array(
'types' => NULL,
'prefix' => NULL,
'suffix' => NULL,
),
),
'flag_admin_page' => array(
'arguments' => array(
'flags' => NULL,
'default_flags' => NULL,
),
),
'flag_rules_radios' => array(
'arguments' => array(),
),
);
}
function template_preprocess_flag(&$variables) {
static $first_time = TRUE;
$flag =& $variables['flag'];
$action = $variables['action'];
$content_id = $variables['content_id'];
$link_types = flag_get_link_types();
$link_type_module = $link_types[$flag->link_type]['module'];
$link = module_invoke($link_type_module, 'flag_link', $flag, $action, $content_id);
if (isset($link['title']) && empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
if ($flag->link_type == 'toggle' && $first_time) {
$variables['setup'] = $first_time;
$first_time = FALSE;
}
else {
$variables['setup'] = FALSE;
}
$variables['link_href'] = check_url(url($link['href'], $link));
$variables['link_text'] = isset($link['title']) ? $link['title'] : $flag
->get_label($action . '_short', $content_id);
$variables['link_title'] = isset($link['attributes']['title']) ? check_plain($link['attributes']['title']) : check_plain(strip_tags($flag
->get_label($action . '_long', $content_id)));
$variables['flag_name_css'] = str_replace('_', '-', $flag->name);
$variables['last_action'] = $action == 'flag' ? 'unflagged' : 'flagged';
$variables['flag_classes_array'] = array();
$variables['flag_classes_array'][] = 'flag';
$variables['flag_classes_array'][] = $variables['action'] . '-action';
$variables['flag_classes_array'][] = 'flag-link-' . $flag->link_type;
if (isset($link['attributes']['class'])) {
$variables['flag_classes_array'][] = $link['attributes']['class'];
}
if ($variables['after_flagging']) {
$inverse_action = $action == 'flag' ? 'unflag' : 'flag';
$variables['message_text'] = $flag
->get_label($inverse_action . '_message', $content_id);
$variables['flag_classes_array'][] = $variables['last_action'];
}
$variables['flag_classes'] = implode(' ', $variables['flag_classes_array']);
}
function _flag_format_plural($count, $format) {
$elements = explode('|', $format ? $format : '@count', 3);
if (count($elements) == 3) {
list($zero, $singular, $plural) = $elements;
}
elseif (count($elements) == 2) {
list($singular, $plural) = $elements;
$zero = NULL;
}
else {
$singular = $plural = $elements[0];
$zero = NULL;
}
if (isset($zero) && intval($count) == 0) {
return $zero;
}
else {
return format_plural(intval($count), $singular, $plural);
}
}
function _flag_get_flag_names() {
$flags = flag_get_flags();
$flag_names = array();
foreach ($flags as $flag) {
$flag_names[$flag->fid] = $flag->name;
}
return $flag_names;
}
function _flag_link_type_options() {
$options = array();
$types = flag_get_link_types();
foreach ($types as $type_name => $type) {
$options[$type_name] = $type['title'];
}
return $options;
}
function flag_get_counts($content_type, $content_id, $reset = FALSE) {
static $counts;
if ($reset) {
$counts = array();
if (!isset($content_type)) {
return;
}
}
if (!isset($counts[$content_type][$content_id])) {
$counts[$content_type][$content_id] = array();
$result = db_query("SELECT f.name, fc.count FROM {flags} f LEFT JOIN {flag_counts} fc ON f.fid = fc.fid WHERE fc.content_type = '%s' AND fc.content_id = %d", $content_type, $content_id);
while ($row = db_fetch_object($result)) {
$counts[$content_type][$content_id][$row->name] = $row->count;
}
}
return $counts[$content_type][$content_id];
}
function flag_get_flag($name = NULL, $fid = NULL) {
$flags = flag_get_flags();
if (isset($name)) {
if (isset($flags[$name])) {
return $flags[$name];
}
}
elseif (isset($fid)) {
foreach ($flags as $flag) {
if ($flag->fid == $fid) {
return $flag;
}
}
}
}
function flag_get_flags($content_type = NULL, $content_subtype = NULL, $account = NULL, $reset = FALSE) {
static $flags;
if (!isset($flags) || $reset) {
$flags = array();
$result = db_query("SELECT f.*, fn.type FROM {flags} f LEFT JOIN {flag_types} fn ON fn.fid = f.fid");
while ($row = db_fetch_object($result)) {
if (!isset($flags[$row->name])) {
$flags[$row->name] = flag_flag::factory_by_row($row);
}
else {
$flags[$row->name]->types[] = $row->type;
}
}
$default_flags = flag_get_default_flags();
foreach ($default_flags as $name => $default_flag) {
if ($default_flag->status && !isset($flags[$name])) {
$default_flag
->save();
$flags[$name] = $default_flag;
}
if (isset($flags[$name])) {
$flags[$name]->module = $default_flag->module;
if (isset($default_flag->locked)) {
$flags[$name]->locked = $default_flag->locked;
foreach ($default_flag->locked as $property) {
$flags[$name]->{$property} = $default_flag->{$property};
}
}
}
}
}
$filtered_flags = $flags;
if (isset($content_type) || isset($content_subtype)) {
foreach ($filtered_flags as $name => $flag) {
if (!flag_content_enabled($flag, $content_type, $content_subtype)) {
unset($filtered_flags[$name]);
}
}
}
if (isset($account) && $account->uid != 1) {
foreach ($filtered_flags as $name => $flag) {
if (!flag_access($flag, $account)) {
unset($filtered_flags[$name]);
}
}
}
return $filtered_flags;
}
function flag_get_default_flags($include_disabled = FALSE) {
$default_flags = array();
$flag_status = variable_get('flag_default_flag_status', array());
foreach (module_implements('flag_default_flags') as $module) {
$function = $module . '_flag_default_flags';
foreach ($function() as $config) {
$flag = flag_flag::factory_by_array($config);
$flag->module = $module;
if (!isset($flag_status[$flag->name]) && (!isset($flag->status) || $flag->status) || !empty($flag_status[$flag->name])) {
$flag->status = TRUE;
$default_flags[$flag->name] = $flag;
}
elseif ($include_disabled) {
$flag->status = FALSE;
$default_flags[$flag->name] = $flag;
}
}
}
return $default_flags;
}
function flag_get_user_flags($content_type, $content_id = NULL, $uid = NULL, $reset = FALSE) {
static $flagged_content;
if ($reset) {
$flagged_content = array();
if (!isset($content_type)) {
return;
}
}
$uid = !isset($uid) ? $GLOBALS['user']->uid : $uid;
if (isset($content_id)) {
if (!isset($flagged_content[$uid][$content_type][$content_id])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$content_type][$content_id] = array();
$result = db_query("SELECT * FROM {flag_content} WHERE content_type = '%s' AND content_id = %d AND (uid = %d OR uid = 0)", $content_type, $content_id, $uid);
while ($flag_content = db_fetch_object($result)) {
$flagged_content[$uid][$content_type][$content_id][$flag_names[$flag_content->fid]] = $flag_content;
}
}
return $flagged_content[$uid][$content_type][$content_id];
}
else {
if (!isset($flagged_content[$uid][$content_type]['all'])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$content_type]['all'] = array();
$result = db_query("SELECT * FROM {flag_content} WHERE content_type = '%s' AND (uid = %d OR uid = 0)", $content_type, $uid);
while ($flag_content = db_fetch_object($result)) {
$flagged_content[$uid][$content_type]['all'][$flag_names[$flag_content->fid]][$flag_content->content_id] = $flag_content;
}
}
return $flagged_content[$uid][$content_type]['all'];
}
}
function flag_get_content_flags($content_type, $content_id, $reset = FALSE) {
static $content_flags;
if (!isset($content_flags[$content_type][$content_id]) || $reset) {
$flag_names = _flag_get_flag_names();
$result = db_query("SELECT * FROM {flag_content} WHERE content_type = '%s' AND content_id = %d ORDER BY timestamp DESC", $content_type, $content_id);
while ($flag_content = db_fetch_object($result)) {
$content_flags[$content_type][$content_id]['users'][$flag_content->uid][$flag_names[$flag_content->fid]] = $flag_content;
}
}
return $content_flags[$content_type][$content_id]['users'];
}
function flag_create_link($flag_name, $content_id) {
$flag = flag_get_flag($flag_name);
if (!$flag) {
return;
}
if (!$flag
->user_access()) {
return;
}
if (!$flag
->applies_to_content_id($content_id)) {
return;
}
return $flag
->theme($flag
->is_flagged($content_id) ? 'unflag' : 'flag', $content_id);
}
function flag_get_link_types($reset = FALSE) {
static $link_types;
if (!isset($link_types) || $reset) {
$link_types = array();
foreach (module_implements('flag_link_types') as $module) {
$module_types = module_invoke($module, 'flag_link_types');
foreach ($module_types as $type_name => $type_title) {
$link_types[$type_name] = array(
'module' => $module,
'title' => $type_title,
);
}
}
}
return $link_types;
}
function flag_get_token($nid) {
return drupal_get_token($nid);
}
function flag_check_token($token, $seed) {
return drupal_get_token($seed) == $token;
}