migrate_materio.materiau.inc 5.9 KB

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