materio-base-legacy/sites/all/modules/gui/migrate_materio/migrate_materio.company.inc
2015-04-19 16:15:14 +02:00

233 lines
7.3 KiB
PHP

<?php
/**
* MaterioIndustrialUserMigration
*
*/
class MaterioCompanyMigration extends MaterioBasicMigration {
public function __construct() {
parent::__construct();
$this->description = t('Migrate Materio Company Node');
// provide better description for source fields
// and add new field source not from sql
$source_fields = array(
'id_industrial' => t('Industrial id from source'),
'department'=> t('en-fr department implemented on prepare'),
'language'=> t('en-fr langauge description, used in prepare'),
'premise'=>'',
'sub_premise'=>'',
);
$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');
$ilfr_select = db_select(MIG_MAT_SRC_DB .'.industrial_lang', 'ilfr_select')
->fields('ilfr_select')->condition('ilfr_select.id_locale',1)->orderBy('ilfr_select.id_industrial', 'DESC')->distinct();
$query->join($ilfr_select, 'ilfr', 'iid.id_industrial = ilfr.id_industrial');
$ilen_select = db_select(MIG_MAT_SRC_DB .'.industrial_lang', 'ilen_select')
->fields('ilen_select')->condition('ilen_select.id_locale',2)->orderBy('ilen_select.id_industrial', 'ASC')->distinct();
$query->join($ilen_select, 'ilen', 'iid.id_industrial = ilen.id_industrial');
$query
->fields('iid', array('id_industrial', 'date_creation', 'date_modif', 'memo'))
->fields('iinf', array('company', 'address', 'city', 'zip', 'web', 'ccode1', 'tel1', 'ccode2', 'tel2', 'ccode3', 'tel3'))
->orderBy('id_industrial', 'DESC');
$query->addField('ilfr', 'department', 'department_fr');
$query->addField('ilfr', 'country', 'country_fr');
$query->addField('ilen', 'department', 'department_en');
$query->addField('ilen', 'country', 'country_en');
$query->groupBy('id_industrial')
->orderBy('id_industrial', 'DESC');
$this->source = new MigrateSourceSQL($query, $source_fields);
$this->destination = new MigrateDestinationNode('company');
$this->map = new MigrateSQLMap($this->machineName,
array(
'id_industrial' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'OLD Unique industrial ID',
'alias' => 'iid',
)
),
MigrateDestinationNode::getKeySchema()
);
// Make the mappings
$this->addFieldMapping('language')->defaultValue('en');
$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('field_tode_company', 'company')
->arguments(array('create_term' => TRUE));
// see on prepareRow()
$this->addFieldMapping('field_department', 'department');
$this->addFieldMapping('field_department:language', 'language');
$this->addFieldMapping(NULL, 'department_en');
$this->addFieldMapping(NULL, 'department_fr');
$arguments = array(
'thoroughfare' => array('source_field' => 'address'),
'premise' => array('source_field' => 'premise'),
'sub_premise' => array('source_field' => 'sub_premise'),
'locality' => array('source_field' => 'city'),
'postal_code' => array('source_field' => 'zip'),
);
$this->addFieldMapping('field_public_address', 'country_en')
->arguments($arguments);
$this->addFieldMapping(NULL, 'address');
$this->addFieldMapping(NULL, 'premise');
$this->addFieldMapping(NULL, 'sub_premise');
$this->addFieldMapping(NULL, 'city');
$this->addFieldMapping(NULL, 'zip');
$this->addFieldMapping(NULL, 'country_fr');
// $arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'web_title'));
$this->addFieldMapping('field_website', 'web');
// ->arguments($arguments);
$this->addFieldMapping('body')->defaultValue('');
$this->addFieldMapping('field_infos_from_company')->defaultValue('');
$arguments = array(
'number' => array('source_field' => 'tel1'),
);
$this->addFieldMapping('field_public_phone', 'ccode1')
->arguments($arguments);
$this->addFieldMapping(NULL, 'tel1');
$this->addFieldMapping(NULL, 'tel2');
$this->addFieldMapping(NULL, 'ccode2');
$this->addFieldMapping(NULL, 'tel3');
$this->addFieldMapping(NULL, 'ccode3');
/*
TODO
multiple field phone number ?? tel2 ccode2 tel3 ccode3
for now added to memo
*/
$this->addFieldMapping('field_note')->defaultValue(2);
$this->addFieldMapping('field_memo', 'memo');
$this->addFieldMapping('pathauto')->defaultValue(1);
$this->addFieldMapping('comment')->defaultValue(0);
$this->addFieldMapping('revision')->defaultValue(0);
}
public function prepareRow($cr) {
$cr->address = preg_replace('/\\n/', ', ', $cr->address);
if(strlen($cr->address) > 255){
$adress = $cr->address;
$cr->address = substr($adress, 0, 250);
$cr->premise = substr($adress, 250, 500);
$sub = substr($adress, 500, 750);
$cr->sub_premise = $sub ? $sub : '';
}else{
$cr->premise = '';
$cr->sub_premise = '';
}
$cr->city = preg_replace('/\\n/', ' ', $cr->city);
$cr->department = array($cr->department_en, $cr->department_fr);
$cr->language = array('en', 'fr');
$cr->web = str_replace('http://', '', $cr->web);
/*
TODO multiple company with same name
how to force new term creation ?
then how to recognize the right comp. with materieaux ?
*/
$cr->company = trim($cr->company);
$same_comp = taxonomy_get_term_by_name($cr->company);
if(count($same_comp)){
$cr->company .= '-'.$cr->id_industrial;
$cr->memo .= "\n".'#multiple-term';
}
$ccs = cck_phone_countrycodes();
$match = 0;
$ccode1 = '';
foreach ($ccs as $cc => $cc_values) {
if('+'.$cr->ccode1 == $cc_values['code']){
$cr->ccode1 = $cc;
$ccode1 = $ccs[$cc];
$match++;
}
if('+'.$cr->ccode2 == $cc_values['code']){
$cr->ccode2 = $ccs[$cc];
$match++;
}
if('+'.$cr->ccode3 == $cc_values['code']){
$cr->ccode3 = $ccs[$cc];
$match++;
}
if($match == 3)
break;
}
$cr->ccode1 = strlen($cr->ccode1) == 2 ? $cr->ccode1 : false;
if(($cr->tel1 != '' && !$cr->ccode1) || strlen($cr->tel1) > 15){
$cr->tel1 = '';
$cc = is_array($ccode1) ? $ccode1['country'] . ' (' . $ccode1['code'] . ')' : '';
$cr->memo .= "\n".'tel1 : '. $cc .' '. $cr->tel1;
}
// do this because i don't know how to insert multiple phone fields
if($cr->tel2 != ''){
$cc = is_array($cr->ccode2) ? $cr->ccode2['country'] . ' (' . $cr->ccode2['code'] . ')' : '';
$cr->memo .= "\n".'tel2 : '. $cc .' '. $cr->tel2;
}
if($cr->tel3 != ''){
$cc = is_array($cr->ccode3) ? $cr->ccode3['country'] . ' (' . $cr->ccode3['code'] . ')' : '';
$cr->memo .= "\n".'tel3 : '. $cc .' '. $cr->tel3;
}
// dsm($cr, '- - - - $cr - - - -');
return TRUE;
// return FALSE if you wish to skip a particular row
}
public function prepare($node, stdClass $row) {
// dsm('-- prepare --');
// dsm($node, '$node');
// dsm($row, '$row');
$node->workflow = 4;
// $node->field_public_email = array('und'=>array($row->email_contact));
}
}