userfields_to_groups.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // First get the IDs of the selected fields
  17. $sql = "SELECT id, field_type, variable FROM $tUserField";
  18. $result = Database::query($sql);
  19. $foundFields = array();
  20. $fieldsNames = array();
  21. while ($row = Database::fetch_assoc($result)) {
  22. if ($row['field_type'] == 1 && in_array($row['variable'], $referenceFields)) {
  23. $foundFields[$row['variable']] = array('id' => $row['id']);
  24. $fieldsNames[$row['id']] = $row['variable'];
  25. }
  26. }
  27. // Second get all the possible values of this field (in user data)
  28. $usersData = array();
  29. foreach ($foundFields as $key => $value) {
  30. $sql = "SELECT item_id as user_id, value FROM $tUserFieldValue WHERE field_id = " . $value['id'];
  31. $result = Database::query($sql);
  32. while ($row = Database::fetch_assoc($result)) {
  33. $foundFields[$key]['options'][$row['value']][] = $row['user_id'];
  34. if (empty($usersData[$row['user_id']])) {
  35. $usersData[$row['user_id']] = '';
  36. }
  37. if ($referenceFields[0] == $key) {
  38. $usersData[$row['user_id']] = $row['value'] . ' - ' . $usersData[$row['user_id']];
  39. } else {
  40. $usersData[$row['user_id']] .= $row['value'] . ' - ';
  41. }
  42. }
  43. }
  44. // Clean the user string
  45. $distinctGroups = array();
  46. foreach ($usersData as $userId => $value) {
  47. $usersData[$userId] = substr($usersData[$userId], 0, -3);
  48. $distinctGroups[$usersData[$userId]][] = $userId;
  49. }
  50. // Third, we create groups based on the combined strings by user and insert
  51. // users in them (as reader)
  52. /*foreach ($distinctGroups as $name => $usersList) {
  53. $now = api_get_utc_datetime();
  54. $sql = "INSERT INTO $tGroup (name, visibility, updated_on, created_on) VALUES ('$name', 1, '$now', '$now')";
  55. echo $sql . PHP_EOL;
  56. $result = Database::query($sql);
  57. $groupId = Database::insert_id();
  58. echo $groupId . PHP_EOL;
  59. foreach ($usersList as $user) {
  60. $sql = "INSERT INTO $tGroupUser (group_id, user_id, relation_type) VALUES ($groupId, $user, 2)";
  61. echo $sql . PHP_EOL;
  62. $result = Database::query($sql);
  63. }
  64. }*/