<?php
/**
 * @file
 * Module file for content slider module.
 *
 */


/**
 * Implements hook_permission().
 *
 * We don't need view permission as the module only defines blocks
 * so users can set up proper permissions with Drupal blocks permissions
 */
 
function content_slider_permission() {
  return array(
    'administer content slider' => array(
      'title' => t('Administer Content Slider'),
    ),
  );
}


/**
 * Implements hook_menu().
 */
function content_slider_menu() {
  $items = array();
  $items['admin/settings/content_slider'] = array(
    'title' => 'Content Slider',
    'description' => 'Setting Content Slider.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('content_slider_admin_settings'),
    'access arguments' => array('administer content slider'),
	'file' => 'includes/content_slider.admin.inc',
  );
  return $items;
}


/**
 * Implements hook_block_info().
 */
function content_slider_block_info() {
   // This is an hard limit which isn't good
   $blocks = array();
      for ($i=0; $i < 3; $i++) {
        $content_type = variable_get('content_slider_source_'. $i, '');
        if ($content_type != '') {
          $blocks['content_slider_source_'. $i]['info'] = t('Content Slider !delta - !content_type', array('!delta' => $i, '!content_type' => $content_type));
        }
      }      
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function content_slider_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'content_slider_source_0' :
	case 'content_slider_source_1' :
	case 'content_slider_source_2' :
		 
       $content_type = variable_get($delta, '');
      
      if ($content_type != '') {
        // We add necessary CSS and JavaScript files
        drupal_add_css(drupal_get_path('module', 'content_slider') . '/css/contentslider.css');
        drupal_add_js(drupal_get_path('module', 'content_slider'). '/js/contentslider.js');
        $nodes = array();
        $nodes = content_slider_load_nodes($content_type);
		
        $block['subject'] = t('Content Slider !delta', array('!delta' => $delta));
        $block['content'] = theme('content_slider', array('nodes' => $nodes, 'delta' => $delta));
        
        $previous = t('Pre');
        $next = t('Next');
        $speed = variable_get('content_slider_speed', 1000);
        $auto_rotate = (bool) variable_get('content_slider_auto', 1);
       
	   drupal_add_js(array('delta' => $delta, 'next' => $next, 'previous' => $previous, 'auto_rotate' => $auto_rotate, 'speed' => $speed), array('type' => 'setting'));
       drupal_add_js(drupal_get_path('module', 'content_slider') . '/js/slider_settings.js', array('scope' => 'header', 'weight' => 5));

      }
     break;
   }
  return $block;
}


/**
 * Collect all node of a specific content type.
 *@para string $content_type
 *  machine name of content type
 *@return array $nodes
 *  associative array contains node's nid as key and node's object as value
 */
function content_slider_load_nodes($content_type) {
   $nodes = array();
   $nids = db_select('node', 'n')
      ->fields('n', array('nid', 'created'))
      ->condition('n.status', 1)
	  ->condition('n.type', array($content_type), 'IN')
      ->orderBy('n.created', 'DESC')
      ->range(0, 10)
      ->addTag('node_access')
      ->execute()
      ->fetchCol();
   $nodes = node_load_multiple($nids);
 return $nodes ? $nodes : array();
}

/**
 * You can easily override this function at the theme level
 */
function theme_content_slider($variables) {
  $nodes = $variables['nodes'];
  $delta = $variables['delta'];
  $output['nodedata'] = array(
    '#prefix' => "<div id='slider". $delta ."' class='sliderwrapper'>",
	'#suffix' => "</div><div id='paginate-slider". $delta ."' class='pagination'></div>",
  ); 
 if(sizeof($nodes) > 0 ):
   foreach ($nodes as $key => $node) :
     $output['nodedata'][$node->nid] = array(
	   '#prefix' => "<div class='contentdiv'>",
	   '#suffix' => "</div>",
	   '#markup' => "    <a href='". url('node/'. $node->nid) ."'>".drupal_render(node_view($node))."</a>",
	  );
     endforeach;
  endif;
 return $output;
}


/**
 * Implementation of hook_theme().
 */
function content_slider_theme() {
  return array(
    'content_slider' => array(
      'variables' => array('nodes' => NULL, 'delta' => NULL),
    ),
   );
 }



/**
 * Implements hook_node_info().
 *
 * We use hook_node_info() to define our node content type.
 */
function content_slider_node_info() {
  // We define the node type as an associative array.
  return array(
    'content_slider' => array(
      'name' => t('Content Slider'),
      'base' => 'content_slider',
      'description' => t('Slider is a single content entry for the slider.'),
	  'title_label' => t('Content Slider'),
	  'locked' => TRUE,
    ),
  );
}

/**
 * Implements hook_node_type_insert().
 */
function content_slider_node_type_insert($content_type) {
  if ($content_type->type == 'content_slider') {
     $body_instance = node_add_body_field($content_type, t('Description'));
    
    // Save our changes to the body field instance.
    field_update_instance($body_instance);

    // Create all the fields we are adding to our content type.
    foreach (_content_slider_installed_fields() as $field) {
      field_create_field($field);
    }

    // Create all the instances for our fields.
    foreach (_content_slider_installed_instances() as $instance) {
      $instance['entity_type'] = 'node';
      $instance['bundle'] = 'content_slider';
      field_create_instance($instance);
    }
  }
}


/**
 * Implement hook_form().
 */
function content_slider_form($node, $form_state) {
  return node_content_form($node, $form_state);
}



/**
 * Implementation of hook_help().
 */
function content_slider_help($section) {
  switch ($section) {
    case 'admin/help#content_slider':
      $output = t("The content_slider module: Display content and image in Slide show mode using jQuery.");
      return $output;

    case 'admin/modules#description':
      return t("The content_slider module: Display content and image in Slide show mode using jQuery.");
  }
}


/**
 * Define the fields for our content type.
 *
 * This big array is factored into this function for readability.
 *
 * @return
 *  An associative array specifying the fields we wish to add to our
 *  new node type.
 */
function _content_slider_installed_fields() {
  return array(
     'field_slider_image' => array(
       'field_name' => 'field_slider_image',
       'type'       => 'image',
       'cardinality' => 1,
    ),
  );
}

/**
 * Define the field instances for our content type.
 *
 * The instance lets Drupal know which widget to use to allow the user to enter
 * data and how to react in different view modes.  We are going to display a
 * page that uses a custom "content_slider_list" view mode.  We will set a
 * cardinality of three allowing our content type to give the user three color
 * fields.
 *
 * This big array is factored into this function for readability.
 *
 * @return
 *  An associative array specifying the instances we wish to add to our new
 *  node type.
 */
function _content_slider_installed_instances() {
  return array(
    'field_slider_image' => array(
      'field_name'  => 'field_slider_image',
      'label'       => t('Upload an images'),
      'required'    => FALSE,
      'widget' => array(
        'type'    => 'image_image',
        'weight'  => 2.10,
      ),
	  'display' => array(
          'default' => array(
            'label' => 'hidden',
            'type' => 'image',
            'module' => 'image',
            'weight' => 1,
          ),
	   ),
    ),
   );
}

/**
 * @} End of "defgroup content_slider".
 *
*/