<?php
/**
 * MaterioIndustrialUserMigration
 * 
 */
class MaterioIndustrialSampleUserMigration extends MaterioBasicMigration {
  public function __construct() {
    parent::__construct();
    $this->description = t('Migrate Materio Industrials Email Sample Users');

		// provide better description for source fields
		// and add new field source not from sql
    $source_fields = array(
      'id_industrial' => t('Industrial id from source'),
			'memo' => t('memo'),
    );

	  $query = db_select(MIG_MAT_SRC_DB .'.industrial_id', 'iid');
		$query->join(MIG_MAT_SRC_DB .'.industrial_info', 'iinf', 'iid.id_industrial = iinf.id_industrial');
		
		$query
			->fields('iid', array('id_industrial', 'date_creation', 'date_modif'))
			->fields('iinf', array('company', 'email_sample'))
			->condition('email_sample', '', '<>')
			->orderBy('iid.id_industrial', 'DESC')
			->groupBy('iid.id_industrial');
		
	  $this->source = new MigrateSourceSQL($query, $source_fields);
    $this->destination = new MigrateDestinationUser();
	  
    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'id_industrial' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'OLD Unique industrial ID',
          'alias' => 'iid',
        )
      ),
      MigrateDestinationUser::getKeySchema()
    );

	  // Make the mappings
	  $this->addFieldMapping('name')->defaultValue('aname');
	  $this->addFieldMapping('pass')->defaultValue(NULL);
	  $this->addFieldMapping('mail', 'email_sample');
		# NOTE : maybe if we provide only init mail, we wiil be able to ask email confirmation for users
	  $this->addFieldMapping('init', 'email_sample');
	  $this->addFieldMapping('language')->defaultValue('');
	  $this->addFieldMapping('theme')->defaultValue('');
	  $this->addFieldMapping('signature')->defaultValue('');
	  $this->addFieldMapping('signature_format')->defaultValue('filtered_html');
	 	$this->addFieldMapping('created', 'date_creation');
	  $this->addFieldMapping('access', 'date_modif');
	  $this->addFieldMapping('login')->defaultValue(0);
	  $this->addFieldMapping('status')->defaultValue(0);
	  $this->addFieldMapping('picture')->defaultValue(0);
	  $this->addFieldMapping('timezone')->defaultValue(NULL);
	  // $this->addFieldMapping('path')->issueGroup(t('DNM'));
	  $this->addFieldMapping('pathauto')->defaultValue(1);
		$this->addFieldMapping('roles')->defaultValue(array(5));
		$this->addFieldMapping('is_new')->defaultValue(TRUE);
	
		$this->addFieldMapping('field_memo', 'memo');
	
		$this->addFieldMapping('field_company', 'company');
	
	}
	
	public function prepareRow($cr) {
		if($cr->email_sample == '')
			return false;
		
		$cr->company = trim($cr->company);
		if(count(taxonomy_get_term_by_name($cr->company.'-'.$cr->id_industrial)))
			$cr->company .= '-'.$cr->id_industrial;
		
		$cr->memo = "#sample";
		
    return TRUE;
    // return FALSE if you wish to skip a particular row
  }
	
}

