You are here

anonymous_publishing_pet.module in Anonymous Publishing 7

Main hooks for Anonymous Publishing PET module.

File

modules/pet/anonymous_publishing_pet.module
View source
<?php

/**
 * @file
 * Main hooks for Anonymous Publishing PET module.
 */

/**
 * Helper function to get the system alias for the anonymous user.
 */
function _anonymous_publishing_pet_alias() {
  $alias = variable_get('anonymous', 'Anonymous');
  return $alias;
}

/**
 * Menu callback: Provide module settings page.
 *
 * @return array
 *   Form.
 */
function anonymous_publishing_pet_admin_settings() {
  $ctypes = node_type_get_names();
  if (module_exists('comment')) {
    $ctypes['comment'] = 'Comment';
    $ctext = '(+ Comment)';
  }
  else {
    $ctext = '';
  }
  $form['anonymous_publishing_pet_types'] = array(
    '#type' => 'checkboxes',
    '#multiple' => TRUE,
    '#title' => t('Content types !comments where anonymous publishing is managed by this submodule:', array(
      '!comments' => $ctext,
    )),
    '#default_value' => variable_get('anonymous_publishing_pet_types', array()),
    '#options' => $ctypes,
    '#description' => t('To enable, check the box to the left of the item.'),
  );
  $alias = _anonymous_publishing_pet_alias();
  $form['anonymous_publishing_pet_options'] = array(
    '#type' => 'checkboxes',
    '#multiple' => TRUE,
    '#title' => t('Option:'),
    '#default_value' => variable_get('anonymous_publishing_pet_options', array(
      'asanon' => 1,
    )),
    '#options' => array(
      'asanon' => t('Allow authenticated users to publish as %alias.', array(
        '%alias' => $alias,
      )),
    ),
    '#description' => t('Check the option if you want to enable.'),
  );
  return system_settings_form($form);
}

/**
 * Menu callback: Form to manage Anonymous Publishing PET real names.
 *
 * @return array
 *   $form
 */
function anonymous_publishing_pet_realnames($form, &$form_state) {
  $form = array();
  $form['#tree'] = TRUE;
  $form['apu_info'] = array(
    '#markup' => t('<p>The following table shows the real names and subject lines for nodes published as anonymous.</p>'),
  );

  // Fetch all real names.
  $rows = db_query("SELECT a.rnid, a.nid, a.cid, a.uid FROM {anonymous_publishing_realname} a ORDER BY nid")
    ->fetchAll(PDO::FETCH_ASSOC);
  $form['realnames'] = array();
  foreach ($rows as $row) {
    $form['realnames'][$row['rnid']] = array();
    $form['realnames'][$row['rnid']]['rnid'] = array(
      '#markup' => $row['rnid'],
    );
    $form['realnames'][$row['rnid']]['nid'] = array(
      '#markup' => !$row['cid'] ? l($row['nid'], 'node/' . $row['nid']) : $row['nid'],
    );
    $form['realnames'][$row['rnid']]['cid'] = array(
      '#markup' => $row['cid'] ? l($row['cid'], 'comment/' . $row['cid'], array(
        'fragment' => 'comment-' . $row['cid'],
      )) : $row['cid'],
    );
    $theuser = user_load($row['uid']);
    $form['realnames'][$row['rnid']]['realname'] = array(
      '#markup' => check_plain($theuser->name) . ' (#' . $row['uid'] . ')',
    );
    if ($row['cid']) {
      $comm = comment_load($row['cid']);
      $title = $comm ? check_plain($comm->subject) : '-deleted-';
    }
    else {
      $node = node_load($row['nid']);
      $title = $node ? check_plain($node->title) : '-deleted-';
    }
    $form['realnames'][$row['rnid']]['subhect'] = array(
      '#markup' => check_plain($title),
    );
  }
  return $form;
}

/**
 * Submit for the anonymous_publishing_admin_privacy form.
 */
function anonymous_publishing_pet_admin_privacy_submit($form, &$form_state) {
  if (!array_key_exists('op', $form_state['values'])) {
    drupal_set_message(t('No operation?'), 'error');
    return;
  }
  if ('Purge now' == $form_state['values']['op']) {
    db_truncate('anonymous_publishing_realname')
      ->execute();
    drupal_set_message(t('All real name records have been purged.'));
  }
  elseif ('Save setting' == $form_state['values']['op']) {
    variable_set('anonymous_publishing_pet_period', $form_state['values']['anonymous_publishing_pet_period']);
    drupal_set_message(t('Rentention period updated.'));
  }
  else {
    drupal_set_message(t('Unknown operation.', 'error'));
    return;
  }
}

/**
 * Menu callback: Form to change privacy settings and flush queue.
 *
 * @return array
 *   Form.
 */
function anonymous_publishing_pet_admin_privacy($form, &$form_state) {
  $period = array(
    0 => t('Delete ASAP'),
    3600 => format_interval(3600),
    21600 => format_interval(21600),
    43200 => format_interval(43200),
    86400 => format_interval(86400),
    259200 => format_interval(259200),
    604800 => format_interval(604800),
    2592000 => format_interval(2592000),
    -1 => t('Indefinitely'),
  );
  $form = array();
  $form['anonymous_publishing_privacy'] = array(
    '#markup' => t('<p>For enhanced privacy, you can set a limited retention period for the real name records, or purge this information instantly.</p>'),
  );
  $form['apperiod'] = array(
    '#type' => 'fieldset',
    '#title' => t('Retention period'),
    '#collapsible' => FALSE,
  );
  $form['apperiod']['anonymous_publishing_pet_period'] = array(
    '#type' => 'select',
    '#title' => t('Maximum period to retain real name records:'),
    '#default_value' => variable_get('anonymous_publishing_pet_period', -1),
    '#options' => $period,
    '#description' => t('Select “Indefinitely” to make the real name records persistent.'),
  );
  $form['apperiod']['submit'] = array(
    '#type' => 'submit',
    '#disabled' => FALSE,
    '#value' => t('Save setting'),
  );
  $form['appurge'] = array(
    '#type' => 'fieldset',
    '#title' => t('Purge'),
    '#collapsible' => FALSE,
  );
  $form['appurge']['info'] = array(
    '#markup' => t('<p>Press button below to immediately purge all real name records.  This operation can not be reversed.</p>'),
  );
  $form['appurge']['submit'] = array(
    '#type' => 'submit',
    '#disabled' => FALSE,
    '#value' => t('Purge now'),
  );
  return $form;
}

/**
 * Theme function to theme the real names form in a table format.
 */
function theme_anonymous_publishing_pet_realnames($variables) {
  $form = $variables['form'];
  $output = drupal_render($form['apu_info']);
  $header = array(
    t('rnid'),
    t('nid'),
    t('cid'),
    t('real name (#uid)'),
    t('subject'),
  );
  $rows = array();
  foreach (element_children($form['realnames']) as $nid) {
    $row = array();
    foreach (element_children($form['realnames'][$nid]) as $entry_key) {
      unset($form['realnames'][$nid][$entry_key]['#title']);
      $row[] = drupal_render($form['realnames'][$nid][$entry_key]);
    }
    $rows[] = $row;
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('There is no real names for authenticated users publishing as anonymous on file.'),
        'colspan' => 4,
      ),
    );
  }
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
  $output .= drupal_render_children($form);
  $output .= theme('admin_block', array(
    'title' => 'title',
    'content' => 'content',
  ));
  $output .= drupal_render($form['addnode']);
  return $output;
}

/**
 * Implements hook_theme().
 *
 * Register the form data theme into a table for the tabs listed.
 */
function anonymous_publishing_pet_theme() {
  return array(
    'anonymous_publishing_pet_realnames' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function anonymous_publishing_pet_menu() {
  $items = array();
  $items['admin/config/people/anonymous_publishing_pet'] = array(
    'title' => 'Anonymous Publishing PET',
    'description' => 'Administrator settings for the Anonymous Publishing PET (Privacy Enhanching Technology) submodule.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'anonymous_publishing_pet_admin_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer anonymous_publishing',
    ),
    'type' => MENU_NORMAL_ITEM,
    'weight' => 10,
  );
  $items['admin/config/people/anonymous_publishing_pet/settings'] = array(
    'title' => 'Main settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/config/people/anonymous_publishing_pet/realnames'] = array(
    'title' => 'Real names',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'anonymous_publishing_pet_realnames',
      2,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer anonymous_publishing',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -5,
  );
  $items['admin/config/people/anonymous_publishing_pet/privacy'] = array(
    'title' => 'Privacy',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'anonymous_publishing_pet_admin_privacy',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer anonymous_publishing',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -4,
  );
  return $items;
}

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

/**
 * Helper function to make form alterations to input form.
 *
 * Changes form for content type managed by Anonymous Publishing PET
 * to add field for toggling self/alias.
 *
 * @param array $form
 *   The form definition.
 */
function _anonymous_publishing_pet_node_form(array &$form) {
  global $user;
  $form['anonymous_publishing'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $options = variable_get('anonymous_publishing_pet_options', array(
    'asanon' => 1,
  ));
  if (user_is_logged_in()) {

    // Allow anon. publishing for auth. users.
    if ($options['asanon']) {
      $alias = _anonymous_publishing_pet_alias();
      $form['anonymous_publishing']['#title'] = t('Select whether to post as yourself or as %alias', array(
        '%alias' => $alias,
      ));
      $form['anonymous_publishing']['options'] = array(
        '#type' => 'radios',
        '#default_value' => 0,
        '#options' => array(
          t('Post as %name', array(
            '%name' => $user->name,
          )),
          t('Post as %alias', array(
            '%alias' => $alias,
          )),
        ),
      );
    }
  }
  if (!count(element_children($form['anonymous_publishing']))) {
    unset($form['anonymous_publishing']);
  }
}

/**
 * Helper function to filter on allowed types.
 */
function _anonymous_publishing_pet_alter_comment_or_node_form(&$form, &$form_state, $type) {
  if (user_is_logged_in()) {
    if (anonymous_publishing_pet_content_type_allowed($type)) {
      _anonymous_publishing_pet_node_form($form);
      if ($type === 'comment') {

        // We disable the author field in the comment form.
        $form['author']['name']['#access'] = FALSE;
      }
    }
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function anonymous_publishing_pet_form_node_form_alter(&$form, &$form_state, $form_id) {
  _anonymous_publishing_pet_alter_comment_or_node_form($form, $form_state, $form['#bundle']);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function anonymous_publishing_pet_form_comment_form_alter(&$form, &$form_state) {
  _anonymous_publishing_pet_alter_comment_or_node_form($form, $form_state, 'comment');
}

/**
 * Implements hook_comment_presave().
 */
function anonymous_publishing_pet_comment_presave($comment) {
  if (user_is_logged_in() && isset($comment->anonymous_publishing) && $comment->anonymous_publishing['options']) {
    $comment->uid = 0;
    $comment->anonymous_publishing['originalname'] = $comment->name;
    $comment->name = _anonymous_publishing_pet_alias();
  }
}

/**
 * Implements hook_node_presave().
 */
function anonymous_publishing_pet_node_presave($node) {
  if (user_is_logged_in() && isset($node->anonymous_publishing) && $node->anonymous_publishing['options']) {
    $node->uid = 0;
  }
}

/**
 * Implements hook_comment_insert().
 */
function anonymous_publishing_pet_comment_insert($content) {
  global $user;
  if (user_is_logged_in() && isset($content->anonymous_publishing['options'])) {
    $asanon = $content->anonymous_publishing['options'];
    if ($asanon) {
      db_insert('anonymous_publishing_realname')
        ->fields(array(
        'nid' => $content->nid,
        'cid' => $content->cid,
        'uid' => $user->uid,
      ))
        ->execute();
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function anonymous_publishing_pet_node_insert($node) {
  global $user;
  if (user_is_logged_in() && isset($node->anonymous_publishing['options'])) {
    $asanon = $node->anonymous_publishing['options'];
    if ($asanon) {
      db_insert('anonymous_publishing_realname')
        ->fields(array(
        'nid' => $node->nid,
        'cid' => 0,
        'uid' => $user->uid,
      ))
        ->execute();
    }
  }
}

Functions

Namesort descending Description
anonymous_publishing_pet_admin_privacy Menu callback: Form to change privacy settings and flush queue.
anonymous_publishing_pet_admin_privacy_submit Submit for the anonymous_publishing_admin_privacy form.
anonymous_publishing_pet_admin_settings Menu callback: Provide module settings page.
anonymous_publishing_pet_comment_insert Implements hook_comment_insert().
anonymous_publishing_pet_comment_presave Implements hook_comment_presave().
anonymous_publishing_pet_content_type_allowed Check if the content type can be published anonymously.
anonymous_publishing_pet_form_comment_form_alter Implements hook_form_BASE_FORM_ID_alter().
anonymous_publishing_pet_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter().
anonymous_publishing_pet_menu Implements hook_menu().
anonymous_publishing_pet_node_insert Implements hook_node_insert().
anonymous_publishing_pet_node_presave Implements hook_node_presave().
anonymous_publishing_pet_realnames Menu callback: Form to manage Anonymous Publishing PET real names.
anonymous_publishing_pet_theme Implements hook_theme().
theme_anonymous_publishing_pet_realnames Theme function to theme the real names form in a table format.
_anonymous_publishing_pet_alias Helper function to get the system alias for the anonymous user.
_anonymous_publishing_pet_alter_comment_or_node_form Helper function to filter on allowed types.
_anonymous_publishing_pet_node_form Helper function to make form alterations to input form.