snipe-it/app/Http/Transformers/SelectlistTransformer.php
Brady Wetherington 9a224a07ba
Modified how we do Select2 dynamic drop-down menus to be more secure (#9079)
* Modified how we do Select2 dynamic drop-down menus to be more secure

As noted by the author of select2, the more-secure way of creating
rich Select-dropdowns is to use jquery to create HTML snippets and
carefully modify text attributes within there. This prevents any
XSS from being brought to the page. As a side-effect, the extra
escaping that we had to do in all of the internal selectlist calls
is now no longer necessary, and has been removed. Rebased and
squashed from the original.

* Rebuilt all assets, but this still feels like it's too much stuff in here.

* Whoops, need to run that in dev, not prod
2021-02-02 15:55:21 -08:00

52 lines
1.4 KiB
PHP

<?php
namespace App\Http\Transformers;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Class SelectlistTransformer
*
* This handles the standardized formatting of the API response we need to provide for
* the rich (text and images) Select2 javascript.
*
* @package App\Http\Transformers
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0.16]
* @return \Illuminate\Http\Response
*/
class SelectlistTransformer
{
public function transformSelectlist (LengthAwarePaginator $select_items)
{
$items_array=[];
// Loop through the paginated collection to set the array values
foreach ($select_items as $select_item) {
$items_array[]= [
'id' => (int) $select_item->id,
'text' => ($select_item->use_text) ? $select_item->use_text : $select_item->name,
'image' => ($select_item->use_image) ? $select_item->use_image : null,
];
}
$results = [
'results' => $items_array,
'pagination' =>
[
'more' => ($select_items->currentPage() >= $select_items->lastPage()) ? false : true,
'per_page' => $select_items->perPage()
],
'total_count' => $select_items->total(),
'page' => $select_items->currentPage(),
'page_count' => $select_items->lastPage()
];
return $results;
}
}