Export users to CSV

Bypasses the weird limit bug in the javascript
This commit is contained in:
snipe 2016-10-25 02:41:34 -07:00
parent d7434797ec
commit 30f0f6f527
3 changed files with 89 additions and 1 deletions

View file

@ -14,6 +14,7 @@ use App\Models\Setting;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use App\Http\Requests\SaveUserRequest; use App\Http\Requests\SaveUserRequest;
use App\Http\Requests\UpdateUserRequest; use App\Http\Requests\UpdateUserRequest;
use Symfony\Component\HttpFoundation\StreamedResponse;
use App\Models\User; use App\Models\User;
use App\Models\Ldap; use App\Models\Ldap;
use Auth; use Auth;
@ -1262,4 +1263,87 @@ class UsersController extends Controller
$assets = Asset::where('assigned_to', '=', $userId)->with('model')->get(); $assets = Asset::where('assigned_to', '=', $userId)->with('model')->get();
return response()->json($assets); return response()->json($assets);
} }
/**
* Exports users to CSV
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.5]
* @return \Illuminate\Http\Response
*/
public function getExportUserCsv()
{
\Debugbar::disable();
$response = new StreamedResponse(function() {
// Open output stream
$handle = fopen('php://output', 'w');
User::with('assets', 'accessories', 'consumables', 'licenses', 'manager', 'groups', 'userloc', 'company','throttle')->orderBy('created_at', 'DESC')->chunk(500, function($users) use($handle) {
$headers=[
// strtolower to prevent Excel from trying to open it as a SYLK file
strtolower(trans('general.id')),
trans('general.company'),
trans('admin/companies/table.title'),
trans('admin/users/table.employee_num'),
trans('admin/users/table.name'),
trans('admin/users/table.email'),
trans('admin/users/table.manager'),
trans('admin/users/table.location'),
trans('general.assets'),
trans('general.licenses'),
trans('general.accessories'),
trans('general.consumables'),
trans('admin/users/table.groups'),
trans('general.notes'),
trans('admin/users/table.activated'),
trans('general.created_at')
];
fputcsv($handle, $headers);
foreach ($users as $user) {
$user_groups = '';
foreach ($user->groups as $user_group) {
$user_groups .= $user_group->name.', ';
}
// Add a new row with data
$values = [
$user->id,
($user->company) ? $user->company->name : '',
$user->title,
$user->employee_num,
$user->fullName(),
$user->email,
($user->manager) ? $user->manager->fullName() : '',
($user->location) ? $user->location->name : '',
$user->assets->count(),
$user->licenses->count(),
$user->accessories->count(),
$user->consumables->count(),
$user_groups,
$user->notes,
($user->activated=='1') ? trans('general.yes') : trans('general.no'),
$user->created_at,
];
fputcsv($handle, $values);
}
});
// Close the output stream
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="users-'.date('Y-m-d-his').'.csv"',
]);
return $response;
}
} }

View file

@ -765,6 +765,7 @@ Route::group([ 'prefix' => 'admin','middleware' => ['web','auth']], function ()
Route::post('create', [ 'uses' => 'UsersController@postCreate', 'middleware' => ['authorize:users.edit'] ]); Route::post('create', [ 'uses' => 'UsersController@postCreate', 'middleware' => ['authorize:users.edit'] ]);
Route::get('import', [ 'as' => 'import/user', 'uses' => 'UsersController@getImport', 'middleware' => ['authorize:users.edit'] ]); Route::get('import', [ 'as' => 'import/user', 'uses' => 'UsersController@getImport', 'middleware' => ['authorize:users.edit'] ]);
Route::post('import', [ 'uses' => 'UsersController@postImport', 'middleware' => ['authorize:users.edit'] ]); Route::post('import', [ 'uses' => 'UsersController@postImport', 'middleware' => ['authorize:users.edit'] ]);
Route::get('export', [ 'uses' => 'UsersController@getExportUserCsv', 'middleware' => ['authorize:users.view'] ]);
Route::get('{userId}/edit', [ 'as' => 'update/user', 'uses' => 'UsersController@getEdit', 'middleware' => ['authorize:users.edit'] ]); Route::get('{userId}/edit', [ 'as' => 'update/user', 'uses' => 'UsersController@getEdit', 'middleware' => ['authorize:users.edit'] ]);
Route::post('{userId}/edit', [ 'uses' => 'UsersController@postEdit', 'middleware' => ['authorize:users.edit'] ]); Route::post('{userId}/edit', [ 'uses' => 'UsersController@postEdit', 'middleware' => ['authorize:users.edit'] ]);
Route::get('{userId}/clone', [ 'as' => 'clone/user', 'uses' => 'UsersController@getClone', 'middleware' => ['authorize:users.edit'] ]); Route::get('{userId}/clone', [ 'as' => 'clone/user', 'uses' => 'UsersController@getClone', 'middleware' => ['authorize:users.edit'] ]);

View file

@ -28,6 +28,9 @@
@else @else
<a class="btn btn-default pull-right" href="{{ URL::to('admin/users?status=deleted') }}" style="margin-right: 5px;">{{ trans('admin/users/table.show_deleted') }}</a> <a class="btn btn-default pull-right" href="{{ URL::to('admin/users?status=deleted') }}" style="margin-right: 5px;">{{ trans('admin/users/table.show_deleted') }}</a>
@endif @endif
@can('users.view')
<a class="btn btn-default pull-right" href="{{ URL::to('admin/users/export') }}" style="margin-right: 5px;">Export</a>
@endcan
@stop @stop
@ -80,7 +83,7 @@
<th data-switchable="true" data-sortable="true" data-field="employee_num" data-visible="false">{{ trans('admin/users/table.employee_num') }}</th> <th data-switchable="true" data-sortable="true" data-field="employee_num" data-visible="false">{{ trans('admin/users/table.employee_num') }}</th>
<th data-sortable="true" data-field="name">{{ trans('admin/users/table.name') }}</th> <th data-sortable="true" data-field="name">{{ trans('admin/users/table.name') }}</th>
<th data-sortable="true" data-field="email"> <th data-sortable="true" data-field="email">
<span class="hidden-md hidden-lg">Email</span> <span class="hidden-md hidden-lg">{{ trans('admin/users/table.email') }}</span>
<span class="hidden-xs"><i class="fa fa-envelope fa-lg"></i></span> <span class="hidden-xs"><i class="fa fa-envelope fa-lg"></i></span>
</th> </th>
<th data-sortable="true" data-field="username">{{ trans('admin/users/table.username') }}</th> <th data-sortable="true" data-field="username">{{ trans('admin/users/table.username') }}</th>