You are here

function simplenews_block in Simplenews 6

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

Implementation of hook_block().

File

./simplenews.module, line 852
Simplnews 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();
      foreach (taxonomy_get_tree(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
        $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':
      $form['simplenews_block_' . $delta]['simplenews_block_m_' . $delta] = array(
        '#type' => 'textfield',
        '#title' => t('Block message'),
        '#size' => 60,
        '#maxlength' => 128,
        '#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':
      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':
      global $language;
      if ($tree = taxonomy_get_tree(variable_get('simplenews_vid', ''))) {

        // Only display a block if $delta is a valid newsletter term id.
        foreach ($tree as $taxonomy) {
          $tids[] = $taxonomy->tid;
        }
        if (in_array($delta, $tids)) {

          // $delta is validated, the block can be displayed.
          $newsletter = (array) taxonomy_get_term($delta);

          // Translate newsletter name if required.
          if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
            $newsletter['name'] = tt('taxonomy:term:' . $newsletter['tid'] . ':name', $newsletter['name'], $language->language);
          }
          $block = array(
            'subject' => check_plain($newsletter['name']),
            'content' => theme(array(
              'simplenews_block__' . $newsletter['tid'],
              'simplenews_block',
            ), $newsletter['tid']),
          );
          return $block;
        }
      }
      break;
  }
}