<?php

abstract class PerfApiMigration extends XMLMigration {
  
  public function __construct(){
  		parent::__construct(MigrateGroup::getInstance('PerfMigrate'));  
  }
  
  public function getTextsAuthors($xml, $attr, $l){
	  $values = array();
    $authors = array();
    $language = array();
    foreach ($xml as $xml_value) {
      $values = array_merge( $values, array( $this->getAttribute($xml_value, $attr), $this->getAttribute($xml_value, 'TRADUCTION')) );
      $auteur_nom = $this->getAttribute($xml_value, 'NOM_AUTEUR');
      $auteur_prenom = $this->getAttribute($xml_value, 'PRENOM_AUTEUR');
      $auteur = $auteur_nom != '' || $auteur_prenom != '' ? trim($auteur_prenom .' '. $auteur_nom) : '';
      $authors = array_merge($authors, array($auteur,$auteur));
      $language = array_merge($language, $l);
    }
    
    return array(
      'values'=>$values,
      'authors'=>$authors,
      'language'=>$language,
    );
    
	}
	
	public function getAttribute($object, $attr){
		$attr_objects = $object->xpath('@'.$attr);
		return isset($attr_objects[0]) ? (string) $attr_objects[0] : '';
	}

	public function getPersonne($object, $suffix = ''){
		$nom = $this->getAttribute($object, 'NOM'.$suffix);
		$prenom = $this->getAttribute($object, 'PRENOM'.$suffix);
		return $nom != '' || $prenom != '' ? trim($prenom .' '. $nom) : '';
	}
	
	public function getDate($o, $moment){
		$date = $this->getAttribute($o, 'ANNEE_'.$moment) 
			.'/'. $this->getAttribute($o, 'MOIS_'.$moment) 
			.'/'. $this->getAttribute($o, 'JOUR_'.$moment) 
			.'-'. $this->getAttribute($o, 'HEURE_'.$moment) 
			.':'. $this->getAttribute($o, 'MINUTE_'.$moment);
		do {
			$date = preg_replace('/(\/+|:+|-+)$/', '', $date, 1, $count);
		} while ($count);
		
		
		return $date;
	}


	public function recordGroup(&$node, $field, $xml, $attr){
		
		foreach ($xml->xpath('GROUPE_ARTISTES[@'.$attr.'="oui"]') as $xml_group){
			# group d'artiste
			$gname = trim($this->getAttribute($xml_group, 'NOM'));
			// drush_log(dt('recordGroup !name', array('!name'=>$gname)), 'status');
			$gts = taxonomy_get_term_by_name($gname);
			// 
			// drush_log(dt('group_term : !gt', array('!gt'=>print_r($gt, true))));
			if(!count($gts)){
				$gt = new stdClass();
				$gt->name = $gname;
				$gt->vid = 7;
				taxonomy_term_save($gt);
			}else{
				// print_r($gts);
				foreach ($gts as $tid => $t) {
					$gt = $t;
					break;
				}
			}
			// drush_log(dt('term group !name, tid : !tid', array('!name'=>$gt->name,'!tid'=>$gt->tid)), 'status');
			$node->{$field}['und'][] = array('tid'=>$gt->tid);

			# personne dans le group d'artist
			//!\\ ici ça ne boucle pas
			foreach ($xml->xpath('GROUPE_ARTISTES[@NOM="'.$gname.'"]/PERSONNE[@'.$attr.'="oui"]') as $xml_p){
				$term_name = trim($this->getPersonne($xml_p));
				// drush_log(dt('recordGroup :: personne : !p', array('!p'=>$term_name)), 'status');
				$tbn = taxonomy_get_term_by_name($term_name);
				if(!count($tbn)){
					$t = new stdClass();
					$t->name = $term_name;
					$t->vid = 7;
					taxonomy_term_save($t);
				}else{
					foreach ($tbn as $tid => $tt) {
						$t = $tt;
						break;
					}
				}
				$parents = taxonomy_get_parents($t->tid);
				$t->parent = array();
				foreach ($parents as $tid => $parent_term) {
					$t->parent[] = $tid;
				}
				if(!in_array($gt->tid, $t->parent)){
					$t->parent[] = $gt->tid;
				}
				$nom = trim($this->getAttribute($xml_p, 'NOM'));
				$prenom = trim($this->getAttribute($xml_p, 'PRENOM'));
				$t->field_nom = array(
					'und'=>array(
						array(
							'value' => $nom,
							'format' => null,
							'safe_value' => $nom,
						)
					)
				);
				$t->field_prenom = array(
					'und'=>array(
						array(
							'value' => $prenom,
							'format' => null,
							'safe_value' => $prenom,
						)
					)
				);
				taxonomy_term_save($t);
				// drush_log(dt('term personne !name, tid : !tid', array('!name'=>$t->name,'!tid'=>$t->tid)), 'status');
				$node->{$field}['und'][] = array('tid'=>$t->tid);
			}
		}
	}

	public function updatePersonneTerms($xml){
		foreach ($xml->xpath('PERSONNE') as $xml_concepteur){
			$nom = trim($this->getAttribute($xml_concepteur, 'NOM'));
			$prenom = trim($this->getAttribute($xml_concepteur, 'PRENOM'));

			$term_name = $nom != '' || $prenom != '' ? trim($prenom .' '. $nom) : '';
			$tbn = taxonomy_get_term_by_name($term_name);
			// if(!count($tbn)){
			// 	$t = new stdClass();
			// 	$t->name = $term_name;
			// 	$t->vid = 7;
			// 	taxonomy_term_save($t);
			// }else{
				foreach ($tbn as $tid => $tt) {
					$term = $tt;
					break;
				}
			// }
			
			$term->field_nom = array(
				'und'=>array(
					array(
						'value' => $nom,
						'format' => null,
						'safe_value' => $nom,
					)
				)
			);
			
			$term->field_prenom = array(
				'und'=>array(
					array(
						'value' => $prenom,
						'format' => null,
						'safe_value' => $prenom,
					)
				)
			);

			taxonomy_term_save($term);

		}




	}


  
}