<?php

/**
 *  @file
 *  Views Slideshow: ddblock adds ddblock functionality to Views Slideshows.
 *
 */

/**
 * Implements hook_init().
 */
function views_slideshow_ddblock_init() {
  // add formoptions.js for admin form
  drupal_add_js(drupal_get_path('module', 'views_slideshow_ddblock') . "/js/formoptions.js");
}

/**
 * Implements hook_menu().
 */
function views_slideshow_ddblock_menu() {
  $items = array();

  $items['views_slideshow_ddblock_mapping/js/%'] = array(
    'page callback' => 'views_slideshow_ddblock_mapping_js',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type ' => MENU_CALLBACK,
  );

  // Ahah update available template sizes of themes.
  // This block defines the path to which the AHAH call points.
  // The path relays the call to a specific AHAH callback function.
  $items['views_slideshow_ddblock/js'] = array(
    'page callback' => 'views_slideshow_ddblock_select_template_ahah', // the AHAH callback function
    'access callback' => TRUE,
    'file' => 'views_slideshow_ddblock.views_slideshow.inc',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_theme().
 */
function views_slideshow_ddblock_theme($existing, $type, $theme, $path) {
  return array(
    'views_slideshow_ddblock_pager_content' => array(
      'variables' => array(
        'views_slideshow_ddblock_pager_settings' => NULL,
        'views_slideshow_ddblock_pager_content' => NULL,
      ),
      'template' => 'theme/views-slideshow-ddblock-pager-content',
      'file' => 'theme/views_slideshow_ddblock.theme.inc',
    ),
    'views_slideshow_ddblock_main_frame' => array(
      'variables' => array(
        'vss_id' => NULL,
        'view' => NULL,
        'settings' => NULL,
        'rows' => NULL
      ),
      'template' => 'theme/views-slideshow-ddblock-main-frame',
      'file' => 'theme/views_slideshow_ddblock.theme.inc',
    ),

    'views_slideshow_ddblock_mappings_table' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Show debug information for content preprocess function helper function
 *
 * @param $vars             - An array of variables to pass to the theme template.
 */
function views_slideshow_ddblock_show_content_debug_info($vars) {
  $settings = $vars['views_slideshow_ddblock_slider_settings'];
  switch ($settings['debug_info']) {
    case 'devel':
      dpm($settings['view_name'], t('View - name'));
      dpm($settings['view_display_id'], t('View - display_id'));
      dpm(count($vars['views_slideshow_ddblock_content']), t('Number of slides'));
      foreach ($vars['views_slideshow_ddblock_content'][0] as $key => $value ) {
        $content_result[$key] = $value;
      }
      drupal_set_message('<pre>' . t('Content first slide') . '</pre>');
      dvm($content_result, t('Content first slide'));
      break;
    case 'drupal':
      drupal_set_message('<pre>' . t('View - name => @view_name', array('@view_name' => var_export($settings['view_name'], TRUE))) . '</pre>');
      drupal_set_message('<pre>' . t('View - display_Id => @view_display_id', array('@view_display_id' => var_export($settings['view_display_id'], TRUE))) . '</pre>');
      drupal_set_message('<pre>' . t('Number of slides => @nr_of_slides', array('@nr_of_slides' => count($vars['views_slideshow_ddblock_content']))) . '</pre>');
      $content_result = array();
      foreach ($vars['views_slideshow_ddblock_content'][0] as $key => $value ) {
        $content_result[$key] = $value;
      }
      drupal_set_message('<pre>' . t('Content first slide => @content_result', array('@content_result' => var_export($content_result, TRUE))) . '</pre>');
      break;
    case 'none':
      break;
  }
}

/**
 * Show debug information for pager preprocess function helper function
 *
 * @param $vars             - An array of variables to pass to the theme template.
 */
function views_slideshow_ddblock_show_pager_debug_info($vars) {
  $settings = $vars['views_slideshow_ddblock_pager_settings'];
  switch ($settings['debug_info']) {
    case 'devel':
      dpm(count($vars['views_slideshow_ddblock_content']), t('Number of pager items'));
      drupal_set_message('<pre>' . t('Pager Settings:') . '</pre>');
      dpm($vars['views_slideshow_ddblock_pager_settings'], t('Pager Settings'));
      break;
    case 'drupal':
      drupal_set_message('<pre>' . t('Pager Settings => @pager_settings', array('@pager_settings' => var_export($settings, TRUE))) . '</pre>');
      drupal_set_message('<pre>' . t('Number of pager items  => @nr_of_pager_items', array('@nr_of_pager_items' => count($vars['views_slideshow_ddblock_content']))) . '</pre>');
      break;
    case 'none':
      break;
  }
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function theme_views_slideshow_ddblock_mappings_table($variables) {

  $form = $variables['form'];
  $rows = array();
  $headers = array(
    t('Target'),
    t('Source'),
  );

  foreach (element_children($form) as $key) {
    // No need to print the field title every time.
    unset(
      $form[$key]['target']['#title'],
      $form[$key]['source']['#title']
    );

    // Build the table row.
    $row = array(
      'data' => array(
        array(
          'data' => drupal_render($form[$key]['target']) . drupal_render($form[$key]['target']),
          'class' => 'target',
        ),
        array(
          'data' => drupal_render($form[$key]['source']) . drupal_render($form[$key]['source']),
          'class' => 'source',
        ),
      ),
    );

    // Add additional attributes to the row, such as a class for this row.
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
    $rows[] = $row;
  }

  $output = theme('table', array('header' => $headers, 'rows' => $rows));
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Helper function to define populated form field elements for album track node form.
 */
function views_slideshow_ddblock_mapping_display_form($delta, $target, $source, $source_fields) {

  $form = array(
    '#tree' => TRUE,
  );

  $form['target'] = array(
    '#type' => 'textfield',
    '#title' => t('Target'),
    '#weight' => 1,
    '#size' => 20,
    '#parents' => array('style_options', 'views_slideshow_ddblock', 'mapping_wrapper', 'mappings', $delta, 'target'),
    '#default_value' => $target,
  );

  $form['source'] = array(
    '#type' => 'select',
    '#title' => t('Source'),
    '#weight' => 2,
    '#parents' => array('style_options', 'views_slideshow_ddblock', 'mapping_wrapper', 'mappings', $delta, 'source'),
    '#default_value' => $source,
    '#size' => 1,
    '#options' => $source_fields,
  );

  return $form;
}

// Trying to make sure the theme.inc get's loaded.
include_once('theme/views_slideshow_ddblock.theme.inc');

function template_preprocess_views_slideshow_ddblock_main_frame(&$vars) {
  _views_slideshow_ddblock_preprocess_views_slideshow_ddblock_main_frame($vars);
}

function template_preprocess_views_slideshow_ddblock_pager_content(&$vars) {
  _views_slideshow_ddblock_preprocess_views_slideshow_ddblock_pager_content($vars);
}
