materio-base-legacy/migrate_materio.company.inc
bachy 93b7db54fe debug
- company : dispatch address filed in premise and sub_premise if more than 255 char
- company : avoid and record on memo tel1 if number is more than 15 char
- materiau : move product_info query in prepareRow()

Signed-off-by: bachy <git@g-u-i.net>
2012-05-16 08:05:22 +02:00

229 lines
7.4 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'=>t('premise'),
'sub_premise'=>t('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('');
$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(
// 'name_line' => array('source_field' => 'name'),
'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'),
// 'extension' => array('source_field' => 'ccode1_contact'),
);
$this->addFieldMapping('field_public_phone', 'ccode1')
->arguments($arguments);
$this->addFieldMapping(NULL, 'tel1');
// $this->addFieldMapping(NULL, 'ccode1_contact');
$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 : '';
}
$cr->city = preg_replace('/\\n/', ' ', $cr->city);
$cr->department = array($cr->department_en, $cr->department_fr);
// $cr->contact_quality = array($cr->contact_quality_en, $cr->contact_quality_fr);
// $cr->admin_quality = array($cr->admin_quality_en, $cr->admin_quality_fr);
$cr->language = array('en', 'fr');
$cr->web_title = str_replace('http://', '', $cr->web);
$cr->web = 'http://' . $cr->web_title;
$cr->company = trim($cr->company);
// if($cr->email_general != "")
// $cr->email_sample .= ','.$cr->email_general;
$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));
}
}