You are here

function antispam_block in AntiSpam 6

Implementation of hook_block().

File

./antispam.module, line 1478

Code

function antispam_block($op = 'list', $delta = 0, $edit = array()) {
  static $block_fields = FALSE;
  if (!$block_fields) {
    $block_fields = array(
      'type' => array(
        '#type' => 'radios',
        '#title' => t('Display counter as'),
        '#options' => array(
          'image' => t('Image'),
          'text' => t('Text'),
        ),
        '#default_value' => 'image',
      ),
      'sitename' => array(
        '#type' => 'radios',
        '#title' => t('Show site name'),
        '#options' => array(
          '1' => t('Enabled'),
          '0' => t('Disabled'),
        ),
        '#default_value' => '1',
      ),
      'newwin' => array(
        '#type' => 'radios',
        '#title' => t('Open AntiSpam link in new window'),
        '#options' => array(
          '1' => t('Enabled'),
          '0' => t('Disabled'),
        ),
        '#default_value' => '1',
      ),
    );
  }
  switch ($op) {
    case 'list':
      $blocks_counter = variable_get('antispam_blocks_counter', 1);
      $blocks = array();
      for ($i = 0; $i < $blocks_counter; $i++) {
        $blocks[] = array(
          'info' => t('AntiSpam spam counter (block: @count)', array(
            '@count' => $i + 1,
          )),
        );
      }
      return $blocks;
    case 'configure':
      if ($delta < 0 || $delta >= variable_get('antispam_blocks_counter', 1)) {
        drupal_set_message(t('Oops! You are requesting a non-existing block, changes will not be saved.'), 'error');
        break;
      }
      $form = array();
      $form['description'] = array(
        '#type' => 'markup',
        '#value' => '<div class="description"><p>' . t('These options allow to customize the look of this <em>antispam spam counter</em> block.') . '</p></div>',
      );
      $block_settings = variable_get('antispam_blocks_' . $delta, FALSE);
      foreach ($block_fields as $field_key => $field_info) {
        $field_name = 'antispam_blocks_' . $delta . '_' . $field_key;
        $form[$field_name] = array();
        foreach ($field_info as $key => $value) {
          $form[$field_name][$key] = $value;
        }
        if ($block_settings && isset($block_settings[$field_key])) {
          $form[$field_name]['#default_value'] = $block_settings[$field_key];
        }
      }
      return $form;
    case 'save':
      if ($delta < 0 || $delta >= variable_get('antispam_blocks_counter', 1)) {
        drupal_set_message(t('Oops! You have requested a non-existing block, changes were not saved.'), 'error');
        break;
      }
      $block_settings = array();
      foreach ($block_fields as $field_key => $field_info) {
        $field_name = 'antispam_blocks_' . $delta . '_' . $field_key;
        $block_settings[$field_key] = $edit[$field_name];
      }
      variable_set('antispam_blocks_' . $delta, $block_settings);
      break;
    case 'view':
      if ($delta >= 0 && $delta < variable_get('antispam_blocks_counter', 1)) {
        $block_settings = variable_get('antispam_blocks_' . $delta, FALSE);
        if (!$block_settings) {
          $block_settings = array();
        }
        $block_args = array(
          'content' => '',
          // Built below.
          'counter' => antispam_get_total_counter(ANTISPAM_COUNT_SPAM_DETECTED),
          'since' => antispam_get_counting_since(),
          'text' => '',
          // Built below.
          'image' => '',
          // Built below.
          'block' => array(
            'delta' => $delta,
          ),
        );
        foreach ($block_fields as $field_key => $field_info) {
          if (!isset($block_settings[$field_key])) {
            $block_settings[$field_key] = $field_info['#default_value'];
          }
          $block_args['block'][$field_key] = $block_settings[$field_key];
        }
        $target = $block_settings['newwin'] ? ' target="_blank"' : '';
        $sitename = $block_settings['sitename'] ? variable_get('site_name', 'drupal') : t('This site');
        $block_args['text'] = array(
          'plain' => t('@site_name is proudly protected by AntiSpam, !spams caught since @since.', array(
            '@site_name' => $sitename,
            '!spams' => format_plural($block_args['counter'], '1 spam', '@count spams'),
            '@since' => $block_args['since'],
          )),
          'html' => t('@site_name is proudly protected by <a href="!antispam"@target>AntiSpam</a>, %spams caught since @since', array(
            '@site_name' => $sitename,
            '!antispam' => url('http://drupal.org/project/antispam'),
            '@target' => $target,
            '%spams' => format_plural($block_args['counter'], '1 spam', '@count spams'),
            '@since' => $block_args['since'],
          )),
        );
        $title_text = $block_args['text']['plain'];
        $image_url = base_path() . drupal_get_path('module', 'antispam') . '/antispam.png';
        $block_args['image'] = '<img src="' . $image_url . '" title="' . $title_text . '" alt="' . $title_text . '" />';
        if ($block_settings['type'] == 'image') {
          $block_args['content'] = '<a href="http://drupal.org/project/antispam" title="' . $title_text . '"' . $target . '>' . $block_args['image'] . '</a>';
        }
        else {
          $block_args['content'] = $block_args['text']['html'];
        }
        $block = array();
        $block['subject'] = t('AntiSpam spam counter');
        $block['content'] = theme('antispam_counter_block', $block_args);
        return $block;
      }
      break;
  }
}