Browse Source

regen user names, removed profiles & member_type field from user form

bach 2 years ago
parent
commit
a2a53addf3
2 changed files with 194 additions and 32 deletions
  1. 8 32
      config/sync/core.entity_form_display.user.user.default.yml
  2. 186 0
      regene-usernames.script

+ 8 - 32
config/sync/core.entity_form_display.user.user.default.yml

@@ -10,7 +10,6 @@ dependencies:
     - field.field.user.user.field_showroom
   module:
     - path
-    - profile
     - user
 id: user.user.default
 targetEntityType: user
@@ -22,20 +21,6 @@ content:
     region: content
     settings: {  }
     third_party_settings: {  }
-  contact_company_profiles:
-    type: profile_form
-    weight: 8
-    region: content
-    settings:
-      form_mode: default
-    third_party_settings: {  }
-  customer_profiles:
-    type: profile_form
-    weight: 7
-    region: content
-    settings:
-      form_mode: default
-    third_party_settings: {  }
   field_company:
     type: entity_reference_autocomplete
     weight: 1
@@ -46,12 +31,6 @@ content:
       size: 60
       placeholder: ''
     third_party_settings: {  }
-  field_member_type:
-    type: options_select
-    weight: 4
-    region: content
-    settings: {  }
-    third_party_settings: {  }
   field_memo:
     type: string_textarea
     weight: 3
@@ -71,7 +50,7 @@ content:
       placeholder: ''
     third_party_settings: {  }
   language:
-    weight: 9
+    weight: 5
     region: content
     settings: {  }
     third_party_settings: {  }
@@ -80,31 +59,28 @@ content:
     region: content
     settings: {  }
     third_party_settings: {  }
-  member_profiles:
-    type: profile_form
-    weight: 6
-    region: content
-    settings:
-      form_mode: default
-    third_party_settings: {  }
   path:
     type: path
-    weight: 11
+    weight: 7
     region: content
     settings: {  }
     third_party_settings: {  }
   simplenews:
-    weight: 5
+    weight: 4
     region: content
     settings: {  }
     third_party_settings: {  }
   timezone:
-    weight: 10
+    weight: 6
     region: content
     settings: {  }
     third_party_settings: {  }
 hidden:
   commerce_remote_id: true
   contact: true
+  contact_company_profiles: true
+  customer_profiles: true
+  field_member_type: true
   google_analytics: true
   langcode: true
+  member_profiles: true

+ 186 - 0
regene-usernames.script

@@ -0,0 +1,186 @@
+<?php
+
+//
+// This example demonstrates how to write a drush
+// script.  These scripts are run with the php-script command.
+//
+use Drush\Drush;
+
+$this->output()->writeln("User names regenerating");
+
+$this->output()->writeln("The extra options/arguments to this command were:");
+$this->output()->writeln(print_r($extra, true));
+// if ($extra[0] === "delete") {
+//   $delete = true;
+// } else {
+//   $delete = false;
+// }
+
+
+$database = \Drupal::database();
+
+//  __ __       _  _     
+// |  \  \ ___ <_>| | ___
+// |     |<_> || || |<_-<
+// |_|_|_|<___||_||_|/__/
+                      
+
+$this->output()->writeln("");
+
+$result = $database->query("
+    SELECT mail
+    FROM {users_field_data}
+    GROUP BY mail
+    HAVING COUNT(*) > 1");
+
+$dupmail = [];
+foreach ($result as $user) {
+  // $this->output()->writeln($user->mail);
+  $dupmail[] = $user->mail;
+}
+
+$this->output()->writeln(count($dupmail) . ' dup emails');
+
+
+$this->output()->writeln("");
+
+
+$this->output()->writeln("All users Name <- Mail");
+$ufd_query = $database->select('users_field_data', 'ufd')
+  ->fields('ufd', ['uid', 'name', 'mail'])
+  ->condition('ufd.uid', [0,1], 'NOT IN')
+  ->condition('mail', $dupmail, 'NOT IN');
+
+
+$ufd_result = $ufd_query->execute();
+
+
+foreach ($ufd_result as $ufd) {
+  $user_updated = $database->update('users_field_data')
+  ->fields([
+    'name' => $ufd->mail
+  ])
+  ->condition('uid', $ufd->uid)
+  ->execute();
+}
+
+
+//  __ __              _                 
+// |  \  \ ___ ._ _ _ | |_  ___  _ _  ___
+// |     |/ ._>| ' ' || . \/ ._>| '_><_-<
+// |_|_|_|\___.|_|_|_||___/\___.|_|  /__/
+                                      
+$this->output()->writeln("");
+
+$this->output()->writeln("All users Name <- Profile member");
+
+$p_query = $database->select('profile', 'p');
+$p_query->join('profile__field_name', 'pfn', 'pfn.entity_id = p.profile_id');
+$p_query->join('profile__field_first_name', 'pffn', 'pffn.entity_id = p.profile_id');
+$p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
+$p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
+$p_query
+  ->fields('p', ['profile_id', 'uid'])
+  ->fields('pfn', ['field_name_value'])
+  ->fields('pffn', ['field_first_name_value'])
+  ->fields('ur', ['entity_id', 'roles_target_id'])
+  ->fields('ufd', ['mail'])
+  ->condition('p.type', 'member')
+  ->condition('ur.roles_target_id', 'adherent');
+
+$p_result = $p_query->execute();
+
+// avoid duplicates
+$members = [];
+foreach ($p_result as $p) {
+  // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
+  $name = $p->field_first_name_value . ' ' . $p->field_name_value;
+  $name = str_replace(' Col.', '', $name);
+  if (!in_array($name, array_values($members), true)) {
+    $members[] = [
+      "name" => $name,
+      "mail" => $p->mail,
+      "uid" => $p->uid
+    ];
+  }
+}
+
+// $members = array_unique($members);
+// array_multisort($members);
+// $this->output()->writeln(print_r($members, true));
+
+// $this->output()->writeln(print_r(array_count_values($members)));
+
+$this->output()->writeln("members count ". count($members));
+
+$prev_name = "";
+foreach ($members as $member) {
+  $this->output()->writeln("updating " . $member['uid'] . " " . $member['name'] . " | " . $member['mail']);
+  $user_updated = $database->update('users_field_data')
+  ->fields([
+    'name' => $member['name']
+  ])
+  ->condition('uid', $member['uid'])
+  ->execute();
+}
+  
+//  ___             _                            
+// |  _> _ _  ___ _| |_ ___ ._ _ _  ___  _ _  ___
+// | <__| | |<_-<  | | / . \| ' ' |/ ._>| '_><_-<
+// `___/`___|/__/  |_| \___/|_|_|_|\___.|_|  /__/
+                                              
+
+$this->output()->writeln("All users Name <- Profile customer");
+
+$p_query = $database->select('profile', 'p');
+$p_query->join('profile__address', 'pa', 'pa.entity_id = p.profile_id');
+$p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
+$p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
+$p_query
+  ->fields('p', ['profile_id', 'uid'])
+  ->fields('pa', ['address_given_name', 'address_family_name'])
+  ->fields('ur', ['entity_id', 'roles_target_id'])
+  ->fields('ufd', ['mail'])
+  ->condition('p.type', 'customer')
+  ->condition('ufd.mail', ['info@materio.com'], 'NOT IN')
+  ->condition('ur.roles_target_id', 'adherent');
+
+$p_result = $p_query->execute();
+
+// avoid duplicates
+$customers = [];
+foreach ($p_result as $p) {
+  // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
+  $name = $p->address_given_name . ' ' . $p->address_family_name;
+  $name = str_replace(' Col.', '', $name);
+  if (!in_array($name, array_values($customers), true)) {
+    $customers[] = [
+      "name" => $name,
+      "mail" => $p->mail,
+      "uid" => $p->uid
+    ];
+  }
+}
+
+// $customers = array_unique($customers);
+// array_multisort($customers);
+// $this->output()->writeln(print_r($customers, true));
+
+// $this->output()->writeln(print_r(array_count_values($customers)));
+
+
+$this->output()->writeln("customers count ". count($customers));
+
+$prev_name = "";
+foreach ($customers as $customer) {
+  $this->output()->writeln("updating " . $customer['uid'] . " " . $customer['name'] . " | " . $customer['mail']);
+  $user_updated = $database->update('users_field_data')
+  ->fields([
+    'name' => $customer['name']
+  ])
+  ->condition('uid', $customer['uid'])
+  ->execute();
+}
+  
+
+