Skip to content
Snippets Groups Projects

[FEATURE] Export FE users to CSV from the BE Module Filtered Results

Merged Stefan Galinski requested to merge feature_ExportUsersToCSVFromBEModule into master
4 files
+ 133
3
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -30,6 +30,7 @@ use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\CsvUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
@@ -109,6 +110,115 @@ class BackendController extends ActionController {
}
/**
* Export the user results to a .csv file
*
* @param array $filters
* @throws \InvalidArgumentException
* @throws \UnexpectedValueException
*/
public function exportAction(array $filters = []) {
$pageUid = (int) GeneralUtility::_GP('id');
/** @var array $users */
list($totalUserCount, $users) = $this->frontendUserService->getUsersByPid($pageUid, $filters);
$exportString = '';
if ($totalUserCount && $users) {
$ignoreFields = ['tstamp', 'password', 'starttime', 'endtime', 'deleted', 'uc', 'image', 'TSconfig',
'is_online', 'tx_extbase_type', 'tx_sgaccount_static_info_country', 'tx_sgaccount_images',
'tx_sgaccount_hash', 'tx_sgaccount_hash_date', 'tx_sgaccount_forgot_hash',
'tx_sgaccount_forgot_hash_date', 'tx_sgaccount_first_confirm', 'tx_sgaccount_login_hash',
'tx_sgaccount_forgot_password_used_email'];
$dateFields = ['tstamp', 'starttime', 'endtime', 'crdate', 'lastlogin', 'tx_sgaccount_hash_date',
'tx_sgaccount_forgot_hash_date'];
$export = [[]];
$first = TRUE;
$userUids = [];
/** @var array $user */
foreach ($users as $user) {
if ($first) {
$first = FALSE;
foreach ($user as $field => $value) {
if (!in_array($field, $ignoreFields, TRUE)) {
$label = isset($GLOBALS['TCA']['fe_users']['columns'][$field]) ?
$GLOBALS['TCA']['fe_users']['columns'][$field]['label'] : '';
if (strpos($label, 'LLL:') === 0) {
$label = $GLOBALS['LANG']->sL($label);
}
$export[0][] = $label ?: $field;
}
}
}
$userUids[] = (int) $user['uid'];
}
/** @var array $groups */
$groups = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid, title', 'fe_groups', 'deleted = 0 ');
$groupTitles = [];
foreach($groups as $group) {
$groupTitles[(int)$group['uid']] = $group['title'];
}
unset($groups);
/** @var array $emails */
$emails = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_sgaccount_domain_model_user_email',
'deleted = 0 AND user IN(' . implode(',', $userUids) . ')');
$line = 1;
foreach ($users as $user) {
foreach ($user as $field => $value) {
if (!in_array($field, $ignoreFields, TRUE)) {
if (in_array($field, $dateFields, TRUE)) {
$export[$line][] = $value ? date('d.m.Y', $value) : '';
} elseif ($field === 'tx_sgaccount_emails') {
$emailString = '';
$usedIndexes = [];
foreach ($emails as $index => $email) {
if ((int) $email['user'] === (int) $user['uid']) {
$usedIndexes[] = $index;
if ($emailString !== '') {
$emailString .= ', ';
}
$emailString .= $email['email'] . '(' . $email['type'] . ', ' .
((bool) $email['confirmed'] ? 'confirmed' : 'unconfirmed') .
((bool) $email['disable'] ? ', disabled' : '') . ')';
}
}
$export[$line][] = $emailString;
foreach ($usedIndexes as $index) {
unset($emails[$index]);
}
} elseif ($field === 'usergroup') {
$groupString = '';
$userGroups = GeneralUtility::intExplode(',', $value, TRUE);
foreach ($userGroups as $groupUid) {
if (isset($groupTitles[$groupUid])) {
if ($groupString !== '') {
$groupString .= ', ';
}
$groupString .= $groupTitles[$groupUid];
}
}
$export[$line][] = $groupString;
} else {
$export[$line][] = (string) $value;
}
}
}
$line++;
}
foreach($export as $line) {
$exportString .= CsvUtility::csvValues($line, ';') . LF;
}
}
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: Binary');
header('Content-Disposition: attachment; filename="export.csv"');
header('Content-Length: ' . strlen($exportString));
echo $exportString;
exit(0);
}
/**
* Generates the doc header buttons for the module
*
* @return void
Loading