<?php
/**
 * PerfGroupeNodeMigration
 * 
 */
abstract class PerfDocsNodeMigration extends PerfApiMigration {
  public function __construct() {
		
    parent::__construct();

		$this->fields += array(
			'ID' => t('id'),
			'title' => t('title'),
			'technique_description' => t('technique_description'),
			'dimensions' => t('dimensions'),
			'proprietaire' => t('proprietaire'),
			'images' => t('images'),
      'images_title' => t('images title'),
      'images_alt' => t('images alt'),
			'serie' => t('serie'),
      '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 . $this->itemsfile;


    $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, $this->fields);
    
    $this->destination = new MigrateDestinationNode('document');
  	
  	$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');
    
    $this->addFieldMapping('field_technique_description', 'technique_description')
        ->xpath('@TECHNIQUE_DESCRIPTION_REFERENCE');

    $this->addFieldMapping('field_dimensions', 'dimensions')
        ->xpath('@DIMENSIONS_OBJET');

    $this->addFieldMapping('field_proprietaire', 'proprietaire')
        ->xpath('@ID_PROPRIETAIRE_OBJET');
  
		#images
		$this->addFieldMapping('field_images', 'images');
		$this->addFieldMapping('field_images:source_dir')->defaultValue('public://SRC_IMAGES');
		$this->addFieldMapping('field_images:title', 'images_title');
    $this->addFieldMapping('field_images:alt', 'images_alt');


  	# serie
		$this->addFieldMapping('field_serie', 'serie')
				 ->xpath('@SERIE');
		$this->addFieldMapping('field_serie:create_term')->defaultValue(TRUE);
	
  
    $this->addFieldMapping('field_notes', 'notes')
        ->xpath('@NOTES');

    $this->addUnmigratedDestinations(array('revision_uid', 'created', 'changed', 'revision', 'log', 'tnid','comment', 'uid','path', 'pathauto',
      'field_technique_description:language', 'field_technique_description:format',
      'field_dimensions:language', 'field_dimensions:format',
	    'field_proprietaire:language', 'field_proprietaire:format',
      'field_images:file_class', 'field_images:language', 'field_images:destination_dir', 'field_images:destination_file', 'field_images:file_replace', 'field_images:preserve_files',
      // 'field_images:title', 'field_images:alt', 
      'field_serie:source_type',
      'field_notes:language', 'field_notes:format',
      'field_performances',
		));
  }
  
  public function prepareRow($row){
		// dsm($row , '--- $row ---');
		$xml = $row->xml;
		
		$title = $this->getAttribute($xml, 'TITRE');
    if(strlen($title) > 255)
      $title = substr($title, 0, 250) . '...';
    $row->title = $title;
    
		
		#images
		$images = array();
    $titles = array();
    $alts = array();
		$i = 0;
		foreach ($xml->xpath('IMAGE_CONSULTABLE') as $xml_image) {
			$images[] = $this->getAttribute($xml_image, 'SRC');
      $alts[] = $this->getAttribute($xml_image, 'ALT');
      $titles[] = $this->getAttribute($xml_image, 'TITLE');
			$i++;
		}
		$row->images = $images;
    $row->images_title = $titles;
    $row->images_alt = $alts;
		// dsm($row->images, '$row->images');
		
	}

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