ea9457c42df279a9ad3c97c326dec8d52518c926
[yaffs-website] / web / core / modules / user / src / Plugin / migrate / source / d6 / User.php
1 <?php
2
3 namespace Drupal\user\Plugin\migrate\source\d6;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal 6 user source from database.
10  *
11  * @MigrateSource(
12  *   id = "d6_user",
13  *   source_module = "user"
14  * )
15  */
16 class User extends DrupalSqlBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     return $this->select('users', 'u')
23       ->fields('u', array_keys($this->baseFields()))
24       ->condition('u.uid', 0, '>');
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function fields() {
31     $fields = $this->baseFields();
32
33     // Add roles field.
34     $fields['roles'] = $this->t('Roles');
35
36     return $fields;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function prepareRow(Row $row) {
43     // User roles.
44     $roles = $this->select('users_roles', 'ur')
45       ->fields('ur', ['rid'])
46       ->condition('ur.uid', $row->getSourceProperty('uid'))
47       ->execute()
48       ->fetchCol();
49     $row->setSourceProperty('roles', $roles);
50
51     // We are adding here the Event contributed module column.
52     // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
53     if ($row->hasSourceProperty('timezone_id') && $row->getSourceProperty('timezone_id')) {
54       if ($this->getDatabase()->schema()->tableExists('event_timezones')) {
55         $event_timezone = $this->select('event_timezones', 'e')
56           ->fields('e', ['name'])
57           ->condition('e.timezone', $row->getSourceProperty('timezone_id'))
58           ->execute()
59           ->fetchField();
60         if ($event_timezone) {
61           $row->setSourceProperty('event_timezone', $event_timezone);
62         }
63       }
64     }
65
66     // Unserialize Data.
67     $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
68
69     return parent::prepareRow($row);
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getIds() {
76     return [
77       'uid' => [
78         'type' => 'integer',
79         'alias' => 'u',
80       ],
81     ];
82   }
83
84   /**
85    * Returns the user base fields to be migrated.
86    *
87    * @return array
88    *   Associative array having field name as key and description as value.
89    */
90   protected function baseFields() {
91     $fields = [
92       'uid' => $this->t('User ID'),
93       'name' => $this->t('Username'),
94       'pass' => $this->t('Password'),
95       'mail' => $this->t('Email address'),
96       'theme' => $this->t('Theme'),
97       'signature' => $this->t('Signature'),
98       'signature_format' => $this->t('Signature format'),
99       'created' => $this->t('Registered timestamp'),
100       'access' => $this->t('Last access timestamp'),
101       'login' => $this->t('Last login timestamp'),
102       'status' => $this->t('Status'),
103       'timezone' => $this->t('Timezone'),
104       'language' => $this->t('Language'),
105       'picture' => $this->t('Picture'),
106       'init' => $this->t('Init'),
107       'data' => $this->t('User data'),
108     ];
109
110     // Possible field added by Date contributed module.
111     // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
112     if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_name')) {
113       $fields['timezone_name'] = $this->t('Timezone (Date)');
114     }
115
116     // Possible field added by Event contributed module.
117     // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
118     if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_id')) {
119       $fields['timezone_id'] = $this->t('Timezone (Event)');
120     }
121
122     return $fields;
123   }
124
125 }