.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fisharebest\Webtrees\Carbon;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
use Fisharebest\Webtrees\Services\DatatablesService;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Services\UserService;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Query\JoinClause;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use stdClass;
use function e;
/**
* List of users.
*/
class UserListData implements RequestHandlerInterface
{
/** @var DatatablesService */
private $datatables_service;
/** @var ModuleService */
private $module_service;
/** @var UserService */
private $user_service;
/**
* UserListData constructor.
*
* @param DatatablesService $datatables_service
* @param ModuleService $module_service
* @param UserService $user_service
*/
public function __construct(
DatatablesService $datatables_service,
ModuleService $module_service,
UserService $user_service
) {
$this->datatables_service = $datatables_service;
$this->module_service = $module_service;
$this->user_service = $user_service;
}
/**
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$user = $request->getAttribute('user');
$languages = $this->module_service->findByInterface(ModuleLanguageInterface::class, true)
->mapWithKeys(static function (ModuleLanguageInterface $module): array {
$locale = $module->locale();
return [$locale->languageTag() => $locale->endonym()];
});
$query = DB::table('user')
->leftJoin('user_setting AS us1', static function (JoinClause $join): void {
$join
->on('us1.user_id', '=', 'user.user_id')
->where('us1.setting_name', '=', 'language');
})
->leftJoin('user_setting AS us2', static function (JoinClause $join): void {
$join
->on('us2.user_id', '=', 'user.user_id')
->where('us2.setting_name', '=', UserInterface::PREF_TIMESTAMP_REGISTERED);
})
->leftJoin('user_setting AS us3', static function (JoinClause $join): void {
$join
->on('us3.user_id', '=', 'user.user_id')
->where('us3.setting_name', '=', UserInterface::PREF_TIMESTAMP_ACTIVE);
})
->leftJoin('user_setting AS us4', static function (JoinClause $join): void {
$join
->on('us4.user_id', '=', 'user.user_id')
->where('us4.setting_name', '=', UserInterface::PREF_IS_EMAIL_VERIFIED);
})
->leftJoin('user_setting AS us5', static function (JoinClause $join): void {
$join
->on('us5.user_id', '=', 'user.user_id')
->where('us5.setting_name', '=', UserInterface::PREF_IS_ACCOUNT_APPROVED);
})
->where('user.user_id', '>', '0')
->select([
'user.user_id AS edit_menu', // Hidden column
'user.user_id',
'user_name',
'real_name',
'email',
'us1.setting_value AS language',
'us2.setting_value AS registered_at_sort', // Hidden column
'us2.setting_value AS registered_at',
'us3.setting_value AS active_at_sort', // Hidden column
'us3.setting_value AS active_at',
'us4.setting_value AS verified',
'us5.setting_value AS verified_by_admin',
]);
$search_columns = ['user_name', 'real_name', 'email'];
$sort_columns = [];
$callback = function (stdClass $row) use ($languages, $user): array {
$row_user = $this->user_service->find($row->user_id);
$datum = [
view('admin/users-table-options', ['row' => $row, 'self' => $user, 'user' => $row_user]),
$row->user_id,
'' . e($row->user_name) . '',
'' . e($row->real_name) . '',
'' . e($row->email) . '',
$languages->get($row->language, $row->language),
$row->registered_at,
$row->registered_at ? view('components/datetime-diff', ['timestamp' => Carbon::createFromTimestamp((int) $row->registered_at)]) : '',
$row->active_at,
$row->active_at ? view('components/datetime-diff', ['timestamp' => Carbon::createFromTimestamp((int) $row->active_at)]) : I18N::translate('Never'),
$row->verified ? I18N::translate('yes') : I18N::translate('no'),
$row->verified_by_admin ? I18N::translate('yes') : I18N::translate('no'),
];
// Highlight old registrations.
if (!$datum[10] && date('U') - $datum[6] > 604800) {
$datum[7] = '' . $datum[7] . '';
}
return $datum;
};
return $this->datatables_service->handleQuery($request, $query, $search_columns, $sort_columns, $callback);
}
}