<?php
$element = $vars['element'];
// dsm($vars, $element['#field_name'].' vars');


# QUEST-CE QUE C4EST QUE CE TRUC ?
if(count($vars['items']) == 1 && $element["#field_type"] == 'text' && $vars['element']['#field_name'] != 'field_date_de_debut'){
  if($vars['items'][0]['#markup'] == '' || count($vars['items'][0]['#markup']) < 5)
    $vars['items'] = array();
}

if(($element["#field_type"] == 'text_long') && count($vars['items']) == 1){
  if($vars['items'][0]['#markup'] == '')
    $vars['items'] = array();
}

$personnes_fields = array('field_concepteur', 'field_executant', 'field_organisateur', 'field_temoin');
if(in_array($vars['element']['#field_name'], $personnes_fields)){
  $vars['classes_array'][] = 'field-personne';
  // dsm($vars, 'vars');
  foreach ($vars['element']['#items'] as $index => $item) {
    $term = $item['taxonomy_term'];
    // dsm($term, 'term');
    $children = array();
    foreach(taxonomy_get_children($term->tid, $term->vid) as $child){
      // dsm($child, 'child');
      $children[] = $child->name;
    }
    if(count($children)){
      // dsm($children, 'children');
      $vars['items'][$index]["#markup"] .= ' ('.implode(', ', $children).')';
    }
  }
}
  
  
#images
if($element['#field_type'] == 'image' && $element['#object']->type == 'performance'){
  $vars['item_classes'] = array();  
  $delta = 0;
  foreach ($vars['items'] as $item) {
    // $classes = array();
    $classes[] = 'image image-'.$delta;//.' '.($delta % 2 ? 'odd' : 'even');
    
    // if($delta == 0){
    //   $vars['items'][$delta]['#image_style'] = "grid_large";
    //   $classes[] = 'grid-large-image';
    // }else if($delta < 4){
    //   $vars['items'][$delta]['#image_style'] = "grid_medium";
    //   $classes[] = 'grid-medium-image';
    // }else{
      // $vars['items'][$delta]['#image_style'] = "grid_small";
      // $classes[] = 'grid-small-image';
    // }





    $vars['item_classes'][] = implode(' ', $classes);
    $delta++;
  }
}


#date
if($vars['element']['#field_name'] == 'field_date_de_debut' || $vars['element']['#field_name'] == 'field_date_de_fin'){
  global $language;
   //dsm($vars, "vars");
  foreach ($vars['items'] as $i => $item) {
    if($item['#markup'] != ''){
      $date_parts = explode('/', $item['#markup']);
      $date_str = $date_parts[0].'-'.(isset($date_parts[1]) ? $date_parts[1] : '1').'-'.(isset($date_parts[2]) ? $date_parts[2] : '1').' 00:00:00';
      $date = strtotime($date_str);

      //$vars['items'][$i]['#markup'] .= ' || '.$date_str . ' || '. date('d m Y', $date);
      switch($language->language){
        case 'fr':
          $date_frmt = (isset($date_parts[1]) && isset($date_parts[2])) ? 'd m Y' : 'Y' ;
          break;
        case 'en':
          $date_frmt = (isset($date_parts[1]) && isset($date_parts[2])) ? 'm d Y' : 'Y' ;
          break;
      }
      
      $vars['items'][$i]['#markup'] = date($date_frmt, $date);
  
    }
    
  }


}