<?php
/**
 * @file 
 * Adds a block with a series of icons which link off-site to other web presences.
 */

/**
 * Implementation of hook_block_info().
 */
function on_the_web_menu() {
  $items['admin/config/services/on_the_web'] = array(
    'title' => 'On The Web Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('on_the_web_settings_form'),
    'access arguments' => array('administer blocks'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Settings form.
 */
function on_the_web_settings_form($form, &$form_state) {
  $form = array();

  $form['on_the_web_size'] = array(
    '#type' => 'select',
    '#title' => t('Icon size'),
    '#options' => array(
      'sm' => t('Small: 32px square'),
      'lg' => t('Large: 64px square'),
    ),
    '#default_value' => array(variable_get('on_the_web_size', 'sm')),
  );
  $form['on_the_web_sitename'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use <a href="!url">Site name</a> in link titles', array('!url' => url('admin/config/system/site-information'))),
    '#default_value' => variable_get('on_the_web_sitename', TRUE),
    '#description' => t('Unchecking this box will use "us" as the title attribute for these links, instead of <em>Find !us on Facebook</em>.', array('!us' => variable_get('site_name', 'Drupal'))),
  );
  $form['on_the_web_target'] = array(
    '#type' => 'checkbox',
    '#title' => t('Open links in new windows'),
    '#default_value' => variable_get('on_the_web_target', TRUE),
    '#description' => t('Unchecking this box his will remove the target="_blank" attribute from these links.'),
  );

  // Get all the services.
  $services = on_the_web_get_services();

  foreach ($services as $service => $info) {
    $weight = variable_get('on_the_web_' . $service . '_weight', 0);

    $form['services'][$service] = array(
      '#tree' => TRUE,
      '#weight' => $weight,
    );

    $form['services'][$service]['name'] = array('#hidden' => $service);

    $form['services'][$service]['title'] = array('#markup' => $info['name']);

    $form['services'][$service]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $weight,
    );

    $form['services'][$service]['page'] = array(
      '#type' => 'textfield',
      '#default_value' => variable_get('on_the_web_' . $service . '_page', ''),
    );
  }

  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));

  return $form;
}


/**
 * Submit handler for on_the_web_settings_form().
 */
function on_the_web_settings_form_submit($form, &$form_state) {
  //dpm($form_state['values']);
  $services = on_the_web_get_services();
  foreach ($form_state['values'] as $key => $value) {
    // Save the global (string) variables.
    if (strstr($key, 'on_the_web')) {
      variable_set($key, $value);
    }

    // Save the service (array) variables.
    if (array_key_exists($key, $services)) {
      foreach ($value as $name => $setting) {
        variable_set('on_the_web_' . $key . '_' . $name, $setting);
      }
    }
  }
}

/**
 * Implementation of hook_block_info().
 */
function on_the_web_block_info() {
  $blocks[0] = array(
    'info' => t('Social Media Icons'),
  );
  return $blocks;
}

/**
 * Implementation of hook_block_configure().
 */
function on_the_web_block_configure($delta = 0) {
  $form = array();
  if ($delta == 0) {

    $form['services'] = array(
      '#type' => 'fieldset',
      '#title' => t('Icons provided for the following services:'),
      '#description' => t('The URL for each service must be entered on the OTW
        <a href="!url">settings page</a>.', array('!url' => url('admin/config/services/on_the_web'))),
    );

    $form['services']['display'] = array(
      '#type' => 'radios',
      '#title' => t('Icon display'),
      '#options' => array(
        'auto' => t('Automatic'),
        'manual' => t('Explicit'),
      ),
      '#default_value' => variable_get('on_the_web_display', 'auto'),
    );

    // Add descriptions to the radio buttons.
    $form['services']['display']['auto'] = array(
      '#description' => t('Show all icons that have corresponding links entered.'),
    );
    $form['services']['display']['manual'] = array(
      '#description' => t('Explicity choose icons here.'),
    );

    $services = on_the_web_get_services();
    $options = array();
    $defaults = array();
    foreach ($services as $service => $info) {
      // Check the box by default if the service has a URL.
      $info['page'] = variable_get('on_the_web_' . $service . '_page', FALSE);
      if ($info['page']) {
        $info['weight'] = variable_get('on_the_web_' . $service . '_weight', 0);
        $options[$service] = $info;
        $defaults[$service] = $service;
      }
    }

    // Sort the options on the block form to match weight.
    uasort($options, 'drupal_sort_weight');

    $sorted_options = array();
    foreach ($options as $service => $info) {
      $sorted_options[$service] = $info['name'];
    }

    if (!empty($options)) {
      $form['services']['enabled'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Icons'),
        '#options' => $sorted_options,
        '#default_value' => variable_get('on_the_web_enabled', $defaults),
        '#description' => t('Please select the icons you would like displayed in this block.'),
        '#states' => array(
          'visible' => array(
            ':input[name="display"]' => array('value' => 'manual'),
          ),
        ),
      );
    }
    else {
      $form['services']['none'] = array(
        '#markup' => '<p class="warning">' . t('No services have been configured yet.') . '</p>',
      );
    }
  }

  return $form;
}

 
/**
 * Implementation of hook_block_save().
 */
function on_the_web_block_save($delta = 0, $edit = array()) {
  // This example comes from node.module.
  if ($delta == 0) {
    variable_set('on_the_web_display', $edit['display']);
    if ($edit['display'] == 'manual') {
      variable_set('on_the_web_enabled', $edit['enabled']);
    }
  }
}
 
 
/**
 * Implementation of hook_block_view().
 */
function on_the_web_block_view($delta = 0) {
  $block = array(
    'subject' => t('Find Us On...'),
    'content' => on_the_web_display_block($delta)
  );

  return $block;
}

/**
 * Block Display
 *
 * @return
 *    Content for block.
 */
function on_the_web_display_block($delta) {
  $output = '';

  // Get the appropriate name to use in links.
  if (variable_get('on_the_web_sitename', TRUE)) {
    $name = variable_get('site_name', 'Drupal');
  }
  else {
    $name = t('Us');
  }

  // Get the display style, and enabled icons.
  $display = variable_get('on_the_web_display', 'auto');
  if ($display == 'manual') {
    $enabled = variable_get('on_the_web_enabled', array());
  }

  $services = on_the_web_get_services();

  $links = array();
  foreach ($services as $service => $info) {
    $link = variable_get('on_the_web_' . $service . '_page', FALSE);

    if (($display == 'auto' && $link) || 
        ($display == 'manual' && array_key_exists($service, $enabled) && $enabled[$service] === $service)) {

      if ($service != 'rss') {
        $title = t('Find !us on !service', array('!us' => $name, '!service' => $info['name']));
      }
      else {
        $title = t('!us RSS feed', array('!us' => $name));
      }

      $weight = variable_get('on_the_web_' . $service . '_weight', FALSE);
      $linked_image = theme('on_the_web_item', array('service' => $service, 'link' => $link, 'title' => $title));
      $classes = array('on-the-web', 'otw-' . $service);

      $links[] = array(
        '#weight' => $weight,
        '#theme' => 'on_the_web_icon',
        '#linked_image' => $linked_image,
        '#classes' => $classes,
      );
    }
  }

  return drupal_render($links);
}

/**
 * Gets the available services.
 *
 * @return array
 *   List of services with icons.
 */
function on_the_web_get_services() {
  $services = array(
    'twitter' => array('name' => 'Twitter'),
    'facebook' => array('name' => 'Facebook'),
    'pinterest' => array('name' => 'Pinterest'),
    'instagram' => array('name' => 'Instagram'),
    'google' => array('name' => 'Google+'),
    'youtube' => array('name' => 'YouTube'),
    'flickr' => array('name' => 'Flickr'),
    'myspace' => array('name' => 'MySpace'),
    'linkedin' => array('name' => 'LinkedIn'),
    'itunes' => array('name' => 'iTunes'),
    'delicious' => array('name' => 'Delicious'),
    'soundcloud' => array('name' => 'Soundcloud'),
    'friendfeed' => array('name' => 'FriendFeed'),
    'rss' => array('name' => 'RSS'),
  );

  drupal_alter('on_the_web_get_services', $services);

  return $services;
}

/**
 * Implementation of hook_theme().
 */
function on_the_web_theme($existing, $type, $theme, $path) {
  return array(
    'on_the_web_settings_form' => array(
      'render element' => 'form',
      'file' => 'on_the_web.theme.inc',
    ),
    'on_the_web_icon' => array(
      'variables' => array('linked_image' => NULL, 'classes' => NULL),
      'file' => 'on_the_web.theme.inc',
    ),
    'on_the_web_item' => array(
      'variables' => array('service' => NULL, 'link' => NULL, 'title' => NULL),
      'file' => 'on_the_web.theme.inc',
    ),
    'on_the_web_image' => array(
      'variables' => array('service' => NULL, 'title' => NULL),
      'file' => 'on_the_web.theme.inc',
    ),
  );
}
