migrate_materio.materiau.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * MaterioMateriauNodeMigration
  4. *
  5. */
  6. class MaterioMateriauNodeMigration extends MaterioBasicMigration {
  7. public function __construct() {
  8. parent::__construct();
  9. $this->description = t('Migrate Materio Materiau nodes');
  10. // provide better description for source fields
  11. // and add new field source not from sql
  12. $source_fields = array(
  13. 'id_product' => t('Materiau id from source'),
  14. 'company_fab'=>t('company_fab'),
  15. 'company_distrib'=>t('company_distrib'),
  16. // 'name'=> t('en-fr name implemented on prepare'),
  17. // 'description'=> t('en-fr description implemented on prepare'),
  18. // 'nature'=> t('en-fr nature implemented on prepare'),
  19. // 'language'=> t('en-fr langauge description, used in prepare'),
  20. );
  21. $query = db_select(MIG_MAT_SRC_DB .'.product_id', 'pid');
  22. $query->join(MIG_MAT_SRC_DB .'.product_industrial', 'pind', 'pind.id_product = pid.id_product');
  23. // $industrial_query = db_select(MIG_MAT_SRC_DB .'.industrial_info', 'iif_select')
  24. // ->fields('iif_select', array('id_industrial', 'company'))->distinct();
  25. // $query->join($industrial_query, 'iif', 'iif.id_industrial = pind.id_industrial');
  26. // WARNING ! product_info is double langauge + duplicate -> 4 rows each product
  27. // $pif_fr_query = db_select(MIG_MAT_SRC_DB .'.product_info', 'pif_fr_select')
  28. // //->fields('pif_fr_select')// , array('id_product') //, 'name', 'nature', 'description', 'usage', 'technical', 'keywords',
  29. // ->condition('pif_fr_select.id_locale', 2)->distinct();
  30. // $pif_fr_query->addField('pif_fr_select', 'id_product', 'id_product');
  31. // $query->join($pif_fr_query, 'pif_fr', 'pif_fr.id_product = pid.id_product');
  32. // $pif_en_query = db_select(MIG_MAT_SRC_DB .'.product_info', 'pif_en_select')
  33. // ->fields('pif_en_select')//, array('id_product') //, 'name', 'nature', 'description', 'usage', 'technical', 'keywords',
  34. // ->condition('pif_en_select.id_locale', 1)->distinct();
  35. // // $pif_en_query->addField('pif_en_select', 'name', 'name_en');
  36. // $query->join($pif_en_query, 'pif_en', 'pif_en.id_product = pid.id_product');
  37. $query
  38. ->fields('pid', array('id_product', 'id_materio', 'status', 'memo', 'source', 'date_creation', 'date_modif'))
  39. ->fields('pind', array('id_industrial', 'quarity'));
  40. // ->fields('iff', array('company'))
  41. $query->addExpression('GROUP_CONCAT(DISTINCT pind.id_industrial)','list_industrial');
  42. $query->addExpression('GROUP_CONCAT(pind.quarity)','list_quarity');
  43. $query
  44. ->orderBy('pid.id_product', 'DESC')
  45. ->groupBy('pid.id_product');
  46. //'name', 'nature', 'description', 'usage', 'technical', 'keywords'
  47. // $query->addField('pif_fr', 'name', 'name_fr');
  48. // $query->addField('pif_fr', 'nature', 'nature_fr');
  49. // $query->addField('pif_fr', 'description', 'description_fr');
  50. // $query->addField('pif_fr', 'usage', 'usage_fr');
  51. // $query->addField('pif_fr', 'technical', 'technical_fr');
  52. // $query->addField('pif_fr', 'keywords', 'keywords_fr');
  53. //
  54. // $query->addField('pif_en', 'name', 'name_en');
  55. // $query->addField('pif_en', 'nature', 'nature_en');
  56. // $query->addField('pif_en', 'description', 'description_en');
  57. // $query->addField('pif_en', 'usage', 'usage_en');
  58. // $query->addField('pif_en', 'technical', 'technical_en');
  59. // $query->addField('pif_en', 'keywords', 'keywords_en');
  60. $this->source = new MigrateSourceSQL($query, $source_fields);
  61. $this->destination = new MigrateDestinationNode('materiau');
  62. $this->map = new MigrateSQLMap($this->machineName,
  63. array(
  64. 'id_product' => array(
  65. 'type' => 'int',
  66. 'unsigned' => TRUE,
  67. 'not null' => TRUE,
  68. 'description' => 'OLD Unique Materiau ID',
  69. 'alias' => 'pid',
  70. )
  71. ),
  72. MigrateDestinationNode::getKeySchema()
  73. );
  74. $this->addFieldMapping('is_new')->defaultValue(TRUE);
  75. $this->addFieldMapping('created', 'date_creation');
  76. $this->addFieldMapping('changed', 'date_modif');
  77. $this->addFieldMapping('status')->defaultValue(1);
  78. $this->addFieldMapping('promote')->defaultValue(0);
  79. $this->addFieldMapping('sticky')->defaultValue(0);
  80. // $this->addFieldMapping('title', 'name_fr');
  81. $this->addFieldMapping('field_memo', 'memo');
  82. // $this->addFieldMapping('title_field', 'name');
  83. // $this->addFieldMapping('title_field:language', 'language');
  84. // $this->addFieldMapping(NULL, 'name_en');
  85. // $this->addFieldMapping(NULL, 'name_fr');
  86. // see on prepareRow()
  87. // $this->addFieldMapping('field_description', 'description');
  88. // $this->addFieldMapping('field_description:language', 'language');
  89. // $this->addFieldMapping(NULL, 'description_en');
  90. // $this->addFieldMapping(NULL, 'description_fr');
  91. // $this->addFieldMapping('field_nature_titre', 'nature');
  92. // $this->addFieldMapping('field_nature_titre:language', 'language');
  93. // $this->addFieldMapping(NULL, 'nature_en');
  94. // $this->addFieldMapping(NULL, 'nature_fr');
  95. $this->addFieldMapping('field_company_fab', 'company_fab');
  96. $this->addFieldMapping('field_company_fab:create_term')->defaultValue(false);
  97. $this->addFieldMapping('field_company_distrib', 'company_distrib');
  98. $this->addFieldMapping('pathauto')->defaultValue(1);
  99. $this->addFieldMapping('comment')->defaultValue(0);
  100. $this->addFieldMapping('revision')->defaultValue(0);
  101. $this->addFieldMapping(null, 'id_industrial');
  102. $this->addFieldMapping(null, 'quarity');
  103. $this->addFieldMapping('field_onthologie')->defaultValue(null);
  104. }
  105. public function prepareRow($cr){
  106. // dsm($cr);
  107. // $result = db_select(MIG_MAT_SRC_DB .'.product_info', 'pif_fr_select')
  108. // ->fields('pif_fr_select' , array('name', 'nature', 'description'))// , 'usage', 'technical', 'keywords',
  109. // ->condition('pif_fr_select.id_locale', 2)
  110. // ->condition('pif_fr_select.id_product', $cr->id_product)
  111. // ->distinct();//->exectute();
  112. // $prod_infos_fr = $result->fetchAll();
  113. // dsm($result->__toString(), '$query to string');
  114. //
  115. // $cr->name = array($cr->name_en, $cr->name_fr);
  116. // $cr->description = array($cr->description_en, $cr->description_fr);
  117. // $cr->nature = array($cr->nature_en, $cr->nature_fr);
  118. // $cr->language = array('en', 'fr');
  119. # parse companies
  120. dsm($cr->list_industrial, '-- $cr->list_industrial');
  121. dsm($cr->list_quarity, '$cr->list_quarity');
  122. $industrials = explode(',', $cr->list_industrial);
  123. $quarities = explode(',', $cr->list_quarity);
  124. $companies_fab = array();
  125. $companies_distrib = array();
  126. $l = count($industrials);
  127. for ($i=0; $i < $l; $i++) {
  128. $result = db_select(MIG_MAT_SRC_DB .'.industrial_info', 'iif')
  129. ->fields('iif', array('company'))
  130. ->condition('iif.id_industrial', $industrials[$i])
  131. ->distinct()->execute();
  132. foreach ($result as $record) {
  133. dsm($record);
  134. $company = trim($record->company);
  135. if($quarities[$i] == 'M'){
  136. $companies_fab[] = $company;
  137. }else{
  138. $companies_distrib[] = $company;
  139. }
  140. }
  141. }
  142. $cr->company_fab = implode(',', $companies_fab);
  143. $cr->company_distrib = implode(',', $companies_distrib);
  144. //
  145. // preg_match('/^(\w)([^-]+)-(\d+)$/', $cr->id_materio, $matches);
  146. // dsm($matches);
  147. //
  148. return TRUE;
  149. }
  150. public function prepare($node, stdClass $row) {
  151. // dsm('-- prepare --');
  152. // dsm($node, '$node');
  153. // dsm($row, '$row');
  154. $node->workflow = 4;
  155. // $node->field_public_email = array('und'=>array($row->email_contact));
  156. }
  157. }