You are here

anonymous_publishing.module in Anonymous Publishing 5

Same filename and directory in other branches
  1. 8 anonymous_publishing.module
  2. 7 anonymous_publishing.module

Installation file for the anonymous_publishing module.

@author Ronny López <developer@dropcube.com>

File

anonymous_publishing.module
View source
<?php

/**
 * @file
 * Installation file for the anonymous_publishing module.
 *
 * @author
 * Ronny López <developer@dropcube.com>
 */

/**
 * Check if the content type can be published anonymously.
 *
 * @param $type
 *   The content type to check
 * @return
 *   TRUE if the content type can be published anonymously, FALSE
 *   otherwise.
 */
function anonymous_publishing_content_type_allowed($type) {
  $types = variable_get('anonymous_publishing_types', array());
  return !empty($types[$type]);
}

/**
 * Implementation of hook_form_alter().
 */
function anonymous_publishing_form_alter($form_id, &$form) {
  $types = variable_get('anonymous_publishing_types', array());
  if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form' && anonymous_publishing_content_type_allowed($form['#node']->type)) {
    _anonymous_publishing_node_form($form, $form['#node']);
  }
}

/**
 * Helper function to make node form alterations to add the
 * anonymous publishing feature.
 *
 * @param $form
 *   The form definition.
 * @param $node
 *   The node object.
 */
function _anonymous_publishing_node_form(&$form, $node) {
  global $user;
  $form['anonymous_publishing'] = array(
    '#type' => 'fieldset',
    '#title' => t('Anonymous publishing'),
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );
  if ($user->uid) {
    $form['anonymous_publishing']['options'] = array(
      '#type' => 'radios',
      '#options' => array(
        t('Publish as %name', array(
          '%name' => $user->name,
        )),
        t('Publish as Anonymous'),
      ),
    );
  }
  elseif (!$node->nid) {
    $form['anonymous_publishing']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Verification email'),
      '#maxlength' => EMAIL_MAX_LENGTH,
      '#description' => t('A verification email will be sent to this address. Your email address will NOT be shared with others.'),
      '#required' => TRUE,
    );
  }
  if (!count(element_children($form['anonymous_publishing']))) {
    unset($form['anonymous_publishing']);
  }
}

/**
 * Helper function; return email subject and body.
 * If admin settings are set, return the overrided settings,
 * else return defaults.
 *
 * @param $id
 *   The message ID.
 * @param $variables
 *   An array of substitutions.
 * @return
 *   The text with the substitution applied.
 */
function _anonymous_publishing_email_text($id, $variables = array()) {

  // Check if an admin setting overrides the default string.
  if ($admin_setting = variable_get('anonymous_publishing_email_' . $id, FALSE)) {
    return strtr($admin_setting, $variables);
  }
  else {
    switch ($id) {
      case 'subject':
        return t("Activate your post at !site", $variables);
      case 'body':
        return t("Your post at !site is ready to be activated.\n\n1.- Activate your post\nClick or copy the following link into your browser:\n\n!activation_uri\n\n2.-Manage your content\nSave this email to manage your post via the following links:\n\nView post: !view_uri\n\nEdit post: !edit_uri\n\nDelete post: !delete_uri\n\n", $variables);
    }
  }
}

/**
 * Page callback; Provide module settings page.
 *
 * @return unknown
 */
function anonymous_publishing_admin_settings() {
  $form['anonymous_publishing_types'] = array(
    '#type' => 'checkboxes',
    '#multiple' => true,
    '#title' => t('Contet types that allow anonymous publishing.'),
    '#default_value' => variable_get('anonymous_publishing_types', array()),
    '#options' => node_get_types('names'),
  );
  $form['anonymous_publishing_email_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject of the verification e-mail'),
    '#maxlength' => 180,
    '#default_value' => _anonymous_publishing_email_text('subject'),
    '#description' => t('Customize the subject of the verification e-mail, which is sent to anonymous users upon creting content.') . ' ' . t('Available variables are:') . ' ' . '!site, !nid, !title',
  );
  $form['anonymous_publishing_email_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body of verification e-mail'),
    '#default_value' => _anonymous_publishing_email_text('body'),
    '#rows' => 15,
    '#description' => t('Customize the body of the verification e-mail, which is sent to anonymous users upon creting content.') . ' ' . t('Available variables are:') . ' ' . '!site, !nid, !title, !activation_uri, !view_uri, !edit_uri, !delete_uri.',
  );
  return system_settings_form($form);
}

/**
 * Page callback; activate a node published anonymously.
 * This is the page callback of the links sent to the user by email
 * to activate the node.
 *
 * @param $node
 *   The node object.
 * @param $akey
 *   The activation key.
 */
function anonymous_publishing_activate($node, $akey) {
  db_query("UPDATE {node} SET status = 1 WHERE nid = %d", $node->nid);
  db_query("UPDATE {anonymous_publishing} SET activated = 1 WHERE nid = %d", $node->nid);
  node_load($node->nid, NULL, TRUE);
  drupal_set_message(t('Congratulations, your post has been activated.'));
  if (node_access('view', $node)) {
    drupal_goto('node/' . $node->nid, '/view');
  }
  else {
    drupal_goto();
  }
}

/**
 * Implementation of hook_menu().
 */
function anonymous_publishing_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/anonymous-publishing',
      'title' => t('Anonymous publishing'),
      'description' => t('Settings for Anonymous publishing feature'),
      'callback arguments' => array(
        'anonymous_publishing_admin_settings',
      ),
      'callback' => 'drupal_get_form',
      'access' => user_access('administer site configuration'),
    );
  }
  else {
    if (isset($_GET['akey']) && arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'activate') {
      $node = node_load(arg(1));
      if ($node && !empty($node->anonymous_publishing) && $node->anonymous_publishing['akey'] == $_GET['akey']) {
        $items[] = array(
          'path' => 'node/' . arg(1) . '/activate',
          'type' => MENU_CALLBACK,
          'callback' => 'anonymous_publishing_activate',
          'callback arguments' => array(
            $node,
            $akey,
          ),
          'access' => TRUE,
        );
      }
    }
  }
  return $items;
}

/**
 * Implementation of hook_node_api().
 */
function anonymous_publishing_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;
  if (anonymous_publishing_content_type_allowed($node->type)) {
    switch ($op) {
      case 'validate':

        // Validate the e-mail address:
        if (empty($node->nid) && empty($user->uid) && ($error = user_validate_mail($node->anonymous_publishing['email']))) {
          form_set_error('anonymous_publishing][email', $error);
        }
        break;
      case 'submit':
        _anonymous_publishing_node_submit($node);
        break;
      case 'insert':
        _anonymous_publishing_node_insert($node);
        break;
      case 'load':
        $node->anonymous_publishing = db_fetch_array(db_query("SELECT * FROM {anonymous_publishing} WHERE nid = %d", $node->nid));
        break;
      case 'view':
        if ($node->anonymous_publishing['anonymous']) {
          $node->uid = 0;
          $node->name = 'Anonymous-- Change this text';
        }
        break;
      case 'update':
        break;
      case 'delete':
        db_query("DELETE FROM {anonymous_publishing} WHERE nid = %d", $node->nid);
        break;
    }
  }
}

/**
 * Node API submit helper.
 *
 * @param $node
 *   The node object.
 */
function _anonymous_publishing_node_submit(&$node) {
  global $user;
  if (!$node->nid && !$user->uid) {
    $node->status = 0;
  }
}

/**
 * Node API insert helper.
 *
 * @param $node
 *   The node object.
 */
function _anonymous_publishing_node_insert(&$node) {
  global $user;
  if ($user->uid) {
    db_query("INSERT INTO {anonymous_publishing} (nid, uid, anonymous) VALUES (%d, %d, %d)", $node->nid, $user->uid, $node->anonymous_publishing['options']);
  }
  else {
    $akey = uniqid(md5(uniqid(mt_rand(), TRUE)), TRUE);
    db_query("INSERT INTO {anonymous_publishing} (nid, uid, akey, email, anonymous) VALUES (%d, %d, '%s', '%s', 1)", $node->nid, $user->uid, $akey, $node->anonymous_publishing['email']);
    $variables = array(
      '!site' => variable_get('site_name', 'Drupal'),
      '!title' => check_plain($node->title),
      '!nid' => $node->nid,
      '!activation_uri' => url('node/' . $node->nid . '/activate', "akey={$akey}", NULL, TRUE),
      '!view_uri' => url('node/' . $node->nid . '/view', NULL, NULL, TRUE),
      '!edit_uri' => url('node/' . $node->nid . '/edit/' . $akey, NULL, NULL, TRUE),
      '!delete_uri' => url('node/' . $node->nid . '/delete/' . $akey, NULL, NULL, TRUE),
    );
    $to = $node->anonymous_publishing['email'];
    $subject = _anonymous_publishing_email_text('subject', $variables);
    $body = _anonymous_publishing_email_text('body', $variables);
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    $mail_success = drupal_mail('anonymous_publishing_verification', $to, $subject, $body, $from);
    if ($mail_success) {
      drupal_set_message(t('Activation link and further instructions have been sent to your e-mail address.'));
    }
    else {
      watchdog('anonymous_publishing', t('Error mailing activation link.'), WATCHDOG_ERROR);
      drupal_set_message(t('Unable to send mail. Please contact the site admin.'));
    }
  }
}

/**
 * Node access system integration.
 */

/**
 * Implementation of hook_node_grants().
 *
 * Tell the node access system what GIDs the user belongs to for each realm.
 * @ingroup node_access
 */
function anonymous_publishing_node_grants($account, $op) {
  $grants['anonymous_publishing'] = array(
    0,
  );
  $akey = isset($_GET['akey']) ? $_GET['akey'] : arg(3);
  if (!empty($akey)) {
    if (db_result(db_query("SELECT nid FROM {anonymous_publishing} WHERE akey = '%s'", $akey))) {
      $grants['anonymous_publishing'] = array(
        1,
      );
    }
  }
  return $grants;
}

/**
 * Implementation of hook_node_access_records().
 * @ingroup node_access
 */
function anonymous_publishing_node_access_records($node) {

  // We only care about the node if it's been published anonymously.
  // If not, it is treated just like any other node and we completely ignore it.
  if (!empty($node->anonymous_publishing)) {
    $grants = array();
    $grants[] = array(
      'realm' => 'anonymous_publishing',
      'gid' => 1,
      'grant_view' => TRUE,
      'grant_update' => TRUE,
      'grant_delete' => TRUE,
      'priority' => 0,
    );
    return $grants;
  }
}

/**
 * Views integration.
 *
 * @ingroup views
 *
 */
function anonymous_publishing_views_tables() {
  $tables['anonymous_publishing'] = array(
    'name' => 'anonymous_publishing',
    'provider' => 'internal',
    'join' => array(
      'type' => 'left',
      'left' => array(
        'table' => 'node',
        'field' => 'nid',
      ),
      'right' => array(
        'field' => 'nid',
      ),
    ),
    'filters' => array(
      'anonymous' => array(
        'name' => t('Anonymous Publishing: Node published anonymously'),
        'field' => 'anonymous',
        'help' => t('This allows you to filter by whether or not the node has been published anonymously.'),
        'operator' => array(
          '=' => t('Equals'),
        ),
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
      ),
      'activated' => array(
        'name' => t('Anonymous Publishing: Node activated'),
        'field' => 'activated',
        'help' => t('This allows you to filter by whether or not a node that has been published anonymously was activated.'),
        'operator' => array(
          '=' => t('Equals'),
        ),
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
        'handler' => 'anonymous_publishing_views_handler_filter_activated',
      ),
    ),
  );
  return $tables;
}
function anonymous_publishing_views_handler_filter_activated($op, $filter, $filterinfo, &$query) {
  $query
    ->ensure_table('anonymous_publishing');
  if ($filter['value'] == 1) {
    $where = "anonymous_publishing.activated = 1";
  }
  else {
    $where = "anonymous_publishing.activated = 0 OR anonymous_publishing.activated = NULL";
  }
  $query
    ->add_where($where);
}

Functions

Namesort descending Description
anonymous_publishing_activate Page callback; activate a node published anonymously. This is the page callback of the links sent to the user by email to activate the node.
anonymous_publishing_admin_settings Page callback; Provide module settings page.
anonymous_publishing_content_type_allowed Check if the content type can be published anonymously.
anonymous_publishing_form_alter Implementation of hook_form_alter().
anonymous_publishing_menu Implementation of hook_menu().
anonymous_publishing_nodeapi Implementation of hook_node_api().
anonymous_publishing_node_access_records Implementation of hook_node_access_records().
anonymous_publishing_node_grants Implementation of hook_node_grants().
anonymous_publishing_views_handler_filter_activated
anonymous_publishing_views_tables Views integration.
_anonymous_publishing_email_text Helper function; return email subject and body. If admin settings are set, return the overrided settings, else return defaults.
_anonymous_publishing_node_form Helper function to make node form alterations to add the anonymous publishing feature.
_anonymous_publishing_node_insert Node API insert helper.
_anonymous_publishing_node_submit Node API submit helper.