migrate_materio.company.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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'=>'',
  17. '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('en');
  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. 'thoroughfare' => array('source_field' => 'address'),
  68. 'premise' => array('source_field' => 'premise'),
  69. 'sub_premise' => array('source_field' => 'sub_premise'),
  70. 'locality' => array('source_field' => 'city'),
  71. 'postal_code' => array('source_field' => 'zip'),
  72. );
  73. $this->addFieldMapping('field_public_address', 'country_en')
  74. ->arguments($arguments);
  75. $this->addFieldMapping(NULL, 'address');
  76. $this->addFieldMapping(NULL, 'premise');
  77. $this->addFieldMapping(NULL, 'sub_premise');
  78. $this->addFieldMapping(NULL, 'city');
  79. $this->addFieldMapping(NULL, 'zip');
  80. $this->addFieldMapping(NULL, 'country_fr');
  81. // $arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'web_title'));
  82. $this->addFieldMapping('field_website', 'web');
  83. // ->arguments($arguments);
  84. $this->addFieldMapping('body')->defaultValue('');
  85. $this->addFieldMapping('field_infos_from_company')->defaultValue('');
  86. $arguments = array(
  87. 'number' => array('source_field' => 'tel1'),
  88. );
  89. $this->addFieldMapping('field_public_phone', 'ccode1')
  90. ->arguments($arguments);
  91. $this->addFieldMapping(NULL, 'tel1');
  92. $this->addFieldMapping(NULL, 'tel2');
  93. $this->addFieldMapping(NULL, 'ccode2');
  94. $this->addFieldMapping(NULL, 'tel3');
  95. $this->addFieldMapping(NULL, 'ccode3');
  96. /*
  97. TODO
  98. multiple field phone number ?? tel2 ccode2 tel3 ccode3
  99. for now added to memo
  100. */
  101. $this->addFieldMapping('field_note')->defaultValue(2);
  102. $this->addFieldMapping('field_memo', 'memo');
  103. $this->addFieldMapping('pathauto')->defaultValue(1);
  104. $this->addFieldMapping('comment')->defaultValue(0);
  105. $this->addFieldMapping('revision')->defaultValue(0);
  106. }
  107. public function prepareRow($cr) {
  108. $cr->address = preg_replace('/\\n/', ', ', $cr->address);
  109. if(strlen($cr->address) > 255){
  110. $adress = $cr->address;
  111. $cr->address = substr($adress, 0, 250);
  112. $cr->premise = substr($adress, 250, 500);
  113. $sub = substr($adress, 500, 750);
  114. $cr->sub_premise = $sub ? $sub : '';
  115. }else{
  116. $cr->premise = '';
  117. $cr->sub_premise = '';
  118. }
  119. $cr->city = preg_replace('/\\n/', ' ', $cr->city);
  120. $cr->department = array($cr->department_en, $cr->department_fr);
  121. $cr->language = array('en', 'fr');
  122. $cr->web = str_replace('http://', '', $cr->web);
  123. /*
  124. TODO multiple company with same name
  125. how to force new term creation ?
  126. then how to recognize the right comp. with materieaux ?
  127. */
  128. $cr->company = trim($cr->company);
  129. $same_comp = taxonomy_get_term_by_name($cr->company);
  130. if(count($same_comp)){
  131. $cr->company .= '-'.$cr->id_industrial;
  132. $cr->memo .= "\n".'#multiple-term';
  133. }
  134. $ccs = cck_phone_countrycodes();
  135. $match = 0;
  136. $ccode1 = '';
  137. foreach ($ccs as $cc => $cc_values) {
  138. if('+'.$cr->ccode1 == $cc_values['code']){
  139. $cr->ccode1 = $cc;
  140. $ccode1 = $ccs[$cc];
  141. $match++;
  142. }
  143. if('+'.$cr->ccode2 == $cc_values['code']){
  144. $cr->ccode2 = $ccs[$cc];
  145. $match++;
  146. }
  147. if('+'.$cr->ccode3 == $cc_values['code']){
  148. $cr->ccode3 = $ccs[$cc];
  149. $match++;
  150. }
  151. if($match == 3)
  152. break;
  153. }
  154. $cr->ccode1 = strlen($cr->ccode1) == 2 ? $cr->ccode1 : false;
  155. if(($cr->tel1 != '' && !$cr->ccode1) || strlen($cr->tel1) > 15){
  156. $cr->tel1 = '';
  157. $cc = is_array($ccode1) ? $ccode1['country'] . ' (' . $ccode1['code'] . ')' : '';
  158. $cr->memo .= "\n".'tel1 : '. $cc .' '. $cr->tel1;
  159. }
  160. // do this because i don't know how to insert multiple phone fields
  161. if($cr->tel2 != ''){
  162. $cc = is_array($cr->ccode2) ? $cr->ccode2['country'] . ' (' . $cr->ccode2['code'] . ')' : '';
  163. $cr->memo .= "\n".'tel2 : '. $cc .' '. $cr->tel2;
  164. }
  165. if($cr->tel3 != ''){
  166. $cc = is_array($cr->ccode3) ? $cr->ccode3['country'] . ' (' . $cr->ccode3['code'] . ')' : '';
  167. $cr->memo .= "\n".'tel3 : '. $cc .' '. $cr->tel3;
  168. }
  169. // dsm($cr, '- - - - $cr - - - -');
  170. return TRUE;
  171. // return FALSE if you wish to skip a particular row
  172. }
  173. public function prepare($node, stdClass $row) {
  174. // dsm('-- prepare --');
  175. // dsm($node, '$node');
  176. // dsm($row, '$row');
  177. $node->workflow = 4;
  178. // $node->field_public_email = array('und'=>array($row->email_contact));
  179. }
  180. }