<?php	

/**
 * Implements hook_permission().
 */
function materio_didactique_permission() {
  return array(
    'view materio didactique home block' =>  array(
      'title' => t('view materio didactique home block'),
      'description' => t(''),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function materio_didactique_block_info() {
  $blocks['materio_didactique_home'] = array(
    'info' => t('Materio didactique home block'),
    'cache' => DRUPAL_NO_CACHE
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function materio_didactique_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'materio_didactique_home':
    	if(user_access('view materio didactique home block')){
	    	$query = new EntityFieldQuery;
	    	$query
	    		->entityCondition('entity_type', 'node')
	      	->propertyCondition('status', 1)
	  	   	->entityCondition('bundle', array('didactique'))
		      ->fieldCondition('field_displayed_in_home', 'value', 1)
		      ->fieldOrderBy('field_weight', 'value', 'ASC');

		    $result = $query->execute();
	  		// dsm($result, '$result');
	  		
	  		if (isset($result['node'])) {
		      $block['subject'] = t('Didactique home block');
		      $block['content'] = theme('materio_didactique_home_block', array('items' => $result['node']));
	    	}
	    }
      break;
    
  }
  return $block;
}

/**
 * Implements hook_theme().
 */
function materio_didactique_theme($existing, $type, $theme, $path) {
  return array(
    'materio_didactique_home_block' => array(
      'arguments' => array('items'=>NULL),
      'template' => 'materio-didactique-home-block',
      'path' => drupal_get_path('module', 'materio_didactique').'/templates',
    ),
  );
}



function template_preprocess_materio_didactique_home_block(&$vars){
	// dsm($vars, 'template_preprocess_materio_didactique_home_block | $vars');

	$items = array();
	foreach ($vars['items'] as $nid => $item) {
		$items[] = node_load($nid);
	}
	$vars['items'] = $items;

}

