<?php 

function ds_bootstrap_slider_ctools_plugin_directory($module, $plugin) {
	if (  $module == 'panels' && $plugin == 'styles' ) {
		return 'plugins/' . $plugin;
	}
}


function ds_bootstrap_slider_field_formatter_info() {
	return array(
		'ds_bootstrap_slider' => array( //Machine name of the formatter
			'label' => t('Bootstrap Slider'),
			'field types' => array('entityreference','file', 'image'), //This will only be available to text fields
			'settings'  => array( //Array of the settings we'll create
				'interval' => 3000, //give a default value for when the form is first loaded
				'pause' => 'hover', //ditto
				'wrap'  => 0,
				'nav'   => 1,
				'view_mode' => 'default'
			),
		),
	);
}

function ds_bootstrap_slider_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
	//This gets the view_mode where our settings are stored
	$display = $instance['display'][$view_mode];
	//This gets the actual settings
	$settings = $display['settings'];
	//Initialize the element variable
	$element = array();
	//Add your select box
	$element['interval'] = array(
		'#type'           => 'textfield',                           // Use a select box widget
		'#title'          => t('Interval'),                   // Widget label
		'#description'    => t('The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.'), // Helper text
		'#default_value'  => $settings['interval'],              // Get the value if it's already been set
	);
	$element['pause'] = array(
		'#type'           => 'textfield',                        // Use a textbox
		'#title'          => t('Pause'),                      // Widget label
		'#description'    => t('Pauses the cycling of the carousel on mouseenter and resumes the cycling of the carousel on mouseleave.'),  // helper text
		'#default_value'  => $settings['pause'],               // Get the value if it's already been set
	);
	$element['wrap'] = array(
		'#type'           => 'checkbox',                        // Use a textbox
		'#title'          => t('Wrap'),                      // Widget label
		'#description'    => t('Whether the carousel should cycle continuously or have hard stops.'),  // helper text
		'#return_value'   => 1,
		'#default_value'  => $settings['wrap'],               // Get the value if it's already been set
	);
	$element['nav'] = array(
		'#type'           => 'checkbox',                        // Use a textbox
		'#title'          => t('Show navigation'),                      // Widget label
		'#return_value'   => 1,
		'#default_value'  => $settings['nav'],               // Get the value if it's already been set
	);
	$options = array('default' => t('Default'));
	
	if ( $field['type'] == 'file' || $field['type'] == 'image') {
		$info = entity_get_info('file');
	} else {
		$info = entity_get_info( $field['settings']['target_type']) ;
	}
	if ( $info['view modes'] ) {
		foreach ( $info['view modes'] as $key => $label  ) {
			$options[$key] = $label['label'] ;
		}
	}
	$element['view_mode'] = array(
		'#type'           => 'select',                        // Use a textbox
		'#title'          => t('View mode'),                      // Widget label
		'#description'    => t('Whether the carousel should cycle continuously or have hard stops.'),  // helper text
		'#options'		  => $options ,
		'#default_value'  => $settings['view_mode'],               // Get the value if it's already been set
	);
	return $element;
}

function ds_bootstrap_slider_field_formatter_settings_summary($field, $instance, $view_mode) {
	$display = $instance['display'][$view_mode];
	$settings = $display['settings'];
	$summary = t('Interval: @interval, Pause: @pause, Wrap: @wrap', array(
			'@interval'     => $settings['interval'],
			'@pause'  => $settings['pause'],
			'@wrap'  => $settings['wrap'],
	));
	return $summary;
}

function ds_bootstrap_slider_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
	$element = array(); // Initialize the var
	$settings = $display['settings']; // get the settings
	$id = drupal_html_id('carousel-' . $field['field_name']) ;
	
	if ( $field['type'] == 'file' || $field['type'] == 'image' ) {
		$entity_to_load = 'file' ;
		$entity_load_key = 'files' ;
	} else {
		$entity_to_load = $entity_load_key = $field['settings']['target_type'] ;
		$entity_load_key = $entity_to_load ;
	}
	
	$element['#prefix'] = '<div id="'.$id.'" class="carousel slide" data-interval="'.$settings['interval'].'" data-wrap="'.$settings['wrap'].'" data-pause="'.$settings['pause'].'" data-ride="carousel"><div class="carousel-inner">' ;
	$element['#suffix'] = '</div></div>' ;

	$entity_id = entity_extract_ids($entity_type,$entity);
	$entity_id = array_shift($entity_id) ;
	$etids = array();
	
	$settings['id'] = $id . '-' . $entity_id ;
	
 	foreach ( $items as $item ) {
 		if ( $field['type'] == 'file' || $field['type'] == 'image' ) {
 			$etids[] = $item['fid'] ;
 		} else $etids[] = $item['target_id'] ;
	}
		
	if ( count($etids) == 0 ) return array();
		
	if (  $field['type'] != 'file' || $field['type'] != 'image' ) {
		
		$entities = entity_load($entity_to_load,$etids) ;
	} else {
		$entities = $items ;
	}
	
	foreach ( $entities as $entity ) {
		$content[] = entity_view($entity_to_load,array($entity),$settings['view_mode']); 
	}

	$element = array(
	  '#theme' => 'ds_bootstrap_slider',
	  '#items' => $content,
	  '#settings' => $settings,
	  '#entity_type' => $entity_to_load
	);

	return $element;
}

function template_preprocess_ds_bootstrap_slider(&$vars) {
	$vars['theme_hook_suggestions'][] = 'ds-bootstrap-slider__' . $vars['entity_type'] ;
	$vars['theme_hook_suggestions'][] = 'ds-bootstrap-slider__' . $vars['entity_type'] . '_' . $vars['settings']['view_mode'] ;
}

function ds_bootstrap_slider_theme($existing, $type, $theme, $path) {
	return array('ds_bootstrap_slider' => array(
		'variables' => array('items' => array() , 'settings' => array(), 'entity_type' => NULL),
		'path' => drupal_get_path('module','ds_bootstrap_slider') . '/theme',
		'template' => 'ds-bootstrap-slider',
	));
}