migrate_materio.materiau.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 'title'=>t('title'),
  17. 'name'=> t('en-fr name implemented on prepare'),
  18. 'description'=> t('en-fr description implemented on prepare'),
  19. 'nature'=> t('en-fr nature implemented on prepare'),
  20. 'language'=> t('en-fr langauge description, used in prepare'),
  21. 'family' => '',
  22. 'localisation' => '',
  23. 'identifiant' => '',
  24. );
  25. $query = db_select(MIG_MAT_SRC_DB .'.product_id', 'pid');
  26. $query->join(MIG_MAT_SRC_DB .'.product_industrial', 'pind', 'pind.id_product = pid.id_product');
  27. $query
  28. ->fields('pid', array('id_product', 'id_materio', 'status', 'memo', 'source', 'date_creation', 'date_modif'))
  29. ->fields('pind', array('id_industrial', 'quarity'));
  30. $query->addExpression('GROUP_CONCAT(DISTINCT pind.id_industrial)','list_industrial');
  31. $query->addExpression('GROUP_CONCAT(pind.quarity)','list_quarity');
  32. $query
  33. ->orderBy('pid.id_product', 'DESC')
  34. ->groupBy('pid.id_product');
  35. $this->source = new MigrateSourceSQL($query, $source_fields);
  36. $this->destination = new MigrateDestinationNode('materiau');
  37. $this->map = new MigrateSQLMap($this->machineName,
  38. array(
  39. 'id_product' => array(
  40. 'type' => 'int',
  41. 'unsigned' => TRUE,
  42. 'not null' => TRUE,
  43. 'description' => 'OLD Unique Materiau ID',
  44. 'alias' => 'pid',
  45. )
  46. ),
  47. MigrateDestinationNode::getKeySchema()
  48. );
  49. $this->addFieldMapping('is_new')->defaultValue(TRUE);
  50. $this->addFieldMapping('created', 'date_creation');
  51. $this->addFieldMapping('changed', 'date_modif');
  52. $this->addFieldMapping('status')->defaultValue(1);
  53. $this->addFieldMapping('promote')->defaultValue(0);
  54. $this->addFieldMapping('sticky')->defaultValue(0);
  55. $this->addFieldMapping('language')->defaultValue('en');
  56. $this->addFieldMapping('title', 'title');
  57. $this->addFieldMapping('field_memo', 'memo');
  58. $this->addFieldMapping('title_field', 'name');
  59. $this->addFieldMapping('title_field:language', 'language');
  60. $this->addFieldMapping('field_description', 'description');
  61. $this->addFieldMapping('field_description:language', 'language');
  62. $this->addFieldMapping('field_description:format')->defaultValue('filtred_html');
  63. $this->addFieldMapping('field_nature_titre', 'nature');
  64. $this->addFieldMapping('field_nature_titre:language', 'language');
  65. $this->addFieldMapping('field_company_fab', 'company_fab');
  66. $this->addFieldMapping('field_company_fab:create_term')->defaultValue(false);
  67. $this->addFieldMapping('field_company_distrib', 'company_distrib');
  68. $this->addFieldMapping('field_company_distrib:create_term')->defaultValue(false);
  69. $this->addFieldMapping('pathauto')->defaultValue(1);
  70. $this->addFieldMapping('comment')->defaultValue(0);
  71. $this->addFieldMapping('revision')->defaultValue(0);
  72. $this->addFieldMapping(null, 'id_industrial');
  73. $this->addFieldMapping(null, 'quarity');
  74. $this->addFieldMapping('field_onthologie')->defaultValue(null);
  75. $this->addFieldMapping('field_famille', 'family');
  76. $this->addFieldMapping('field_localisation', 'localisation');
  77. $this->addFieldMapping('field_identifiant', 'identifiant');
  78. }
  79. public function prepareRow($cr){
  80. // dsm($cr);
  81. # get product infos FR
  82. $infos_fr = db_select(MIG_MAT_SRC_DB .'.product_info', 'pif_fr')
  83. ->condition('pif_fr.id_locale', 2)
  84. ->condition('pif_fr.id_product', $cr->id_product)
  85. ->fields('pif_fr' , array('name', 'nature', 'description', 'technical', 'keywords'))//, 'usage'
  86. ->distinct()->execute();
  87. foreach ($infos_fr as $record)
  88. $infos['fr'] = $record;
  89. if($infos['fr']->technical != '')
  90. $infos['fr']->description .= "<br />technical : ".$infos['fr']->technical; // . "<br />usage".$infos['fr']->usage
  91. # get product infos EN
  92. $infos_en = db_select(MIG_MAT_SRC_DB .'.product_info', 'pif_en')
  93. ->condition('pif_en.id_locale', 1)
  94. ->condition('pif_en.id_product', $cr->id_product)
  95. ->fields('pif_en' , array('name', 'nature', 'description', 'technical', 'keywords'))//, 'usage' , 'keywords'
  96. ->distinct()->execute();
  97. foreach ($infos_en as $record)
  98. $infos['en'] = $record;
  99. if($infos['en']->technical != '')
  100. $infos['en']->description .= "<br />technical : ".$infos['en']->technical; // . "<br />usage".$infos['en']->usage
  101. # set product infos
  102. $cr->title = $infos['fr']->name;
  103. $cr->name = array($infos['en']->name, $infos['fr']->name);
  104. $cr->description = array($infos['en']->description, $infos['fr']->description);
  105. $cr->nature = array($infos['en']->nature, $infos['fr']->nature);
  106. $cr->language = array('en', 'fr');
  107. # record keywords on memo
  108. $cr->memo .= ($cr->memo != '' ? "\n\n" : '') . $infos['fr']->keywords;
  109. # parse companies
  110. // dsm($cr->list_industrial, '-- $cr->list_industrial');
  111. // dsm($cr->list_quarity, '$cr->list_quarity');
  112. $industrials = explode(',', $cr->list_industrial);
  113. $quarities = explode(',', $cr->list_quarity);
  114. $companies_fab = array();
  115. $companies_distrib = array();
  116. $l = count($industrials);
  117. for ($i=0; $i < $l; $i++) {
  118. $result = db_select(MIG_MAT_SRC_DB .'.industrial_info', 'iif')
  119. ->fields('iif', array('id_industrial', 'company'))
  120. ->condition('iif.id_industrial', $industrials[$i])
  121. ->distinct()->execute();
  122. foreach ($result as $record) {
  123. // dsm($record);
  124. $company = trim($record->company);
  125. if(count(taxonomy_get_term_by_name($company.'-'.$record->id_industrial)))
  126. $company .= '-'.$record->id_industrial;
  127. if($quarities[$i] == 'M'){
  128. $companies_fab[] = $company;
  129. }else{
  130. $companies_distrib[] = $company;
  131. }
  132. }
  133. }
  134. $cr->company_fab = implode(',', $companies_fab);
  135. $cr->company_distrib = implode(',', $companies_distrib);
  136. preg_match('/^(\w)([^-]+)-(\d+)$/', $cr->id_materio, $matches);
  137. // dsm($matches);
  138. $cr->family = $matches[1];
  139. $cr->localisation = $matches[2];
  140. $cr->identifiant = $matches[3];
  141. return TRUE;
  142. }
  143. public function prepare($node, stdClass $row) {
  144. // dsm('-- prepare --');
  145. // dsm($node, '$node');
  146. // dsm($row, '$row');
  147. $node->workflow = 4;
  148. $node->field_identifiant['und'][0]['value'] = $row->identifiant;
  149. }
  150. // public function complete($node, $row) {
  151. // // dsm('-- complete --');
  152. // // // Do what you need to do to
  153. // // dsm($row, '$row');
  154. // // dsm($node, '$node');
  155. //
  156. // }
  157. }