userfields_to_groups.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Move user fields "ruc" and "razon_social" to (social) groups (create groups)
  4. * and assign the related users to those groups.
  5. */
  6. if (PHP_SAPI != 'cli') {
  7. die('This script can only be launched from the command line');
  8. }
  9. require __DIR__ . '/../../main/inc/global.inc.php';
  10. // We assume all these fields represent the same value, so they are on a 1-1
  11. // relationship.
  12. $referenceFields = array('razon_social', 'ruc');
  13. $tUserField = Database::get_main_table(TABLE_EXTRA_FIELD);
  14. $tUserFieldValue = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  15. $tUser = Database::get_main_table(TABLE_MAIN_USER);
  16. $tGroup = Database::get_main_table(TABLE_MAIN_GROUP);
  17. $tGroupUser = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  18. // First get the IDs of the selected fields
  19. $sql = "SELECT id, field_type, variable FROM $tUserField";
  20. $result = Database::query($sql);
  21. $foundFields = array();
  22. $fieldsNames = array();
  23. while ($row = Database::fetch_assoc($result)) {
  24. if ($row['field_type'] == 1 && in_array($row['variable'], $referenceFields)) {
  25. $foundFields[$row['variable']] = array('id' => $row['id']);
  26. $fieldsNames[$row['id']] = $row['variable'];
  27. }
  28. }
  29. // Second get all the possible values of this field (in user data)
  30. $usersData = array();
  31. foreach ($foundFields as $key => $value) {
  32. $sql = "SELECT item_id as user_id, value FROM $tUserFieldValue WHERE field_id = " . $value['id'];
  33. $result = Database::query($sql);
  34. while ($row = Database::fetch_assoc($result)) {
  35. $foundFields[$key]['options'][$row['value']][] = $row['user_id'];
  36. if (empty($usersData[$row['user_id']])) {
  37. $usersData[$row['user_id']] = '';
  38. }
  39. if ($referenceFields[0] == $key) {
  40. $usersData[$row['user_id']] = $row['value'] . ' - ' . $usersData[$row['user_id']];
  41. } else {
  42. $usersData[$row['user_id']] .= $row['value'] . ' - ';
  43. }
  44. }
  45. }
  46. // Clean the user string
  47. $distinctGroups = array();
  48. foreach ($usersData as $userId => $value) {
  49. $usersData[$userId] = substr($usersData[$userId], 0, -3);
  50. $distinctGroups[$usersData[$userId]][] = $userId;
  51. }
  52. // Third, we create groups based on the combined strings by user and insert
  53. // users in them (as reader)
  54. foreach ($distinctGroups as $name => $usersList) {
  55. $now = api_get_utc_datetime();
  56. $sql = "INSERT INTO $tGroup (name, visibility, updated_on, created_on) VALUES ('$name', 1, '$now', '$now')";
  57. echo $sql . PHP_EOL;
  58. $result = Database::query($sql);
  59. $groupId = Database::insert_id();
  60. echo $groupId . PHP_EOL;
  61. foreach ($usersList as $user) {
  62. $sql = "INSERT INTO $tGroupUser (group_id, user_id, relation_type) VALUES ($groupId, $user, 2)";
  63. echo $sql . PHP_EOL;
  64. $result = Database::query($sql);
  65. }
  66. }