<?php
/**
 * PerfGroupeNodeMigration
 * 
 */
class PerfLDCNodeMigration extends PerfApiMigration {
  public function __construct() {
		
		$this->description = t('Migrate Lieu Date Contexte (effectuation) Class');

    parent::__construct();

		$fields = array(
		  'language' => t('Lanuguage'),
			'ID' => t('id'),
			'title' => t('title'),

			'structure_organisatrice' => t('Structure organisatrice'), // STRUCTURE_ORGANISATRICE
			
      'topologie' => t('Topologie'),
      
      'date_de_debut' => t('date debut'),
      'date_de_fin' => t('date fin'),
      'duree' => t('duree'), //'UNITE_DUREE' => t('UNITE_DUREE'),
      
      'contexte' => t('contexte'), // CONTEXTE- TRADUCTION_CONTEXTE
      
      'lieu' => t('lieu'),
      'adresse' => t('adresse'),
      'precision'=> t('precision'),
      'ville' => t('ville'),
      'pays' => t('pays'),
      
      'executant'=> t('executant'),
      'concepteur'=> t('concepteur'),
      'organisateur'=> t('organisateur'),
      
      'notes' => t('notes'),
      
    );
    
    // The source ID here is the one retrieved from the XML listing file, and
    // used to identify the specific item's file
    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'ID' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
        )
      ),
      MigrateDestinationNode::getKeySchema()
    );

  	// This can also be an URL instead of a file path.
    $xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', 'PerfMigrate') . '/xml/';
    $items_url = $xml_folder . 'baseperf-ldcs.xml';

    // We use the MigrateSourceMultiItems class for any source where we obtain the list
    // of IDs to process and the data for each item from the same file. Typically the data
    // for an item is not contained in a single line within the source file. Examples include
    // multiple items defined in a single xml file or a single json file where in both cases
    // the id is part of the item.
    $item_xpath = '/ROOT/ITEMS/LIEU_DATE_CONTEXTE';  // relative to document		

    $item_ID_xpath = '@ID';         // relative to item_xpath and gets assembled
                                    // into full path /producers/producer/sourceid


    $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath); // 
    $this->source = new MigrateSourceMultiItems($items_class, $fields);

    $this->destination = new MigrateDestinationNode('effectuation');
  	
  	$this->addFieldMapping('language')->defaultValue('fr');
		$this->addFieldMapping('is_new')->defaultValue(TRUE);
		// $this->addFieldMapping('created', 'date_creation');
		// $this->addFieldMapping('changed', 'date_modif');
		$this->addFieldMapping('status')->defaultValue(1);
		$this->addFieldMapping('promote')->defaultValue(0);
		$this->addFieldMapping('sticky')->defaultValue(0);
		
		$this->addFieldMapping('title', 'title');
		
		# temps
    $this->addFieldMapping('field_date_de_debut', 'date_de_debut');    
    $this->addFieldMapping('field_date_de_fin', 'date_de_fin');
    $this->addFieldMapping('field_duree', 'duree');   
    
    # lieu
    $this->addFieldMapping('field_lieu', 'lieu')
        ->xpath('@NOM_LIEU');
    $this->addFieldMapping('field_lieu:create_term')->defaultValue(TRUE);
    
    # topologie
    $this->addFieldMapping('field_topologie', 'topologie')
        ->xpath('@TYPE');
    $this->addFieldMapping('field_topologie:create_term')->defaultValue(TRUE);
    
    # structure_organisatrice
    $this->addFieldMapping('field_structure', 'structure_organisatrice');
    $this->addFieldMapping('field_structure:create_term')->defaultValue(TRUE);
    
    # contexte
    $this->addFieldMapping('field_contexte', 'contexte');
    $this->addFieldMapping('field_contexte:format')->defaultValue('filtred_html');
    $this->addFieldMapping('field_contexte:language', 'language');
    
		# personnes
		$this->addFieldMapping('field_concepteur', 'concepteur');
		$this->addFieldMapping('field_concepteur:create_term')->defaultValue(TRUE);
		$this->addFieldMapping('field_executant', 'executant');
		$this->addFieldMapping('field_executant:create_term')->defaultValue(TRUE);
		$this->addFieldMapping('field_organisateur', 'organisateur');
		$this->addFieldMapping('field_organisateur:create_term')->defaultValue(TRUE);
		
		# note
    $this->addFieldMapping('field_note', 'notes')
      ->xpath('@NOTES');
    
    # addresse
    // $this->addFieldMapping('field_precision', 'precision')
    //   ->xpath('@PRECISION');
    // $this->addFieldMapping('field_precision:format')->defaultValue('filtred_html');
    // $this->addFieldMapping('field_precision:language')->defaultValue('fr');
		$arguments = array(
      'thoroughfare' => array('source_field' => 'adresse'),
      'premise' => array('source_field' => 'precision'),
      // 'sub_premise' => array('source_field' => 'sub_premise'),
      'locality' => array('source_field' => 'ville'),
      // 'postal_code' => array('source_field' => 'zip'),
    );
		$this->addFieldMapping('field_address', 'pays')
			->arguments($arguments);
		$this->addFieldMapping(NULL, 'adresse');
		$this->addFieldMapping(NULL, 'precision');
		$this->addFieldMapping(NULL, 'ville');
		
		
    $this->addUnmigratedDestinations(array('revision_uid', 'created', 'changed', 'revision', 'log', 'tnid','comment', 'uid','path', 'pathauto',
    	'field_date_de_debut:format', 'field_date_de_debut:language', 'field_date_de_fin:format', 'field_date_de_fin:language',
			'field_dure:format', 'field_dure:language',
			'field_lieu:source_type', 'field_topologie:source_type', 'field_structure:source_type',
      'field_concepteur:source_type','field_executant:source_type','field_organisateur:source_type',
      'field_note:format', "field_note:language",
		));
		
	}
	
	public function prepareRow($row){
		// dsm($row , '--- $row ---');
		$xml = $row->xml;
		
    $row->language = array('fr', 'en');
    
    $title = $this->getAttribute($xml, 'TITLE');
    if(strlen($title) > 255)
      $title = substr($title, 0, 250) . '...';
    $row->title = $title;
    
    # temps
		$row->date_de_debut = $this->getDate($xml, 'DEBUT');
		$row->date_de_fin = $this->getDate($xml, 'FIN');
		$row->duree = $this->getAttribute($xml, 'DUREE') .' '. $this->getAttribute($xml, 'UNITE_DUREE');
		
		# espace
		$row->lieu = $this->getAttribute($xml, 'NOM_LIEU');
		
		# structure
		$structure = array();
		foreach ($xml->xpath('STRUCTURE_ORGANISATRICE') as $xml_strucrture)
			$structure[] = $this->getAttribute($xml_strucrture, 'NOM');
    $row->structure_organisatrice = $structure;
    
    # contexte
    $row->contexte = array(
      $this->getAttribute($xml, "CONTEXTE"),
      $this->getAttribute($xml, "TRADUCTION_CONTEXTE"),
    );
    
    # personnes
		$concepteurs = array();
		foreach ($xml->xpath('PERSONNE[@CONCEPTEUR="oui"]') as $xml_concepteur)
			$concepteurs[] = $this->getPersonne($xml_concepteur);
		$row->concepteur = $concepteurs;

		$executants = array();
		foreach ($xml->xpath('PERSONNE[@EXECUTANT="oui"]') as $xml_executant)
			$executants[] = $this->getPersonne($xml_executant);
		$row->executant = $executants;

		$organisateurs = array();
		foreach ($xml->xpath('PERSONNE[@ORGANISATEUR="oui"]') as $xml_organisateur)
			$organisateurs[] = $this->getPersonne($xml_organisateur);
		$row->organisateur = $organisateurs;
		
    #address
    $row->adresse = $this->getAttribute($xml, 'ADRESSE');
    $row->precision = $this->getAttribute($xml, 'PRECISION');
    $row->ville = $this->getAttribute($xml, 'VILLE');
    $pays = $this->getAttribute($xml, 'PAYS');
    if($pays == 'France' || $pays == 'france' || $pays = ''){
      $pays = 'FR';
    }else if($pays == 'Allemagne' || $pays == 'RFA'){
      $pays = 'DE';
    }else if($pays == 'Monaco' || $pays == "Principauté de Monaco"){
      $pays = 'MC';
    }else if($pays == 'Canada'){
      $pays = 'CA';
    }else if($pays == 'Italie'){
      $pays = 'IT';
    }else if($pays == 'USA'){
      $pays = 'US';
    }else{
      // dsm($pays, '$pays');
      $pays = '';
    }
    
    $row->pays = $pays;

    
	}
	
	public function prepare($node, stdClass $row) {
	  $node->name = 'migration';
	  $node->workflow = 2;
		$node->language = 'fr';
  }
  

	
	
}