<?php

/**
 * @file
 * Custom Service Links field for Displays.
 *
 * This plugin provides fields for Displays Suite, one
 * for each service selected on the admin services page
 * through the "Display" checkbox column and a field
 * containing the whole set of selected services.
 *
 * @author Fabio Mucciante (TheCrow)
 */

/**
 * Implements hook_theme().
 */
function service_links_displays_theme() {
  return array(
    'sld_group_fisheye' => array(
      'variables' => array(),
    ),
    'sld_group_text' => array(
      'variables' => array(),
    ),
    'sld_group_image' => array(
      'variables' => array(),
    ),
    'sld_group_image_and_text' => array(
      'variables' => array(),
    ),
    'sld_single_text' => array(
      'variables' => array(),
    ),
    'sld_single_image' => array(
      'variables' => array(),
    ),
    'sld_single_image_and_text' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Implements hook_ds_fields_info().
 */
function service_links_displays_ds_fields_info($entity_type) {
  $fields = array(
    'service_links_displays_group' => array(
      'title' => t('Service Links Group'),
      'field_type' => DS_FIELD_TYPE_THEME,
      'properties' => array(
        'formatters' => array(
          'sld_group_text' => t('Text'),
          'sld_group_image' => t('Image'),
          'sld_group_image_and_text' => t('Image and text'),
          'sld_group_fisheye' => t('FishEye'),
        ),
      ),
    ),
  );

  $services = array_filter(variable_get('service_links_displays', array()));
  if (!empty($services)) {
    $services = service_links_get_links($services);
    foreach ($services as $service_id => $service) {
      $fields['service_links_displays_'. $service_id] = array(
        'title' => t('Service Links Field @name', array('@name' => $service['name'])),
        'field_type' => DS_FIELD_TYPE_THEME,
        'properties' => array(
          'key' => $service_id,
          'formatters' => array(
            'sld_single_text' => t('Text'),
            'sld_single_image' => t('Image'),
            'sld_single_image_and_text' => t('Image and text'),
          ),
        ),
      );
    }
  }

  return array('node' => $fields);
}

/**
 * Apply the Text format to a single Service.
 */
function theme_sld_single_text($variables) {
  $node = $variables['entity'];
  $service_id = $variables['properties']['key'];

  if (service_links_show($node) && user_access('access service links')) {
    $items = service_links_render_some($service_id, $node, FALSE, SERVICE_LINKS_STYLE_TEXT);
    if (!empty($items)) {
      return implode($items);
    }
  }
}

/**
 * Apply the Image format to a single Service.
 */
function theme_sld_single_image($variables) {
  $node = $variables['entity'];
  $service_id = $variables['properties']['key'];

  if (service_links_show($node) && user_access('access service links')) {
    $items = service_links_render_some($service_id, $node, FALSE, SERVICE_LINKS_STYLE_IMAGE);
    if (!empty($items)) {
      return implode($items);
    }
  }
}

/**
 * Apply the Image and Text format to a single Service.
 */
function theme_sld_single_image_and_text($variables) {
  $node = $variables['entity'];
  $service_id = $variables['properties']['key'];

  if (service_links_show($node) && user_access('access service links')) {
    $items = service_links_render_some($service_id, $node, FALSE, SERVICE_LINKS_STYLE_IMAGE_AND_TEXT);
    if (!empty($items)) {
      return implode($items);
    }
  }
}

/**
 * Apply the FishEye format to the field Service Links Group.
 */
function theme_sld_group_fisheye($variables) {
  $node = $variables['entity'];

  if (service_links_show($node) && user_access('access service links')) {
    $services = array_filter(variable_get('service_links_displays', array()));
    $options = array(
      'style' => SERVICE_LINKS_STYLE_FISHEYE,
      'link_show' => $services,
    );

    return theme('service_links_fisheye_format', array(
      'items' => service_links_render($node, FALSE, $options),
    ));
  }
}

/**
 * Apply the Text format to the field Service Links Group.
 */
function theme_sld_group_text($variables) {
  $node = $variables['entity'];

  if (service_links_show($node) && user_access('access service links')) {
    $services = array_filter(variable_get('service_links_displays', array()));
    $options = array(
      'style' => SERVICE_LINKS_STYLE_TEXT,
      'link_show' => $services,
    );

    return theme('service_links_block_format', array(
      'items' => service_links_render($node, FALSE, $options),
      'style' => SERVICE_LINKS_STYLE_IMAGE,
    ));
  }
}

/**
 * Apply the Image format to the field Service Links Group.
 */
function theme_sld_group_image($variables) {
  $node = $variables['entity'];

  if (service_links_show($node) && user_access('access service links')) {
    $services = array_filter(variable_get('service_links_displays', array()));
    $options = array(
      'style' => SERVICE_LINKS_STYLE_IMAGE,
      'link_show' => $services,
    );

    return theme('service_links_block_format', array(
      'items' => service_links_render($node, FALSE, $options),
      'style' => SERVICE_LINKS_STYLE_IMAGE,
    ));
  }
}

/**
 * Apply the Image and Text format to the field Service Links Group.
 */
function theme_sld_group_image_and_text($variables) {
  $node = $variables['entity'];

  if (service_links_show($node) && user_access('access service links')) {
    $services = array_filter(variable_get('service_links_displays', array()));
    $options = array(
      'style' => SERVICE_LINKS_STYLE_IMAGE_AND_TEXT,
      'link_show' => $services,
    );

    return theme('service_links_block_format', array(
      'items' => service_links_render($node, FALSE, $options),
      'style' => SERVICE_LINKS_STYLE_IMAGE,
    ));
  }
}

/**
 * Implements hook_form_alter().
 */
function service_links_displays_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'service_links_admin_services') {
    $settings['displays'] = variable_get('service_links_displays', array());
    $settings['weight'] = variable_get('service_links_weight', NULL);

    $form['service_links']['service_links_displays'] = array('#tree' => TRUE);
    $services = array_filter(array_keys($form['service_links']['service_links_weight']), '_sld_only_keys');

    foreach ($services as $service_id) {
      $weight = isset($settings['weight'][$service_id]) ? $settings['weight'][$service_id] : 0;
      $form['service_links']['service_links_displays'][$service_id] = array(
        '#weight' => $weight,
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => isset($settings['displays'][$service_id]) ? $settings['displays'][$service_id] : 0,
        '#attributes' => array(),
      );
    }
  }
}

/**
 * Implements hook_sl_servicestable_alter().
 */
function service_links_displays_sl_servicestable_alter(&$table, $form) {
  $table['header'][] = t('Displays');

  if (empty($table['rows'])) {
    return;
  }

  $num_row = 0;
  $col_num = count($table['rows'][0]['data']);
  foreach (element_children($form['service_links_displays']) as $service_id) {
    $service = $form['service_links_displays'][$service_id];

    $service['displays'] = array(
      '#type' => 'checkbox',
      '#checked' => $service['#default_value'],
      '#id' => $service['#id'],
      '#name' => $service['#name'],
      '#attributes' => $service['#attributes'],
    );
    $col =  drupal_render($service['displays']);

    $table['rows'][$num_row]['data'][$col_num] = $col;
      
    $num_row++;
  } 
}

/**
 * Filter the id from other fields.
 */
function _sld_only_keys($item) {
  return preg_match('/^#/', $item) ? FALSE : TRUE;
}
