<?php
// $Id: simplenews_register.module,v 1.1.2.4.2.7 2009/06/22 11:03:05 moonray Exp $

/**
 * @defgroup simplenews_register
 * Allow signing up for newsletters on registration.
 */

/**
 * @file
 * Simplenews on register hooks
 *
 * @ingroup simplenews_register
 */

/**
 * Fix for versions of simplenews greater than 1.2
 */
if (!function_exists('_simplenews_get_vid')) {
  function _simplenews_get_vid() {
    return variable_get('simplenews_vid', '');
  }
}

/**
 * Implementation of hook_form_alter().
 */
function simplenews_register_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'simplenews_admin_settings') {
    $form['buttons']['#weight'] = 10;
    
    $form['simplenews_register_options'] = array(
      '#type' => 'fieldset',
      '#title' => t('User registration page options'),
      '#description' => t('Enable the newsletters you wish people to sign up for on the user registration page. You can change the default setting of opt-in to opt-out for each newsletter as well as changing the option to send a confirmation email before the user is subscribed to the newsletter, or automatically signing them up on submission if they checked to subscribe to that newsletter.<br /> If you <em>uncheck Show</em> and <em>check Opt-out</em> the option will not appear ont he registration page, but the user will automatically be signed up to that particular newsletter.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#theme' => 'simplenews_register_admin_settings',
      '#weight' => 1,
    );
    
    $form['simplenews_register_options']['simplenews_register_weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => variable_get('simplenews_register_weight', 5),
      '#description' => t('Useful if you have profile fields or other elements in the user registration form.<br /> Heavier items will appear below lighter ones in the account registration form.')
    );
    
    foreach (taxonomy_get_tree(_simplenews_get_vid()) as $term) {
      $form['simplenews_register_options']['simplenews_register_'. $term->tid .'_newsletter'] = array(
        '#value' => check_plain($term->name),
      );
      $form['simplenews_register_options']['simplenews_register_'. $term->tid .'_show'] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => variable_get('simplenews_register_'. $term->tid .'_show', TRUE),
      );
      $form['simplenews_register_options']['simplenews_register_'. $term->tid .'_optout'] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => variable_get('simplenews_register_'. $term->tid .'_optout', FALSE),
      );
      $form['simplenews_register_options']['simplenews_register_'. $term->tid .'_desc'] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => variable_get('simplenews_register_'. $term->tid .'_desc', TRUE),
      );
      $form['simplenews_register_options']['simplenews_register_'. $term->tid .'_confirm'] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => variable_get('simplenews_register_'. $term->tid .'_confirm', TRUE),
      );
    }
  }  
}

/**
 * Implementation of hook_theme().
 */
function simplenews_register_theme() {
  return array(
    'simplenews_register_admin_settings' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

/**
 * Custom theme function for a table for simplenews register page settings
 */
function theme_simplenews_register_admin_settings($form) {
  foreach (taxonomy_get_tree(_simplenews_get_vid()) as $term) {
    $row = array();
    $row[] = drupal_render($form['simplenews_register_'. $term->tid .'_newsletter']);
    $row[] = drupal_render($form['simplenews_register_'. $term->tid .'_show']);
    $row[] = drupal_render($form['simplenews_register_'. $term->tid .'_desc']);
    $row[] = drupal_render($form['simplenews_register_'. $term->tid .'_optout']);
    $row[] = drupal_render($form['simplenews_register_'. $term->tid .'_confirm']);
    $rows[] = $row;
  }
  $header = array(t('Newsletter'), t('Show'), t('Show description'), t('Opt-out'), t('Email confirmation'));
  
  $output  = drupal_render($form['simplenews_register_weight']);
  $output .= theme('table', $header, $rows);
  return $output;
}

/**
 * Implementation of hook_user().
 */
function simplenews_register_user($op, &$edit, &$account, $category = NULL) {
  global $language;
  
  switch ($op) {
    case 'register':
      $form = array();
      $form['simplenews'] = array(
        '#type' => 'fieldset',
        '#title' => t('Newsletters'),
        '#description' => t('Select the newsletter(s) to which you wish to subscribe.'),
        '#weight' => variable_get('simplenews_register_weight', 5),
      );

      $hidden = TRUE;

      // Get taxonomy terms for subscription form checkboxes (uses same translations as simplenews).
      // With taxonomy translation and 'Per language terms' only the terms of the
      // current language are listed. With taxonomy translation and 'Localize terms'
      // all taxonomy terms are listed and translated.
      if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_TRANSLATE) {
        // Per language terms
        $terms = i18ntaxonomy_vocabulary_get_terms(variable_get('simplenews_vid', ''), $language->language);
      }
      else {
        $terms = taxonomy_get_tree(variable_get('simplenews_vid', ''));
      }
      
      foreach ($terms as $tid => $term) {
        if (variable_get('simplenews_register_'. $term->tid .'_show', TRUE)) {
          if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
            // Localize terms.
            $title = tt('taxonomy:term:'. $term->tid .':name', $term->name, $language->language);
          }
          else {
            // Untranslated.
            $title = $term->name;
          }
          $form['simplenews']['simplenews-'. $term->tid] = array(
            '#type' => 'checkbox',
            '#title' => t('!newsletter', array('!newsletter' => $title)),
            '#description' => variable_get('simplenews_register_'. $term->tid .'_desc', TRUE) ? $term->description : '',
            '#default_value' => isset($edit['simplenews-'. $term->tid]) ? $edit['simplenews-'. $term->tid] : variable_get('simplenews_register_'. $term->tid .'_optout', FALSE),
          );
          $hidden = FALSE;
        }
        elseif (variable_get('simplenews_register_'. $term->tid .'_optout', TRUE)) {
          // optout is set to true, and show is set to false
          // Let's add a hidden element
          $form['simplenews']['simplenews-'. $term->tid] = array(
            '#type' => 'hidden',
            '#value' => TRUE,
          );
        }
      }
      
      if ($hidden) {
        unset($form['simplenews']['#type']);
        unset($form['simplenews']['#title']);
      }
      
      return $form;
      break;
      
    case 'insert':
    case 'update':
      $display_message = FALSE;
      foreach (taxonomy_get_tree(_simplenews_get_vid()) as $term) {
        if (!empty($edit['simplenews-'. $term->tid])) {
          if (variable_get('simplenews_register_'. $term->tid .'_show', TRUE)) {
            $display_message = TRUE;
          }
          simplenews_subscribe_user($edit['mail'], $term->tid, variable_get('simplenews_register_'. $term->tid .'_confirm', FALSE));
        }
      }
      if ($display_message) {
        drupal_set_message(t('Signing up for newsletter(s).'));
      }
      break;
  }
}
