<?php

abstract class PerfBasicUpdateMigration extends PerfApiMigration {

	public function __construct(){
		
		$this->description = t('Migrate Update Performance Basic Class');
		
    parent::__construct();

		$this->systemOfRecord = Migration::DESTINATION;

    // There isn't a consistent way to automatically identify appropriate "fields"
    // from an XML feed, so we pass an explicit list of source fields
		$fields = array(
			'treeline_id' => t('Treeline id'),
			'language'=>t('language'),
			// 'title' => t('title'),
			'site_internet' => t('Site internet'),
			// 'images' => t('Images'),
      'images_alt' => t('Images Alt'),
      'images_title' => t('Images Title'),
			'sources_bibliographiques' => t('Sources bibliographiques'), // BIBLIOGRAPHIE
    );
	
		// 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(
	      'treeline_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-parsed.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_ID_xpath = '@ID';         // relative to item_xpath and gets assembled
                                    // into full path /producers/producer/sourceid
    

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

    $this->destination = new MigrateDestinationNode('performance');
    
		#site internet
		$this->addFieldMapping('field_site_internet', 'site_internet');
		
		#images
		$this->addFieldMapping('field_images', 'images');
		$this->addFieldMapping('field_images:source_dir')->defaultValue('public://SRC_IMAGES');
		$this->addFieldMapping('field_images:file_replace')->defaultValue(FILE_EXISTS_REPLACE);
    $this->addFieldMapping('field_images:title', 'images_title');
    $this->addFieldMapping('field_images:alt', 'images_alt');

		$this->addFieldMapping('field_sources_bibliographiques', 'sources_bibliographiques')
		    ->xpath('BIBLIOGRAPHIE/@BIBLIOGRAPHIE');

		$this->addUnmigratedDestinations(array());
    
	}
	
	public function prepareRow($row){
		// dsm($row , '--- $row ---');
		$xml = $row->xml;

		#site internet
    $row->site_internet = array();
		foreach ($xml->xpath('SITE_INTERNET') as $xml_site){
			$row->site_internet[] = trim($this->getAttribute($xml_site, 'URL'));
		}

		#images
		$images = array();
    $titles = array();
    $alts = array();
    //drush_log(dt('start images import : !images', array('!images'=>count($xml->IMAGE_CONSULTABLE))), 'status');
    foreach ($xml->IMAGE_CONSULTABLE as $xml_image) { //IMAGE_CONSULTABLE
			$images[] = $this->getAttribute($xml_image, 'SRC');
      $alts[] = $this->getAttribute($xml_image, 'ALT');
      $titles[] = $this->getAttribute($xml_image, 'TITLE');
		}
		$row->images = $images;
    $row->images_title = $titles;
    $row->images_alt = $alts;

	}
	
	// public function prepare($node, stdClass $row) {
	// 	// $xml = $row->xml;

 //  }

  public function complete($node, stdClass $row){
  	$xml = $row->xml;
		
		# images
		# this script only update title and alt field of image files that already exists
		# but i sa that some files are missing after the first import …
  //   $delta = 0;
  //   foreach ($xml->IMAGE_CONSULTABLE as $xml_image) { //IMAGE_CONSULTABLE
  //   	if(isset($node->field_images['und'][$delta])){
	 //    	$node->field_images['und'][$delta]['title'] = $this->getAttribute($xml_image, 'TITLE');
	 //      $node->field_images['und'][$delta]['alt'] = $this->getAttribute($xml_image, 'ALT');
  //   	}
  //     $delta ++;
		// }


		# personnes
		$this->recordGroup($node, 'field_concepteur', $xml, 'CONCEPTEUR');

		$this->recordGroup($node, 'field_executant',$xml, 'EXECUTANT');

		$this->recordGroup($node, 'field_organisateur', $xml, 'ORGANISATEUR');

		$this->recordGroup($node, 'field_temoin', $xml, 'TEMOIN');

    node_save($node);

    # personnes
		$this->updatePersonneTerms($xml);

  }
	
}
