member_export.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Export the members of a group to CSV
  4. */
  5. $group_guid = (int) get_input("group_guid");
  6. if (empty($group_guid)) {
  7. register_error(elgg_echo("InvalidParameterException:MissingParameter"));
  8. forward(REFERER);
  9. }
  10. $group = get_entity($group_guid);
  11. if (empty($group) || !elgg_instanceof($group, "group")) {
  12. register_error(elgg_echo("InvalidParameterException:GUIDNotFound", array($group_guid)));
  13. forward(REFERER);
  14. }
  15. if (!$group->canEdit() || (elgg_get_plugin_setting("member_export", "group_tools") != "yes")) {
  16. register_error(elgg_echo("groups:cantedit"));
  17. forward(REFERER);
  18. }
  19. // create temp file
  20. $fh = tmpfile();
  21. $headers = array(
  22. "name",
  23. "username",
  24. "email",
  25. "member since (unix)",
  26. "member since (YYYY-MM-DD HH:MM:SS)",
  27. );
  28. $profile_fields = elgg_get_config("profile_fields");
  29. if (!empty($profile_fields)) {
  30. foreach ($profile_fields as $metadata_name => $type) {
  31. $lan_key = "profile:" . $metadata_name;
  32. $header = elgg_echo($lan_key);
  33. if ($header == $lan_key) {
  34. $header = $metadata_name;
  35. }
  36. $header = html_entity_decode($header);
  37. $header = str_ireplace("\"", "\"\"", str_ireplace(PHP_EOL, "", $header));
  38. $headers[] = $header;
  39. }
  40. }
  41. fwrite($fh, "\"" . implode("\";\"", $headers) . "\"" . PHP_EOL);
  42. $options = array(
  43. "type" => "user",
  44. "limit" => false,
  45. "relationship" => "member",
  46. "relationship_guid" => $group->getGUID(),
  47. "inverse_relationship" => true
  48. );
  49. $members = new ElggBatch("elgg_get_entities_from_relationship", $options);
  50. foreach ($members as $member) {
  51. $info = array(
  52. $member->name,
  53. $member->username,
  54. $member->email
  55. );
  56. $member_since = group_tools_get_membership_information($member, $group);
  57. $info[] = $member_since;
  58. $info[] = date("Y-m-d G:i:s", $member_since);
  59. if (!empty($profile_fields)) {
  60. foreach ($profile_fields as $metadata_name => $type) {
  61. if ($type == "tags") {
  62. $value = implode(", ", $member->$metadata_name);
  63. } else {
  64. $value = $member->$metadata_name;
  65. }
  66. $value = html_entity_decode($value);
  67. $value = str_ireplace("\"", "\"\"", str_ireplace(PHP_EOL, "", $value));
  68. $info[] = $value;
  69. }
  70. }
  71. fwrite($fh, "\"" . implode("\";\"", $info) . "\"" . PHP_EOL);
  72. }
  73. // read the csv in to a var before output
  74. $contents = "";
  75. rewind($fh);
  76. while (!feof($fh)) {
  77. $contents .= fread($fh, 2048);
  78. }
  79. // cleanup the temp file
  80. fclose($fh);
  81. // output the csv
  82. header("Content-Type: text/csv");
  83. header("Content-Disposition: attachment; filename=\"" . elgg_get_friendly_title($group->name) . ".csv\"");
  84. header("Content-Length: " . strlen($contents));
  85. echo $contents;
  86. exit();