<?php

/**
 * @file
 * Install, update and uninstall functions for the responsive_slideshow module.
 */

/**
 * Implements hook_install().
 */
function responsive_slideshow_install() {
  $t = get_t();
  // Machine name of the content type.
  $name = 'responsive_slideshow';
  // Define the node type.
  $slideshow = array(
    'type' => $name,
    'name' => $t('Responsive Slideshow'),
    'base' => 'node_content',
    'title_label' => $t('Slideshow Title'),
    'description' => $t('Slideshow Content'),
    'custom' => TRUE,
    'comment' => 0,
  );
  // Set other node defaults not declared above.
  $content_type = node_type_set_defaults($slideshow);
  // Add the body field.
  node_add_body_field($content_type, $t('Slideshow Description'));
  // Making comment settings closed.
  variable_set('comment_responsive_slideshow', '1');
  node_type_save($content_type);
  // Add peristant variables that control settings.
  variable_set('additional_settings__active_tab_' . $name, 'edit-menu');
  variable_set('node_preview_' . $name, 1);
  variable_set('node_options_' . $name, array(0 => 'status', 1 => 'promote'));
  variable_set('node_submitted_' . $name, 0);
  variable_set('menu_options_' . $name, array());
  variable_set('menu_parent_' . $name, 'main-menu:0');
  // Create all the fields we are adding to our content type.
  foreach (_responsive_slideshow_installed_fields() as $field) {
    if (!field_info_field('field_slideshow_image') || !field_info_field('field_slideshow_teaser')) {
      field_info_cache_clear();
      field_create_field($field);
    }
  }
  // Create all the instances for our fields.
  foreach (_responsive_slideshow_installed_instances() as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = $name;
    field_create_instance($instance);
  }
  // Create an image style for the carousel.
  $style = image_style_save(array('name' => 'responsive_slideshow_style'));
  $effect = array(
    'name' => 'image_scale_and_crop',
    'data' => array(
      'width' => 1140,
      'height' => 300,
      'upscale' => TRUE,
    ),
    'isid' => $style['isid'],
  );
  image_effect_save($effect);
  variable_set('responsive_slideshow_no_of_slides', 5);
  variable_set('responsive_slideshow_description_length', 180);
  variable_set('responsive_slideshow_interval', 5000);
}

/**
 * Returns a structured array defining the fields created by this content type.
 *
 * @return mixed.
 *   An associative array specifying the fields we wish to add to our
 *   new node type.
 */
function _responsive_slideshow_installed_fields() {
  $t = get_t();
  // Create the field.
  $field_array = array(
    'slideshow_image' => array(
      'field_name' => 'field_slideshow_image',
      'label' => $t('Slideshow Image'),
      'cardinality' => 1,
      'type' => 'image',
      'settings' => array(
        'default_image' => 0,
        'uri_scheme' => 'public',
      ),
    ),
     // A text (textarea) field.
    'slideshow_teaser' => array(
      'field_name'   => 'field_slideshow_teaser',
      'label'        => $t('Teaser'),
      'cardinality'  => 1,
      'type'         => 'text',
      'settings'     => array(
        'max_length'  => 1000,
      ),
    ),
  );
  return $field_array;
}

/**
 * Returns a structured array defining the instances for this content type.
 *
 * @return mixed.
 *   An associative array specifying the instances we wish to add to our new
 *   node type.
 */
function _responsive_slideshow_installed_instances() {
  $t = get_t();
  // Create the instance.
  $instance_array = array(
    // Instance of the image field above.
    'slideshow_image' => array(
      'field_name' => 'field_slideshow_image',
      'label' => $t('Slideshow Image'),
      'cardinality' => 1,
      'required' => TRUE,
      'type' => 'slideshow_image',
      'settings' => array(
        'alt_field' => 1,
        'file_directory' => 'image',
        'file_extensions' => 'png gif jpg jpeg',
        'max_filesize' => '',
        'max_resolution' => '',
        'min_resolution' => '',
        'title_field' => 1,
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'settings' => array(
          'preview_image_style' => 'thumbnail',
          'progress_indicator' => 'throbber',
        ),
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'image',
          'settings' => array('image_style' => 'large', 'image_link' => ''),
          'weight' => -1,
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'image',
          'settings' => array('image_style' => 'thumbnail', 'image_link' => 'content'),
          'weight' => -1,
        ),
      ),
    ),
    // Instance of the textarea field above.
    'slideshow_teaser' => array(
      'field_name'  => 'field_slideshow_teaser',
      'label'       => $t('Teaser'),
      'cardinality' => 1,
      'widget'      => array(
        'type'       => 'text_textarea',
        'settings'   => array('rows' => 5),
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
        ),
      ),
    ),
  );
  return $instance_array;
}

/**
 * Implements hook_uninstall().
 */
function responsive_slideshow_uninstall() {
  // Machine name of the content type.
  $name = 'responsive_slideshow';
  // Gather and delete all job nodes created.
  $results = db_select('node', 'n')
              ->fields('n', array('nid'))
              ->condition('type', $name)
              ->execute();
  foreach ($results as $result) {
    $nids[] = $result->nid;
  }
  if (!empty($nids)) {
    node_delete_multiple($nids);
  }
  // Remove peristant variables that control settings.
  variable_del('additional_settings__active_tab_' . $name);
  variable_del('node_preview_' . $name);
  variable_del('node_options_' . $name);
  variable_del('node_submitted_' . $name);
  variable_del('menu_options_' . $name);
  variable_del('menu_parent_' . $name);
  foreach (array_keys(_responsive_slideshow_installed_fields()) as $field) {
    field_delete_field($field);
  }
  // Delete our content type.
  node_type_delete($name);
  // Purge all field infromation.
  field_purge_batch(1000);
  // Delete created image style.
  image_style_delete(image_style_load('responsive_slideshow_style'));
  // Delete created variables.
  variable_del('responsive_slideshow_no_of_slides');
  variable_del('responsive_slideshow_description_length');
  variable_del('responsive_slideshow_interval');
  // Clear the cache tables.
  drupal_flush_all_caches();
}
