<?php
/**
 * MaterioIndustrialUserMigration
 * 
 */
class MaterioIndustrialAccountUserMigration extends MaterioBasicMigration {
  public function __construct() {
    parent::__construct();
    $this->description = t('Migrate Materio Industrials Account 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_account', 'ind_ac', 'iid.id_industrial = ind_ac.id_industrial');
		$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('ind_ac', array('login', 'password'))
			->fields('iinf', array('company', 'email_general'))
			->condition('email_general', '', '<>')
			->orderBy('iid.id_industrial', 'DESC');
			
		$query->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', 'login');
	  $this->addFieldMapping('pass', 'password');//->defaultValue(NULL);
	  $this->addFieldMapping('mail', 'email_general');
		# NOTE : maybe if we provide only init mail, we wiil be able to ask email confirmation for users
	  $this->addFieldMapping('init', 'email_general');
	  $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_general == '')
				return false;
		
			$cr->company = trim($cr->company);
		
			$cr->memo = $cr->login .' | '. $cr->password;
		
	    return TRUE;
	    // return FALSE if you wish to skip a particular row
	  }
	
}

