You are here

function simplenews_block in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_block()
  2. 6 simplenews.module \simplenews_block()

Implementation of hook_block().

File

./simplenews.module, line 1213
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks = array();

      // Special block for multi
      $blocks[0] = array(
        'info' => t('Newsletter: Multi Subscription'),
      );
      foreach (simplenews_get_newsletters(variable_get('simplenews_vid', '')) as $newsletter) {

        //TODO: 1. without form -> by role; 2. with form -> user caching with refresh on subscribe/unsubscribe (option as setting) or no caching

        // Only list a block if the newsletter is not hidden.
        $blocks[$newsletter->tid] = array(
          'info' => t('Newsletter: @title', array(
            '@title' => $newsletter->name,
          )),
          'cache' => variable_get('simplenews_block_f_' . $newsletter->tid, 1) ? BLOCK_NO_CACHE : BLOCK_CACHE_PER_ROLE,
        );
      }
      return $blocks;
    case 'configure':

      // Special block for multi
      if ($delta == 0) {
        $form['simplenews_block_multiple']['simplenews_block_m_multiple'] = array(
          '#type' => 'textfield',
          '#title' => t('Block message'),
          '#size' => 60,
          '#maxlength' => 128,
          // @todo: clean localization / i18n needed
          '#default_value' => variable_get('simplenews_block_m_multiple', t('Select the newsletter(s) to which you want to subscribe or unsubscribe.')),
        );
      }
      else {
        $form['simplenews_block_' . $delta]['simplenews_block_m_' . $delta] = array(
          '#type' => 'textfield',
          '#title' => t('Block message'),
          '#size' => 60,
          '#maxlength' => 128,
          // @todo: clean localization / i18n needed
          '#default_value' => variable_get('simplenews_block_m_' . $delta, t('Stay informed on our latest news!')),
        );
        $form['simplenews_block_' . $delta]['simplenews_block_f_' . $delta] = array(
          '#type' => 'radios',
          '#title' => t('Subscription interface'),
          '#options' => array(
            '1' => t('Subscription form'),
            '0' => t('Link to form'),
          ),
          '#description' => t("Note: this requires permission 'subscribe to newsletters'."),
          '#default_value' => variable_get('simplenews_block_f_' . $delta, 1),
        );
        $form['simplenews_block_' . $delta]['simplenews_block_l_' . $delta] = array(
          '#type' => 'checkbox',
          '#title' => t('Display link to previous issues'),
          '#return_value' => 1,
          '#default_value' => variable_get('simplenews_block_l_' . $delta, 1),
        );
        $form['simplenews_block_' . $delta]['simplenews_block_i_status_' . $delta] = array(
          '#type' => 'checkbox',
          '#title' => t('Display previous issues'),
          '#return_value' => 1,
          '#default_value' => variable_get('simplenews_block_i_status_' . $delta, 0),
        );
        $form['simplenews_block_' . $delta]['simplenews_block_i_' . $delta] = array(
          '#type' => 'select',
          '#title' => t('Number of issues to display'),
          '#options' => drupal_map_assoc(array(
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
          )),
          '#default_value' => variable_get('simplenews_block_i_' . $delta, 5),
        );
        $form['simplenews_block_' . $delta]['simplenews_block_r_' . $delta] = array(
          '#type' => 'checkbox',
          '#title' => t('Display RSS-feed icon'),
          '#return_value' => 1,
          '#default_value' => variable_get('simplenews_block_r_' . $delta, 1),
        );
      }
      return $form;
    case 'save':

      //Special block for multi
      if ($delta == 0) {
        variable_set('simplenews_block_m_multiple', $edit['simplenews_block_m_multiple']);
      }
      else {
        variable_set('simplenews_block_m_' . $delta, $edit['simplenews_block_m_' . $delta]);
        variable_set('simplenews_block_f_' . $delta, $edit['simplenews_block_f_' . $delta]);
        variable_set('simplenews_block_l_' . $delta, $edit['simplenews_block_l_' . $delta]);
        variable_set('simplenews_block_i_status_' . $delta, $edit['simplenews_block_i_status_' . $delta]);
        variable_set('simplenews_block_i_' . $delta, $edit['simplenews_block_i_' . $delta]);
        variable_set('simplenews_block_r_' . $delta, $edit['simplenews_block_r_' . $delta]);
      }
      break;
    case 'view':

      // Special block for multi
      if ($delta == 0) {
        if (user_access('subscribe to newsletters')) {
          module_load_include('inc', 'simplenews', 'includes/simplenews.subscription');

          // render block only if permitted
          $block = array(
            'subject' => t('Newsletters'),
            'content' => theme('simplenews_multi_block'),
          );
          return $block;
        }
        return NULL;
      }
      else {
        global $language;
        $newsletters = simplenews_get_newsletters(variable_get('simplenews_vid', ''));

        // Only display a block if $delta is a valid newsletter term id.
        if (in_array($delta, array_keys($newsletters))) {
          module_load_include('inc', 'simplenews', 'includes/simplenews.subscription');

          // $delta is validated, the block can be displayed.
          $block = array(
            'subject' => check_plain($newsletters[$delta]->name),
            'content' => theme(array(
              'simplenews_block__' . $delta,
              'simplenews_block',
            ), $delta),
          );
          return $block;
        }
      }
      break;
  }
}