You are here

function google_plusone_block in Google Plus One Button | Google+ Badge 6

Implements hook_block().

File

./google_plusone.module, line 325

Code

function google_plusone_block($op = 'list', $delta = 0, $edit = array()) {
  $default = array(
    'annotation' => 'bubble',
    'width' => '250',
    'url' => '',
    'size' => '',
    // standard
    'css' => 'text-align:center;',
    'alias' => 'aliased',
  );
  $blocks['google_plusone_badge_block'] = array(
    'info' => t('Google Plus One +1 Badge'),
    'cache' => BLOCK_NO_CACHE,
  );
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Google +1 Button');
      $blocks[0]['cache'] = BLOCK_NO_CACHE;
      $blocks[1]['info'] = t('Google +1 Badge');
      $blocks[1]['cache'] = BLOCK_NO_CACHE;
      return $blocks;
      break;
    case 'view':
      if (user_access('view google plusone')) {
        google_plusone_add_js();
        $block = array();
        switch ($delta) {
          case 0:
            $default = array(
              'url' => '',
              'annotation' => 'bubble',
              'width' => '250',
              'size' => '',
              // standard
              'css' => 'text-align:center;',
              'alias' => 'aliased',
            );
            $button_settings = array_merge($default, variable_get('google_plusone_button_settings', array()));
            $settings = array_merge($button_settings, variable_get('google_plusone_block_settings', array()));
            $block['subject'] = NULL;
            $block['content'] = theme('google_plusone_button', $settings);
            return $block;
          case 1:
            $page_id = variable_get('google_plusone_badge_page_id', '');
            drupal_set_html_head('<link src="' . check_url($page_id) . '" rel="publisher" />');
            $style = variable_get('google_plusone_badge_style', 'badge');
            $block['subject'] = NULL;
            $block['content'] = theme('google_plusone_badge', array(
              'style' => $style,
            ));
            return $block;
        }
      }
      break;
    case 'configure':
      module_load_include('inc', 'google_plusone', 'google_plusone.admin');

      // Custom javascript code to preview in real-time the button
      drupal_add_js(drupal_get_path('module', 'google_plusone') . '/google_plusone.admin.js');

      // not good performance in <head>, but anyway, it's only for the admin page and can help for debugging purposes.
      $script = '<script type="text/javascript" src="http://apis.google.com/js/plusone.js">';
      drupal_set_html_head('<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>');
      $form = array();
      if ($delta == 0) {
        $settings = array_merge($default, variable_get('google_plusone_block_settings', array()));
        $available_sizes = array(
          'small' => t('Small (15px)'),
          'medium' => t('Medium (20px)'),
          '' => t('Standard (24px)'),
          'tall' => t('Tall (60px)'),
        );
        $available_annotations = array(
          'none' => t('None'),
          'bubble' => t('Bubble (by default)'),
          'inline' => t('Inline'),
        );
        $form['g1button'] = array(
          '#type' => 'fieldset',
          '#title' => t('Button Settings'),
          '#description' => t('Notice that these settings are exclusively for this block and they cover only some options.<br />The rest of settings will be taken from the <a href="@sett">general settings</a>.', array(
            '@sett' => url('admin/settings/google-plusone'),
          )),
        );
        $form['g1button']['google_plusone_block_url'] = array(
          '#title' => t('URL address to use for Google +1 button'),
          '#type' => 'textfield',
          '#default_value' => $settings['url'],
          '#description' => t('3 different options: <br/><ol><li>Type an fixed, absolute URL. Remember to type the <em>http://</em> part.</li><li>Type <em>&lt;front&gt;</em></front> to use always your frontpage ($base_url), that is language-aware. </li><li>Leave empty. Then it will be used the current URL address present in that moment in that page where the block is.</li></ol>'),
        );
        $form['g1button']['google_plusone_block_alias'] = array(
          '#type' => 'radios',
          '#title' => t('Aliased node path'),
          '#default_value' => $settings['alias'],
          '#options' => array(
            'not_aliased' => t('Not aliased'),
            'aliased' => t('Aliased'),
          ),
          '#description' => t('By default aliased. It only will be applied when the previous setting <em>URL</em> has been left empty and it\'s a node page. <br/> If you change this setting, <strong>be aware</strong> that Google+ will see them as different URLs, so the button will not keep the previous counting.'),
        );
        $form['g1button']['google_plusone_block_annotation'] = array(
          '#type' => 'radios',
          '#title' => t('Annotation: How to show the counting?'),
          '#options' => $available_annotations,
          '#default_value' => $settings['annotation'],
          '#description' => google_plusone_build_preview_button($available_sizes),
        );
        $form['g1button']['google_plusone_block_width'] = array(
          '#type' => 'textfield',
          '#title' => t('Width (only affects to inline annotation)'),
          '#default_value' => $settings['width'],
          '#size' => 20,
          '#description' => t('By default recommended 250 (pixels). Minimum 120'),
        );
        $form['g1button']['google_plusone_block_size'] = array(
          '#type' => 'radios',
          '#title' => t('Size'),
          '#options' => $available_sizes,
          '#default_value' => $settings['size'],
        );
        $form['g1button']['google_plusone_block_wrapper_css'] = array(
          '#type' => 'textfield',
          '#title' => t('Optional wrapper with CSS'),
          '#maxlength' => 256,
          '#default_value' => $settings['css'],
          '#description' => t('To help with the layout and placement of the button, it will be rendered by default wrapped in a div: 	&lt;div class="g-plusone-wrapper"&gt;	&lt;/div&gt;<br/>You can enter CSS rules to style this wrapper. By default <em>text-align:center</em><br />To disable totally the wrapper, input the word <em>nowrapper</em>'),
        );
      }
      elseif ($delta == 1) {
        $available_styles = array(
          'badge' => t('Standard Badge'),
          'smallbadge' => t('Small Badge'),
          'smallicon' => t('Small Icon'),
          'mediumicon' => t('Medium Icon'),
          'largeicon' => t('Large Icon'),
          'no_badge' => t('No Badge'),
        );
        $form['g1badge'] = array(
          '#type' => 'fieldset',
          '#title' => t('Badge Settings'),
          '#description' => t('Notice that these settings are exclusively for this block and they cover the basic options.<br /> The rest of settings will be taken from the <a href="@sett">general settings</a>.', array(
            '@sett' => url('admin/config/services/google-plusone'),
          )),
        );
        $form['g1badge']['google_plusone_badge_page_id'] = array(
          '#title' => t('URL address to use in the Google +1 button'),
          '#type' => 'textfield',
          '#default_value' => variable_get('google_plusone_badge_page_id', ''),
          '#description' => t('Copy and paste here the whole URL of your page, including the <em>https://</em> part.<br /> For example: https://plus.google.com/u/0/101560853443212199687'),
        );
        $form['g1badge']['google_plusone_badge_style'] = array(
          '#title' => t('URL address to use in the Google +1 button'),
          '#type' => 'radios',
          '#options' => $available_styles,
          '#default_value' => variable_get('google_plusone_badge_style', 'badge'),
          '#description' => t('Choose the style of badge. Preview the styles <a href="@preview" target="_blank">in the Google Developers site</a>', array(
            '@preview' => 'https://developers.google.com/+/plugins/badge/preview',
          )),
        );
      }
      return $form;
    case 'save':
      module_load_include('inc', 'google_plusone', 'google_plusone.admin');
      if ($delta == 0) {
        $settings = array(
          'url' => $edit['google_plusone_block_url'],
          'annotation' => $edit['google_plusone_block_annotation'],
          'width' => $edit['google_plusone_block_width'],
          'size' => $edit['google_plusone_block_size'],
          'css' => google_plusone_trim($edit['google_plusone_block_wrapper_css'], ';'),
          'alias' => $edit['google_plusone_block_alias'],
        );
        variable_set('google_plusone_block_settings', $settings);
      }
      elseif ($delta == 1) {
        variable_set('google_plusone_badge_page_id', check_url(google_plusone_trim($edit['google_plusone_badge_page_id'], '/')));
        variable_set('google_plusone_badge_style', $edit['google_plusone_badge_style']);
      }
  }
}