View source
<?php
define('FLAG_API_VERSION', 3);
define('FLAG_ADMIN_PATH', 'admin/structure/flags');
define('FLAG_ADMIN_PATH_START', 3);
function flag_entity_info() {
$return = array(
'flagging' => array(
'label' => t('Flagging'),
'controller class' => 'FlaggingController',
'base table' => 'flagging',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'flagging_id',
'bundle' => 'flag_name',
),
'bundle keys' => array(
'bundle' => 'name',
),
'bundles' => array(),
'save callback' => 'flagging_save',
'creation callback' => 'flagging_create',
),
);
if (db_table_exists('flag')) {
$result = db_query("SELECT name, title FROM {flag}");
$flag_names = $result
->fetchAllKeyed();
foreach ($flag_names as $flag_name => $flag_title) {
$return['flagging']['bundles'][$flag_name] = array(
'label' => $flag_title,
'admin' => array(
'path' => FLAG_ADMIN_PATH . '/manage/%flag',
'real path' => FLAG_ADMIN_PATH . '/manage/' . $flag_name,
'bundle argument' => FLAG_ADMIN_PATH_START + 1,
'access arguments' => array(
'administer flags',
),
),
);
}
}
return $return;
}
function flagging_load($flagging_id, $reset = FALSE) {
$result = entity_load('flagging', array(
$flagging_id,
), array(), $reset);
return reset($result);
}
function flagging_create($values = array()) {
$flagging = (object) array();
if (!isset($values['flag_name'])) {
if (isset($values['fid'])) {
$flag = flag_get_flag(NULL, $values['fid']);
$values['flag_name'] = $flag->name;
}
}
foreach ($values as $key => $value) {
$flagging->{$key} = $value;
}
return $flagging;
}
function flagging_save($flagging) {
if (isset($flagging->flag_name)) {
$flag = flag_get_flag($flagging->flag_name);
}
else {
$flag = flag_get_flag(NULL, $flagging->fid);
}
if (!$flag) {
throw new Exception('Flag not found for flagging entity.');
}
$flagging->fid = $flag->fid;
$flagging->flag_name = $flag->name;
if (!empty($flagging->uid)) {
$account = user_load($flagging->uid);
}
else {
$account = NULL;
}
$result = $flag
->flag('flag', $flagging->entity_id, $account, FALSE, $flagging);
if (!$result) {
throw new Exception('Flag action not allowed for given flagging entity properties.');
}
}
function flag_entity_query_alter(EntityFieldQuery $query) {
$conditions =& $query->entityConditions;
if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'flagging' && isset($conditions['bundle'])) {
$query
->addTag('flagging_flag_names');
$query
->addMetaData('flag_name_value', $conditions['bundle']['value']);
$query
->addMetaData('flag_name_operator', $conditions['bundle']['operator']);
unset($conditions['bundle']);
}
}
function flag_query_flagging_flag_names_alter(QueryAlterableInterface $query) {
$tables =& $query
->getTables();
if (!isset($tables['flagging'])) {
$field_table = reset($tables);
$field_table_alias = $field_table['alias'];
$query
->join('flagging', 'flagging', "{$field_table_alias}.entity_id = flagging.fid");
}
$value = $query
->getMetaData('flag_name_value');
$operator = $query
->getMetaData('flag_name_operator');
$query
->join('flag', 'f', 'flagging.fid = f.fid');
$query
->condition('f.name', $value, $operator);
}
function flag_menu() {
$items[FLAG_ADMIN_PATH] = array(
'title' => 'Flags',
'page callback' => 'flag_admin_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'description' => 'Configure flags for marking content with arbitrary information (such as <em>offensive</em> or <em>bookmarked</em>).',
'file' => 'includes/flag.admin.inc',
);
$items[FLAG_ADMIN_PATH . '/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[FLAG_ADMIN_PATH . '/add'] = array(
'title' => 'Add flag',
'page callback' => 'flag_add_page',
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
);
$items[FLAG_ADMIN_PATH . '/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_import_form',
),
'access arguments' => array(
'use flag import',
),
'file' => 'includes/flag.export.inc',
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
);
$items[FLAG_ADMIN_PATH . '/export'] = array(
'title' => 'Export',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_export_form',
),
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.export.inc',
'type' => MENU_LOCAL_ACTION,
'weight' => 3,
);
$items[FLAG_ADMIN_PATH . '/manage/%flag'] = array(
'load arguments' => array(
TRUE,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_form',
FLAG_ADMIN_PATH_START + 1,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'title callback' => '_flag_menu_title',
'title arguments' => array(
FLAG_ADMIN_PATH_START + 1,
),
);
$items[FLAG_ADMIN_PATH . '/manage/%flag/edit'] = array(
'load arguments' => array(
TRUE,
),
'title' => 'Edit flag',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[FLAG_ADMIN_PATH . '/manage/%flag/export'] = array(
'title' => 'Export',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_export_form',
FLAG_ADMIN_PATH_START + 1,
),
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.export.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items[FLAG_ADMIN_PATH . '/manage/%flag/delete'] = array(
'title' => 'Delete flag',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_delete_confirm',
FLAG_ADMIN_PATH_START + 1,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.admin.inc',
'type' => MENU_CALLBACK,
);
$items[FLAG_ADMIN_PATH . '/manage/%flag/update'] = array(
'load arguments' => array(
TRUE,
),
'title' => 'Update',
'page callback' => 'flag_update_page',
'page arguments' => array(
FLAG_ADMIN_PATH_START + 1,
),
'access arguments' => array(
'administer flags',
),
'file' => 'includes/flag.export.inc',
'type' => MENU_CALLBACK,
);
$items['flag/%/%flag/%'] = array(
'title' => 'Flag',
'page callback' => 'flag_page',
'page arguments' => array(
1,
2,
3,
),
'access callback' => 'flag_page_access',
'access arguments' => array(
1,
2,
3,
),
'file' => 'includes/flag.pages.inc',
'type' => MENU_CALLBACK,
);
$items['flag/confirm/%/%flag/%'] = array(
'title' => 'Flag confirm',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flag_confirm',
2,
3,
4,
),
'access callback' => 'flag_page_access',
'access arguments' => array(
2,
3,
4,
),
'file' => 'includes/flag.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function flag_page_access($action, $flag, $entity_id) {
$access = $flag
->access($entity_id, $action);
return $access;
}
function flag_admin_menu_map() {
if (!user_access('administer flags')) {
return;
}
$map = array();
$map[FLAG_ADMIN_PATH . '/manage/%flag'] = array(
'parent' => FLAG_ADMIN_PATH,
'arguments' => array(
array(
'%flag' => array_keys(flag_get_flags()),
),
),
);
return $map;
}
function flag_load($flag_name, $include_disabled = FALSE) {
if ($flag = flag_get_flag($flag_name)) {
return $flag;
}
else {
if ($include_disabled) {
$default_flags = flag_get_default_flags(TRUE);
if (isset($default_flags[$flag_name])) {
return $default_flags[$flag_name];
}
}
}
return FALSE;
}
function _flag_menu_title($flag) {
return $flag ? $flag
->get_title() : '';
}
function flag_help($path, $arg) {
switch ($path) {
case FLAG_ADMIN_PATH:
$output = '<p>' . t('This page lists all the <em>flags</em> that are currently defined on this system.') . '</p>';
return $output;
case FLAG_ADMIN_PATH . '/add':
$output = '<p>' . t('Select the type of flag to create. An individual flag can only affect one type of object. This cannot be changed once the flag is created.') . '</p>';
return $output;
case FLAG_ADMIN_PATH . '/manage/%/fields':
$link_types = flag_get_link_types();
$form_link_types = array();
foreach (flag_get_link_types() as $link_type) {
if ($link_type['provides form']) {
$form_link_types[] = '<em>' . $link_type['title'] . '</em>';
}
}
$flag = menu_get_object('flag', FLAG_ADMIN_PATH_START + 1);
$output = '<p>' . t('Flags can have fields added to them. For example, a "Spam" flag could have a <em>Reason</em> field where a user could type in why he believes the item flagged is spam. A "Bookmarks" flag could have a <em>Folder</em> field into which a user could arrange her bookmarks.') . '</p>';
$output .= '<p>' . t('On this page you can add fields to flags, delete them, and otherwise manage them.') . '</p>';
if ($flag->link_type == 'form') {
}
elseif ($link_types[$flag->link_type]['provides form']) {
$output .= t("Field values may be edited when flaggings are created because this flag's link type shows a form for the flagging. However, to edit field values on existing flaggings, you will need to set your flag to use the <em>Flagging form</em> link type. This is provided by the <em><a href='!flagging-form-url'>Flagging Form</a></em> module.", array(
'!flagging-form-url' => 'http://drupal.org/project/flagging_form',
));
if (!module_exists('flagging_form')) {
$output .= ' <span class="warning">' . t("You do not currently have this module enabled.") . '</span>';
}
$output .= '</p>';
}
else {
$output .= '<p class="warning">' . t("To allow users to enter values for fields you will need to <a href='!form-link-type-url'>set your flag</a> to use one of the following link types which allow users to access the flagging form: !link-types-list. (In case a form isn't used, the fields are assigned their default values.)", array(
'!form-link-type-url' => url('admin/structure/flags/manage/' . $flag->name, array(
'fragment' => 'edit-link-type',
)),
'!link-types-list' => implode(', ', $form_link_types),
)) . '</p>';
$output .= '<p>' . t("Additionally, to edit field values on existing flaggings, you will need to set your flag to use the Flagging form link type. This is provided by the <em><a href='!flagging-form-url'>Flagging Form</a></em> module.", array(
'!flagging-form-url' => 'http://drupal.org/project/flagging_form',
));
if (!module_exists('flagging_form')) {
$output .= ' <span class="warning">' . t("You do not currently have this module enabled.") . '</span>';
}
$output .= '</p>';
}
return $output;
}
}
function flag_init() {
module_load_include('inc', 'flag', 'includes/flag.actions');
}
function flag_hook_info() {
$hooks['flag_type_info'] = array(
'group' => 'flag',
);
$hooks['flag_type_info_alter'] = array(
'group' => 'flag',
);
$hooks['flag_link_type_info'] = array(
'group' => 'flag',
);
$hooks['flag_link_type_info_alter'] = array(
'group' => 'flag',
);
return $hooks;
}
function flag_fetch_definition($entity_type = NULL) {
$definitions =& drupal_static(__FUNCTION__);
if (!isset($definitions)) {
if ($cache = cache_get('flag_type_info')) {
$definitions = $cache->data;
}
else {
$definitions = module_invoke_all('flag_type_info');
drupal_alter('flag_type_info', $definitions);
cache_set('flag_type_info', $definitions);
}
}
if (isset($entity_type)) {
if (isset($definitions[$entity_type])) {
return $definitions[$entity_type];
}
}
else {
return $definitions;
}
}
function flag_get_types() {
$types =& drupal_static(__FUNCTION__);
if (!isset($types)) {
$types = array_keys(flag_fetch_definition());
}
return $types;
}
function flag_create_handler($entity_type) {
$definition = flag_fetch_definition($entity_type);
if (isset($definition) && class_exists($definition['handler'])) {
$handler = new $definition['handler']();
}
else {
$handler = new flag_broken();
}
$handler->entity_type = $entity_type;
$handler
->construct();
return $handler;
}
function flag_permission() {
$permissions = array(
'administer flags' => array(
'title' => t('Administer flags'),
'description' => t('Create and edit site-wide flags.'),
),
'use flag import' => array(
'title' => t('Use flag importer'),
'description' => t('Access the flag import functionality.'),
'restrict access' => TRUE,
),
);
drupal_static_reset('flag_get_flags');
$flags = flag_get_flags();
foreach ($flags as $flag_name => $flag) {
$permissions += $flag
->get_permissions();
}
return $permissions;
}
function flag_form_user_admin_permissions_alter(&$form, &$form_state, $form_id) {
if (!module_exists('session_api')) {
$flags = flag_get_flags();
foreach ($flags as $flag_name => $flag) {
$form['checkboxes'][DRUPAL_ANONYMOUS_RID]["flag {$flag_name}"]['#disabled'] = TRUE;
$form['checkboxes'][DRUPAL_ANONYMOUS_RID]["unflag {$flag_name}"]['#disabled'] = TRUE;
}
}
}
function flag_flag_link($flag, $action, $entity_id) {
$token = flag_get_token($entity_id);
return array(
'href' => 'flag/' . ($flag->link_type == 'confirm' ? 'confirm/' : '') . "{$action}/{$flag->name}/{$entity_id}",
'query' => drupal_get_destination() + ($flag->link_type == 'confirm' ? array() : array(
'token' => $token,
)),
);
}
function flag_field_extra_fields() {
$extra = array();
$flags = flag_get_flags();
foreach ($flags as $name => $flag) {
if (!$flag instanceof flag_entity) {
continue;
}
$applicable_bundles = $flag->types;
if (empty($applicable_bundles)) {
$entity_info = entity_get_info($flag->entity_type);
$applicable_bundles = array_keys($entity_info['bundles']);
}
foreach ($applicable_bundles as $bundle_name) {
if ($flag->show_on_form) {
$extra[$flag->entity_type][$bundle_name]['form']['flag'] = array(
'label' => t('Flags'),
'description' => t('Checkboxes for toggling flags'),
'weight' => 10,
);
}
if ($flag->show_as_field) {
$extra[$flag->entity_type][$bundle_name]['display']['flag_' . $name] = array(
'label' => t('Flag: @title', array(
'@title' => $flag->title,
)),
'description' => t('Individual flag link'),
'weight' => 10,
);
}
}
}
return $extra;
}
function flag_form_node_type_form_alter(&$form, &$form_state, $form_id) {
global $user;
$flags = flag_get_flags('node', $form['#node_type']->type, $user);
foreach ($flags as $flag) {
if ($flag->show_on_form) {
$flag
->remember_entity('fake', (object) array(
'nid' => NULL,
'type' => $form['#node_type']->type,
'title' => '',
));
$var = 'flag_' . $flag->name . '_default';
$form['workflow']['flag'][$var] = array(
'#type' => 'checkbox',
'#title' => $flag
->get_label('flag_short', 'fake'),
'#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(FLAG_ADMIN_PATH),
)),
'#prefix' => '<div class="form-checkboxes">',
'#suffix' => '</div>',
);
}
}
function flag_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list($id) = entity_extract_ids($entity_type, $entity);
if (empty($id)) {
$id = NULL;
}
$is_existing_entity = !empty($id);
$flags = flag_get_flags($entity_type);
$flags_in_form = 0;
$flags_visible = 0;
foreach ($flags as $flag) {
if (!$flag->show_on_form) {
continue;
}
if ($is_existing_entity) {
$flag_status = $flag
->is_flagged($id);
}
else {
$flag_status = FALSE;
if ($entity_type == 'node') {
$node_type = $entity->type;
$flag_status = variable_get('flag_' . $flag->name . '_default_' . $node_type, 0);
}
$id = 'new';
$flag
->remember_entity($id, $entity);
}
global $user;
$access = $flag
->access($id, $flag_status ? 'unflag' : 'flag');
if (!$access && !$flag->global) {
continue;
}
$form['flag'][$flag->name] = array(
'#type' => 'checkbox',
'#title' => $flag
->get_label('flag_short', $id),
'#description' => $flag
->get_label('flag_long', $id),
'#default_value' => $flag_status,
'#return_value' => 1,
'#attributes' => array(
'title' => $flag
->get_title(),
),
);
if (!$access) {
$form['flag'][$flag->name]['#type'] = 'value';
$form['flag'][$flag->name]['#value'] = $flag_status;
}
else {
$flags_visible++;
}
$flags_in_form++;
}
if ($flags_in_form) {
$form['flag'] += array(
'#weight' => 1,
'#tree' => TRUE,
);
}
if ($flags_visible) {
$form['flag'] += array(
'#type' => 'fieldset',
'#title' => t('Flags'),
'#collapsible' => TRUE,
);
if ($entity_type == 'node') {
$form['flag'] += array(
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'flag-fieldset',
),
),
'#attached' => array(
'js' => array(
'vertical-tabs' => drupal_get_path('module', 'flag') . '/theme/flag-admin.js',
),
),
);
}
}
}
function flag_field_attach_submit($entity_type, $entity, $form, &$form_state) {
if (isset($form['flag'])) {
$parents = $form['flag']['#parents'];
$flag_values = drupal_array_get_nested_value($form_state['values'], $parents);
$entity->flag = $flag_values;
}
}
function flag_field_attach_insert($entity_type, $entity) {
if (isset($entity->flag)) {
flag_field_attach_save($entity_type, $entity);
}
}
function flag_field_attach_update($entity_type, $entity) {
if (isset($entity->flag)) {
flag_field_attach_save($entity_type, $entity);
}
}
function flag_field_attach_save($entity_type, $entity) {
list($id) = entity_extract_ids($entity_type, $entity);
foreach ($entity->flag as $flag_name => $state) {
flag($state ? 'flag' : 'unflag', $flag_name, $id);
}
}
function flag_contextual_links_view_alter(&$element, $items) {
if (isset($element['#element']['#entity_type'])) {
$entity_type = $element['#element']['#entity_type'];
if (isset($element['#element']['#entity'])) {
$entity = $element['#element']['#entity'];
}
elseif (isset($element['#element']['#' . $entity_type])) {
$entity = $element['#element']['#' . $entity_type];
}
else {
return;
}
$flags = flag_get_flags($entity_type);
foreach ($flags as $name => $flag) {
if (!$flag->show_contextual_link) {
continue;
}
list($entity_id) = entity_extract_ids($entity_type, $entity);
if (!$flag
->access($entity_id) && (!$flag
->is_flagged($entity_id) || !$flag
->access($entity_id, 'flag'))) {
continue;
}
$element['#links']['flag-' . $name] = array(
'title' => $flag
->theme($flag
->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id),
'html' => TRUE,
);
}
}
}
function flag_entity_view($entity, $type, $view_mode, $langcode) {
$flags = flag_get_flags($type);
foreach ($flags as $flag) {
if (!($flag->show_as_field || $flag
->shows_in_entity_links($view_mode))) {
continue;
}
$entity_id = $flag
->get_entity_id($entity);
if (is_null($entity_id)) {
$entity_id = 'new';
}
$flag
->remember_entity($entity_id, $entity);
if (!$flag
->access($entity_id) && (!$flag
->is_flagged($entity_id) || !$flag
->access($entity_id, 'flag'))) {
continue;
}
if ($flag
->shows_in_entity_links($view_mode)) {
$links['flag-' . $flag->name] = array(
'title' => $flag
->theme($flag
->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id),
'html' => TRUE,
);
}
if ($flag->show_as_field) {
$entity->content['flag_' . $flag->name] = array(
'#markup' => $flag
->theme($flag
->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, array(
'needs_wrapping_element' => TRUE,
)),
);
}
}
if (isset($links)) {
$entity->content['links']['flag'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
);
}
}
function flag_node_insert($node) {
flag_node_save($node);
}
function flag_node_update($node) {
flag_node_save($node);
}
function flag_node_save($node) {
$remembered = FALSE;
if (isset($node->flag)) {
foreach ($node->flag as $name => $state) {
$flag = flag_get_flag($name);
if (!$remembered) {
$flag
->remember_entity($node->nid, $node);
$remembered = TRUE;
}
$action = $state ? 'flag' : 'unflag';
$flag
->flag($action, $node->nid, NULL, TRUE);
}
}
}
function flag_entity_delete($entity, $type) {
if ($type == 'node' || $type == 'user') {
return;
}
list($id) = entity_extract_ids($type, $entity);
_flag_entity_delete($type, $id);
}
function flag_node_delete($node) {
foreach (flag_get_flags('node') as $flag) {
if (!$flag->i18n || empty($node->tnid)) {
_flag_entity_delete('node', $node->nid, $flag->fid);
}
}
}
function flag_node_translation_change($node) {
if (isset($node->translation_change)) {
$entity_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_update('flagging')
->fields(array(
'entity_id' => $entity_id,
))
->condition('fid', $flag->fid)
->condition('entity_id', $node->translation_change['old_tnid'])
->execute();
db_update('flag_counts')
->fields(array(
'entity_id' => $entity_id,
))
->condition('fid', $flag->fid)
->condition('entity_id', $node->translation_change['old_tnid'])
->execute();
}
}
}
}
function _flag_entity_delete($entity_type, $entity_id, $fid = NULL) {
$query_content = db_delete('flagging')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id);
$query_counts = db_delete('flag_counts')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id);
if (isset($fid)) {
$query_content
->condition('fid', $fid);
$query_counts
->condition('fid', $fid);
}
$query_content
->execute();
$query_counts
->execute();
}
function flag_user_login(&$edit, &$account) {
if (module_exists('session_api') && ($sid = flag_get_sid(0))) {
$duplicate_flaggings = array();
$flaggings = db_select('flagging', 'fc')
->fields('fc', array(
'flagging_id',
'fid',
'entity_id',
))
->condition('uid', 0)
->condition('sid', $sid)
->execute()
->fetchAllAssoc('flagging_id', PDO::FETCH_ASSOC);
foreach ($flaggings as $flagging_id => $flagging) {
try {
db_update('flagging')
->fields(array(
'uid' => $account->uid,
'sid' => 0,
))
->condition('flagging_id', $flagging_id)
->execute();
} catch (Exception $e) {
$duplicate_flaggings[$flagging_id] = $flagging;
}
}
$anonymous_user = drupal_anonymous_user();
foreach ($duplicate_flaggings as $flagging_id => $flagging) {
$flag = flag_get_flag(NULL, $flagging['fid']);
$flag
->flag('unflag', $flagging['entity_id'], $anonymous_user, TRUE);
}
FlagCookieStorage::drop();
}
}
function flag_user_cancel($edit, $account, $method) {
flag_user_account_removal($account);
}
function flag_user_delete($account) {
flag_user_account_removal($account);
}
function flag_user_account_removal($account) {
$query = db_select('flagging', 'fc');
$query
->leftJoin('flag_counts', 'c', 'fc.entity_id = c.entity_id AND fc.entity_type = c.entity_type AND fc.fid = c.fid');
$result = $query
->fields('fc', array(
'fid',
'entity_id',
))
->fields('c', array(
'count',
))
->condition('fc.uid', $account->uid)
->execute();
foreach ($result as $flag_data) {
if ($flag_data->count > 0) {
$flag_data->count--;
db_update('flag_counts')
->fields(array(
'count' => $flag_data->count,
))
->condition('fid', $flag_data->fid)
->condition('entity_id', $flag_data->entity_id)
->execute();
}
elseif ($flag_data->count == 0) {
db_delete('flag_counts')
->condition('fid', $flag_data->fid)
->condition('entity_id', $flag_data->entity_id)
->execute();
}
}
db_delete('flagging')
->condition('uid', $account->uid)
->execute();
_flag_entity_delete('user', $account->uid);
}
function flag_user_view($account, $view_mode) {
$flags = flag_get_flags('user');
$flag_items = array();
foreach ($flags as $flag) {
if (!$flag
->access($account->uid)) {
continue;
}
if (!$flag->show_on_profile) {
continue;
}
$flag_items[$flag->name] = array(
'#type' => 'user_profile_item',
'#title' => $flag
->get_title($account->uid),
'#markup' => $flag
->theme($flag
->is_flagged($account->uid) ? 'unflag' : 'flag', $account->uid),
'#attributes' => array(
'class' => array(
'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' => array(
'flag-profile',
),
),
);
}
}
function flag_session_api_cleanup($arg = 'run') {
if ($arg == 'run') {
$query = db_select('flagging', 'fc');
$query
->leftJoin('session_api', 's', 'fc.sid = s.sid');
$result = $query
->fields('fc', array(
'sid',
))
->condition('fc.sid', 0, '<>')
->isNull('s.sid')
->execute();
foreach ($result as $row) {
db_delete('flagging')
->condition('sid', $row->sid)
->execute();
}
}
elseif (is_array($arg)) {
$outdated_sids = $arg;
db_delete('flagging')
->condition('sid', $outdated_sids, 'IN')
->execute();
}
}
function flag_field_attach_delete_bundle($entity_type, $bundle, $instances) {
db_query("DELETE FROM {flag_types} WHERE type = :bundle AND fid IN (SELECT fid FROM {flag} WHERE entity_type = :entity_type)", array(
':bundle' => $bundle,
':entity_type' => $entity_type,
));
}
function flag($action, $flag_name, $entity_id, $account = NULL, $permissions_check = FALSE) {
if (!($flag = flag_get_flag($flag_name))) {
return FALSE;
}
return $flag
->flag($action, $entity_id, $account, $permissions_check);
}
function flag_flag_flag($flag, $entity_id, $account, $flagging) {
if (module_exists('trigger')) {
flag_flag_trigger('flag', $flag, $entity_id, $account, $flagging);
}
}
function flag_flag_unflag($flag, $entity_id, $account, $flagging) {
if (module_exists('trigger')) {
flag_flag_trigger('unflag', $flag, $entity_id, $account, $flagging);
}
}
function flag_flag_trigger($action, $flag, $entity_id, $account, $flagging) {
$context['hook'] = 'flag';
$context['account'] = $account;
$context['flag'] = $flag;
$context['op'] = $action;
$context = array_merge($flag
->get_relevant_action_objects($entity_id), $context);
$object = $flag
->fetch_entity($entity_id);
foreach (trigger_get_assigned_actions('flag_' . $action) as $aid => $action_info) {
if ($aid) {
actions_do($aid, $object, $context);
}
}
foreach (trigger_get_assigned_actions('flag_' . $action . '_' . $flag->name) as $aid => $action_info) {
if ($aid) {
actions_do($aid, $object, $context);
}
}
}
function flag_flag_access($flag, $entity_id, $action, $account) {
if (empty($flag->access_author)) {
return;
}
if ($flag->entity_type == 'node') {
if (empty($entity_id) || !($node = $flag
->fetch_entity($entity_id))) {
return $flag->access_author == 'others' ? FALSE : NULL;
}
if ($flag->access_author == 'own' && $node->uid != $account->uid) {
return FALSE;
}
elseif ($flag->access_author == 'others' && $node->uid == $account->uid) {
return FALSE;
}
}
if ($flag->entity_type == 'comment') {
if (empty($entity_id) || !($comment = $flag
->fetch_entity($entity_id)) || $entity_id == 'new') {
return $flag->access_author == 'comment_others' ? FALSE : NULL;
}
$node = node_load($comment->nid);
if ($flag->access_author == 'node_own' && $node->uid != $account->uid) {
return FALSE;
}
elseif ($flag->access_author == 'node_others' && $node->uid == $account->uid) {
return FALSE;
}
elseif ($flag->access_author == 'comment_own' && $comment->uid != $account->uid) {
return FALSE;
}
elseif ($flag->access_author == 'comment_others' && $comment->uid == $account->uid) {
return FALSE;
}
}
}
function flag_flag_access_multiple($flag, $entity_ids, $account) {
$access = array();
if (empty($flag->access_author)) {
return $access;
}
if ($flag->entity_type == 'node') {
$result = db_select('node', 'n')
->fields('n', array(
'nid',
'uid',
))
->condition('nid', array_keys($entity_ids), 'IN')
->condition('type', $flag->types, 'IN')
->execute();
foreach ($result as $row) {
if ($flag->access_author == 'own') {
$access[$row->nid] = $row->uid != $account->uid ? FALSE : NULL;
}
elseif ($flag->access_author == 'others') {
$access[$row->nid] = $row->uid == $account->uid ? FALSE : NULL;
}
}
}
if ($flag->entity_type == 'comment') {
$query = db_select('comment', 'c');
$query
->leftJoin('node', 'n', 'c.nid = n.nid');
$query
->fields('c', array(
'cid',
'nid',
'uid',
))
->condition('c.cid', $entity_ids, 'IN');
$query
->addField('c', 'uid', 'comment_uid');
$result = $query
->execute();
foreach ($result as $row) {
if ($flag->access_author == 'node_own') {
$access[$row->cid] = $row->node_uid != $account->uid ? FALSE : NULL;
}
elseif ($flag->access_author == 'node_others') {
$access[$row->cid] = $row->node_uid == $account->uid ? FALSE : NULL;
}
elseif ($flag->access_author == 'comment_own') {
$access[$row->cid] = $row->comment_uid != $account->uid ? FALSE : NULL;
}
elseif ($flag->access_author == 'comment_others') {
$access[$row->cid] = $row->comment_uid == $account->uid ? FALSE : NULL;
}
}
}
return $access;
}
function flag_theme() {
$path = drupal_get_path('module', 'flag') . '/theme';
return array(
'flag' => array(
'variables' => array(
'flag' => NULL,
'action' => NULL,
'entity_id' => NULL,
'after_flagging' => FALSE,
'needs_wrapping_element' => FALSE,
'errors' => array(),
),
'template' => 'flag',
'pattern' => 'flag__',
'path' => $path,
),
'flag_tokens_browser' => array(
'variables' => array(
'types' => array(
'all',
),
'global_types' => TRUE,
),
'file' => 'flag.tokens.inc',
),
'flag_admin_listing' => array(
'render element' => 'form',
'file' => 'includes/flag.admin.inc',
),
'flag_admin_listing_disabled' => array(
'variables' => array(
'flags' => NULL,
'default_flags' => NULL,
),
'file' => 'includes/flag.admin.inc',
),
'flag_admin_page' => array(
'variables' => array(
'flags' => NULL,
'default_flags' => NULL,
'flag_admin_listing' => NULL,
),
'file' => 'includes/flag.admin.inc',
),
'flag_form_roles' => array(
'render element' => 'element',
'file' => 'includes/flag.admin.inc',
),
);
}
function template_preprocess_flag(&$variables) {
global $user;
$initialized =& drupal_static(__FUNCTION__, array());
$flag =& $variables['flag'];
$action = $variables['action'];
$entity_id = $variables['entity_id'];
$errors = implode('<br />', $variables['errors']);
$flag_name_css = str_replace('_', '-', $flag->name);
$link_type = $flag
->get_link_type();
$link = module_invoke($link_type['module'], 'flag_link', $flag, $action, $entity_id);
if (isset($link['title']) && empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
if ($action == 'unflag' && !$flag
->access($entity_id, 'unflag')) {
$link['title'] = $flag
->get_label('unflag_denied_text', $entity_id);
unset($link['href']);
}
if ($user->uid == 0) {
$link_type['uses standard js'] = TRUE;
}
if (!isset($initialized[$link_type['name']])) {
if ($link_type['uses standard css']) {
drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
}
if ($link_type['uses standard js']) {
drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
}
$initialized[$link_type['name']] = TRUE;
}
$variables['link'] = $link;
$variables['link_href'] = isset($link['href']) ? check_url(url($link['href'], $link)) : FALSE;
$variables['link_text'] = isset($link['title']) ? $link['title'] : $flag
->get_label($action . '_short', $entity_id);
$variables['link_title'] = isset($link['attributes']['title']) ? check_plain($link['attributes']['title']) : check_plain(strip_tags($flag
->get_label($action . '_long', $entity_id)));
$variables['status'] = $action == 'flag' ? 'unflagged' : 'flagged';
$variables['flag_name_css'] = $flag_name_css;
$variables['flag_wrapper_classes_array'] = array();
$variables['flag_wrapper_classes_array'][] = 'flag-wrapper';
$variables['flag_wrapper_classes_array'][] = 'flag-' . $flag_name_css;
$variables['flag_wrapper_classes_array'][] = 'flag-' . $flag_name_css . '-' . $entity_id;
$variables['flag_classes_array'] = array();
$variables['flag_classes_array'][] = 'flag';
if (isset($link['href'])) {
$variables['flag_classes_array'][] = $variables['action'] . '-action';
$variables['flag_classes_array'][] = 'flag-link-' . $flag->link_type;
}
else {
$variables['flag_classes_array'][] = $variables['action'] . '-disabled';
}
if (isset($link['attributes']['class'])) {
$link['attributes']['class'] = is_string($link['attributes']['class']) ? array_filter(explode(' ', $link['attributes']['class'])) : $link['attributes']['class'];
$variables['flag_classes_array'] = array_merge($variables['flag_classes_array'], $link['attributes']['class']);
}
$variables['message_classes_array'] = array();
if ($variables['after_flagging']) {
$variables['message_classes_array'][] = 'flag-message';
if ($errors) {
$variables['message_classes_array'][] = 'flag-failure-message';
$variables['message_text'] = $errors;
}
else {
$inverse_action = $action == 'flag' ? 'unflag' : 'flag';
$variables['message_classes_array'][] = 'flag-success-message';
$variables['message_classes_array'][] = 'flag-' . $variables['status'] . '-message';
$variables['message_text'] = $flag
->get_label($inverse_action . '_message', $entity_id);
$variables['flag_classes_array'][] = $variables['status'];
$variables['message_classes_array'][] = 'flag-auto-remove';
}
}
else {
$variables['message_text'] = '';
}
}
function template_process_flag(&$variables) {
$variables['flag_wrapper_classes'] = implode(' ', $variables['flag_wrapper_classes_array']);
$variables['flag_classes'] = implode(' ', $variables['flag_classes_array']);
$variables['message_classes'] = implode(' ', $variables['message_classes_array']);
}
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_link_type_descriptions() {
$options = array();
$types = flag_get_link_types();
foreach ($types as $type_name => $type) {
$options[$type_name] = $type['description'];
}
return $options;
}
function flag_get_entity_flag_counts($flag, $entity_type) {
$counts =& drupal_static(__FUNCTION__);
if (!isset($counts[$flag->name][$entity_type])) {
$counts[$flag->name][$entity_type] = array();
$result = db_select('flagging', 'f')
->fields('f', array(
'fid',
))
->condition('fid', $flag->fid)
->condition('entity_type', $entity_type)
->countQuery()
->execute()
->fetchField();
$counts[$flag->name][$entity_type] = $result;
}
return $counts[$flag->name][$entity_type];
}
function flag_get_user_flag_counts($flag, $user) {
$counts =& drupal_static(__FUNCTION__);
if (!isset($counts[$flag->name][$user->uid])) {
$counts[$flag->name][$user->uid] = array();
$result = db_select('flagging', 'f')
->fields('f', array(
'fid',
))
->condition('fid', $flag->fid)
->condition('uid', $user->uid)
->countQuery()
->execute()
->fetchField();
$counts[$flag->name][$user->uid] = $result;
}
return $counts[$flag->name][$user->uid];
}
function flag_get_counts($entity_type, $entity_id) {
$counts =& drupal_static(__FUNCTION__);
if (!isset($counts[$entity_type][$entity_id])) {
$counts[$entity_type][$entity_id] = array();
$query = db_select('flag', 'f');
$query
->leftJoin('flag_counts', 'fc', 'f.fid = fc.fid');
$result = $query
->fields('f', array(
'name',
))
->fields('fc', array(
'count',
))
->condition('fc.entity_type', $entity_type)
->condition('fc.entity_id', $entity_id)
->execute();
foreach ($result as $row) {
$counts[$entity_type][$entity_id][$row->name] = $row->count;
}
}
return $counts[$entity_type][$entity_id];
}
function flag_get_flag_counts($flag_name, $reset = FALSE) {
$counts =& drupal_static(__FUNCTION__);
if ($reset) {
$counts = array();
}
if (!isset($counts[$flag_name])) {
$flag = flag_get_flag($flag_name);
$counts[$flag_name] = db_select('flag_counts', 'fc')
->fields('fc', array(
'fid',
))
->condition('fid', $flag->fid)
->countQuery()
->execute()
->fetchField();
}
return $counts[$flag_name];
}
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;
}
}
}
return FALSE;
}
function flag_get_flags($entity_type = NULL, $content_subtype = NULL, $account = NULL) {
$flags =& drupal_static(__FUNCTION__);
if (!isset($flags)) {
$flags = array();
$query = db_select('flag', 'f');
$query
->leftJoin('flag_types', 'fn', 'fn.fid = f.fid');
$result = $query
->fields('f', array(
'fid',
'entity_type',
'name',
'title',
'global',
'options',
))
->fields('fn', array(
'type',
))
->execute();
foreach ($result as $row) {
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};
}
}
}
}
uasort($flags, '_flag_compare_weight');
foreach ($flags as $flag) {
drupal_alter('flag', $flag);
}
}
$filtered_flags = $flags;
if (isset($entity_type) || isset($content_subtype)) {
foreach ($filtered_flags as $name => $flag) {
if (!$flag
->access_entity_enabled($entity_type, $content_subtype)) {
unset($filtered_flags[$name]);
}
}
}
if (isset($account) && $account->uid != 1) {
foreach ($filtered_flags as $name => $flag) {
if (!$flag
->user_access('flag', $account)) {
unset($filtered_flags[$name]);
}
}
}
return $filtered_flags;
}
function _flag_compare_weight($flag1, $flag2) {
if ($flag1->weight == $flag2->weight) {
return 0;
}
return $flag1->weight < $flag2->weight ? -1 : 1;
}
function flag_get_default_flags($include_disabled = FALSE) {
$default_flags = array();
$flag_status = variable_get('flag_default_flag_status', array());
$default_flags_info = array();
foreach (module_implements('flag_default_flags') as $module) {
$function = $module . '_flag_default_flags';
foreach ($function() as $flag_name => $flag_info) {
$default_flags_info[$flag_name] = $flag_info + array(
'name' => $flag_name,
'module' => $module,
);
}
}
drupal_alter('flag_default_flags', $default_flags_info);
foreach ($default_flags_info as $flag_info) {
$flag = flag_flag::factory_by_array($flag_info);
if (!$flag
->is_compatible()) {
$flag->status = FALSE;
}
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_flag_flagging_data($flag_name) {
$flag = flag_get_flag($flag_name);
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('fid', $flag->fid)
->execute();
return $result
->fetchAllAssoc('flagging_id');
}
function flag_get_user_flags($entity_type, $entity_id = NULL, $uid = NULL, $sid = NULL) {
$flagged_content =& drupal_static(__FUNCTION__);
$uid = !isset($uid) ? $GLOBALS['user']->uid : $uid;
$sid = !isset($sid) ? flag_get_sid($uid) : $sid;
if (isset($entity_id)) {
if (!isset($flagged_content[$uid][$sid][$entity_type][$entity_id])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$sid][$entity_type][$entity_id] = array();
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id)
->condition(db_or()
->condition('uid', $uid)
->condition('uid', 0))
->condition('sid', $sid)
->execute();
foreach ($result as $flagging_data) {
$flagged_content[$uid][$sid][$entity_type][$entity_id][$flag_names[$flagging_data->fid]] = $flagging_data;
}
}
return $flagged_content[$uid][$sid][$entity_type][$entity_id];
}
else {
if (!isset($flagged_content[$uid][$sid][$entity_type]['all'])) {
$flag_names = _flag_get_flag_names();
$flagged_content[$uid][$sid][$entity_type]['all'] = array();
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('entity_type', $entity_type)
->condition(db_or()
->condition('uid', $uid)
->condition('uid', 0))
->condition('sid', $sid)
->execute();
foreach ($result as $flagging_data) {
$flagged_content[$uid][$sid][$entity_type]['all'][$flag_names[$flagging_data->fid]][$flagging_data->entity_id] = $flagging_data;
}
}
return $flagged_content[$uid][$sid][$entity_type]['all'];
}
}
function flag_get_entity_flags($entity_type, $entity_id, $flag_name = NULL) {
$entity_flags =& drupal_static(__FUNCTION__, array());
if (!isset($entity_flags[$entity_type][$entity_id])) {
$flag_names = _flag_get_flag_names();
$result = db_select('flagging', 'fc')
->fields('fc')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id)
->orderBy('timestamp', 'DESC')
->execute();
$entity_flags[$entity_type][$entity_id] = array();
foreach ($result as $flagging_data) {
$entity_flags[$entity_type][$entity_id]['users'][$flagging_data->uid][$flag_names[$flagging_data->fid]] = $flagging_data;
$entity_flags[$entity_type][$entity_id]['flags'][$flag_names[$flagging_data->fid]][$flagging_data->uid] = $flagging_data;
}
}
if (empty($entity_flags[$entity_type][$entity_id])) {
return array();
}
if (isset($flag_name)) {
if (isset($entity_flags[$entity_type][$entity_id]['flags'][$flag_name])) {
return $entity_flags[$entity_type][$entity_id]['flags'][$flag_name];
}
return array();
}
return $entity_flags[$entity_type][$entity_id]['users'];
}
function flag_create_link($flag_name, $entity_id, $variables = array()) {
$flag = flag_get_flag($flag_name);
if (!$flag) {
return;
}
if (!$flag
->access($entity_id) && (!$flag
->is_flagged($entity_id) || !$flag
->access($entity_id, 'flag'))) {
return;
}
return $flag
->theme($flag
->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, $variables);
}
function flag_trim_flag($flag, $account, $cutoff_size, $trim_newest, $permissions_check = FALSE) {
$query = db_select('flagging', 'fc')
->fields('fc')
->condition('fid', $flag->fid)
->condition(db_or()
->condition('uid', $account->uid)
->condition('uid', 0))
->condition('sid', flag_get_sid($account->uid));
if ($trim_newest) {
$query
->orderBy('timestamp', 'ASC');
}
else {
$query
->orderBy('timestamp', 'DESC');
}
$result = $query
->execute();
$i = 1;
foreach ($result as $row) {
if ($i++ > $cutoff_size) {
flag('unflag', $flag->name, $row->entity_id, $account, $permissions_check);
}
}
}
function flag_reset_flag($flag, $entity_id = NULL) {
$query = db_select('flagging', 'fc')
->fields('fc')
->condition('fid', $flag->fid);
if ($entity_id) {
$query
->condition('entity_id', $entity_id);
}
$result = $query
->execute()
->fetchAllAssoc('flagging_id', PDO::FETCH_ASSOC);
$rows = array();
foreach ($result as $row) {
$rows[] = $row;
}
module_invoke_all('flag_reset', $flag, $entity_id, $rows);
$query = db_delete('flagging')
->condition('fid', $flag->fid);
$count_query = db_delete('flag_counts')
->condition('fid', $flag->fid);
if ($entity_id) {
$query
->condition('entity_id', $entity_id);
$count_query
->condition('entity_id', $entity_id);
}
$count_query
->execute();
return $query
->execute();
}
function flag_get_link_types() {
$link_types =& drupal_static(__FUNCTION__);
if (!isset($link_types)) {
if ($cache = cache_get('flag_link_type_info')) {
$link_types = $cache->data;
}
if (empty($link_types)) {
$link_types = array();
foreach (module_implements('flag_link_type_info') as $module) {
$module_types = module_invoke($module, 'flag_link_type_info');
foreach ($module_types as $type_name => $info) {
$link_types[$type_name] = $info + array(
'module' => $module,
'name' => $type_name,
'title' => '',
'description' => '',
'options' => array(),
'uses standard js' => TRUE,
'uses standard css' => TRUE,
'provides form' => FALSE,
);
}
}
drupal_alter('flag_link_type_info', $link_types);
cache_set('flag_link_type_info', $link_types);
}
}
return $link_types;
}
function flag_get_token($entity_id) {
return $GLOBALS['user']->uid ? drupal_get_token($entity_id) : md5(drupal_get_private_key() . $entity_id);
}
function flag_check_token($token, $entity_id) {
return flag_get_token($entity_id) == $token;
}
function flag_set_sid($uid = NULL, $create = TRUE) {
$sids =& drupal_static(__FUNCTION__, array());
if (!isset($uid)) {
$uid = $GLOBALS['user']->uid;
}
if (!isset($sids[$uid]) || $sids[$uid] == -1 && $create) {
if (module_exists('session_api') && session_api_available() && $uid == 0) {
$sids[$uid] = session_api_get_sid($create);
}
else {
$sids[$uid] = 0;
}
}
return $sids[$uid] == -1 ? 0 : $sids[$uid];
}
function flag_get_sid($uid = NULL, $create = FALSE) {
return flag_set_sid($uid, $create);
}
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_user_operations() {
global $user;
$flags = flag_get_flags('user', NULL, $user);
$operations = array();
foreach ($flags as $flag) {
$operations['flag_' . $flag->name] = array(
'label' => $flag
->get_label('flag_short'),
'callback' => 'flag_users',
'callback arguments' => array(
'flag',
$flag->name,
),
);
$operations['unflag_' . $flag->name] = array(
'label' => $flag
->get_label('unflag_short'),
'callback' => 'flag_users',
'callback arguments' => array(
'unflag',
$flag->name,
),
);
}
return $operations;
}
function flag_users($users, $action, $flag_name) {
foreach ($users as $uid) {
flag($action, $flag_name, $uid);
}
}
function flag_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'flag') . '/includes/views',
);
}
function flag_features_api() {
return array(
'flag' => array(
'name' => t('Flag'),
'feature_source' => TRUE,
'default_hook' => 'flag_default_flags',
'file' => drupal_get_path('module', 'flag') . '/includes/flag.features.inc',
),
);
}
function flag_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/{$plugin}";
}
}
function flag_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
$flags = flag_get_flags($entity_type);
foreach ($flags as $flag) {
foreach ($flag->types as $key => $type) {
if ($type == $bundle_old) {
$flag->types[$key] = $bundle_new;
}
}
$flag
->save();
}
}
function flag_properties_get_flagging_boolean($entity, array $options, $name, $entity_type, $property_info) {
list($entity_id, ) = entity_extract_ids($entity_type, $entity);
$flagging_data = flag_get_user_flags($entity_type, $entity_id);
return isset($flagging_data[$property_info['flag_name']]);
}
function flag_properties_get_flagged_entities($entity, array $options, $name, $entity_type, $property_info) {
$flag_entity_type = $property_info['flag_entity_type'];
$flagging_data = flag_get_user_flags($flag_entity_type, NULL, $entity->uid);
$flag_name = $property_info['flag_name'];
if (isset($flagging_data[$flag_name])) {
return array_keys($flagging_data[$flag_name]);
}
return array();
}
function flag_properties_get_flagging_users($entity, array $options, $name, $entity_type, $property_info) {
list($entity_id, ) = entity_extract_ids($entity_type, $entity);
$flagging_data = flag_get_entity_flags($entity_type, $entity_id, $property_info['flag_name']);
return array_keys($flagging_data);
}
function flag_properties_get_user_sid($entity, array $options, $name, $entity_type, $property_info) {
$sid = flag_get_sid($entity->uid, FALSE);
return $sid;
}