migrate_materio.breves.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * MaterioMateriauNodeMigration
  4. *
  5. */
  6. class MaterioBrevesNodeMigration 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. 'titlefield'=>t('title field'),
  14. 'languages'=> t('languages'),
  15. 'memo' => t('memo'),
  16. 'images'=>t('images'),
  17. 'images_titles'=>t('images titles'),
  18. 'images_alts'=>t('images alts'),
  19. // 'source'=>t("source"),
  20. // 'materiau'=>t('materiau ref'),
  21. 'video'=>t('video'),
  22. );
  23. $query = db_select(MIG_MAT_SRC_DB_D6 .'.fr_i18n_node', 'i');
  24. $query->join(MIG_MAT_SRC_DB_D6 .'.fr_node', 'n', 'n.nid = i.nid');
  25. $query->join(MIG_MAT_SRC_DB_D6 .'.fr_node_revisions', 'nr', 'nr.nid = n.nid');
  26. // $query->join(MIG_MAT_SRC_DB_D6 .'.fr_content_field_imago', 'im', 'im.nid = n.nid');
  27. $query
  28. ->condition('i.status', "0", "=")
  29. ->condition('n.type',"actu")
  30. ->fields('i', array('trid'))
  31. ->fields('n', array('nid', 'status', 'promote', 'sticky', 'created', 'changed', 'language', 'tnid'))
  32. ->fields('nr', array('title', 'body', 'teaser', 'format'));
  33. // ->fields('im', array('title', 'body', 'teaser', 'format'));
  34. // print "\n\n- - - - - - \n\n";
  35. // print_r($query->__toString());
  36. // print "\n\n- - - - - - \n\n";
  37. $this->source = new MigrateSourceSQL($query, $source_fields);
  38. $this->destination = new MigrateDestinationNode('breve');
  39. $this->map = new MigrateSQLMap($this->machineName,
  40. array(
  41. 'nid' => array(
  42. 'type' => 'int',
  43. 'unsigned' => TRUE,
  44. 'not null' => TRUE,
  45. 'description' => 'OLD Unique Materiau NID',
  46. 'alias' => 'i',
  47. )
  48. ),
  49. MigrateDestinationNode::getKeySchema()
  50. );
  51. $this->addFieldMapping('is_new')->defaultValue(TRUE);
  52. $this->addSimpleMappings(array('created', 'changed', 'status', 'promote', 'sticky', 'language', 'title'));
  53. $this->addFieldMapping('field_memo', 'memo');
  54. $this->addFieldMapping('title_field', 'titlefield');
  55. $this->addFieldMapping('title_field:language', 'languages');
  56. $this->addFieldMapping('body', 'body');
  57. $this->addFieldMapping('body:language', 'languages');
  58. $this->addFieldMapping('body:format')->defaultValue('filtred_html');
  59. $this->addFieldMapping('field_visuel', 'images');
  60. $this->addFieldMapping('field_visuel:source_dir')->defaultValue('public://SRC_imago');
  61. $this->addFieldMapping('field_visuel:title', 'images_titles');
  62. $this->addFieldMapping('field_visuel:alt', 'images_alts');
  63. $this->addFieldMapping('field_video_filter', 'video');
  64. $this->addFieldMapping('field_tags_libres', 'tags');
  65. $this->addFieldMapping('field_tags_libres:create_term')->defaultValue(TRUE);
  66. $this->addUnmigratedDestinations(array('revision', 'revision_uid', 'log', 'tnid', 'comment', 'uid', 'path', 'pathauto',
  67. 'title_field:format',
  68. 'body:summary',
  69. 'field_source',
  70. // 'field_video_filter',
  71. 'field_materiau_ref',
  72. 'field_memo:format', 'field_memo:language',
  73. 'field_onthologie', 'field_onthologie:source_type', 'field_onthologie:create_term',
  74. 'field_visuel:file_class','field_visuel:language','field_visuel:destination_dir','field_visuel:destination_file','field_visuel:file_replace','field_visuel:preserve_files',
  75. 'field_tags_libres', 'field_tags_libres:source_type', 'field_tags_libres:create_term',
  76. ));
  77. }
  78. public function prepareRow($cr){
  79. // dsm($cr);
  80. print "\n- - - - - - - - \n";
  81. // print $cr->nid . "\n";
  82. print $cr->title . "\n";
  83. $body = $cr->body;
  84. $cr->memo = '';
  85. if($cr->trid){
  86. $query = db_select(MIG_MAT_SRC_DB_D6 .'.fr_i18n_node', 'i');
  87. $query->join(MIG_MAT_SRC_DB_D6 .'.fr_node', 'n', 'n.nid = i.nid');
  88. $query->join(MIG_MAT_SRC_DB_D6 .'.fr_node_revisions', 'nr', 'nr.nid = n.nid');
  89. $query
  90. ->condition('i.status', "0", "<>")
  91. ->condition('i.trid', $cr->trid, "=")
  92. ->fields('i', array('trid'))
  93. ->fields('n', array('nid', 'status', 'promote', 'sticky', 'created', 'changed', 'language', 'tnid'))
  94. ->fields('nr', array('title', 'body', 'teaser', 'format'));
  95. $result = $query->execute();
  96. foreach ($result as $record) {
  97. $tr = $record;
  98. }
  99. // print $cr->title . "\n";
  100. // print $tr->title . "\n";
  101. $cr->titlefield = array($cr->title, $tr->title);
  102. $cr->body = array($cr->body, $tr->body);
  103. // $cr->teaser = array($cr->teaser, $cr->tnr_teaser);
  104. $cr->languages = array($cr->language, $tr->language);
  105. }else{
  106. $cr->languages = $cr->language;
  107. $cr->titlefield = $cr->title;
  108. // $cr->teaser = htmlspecialchars_decode($cr->teaser);
  109. }
  110. /* IMAGES */
  111. $query = db_select(MIG_MAT_SRC_DB_D6.'.fr_content_field_imago', 'im');
  112. $query->join(MIG_MAT_SRC_DB_D6.'.fr_files', 'f', 'f.fid = im.field_imago_fid');
  113. $query
  114. ->condition('im.nid', $cr->nid, "=")
  115. // ->condition('f.fid', $cr->nid, "=")
  116. ->fields('im', array('field_imago_list', 'field_imago_data'))
  117. ->fields('f', array('filename'));
  118. $images = $query->execute();
  119. $cr->images = array();
  120. $cr->images_titles = array();
  121. $cr->images_alts = array();
  122. foreach ($images as $image) {
  123. // print_r($image);
  124. $cr->images[] = $image->filename;
  125. $data = unserialize($image->field_imago_data);
  126. $cr->images_alts[] = $data['alt'];
  127. $cr->images_titles[] = $data['title'];
  128. }
  129. /* source */
  130. //Source : <a href="http://ucsdnews.ucsd.edu/pressreleases/researchers_create_living_neon_signs_composed_of_millions_of_glowing_bacter/" target="_blank">UCSanDiego</a>
  131. // preg_match('/Source\s:\s<a\shref="([^"]*)"[^>]*>([^<]*)<\/a>/', $body, $matches);
  132. // if(count($matches)){
  133. // print_r($matches);
  134. // $cr->source = str_replace('http://', '', $matches[1]);
  135. // }
  136. /* materio */
  137. // (mat&eacute;riO P0059)
  138. preg_match('/\(mat&eacute;riO\s([^\)]*)\)/', $body, $matches);
  139. if(count($matches)){
  140. print_r($matches);
  141. $cr->memo .= "ref materio : ".$matches[1]."\n";
  142. }
  143. /* video */
  144. //[video:http://www.youtube.com/watch?v=3Fzu2Av6BmE align:center autoplay:1]
  145. preg_match('/\[video:([^\s]*)[^\]]*]/', $body, $matches);
  146. if(count($matches)){
  147. print_r($matches);
  148. $cr->video = $matches[1];
  149. }
  150. $query = db_select(MIG_MAT_SRC_DB_D6 .'.fr_term_node', 'tn');
  151. $query->join(MIG_MAT_SRC_DB_D6 .'.fr_term_data', 'td', 'td.tid = tn.tid');
  152. $query
  153. ->condition('tn.nid', $cr->nid, "=")
  154. ->fields('td', array('name'));
  155. $result = $query->execute();
  156. $terms = array();
  157. foreach ($result as $record) {
  158. $terms[] = $record->name;
  159. }
  160. $cr->memo .= 'terms : '.implode(', ', $terms)."\n";
  161. return TRUE;
  162. }
  163. public function prepare($node, stdClass $row) {
  164. // dsm('-- prepare --');
  165. // dsm($node, '$node');
  166. // dsm($row, '$row');
  167. $node->workflow = 4;
  168. }
  169. // public function complete($node, $row) {
  170. // // dsm('-- complete --');
  171. // // // Do what you need to do to
  172. // // dsm($row, '$row');
  173. // // dsm($node, '$node');
  174. //
  175. // }
  176. }