D7Users.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  5. /**
  6. * Drupal 7 user source from database.
  7. *
  8. * @MigrateSource(
  9. * id = "d7_users",
  10. * source_module = "user"
  11. * )
  12. */
  13. class D7Users extends FieldableEntity {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. return $this->select('users', 'u')
  19. ->fields('u')
  20. ->condition('u.uid', 1, '>');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function fields() {
  26. $fields = [
  27. 'uid' => $this->t('User ID'),
  28. 'name' => $this->t('Username'),
  29. 'pass' => $this->t('Password'),
  30. 'mail' => $this->t('Email address'),
  31. 'signature' => $this->t('Signature'),
  32. 'signature_format' => $this->t('Signature format'),
  33. 'created' => $this->t('Registered timestamp'),
  34. 'access' => $this->t('Last access timestamp'),
  35. 'login' => $this->t('Last login timestamp'),
  36. 'status' => $this->t('Status'),
  37. 'timezone' => $this->t('Timezone'),
  38. 'language' => $this->t('Language'),
  39. 'picture' => $this->t('Picture'),
  40. 'init' => $this->t('Init'),
  41. 'data' => $this->t('User data'),
  42. 'roles' => $this->t('Roles'),
  43. ];
  44. // Profile fields.
  45. if ($this->moduleExists('profile')) {
  46. $fields += $this->select('profile_fields', 'pf')
  47. ->fields('pf', ['name', 'title'])
  48. ->execute()
  49. ->fetchAllKeyed();
  50. }
  51. return $fields;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function prepareRow(Row $row) {
  57. $uid = $row->getSourceProperty('uid');
  58. $roles = $this->select('users_roles', 'ur')
  59. ->fields('ur', ['rid'])
  60. ->condition('ur.uid', $uid)
  61. ->execute()
  62. ->fetchCol();
  63. $row->setSourceProperty('roles', $roles);
  64. $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
  65. // If this entity was translated using Entity Translation, we need to get
  66. // its source language to get the field values in the right language.
  67. // The translations will be migrated by the d7_user_entity_translation
  68. // migration.
  69. $entity_translatable = $this->isEntityTranslatable('user');
  70. $source_language = $this->getEntityTranslationSourceLanguage('user', $uid);
  71. $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
  72. $row->setSourceProperty('entity_language', $language);
  73. // Get Field API field values.
  74. foreach ($this->getFields('user') as $field_name => $field) {
  75. // Ensure we're using the right language if the entity and the field are
  76. // translatable.
  77. $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
  78. $field_values = $this->getFieldValues('user', $field_name, $uid, NULL, $field_language);
  79. // $field_values = [];
  80. // if ($field_name === 'field_company') {
  81. // // print($uid . " ---\n");
  82. // // print_r($tmp_field_values);
  83. // // fix the taxo tid as tode module replace it by node nid
  84. // // get the real tode tid
  85. // foreach ($tmp_field_values as $key => $value) {
  86. // $tode_tid = $this->select('field_data_field_tode_company', 'tode')
  87. // ->condition('entity_type', 'node')
  88. // ->condition('entity_id', $value['tid'])
  89. // ->fields('tode', ['field_tode_company_tid'])
  90. // ->execute()->fetchField();
  91. // $field_values[] = array('tid'=>$tode_tid);
  92. // }
  93. // // print_r($field_values);
  94. // $row->setSourceProperty('companies', $field_values);
  95. // }
  96. $row->setSourceProperty($field_name, $field_values);
  97. }
  98. // Get profile field values. This code is lifted directly from the D6
  99. // ProfileFieldValues plugin.
  100. if ($this->getDatabase()->schema()->tableExists('profile_value')) {
  101. $query = $this->select('profile_value', 'pv')
  102. ->fields('pv', ['fid', 'value']);
  103. $query->leftJoin('profile_field', 'pf', 'pf.fid=pv.fid');
  104. $query->fields('pf', ['name', 'type']);
  105. $query->condition('uid', $row->getSourceProperty('uid'));
  106. $results = $query->execute();
  107. foreach ($results as $profile_value) {
  108. if ($profile_value['type'] == 'date') {
  109. $date = unserialize($profile_value['value']);
  110. $date = date('Y-m-d', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
  111. $row->setSourceProperty($profile_value['name'], ['value' => $date]);
  112. }
  113. elseif ($profile_value['type'] == 'list') {
  114. // Explode by newline and comma.
  115. $row->setSourceProperty($profile_value['name'], preg_split("/[\r\n,]+/", $profile_value['value']));
  116. }
  117. else {
  118. $row->setSourceProperty($profile_value['name'], [$profile_value['value']]);
  119. }
  120. }
  121. }
  122. // Member Type
  123. // check if aaf
  124. $field_memo = $row->getSourceProperty('field_memo');
  125. if ( stripos($field_memo[0]['value'], 'membre aaf') !== false ){
  126. // drush_print($row->getSourceProperty('mail').' member_type: AAF');
  127. $row->setSourceProperty('member_type', 2);
  128. } else {
  129. // get the uber_cart order infos
  130. $query = $this->select('uc_orders', 'uco')
  131. ->fields('uco', ['uid', 'order_id']);
  132. $query->leftJoin('uc_order_products', 'ucop', 'ucop.order_id=uco.order_id');
  133. $query->fields('ucop', ['model']);
  134. $query->condition('uid', $row->getSourceProperty('uid'));
  135. $query->condition('ucop.nid', 11849);
  136. $query->orderBy('uco.created', 'DESC');
  137. $orders = $query->execute()->fetchAll();
  138. if(count($orders)){
  139. $first_order = array_shift($orders);
  140. // drush_print($row->getSourceProperty('mail'));
  141. // drush_print(print_r($first_order, true));
  142. switch($first_order['model']){
  143. case "abo01-A":
  144. $member_type = 0;
  145. break;
  146. case "abo01-B":
  147. $member_type = 1;
  148. break;
  149. case "abo01-C":
  150. $member_type = 1;
  151. // add bad phone number to memo field
  152. $memo .= "#migration: multijoueurs \n";
  153. break;
  154. }
  155. if(isset($member_type)){
  156. // drush_print($row->getSourceProperty('mail').' member_type: '.$member_type);
  157. $row->setSourceProperty('member_type', $member_type);
  158. }
  159. }
  160. // abo01-A 1 (web)
  161. // abo01-B 2 (web+showroom)
  162. // abo01-C 2 (web+showroom)
  163. }
  164. // record migration errors in field_memo
  165. if(isset($memo)){
  166. $field_memo = $row->getSourceProperty('field_memo');
  167. $field_memo[0]['value'] .= "\n".$memo;
  168. $row->setSourceProperty('field_memo', $field_memo);
  169. }
  170. return parent::prepareRow($row);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function getIds() {
  176. return [
  177. 'uid' => [
  178. 'type' => 'integer',
  179. 'alias' => 'u',
  180. ],
  181. ];
  182. }
  183. }