You are here

flag_clear.module in Flag clear 7

Code for the Flag clear module.

File

flag_clear.module
View source
<?php

/**
 * @file
 * Code for the Flag clear module.
 */

/*
* Implements hook_menu().
*/
function flag_clear_menu() {

  // Remove all user flags for a piece of content.
  $items['unflag/%/%/%'] = array(
    'title' => 'Unflag this content completely',
    'page arguments' => array(
      1,
      2,
    ),
    'page callback' => 'flag_clear_clear',
    'access callback' => 'flag_clear_access',
    'access arguments' => array(
      2,
      3,
    ),
    'description' => '',
    'type' => MENU_CALLBACK,
  );

  // Remove a user's flag for a piece of content.
  $items['unflag/user/%/%/%/%'] = array(
    'title' => 'Unflag this content',
    'page arguments' => array(
      2,
      3,
      4,
    ),
    'page callback' => 'flag_clear_unflag',
    'access callback' => 'flag_clear_access',
    'access arguments' => array(
      3,
      5,
    ),
    'description' => '',
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/content/flag-manager'] = array(
    'title' => 'Flag Manager',
    'description' => 'Manage flags for content',
    'page callback' => 'flag_clear_manager',
    'access callback' => 'user_access',
    'access arguments' => array(
      'clear flags',
    ),
  );
  return $items;
}

/*
 * Custom user access function.
 */
function flag_clear_access($entity_id, $token = '') {

  // Make sure the current user has the appropriate level of permissions.
  if (user_access('clear flags')) {

    // Check the token to make sure it is valid.
    return flag_clear_verify_token($entity_id, $token);
  }
  return FALSE;
}

/*
* Implements hook_permission().
*/
function flag_clear_permission() {
  return array(
    'clear flags' => array(
      'title' => t('Clear flags'),
      'description' => t('Clear all flags on entity'),
    ),
  );
}

/*
* Custom menu handler for Flag manager landing page.
*/
function flag_clear_manager() {
  $output = '';
  $flags = flag_get_flags();
  $flag_links = array();
  foreach ($flags as $flag) {
    if (!isset($flag_links[$flag->entity_type])) {
      $flag_links[$flag->entity_type] = array();
    }
    $flag_links[$flag->entity_type][$flag->fid] = $flag->title;
  }
  $flag_paths = array(
    'comment' => 'admin/config/content/flag-manager/comments',
    'node' => 'admin/config/content/flag-manager/content',
    'user' => 'admin/config/content/flag-manager/users',
  );
  foreach ($flag_links as $type => $flag_link) {
    $output .= '
    <div class="admin-panel">
      <h3>' . l(ucwords($type) . ' flags', $flag_paths[$type]) . '</h3>
      <div class="body">
        <ul class="admin-list">';
    foreach ($flag_link as $key => $title) {
      $output .= '<li class="leaf">' . l($title, $flag_paths[$type] . '/' . $key) . '</li>';
    }
    $output .= '
        </ul>
      </div>
    </div>';
  }
  if (empty($output)) {
    $output = '
    <div class="admin-panel">
      <h3>' . t('No flags exist') . '</h3>
      <div class="body">
        <ul class="admin-list">
          <li class="leaf">' . l(t('Create a new flag'), 'admin/structure/flags/add') . '</li>
        </ul>
      </div>
    </div>';
  }
  return $output;
}

/*
* Menu callback for completely unflagging a piece of content for all users.
*/
function flag_clear_clear($type = NULL, $entity = NULL) {
  $removed = array();
  $user_flags = flag_get_entity_flags($type, $entity);
  foreach ($user_flags as $user_flag) {
    $key = key($user_flag);
    if (in_array($key, $removed)) {
      continue;
    }
    $removed[] = $key;
    $flag = flag_get_flag($key);
    flag_reset_flag($flag, $entity);
    drupal_set_message(t('Cleared all "@flag" flags for @type #@entity', array(
      '@flag' => $flag->title,
      '@type' => $type,
      '@entity' => $entity,
    )));
  }

  // Redirect to the entity that has been unflagged.
  $destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : $type . '/' . $entity;
  drupal_goto($destination);
}

/*
* Menu callback for unflagging a piece of content for a specific user.
*/
function flag_clear_unflag($flag = NULL, $entity = NULL, $uid = NULL) {
  $user = user_load($uid);
  $flag = flag_get_flag($flag);
  $flag
    ->flag('unflag', $entity, $user, TRUE);
  drupal_set_message(t('Unflagged "@flag" for entity #@entity by @user', array(
    '@flag' => $flag->title,
    '@entity' => $entity,
    '@user' => $user->name,
  )));

  // Redirect to the entity that has been unflagged.
  $destination = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : 'node/' . $entity;
  drupal_goto($destination);
}
function flag_clear_verify_token($entity_id, $token) {
  $token_key = 'flag_clear:' . $entity_id;
  if ($token != drupal_get_token($token_key)) {
    drupal_set_message(t('Flag clearing token does not match the provided token'), 'error');
    return FALSE;
  }
  return TRUE;
}

/**
 * Implements hook_views_api().
 */
function flag_clear_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'flag_clear') . '/includes/views',
  );
}