migrate_materio.company.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * MaterioIndustrialUserMigration
  4. *
  5. */
  6. class MaterioCompanyMigration extends MaterioBasicMigration {
  7. public function __construct() {
  8. parent::__construct();
  9. $this->description = t('Migrate Materio Company Node');
  10. // provide better description for source fields
  11. // and add new field source not from sql
  12. $source_fields = array(
  13. 'id_industrial' => t('Industrial id from source'),
  14. 'department'=> t('en-fr department implemented on prepare'),
  15. 'language'=> t('en-fr langauge description, used in prepare'),
  16. 'premise'=>t('premise'),
  17. 'sub_premise'=>t('sub_premise'),
  18. );
  19. $query = db_select(MIG_MAT_SRC_DB .'.industrial_id', 'iid');
  20. $query->join(MIG_MAT_SRC_DB .'.industrial_info', 'iinf', 'iid.id_industrial = iinf.id_industrial');
  21. $ilfr_select = db_select(MIG_MAT_SRC_DB .'.industrial_lang', 'ilfr_select')
  22. ->fields('ilfr_select')->condition('ilfr_select.id_locale',1)->orderBy('ilfr_select.id_industrial', 'DESC')->distinct();
  23. $query->join($ilfr_select, 'ilfr', 'iid.id_industrial = ilfr.id_industrial');
  24. $ilen_select = db_select(MIG_MAT_SRC_DB .'.industrial_lang', 'ilen_select')
  25. ->fields('ilen_select')->condition('ilen_select.id_locale',2)->orderBy('ilen_select.id_industrial', 'ASC')->distinct();
  26. $query->join($ilen_select, 'ilen', 'iid.id_industrial = ilen.id_industrial');
  27. $query
  28. ->fields('iid', array('id_industrial', 'date_creation', 'date_modif', 'memo'))
  29. ->fields('iinf', array('company', 'address', 'city', 'zip', 'web', 'ccode1', 'tel1', 'ccode2', 'tel2', 'ccode3', 'tel3'))
  30. ->orderBy('id_industrial', 'DESC');
  31. $query->addField('ilfr', 'department', 'department_fr');
  32. $query->addField('ilfr', 'country', 'country_fr');
  33. $query->addField('ilen', 'department', 'department_en');
  34. $query->addField('ilen', 'country', 'country_en');
  35. $query->groupBy('id_industrial')
  36. ->orderBy('id_industrial', 'DESC');
  37. $this->source = new MigrateSourceSQL($query, $source_fields);
  38. $this->destination = new MigrateDestinationNode('company');
  39. $this->map = new MigrateSQLMap($this->machineName,
  40. array(
  41. 'id_industrial' => array(
  42. 'type' => 'int',
  43. 'unsigned' => TRUE,
  44. 'not null' => TRUE,
  45. 'description' => 'OLD Unique industrial ID',
  46. 'alias' => 'iid',
  47. )
  48. ),
  49. MigrateDestinationNode::getKeySchema()
  50. );
  51. // Make the mappings
  52. $this->addFieldMapping('language')->defaultValue('');
  53. $this->addFieldMapping('is_new')->defaultValue(TRUE);
  54. $this->addFieldMapping('created', 'date_creation');
  55. $this->addFieldMapping('changed', 'date_modif');
  56. $this->addFieldMapping('status')->defaultValue(1);
  57. $this->addFieldMapping('promote')->defaultValue(0);
  58. $this->addFieldMapping('sticky')->defaultValue(0);
  59. $this->addFieldMapping('field_tode_company', 'company')
  60. ->arguments(array('create_term' => TRUE));
  61. // see on prepareRow()
  62. $this->addFieldMapping('field_department', 'department');
  63. $this->addFieldMapping('field_department:language', 'language');
  64. $this->addFieldMapping(NULL, 'department_en');
  65. $this->addFieldMapping(NULL, 'department_fr');
  66. $arguments = array(
  67. // 'name_line' => array('source_field' => 'name'),
  68. 'thoroughfare' => array('source_field' => 'address'),
  69. 'premise' => array('source_field' => 'premise'),
  70. 'sub_premise' => array('source_field' => 'sub_premise'),
  71. 'locality' => array('source_field' => 'city'),
  72. 'postal_code' => array('source_field' => 'zip'),
  73. );
  74. $this->addFieldMapping('field_public_address', 'country_en')
  75. ->arguments($arguments);
  76. $this->addFieldMapping(NULL, 'address');
  77. $this->addFieldMapping(NULL, 'premise');
  78. $this->addFieldMapping(NULL, 'sub_premise');
  79. $this->addFieldMapping(NULL, 'city');
  80. $this->addFieldMapping(NULL, 'zip');
  81. $this->addFieldMapping(NULL, 'country_fr');
  82. $arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'web_title'));
  83. $this->addFieldMapping('field_website', 'web')
  84. ->arguments($arguments);
  85. $this->addFieldMapping('body')->defaultValue('');
  86. $this->addFieldMapping('field_infos_from_company')->defaultValue('');
  87. $arguments = array(
  88. 'number' => array('source_field' => 'tel1'),
  89. // 'extension' => array('source_field' => 'ccode1_contact'),
  90. );
  91. $this->addFieldMapping('field_public_phone', 'ccode1')
  92. ->arguments($arguments);
  93. $this->addFieldMapping(NULL, 'tel1');
  94. // $this->addFieldMapping(NULL, 'ccode1_contact');
  95. $this->addFieldMapping(NULL, 'tel2');
  96. $this->addFieldMapping(NULL, 'ccode2');
  97. $this->addFieldMapping(NULL, 'tel3');
  98. $this->addFieldMapping(NULL, 'ccode3');
  99. /*
  100. TODO
  101. multiple field phone number ?? tel2 ccode2 tel3 ccode3
  102. for now added to memo
  103. */
  104. $this->addFieldMapping('field_note')->defaultValue(2);
  105. $this->addFieldMapping('field_memo', 'memo');
  106. $this->addFieldMapping('pathauto')->defaultValue(1);
  107. $this->addFieldMapping('comment')->defaultValue(0);
  108. $this->addFieldMapping('revision')->defaultValue(0);
  109. }
  110. public function prepareRow($cr) {
  111. $cr->address = preg_replace('/\\n/', ', ', $cr->address);
  112. if(strlen($cr->address) > 255){
  113. $adress = $cr->address;
  114. $cr->address = substr($adress, 0, 250);
  115. $cr->premise = substr($adress, 250, 500);
  116. $sub = substr($adress, 500, 750);
  117. $cr->sub_premise = $sub ? $sub : '';
  118. }
  119. $cr->city = preg_replace('/\\n/', ' ', $cr->city);
  120. $cr->department = array($cr->department_en, $cr->department_fr);
  121. // $cr->contact_quality = array($cr->contact_quality_en, $cr->contact_quality_fr);
  122. // $cr->admin_quality = array($cr->admin_quality_en, $cr->admin_quality_fr);
  123. $cr->language = array('en', 'fr');
  124. $cr->web_title = str_replace('http://', '', $cr->web);
  125. $cr->web = 'http://' . $cr->web_title;
  126. $cr->company = trim($cr->company);
  127. // if($cr->email_general != "")
  128. // $cr->email_sample .= ','.$cr->email_general;
  129. $ccs = cck_phone_countrycodes();
  130. $match = 0;
  131. $ccode1 = '';
  132. foreach ($ccs as $cc => $cc_values) {
  133. if('+'.$cr->ccode1 == $cc_values['code']){
  134. $cr->ccode1 = $cc;
  135. $ccode1 = $ccs[$cc];
  136. $match++;
  137. }
  138. if('+'.$cr->ccode2 == $cc_values['code']){
  139. $cr->ccode2 = $ccs[$cc];
  140. $match++;
  141. }
  142. if('+'.$cr->ccode3 == $cc_values['code']){
  143. $cr->ccode3 = $ccs[$cc];
  144. $match++;
  145. }
  146. if($match == 3)
  147. break;
  148. }
  149. $cr->ccode1 = strlen($cr->ccode1) == 2 ? $cr->ccode1 : false;
  150. if(($cr->tel1 != '' && !$cr->ccode1) || strlen($cr->tel1) > 15){
  151. $cr->tel1 = '';
  152. $cc = is_array($ccode1) ? $ccode1['country'] . ' (' . $ccode1['code'] . ')' : '';
  153. $cr->memo .= "\n".'tel1 : '. $cc .' '. $cr->tel1;
  154. }
  155. // do this because i don't know how to insert multiple phone fields
  156. if($cr->tel2 != ''){
  157. $cc = is_array($cr->ccode2) ? $cr->ccode2['country'] . ' (' . $cr->ccode2['code'] . ')' : '';
  158. $cr->memo .= "\n".'tel2 : '. $cc .' '. $cr->tel2;
  159. }
  160. if($cr->tel3 != ''){
  161. $cc = is_array($cr->ccode3) ? $cr->ccode3['country'] . ' (' . $cr->ccode3['code'] . ')' : '';
  162. $cr->memo .= "\n".'tel3 : '. $cc .' '. $cr->tel3;
  163. }
  164. // dsm($cr, '- - - - $cr - - - -');
  165. return TRUE;
  166. // return FALSE if you wish to skip a particular row
  167. }
  168. public function prepare($node, stdClass $row) {
  169. // dsm('-- prepare --');
  170. // dsm($node, '$node');
  171. // dsm($row, '$row');
  172. $node->workflow = 4;
  173. // $node->field_public_email = array('und'=>array($row->email_contact));
  174. }
  175. }