diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php index f2a5d36ed9..582c3b6974 100644 --- a/app/Http/Controllers/Api/CompaniesController.php +++ b/app/Http/Controllers/Api/CompaniesController.php @@ -141,4 +141,57 @@ class CompaniesController extends Controller } } + + /** + * Display a formatted JSON response for the select2 menus + * + * @todo: create a transformer for handling these responses + * @author [A. Gianotto] [] + * @since [v4.0] + * + * @return \Illuminate\Http\Response + */ + public function selectlist(Request $request) + { + $this->authorize('view', Company::class); + + $companies = Company::select([ + 'companies.id', + 'companies.name', + 'companies.image', + ]); + + + if ($request->has('search')) { + $companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%'); + } + + $companies = $companies->paginate(50); + $companies_array = []; + + foreach ($companies as $company) { + $companies_array[] = + [ + 'id' => (int) $company->id, + 'text' => e($company->name), + 'image' => ($company->image) ? url('/').'/uploads/companies/'.$company->image : null, + ]; + } + + array_unshift($companies_array, ['id'=> '', 'text'=> trans('general.select_company'), 'image' => null]); + + $results = [ + 'items' => $companies_array, + 'pagination' => + [ + 'more' => ($companies->currentPage() >= $companies->lastPage()) ? false : true, + 'per_page' => $companies->perPage() + ], + 'total_count' => $companies->total(), + 'page' => $companies->currentPage(), + 'page_count' => $companies->lastPage() + ]; + + return $results; + } } diff --git a/resources/views/partials/forms/edit/company-select.blade.php b/resources/views/partials/forms/edit/company-select.blade.php new file mode 100644 index 0000000000..f16ccb9a6f --- /dev/null +++ b/resources/views/partials/forms/edit/company-select.blade.php @@ -0,0 +1,12 @@ + +
+ {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }} +
+ +
+ + + {!! $errors->first($fieldname, '
:message
') !!} + +
diff --git a/routes/api.php b/routes/api.php index 0d72b78001..89c35e09b9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -63,6 +63,12 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { /*--- Companies API ---*/ + Route::get( 'companies/selectlist', [ + 'as' => 'companies.selectlist', + 'uses' => 'CompaniesController@selectlist' + ]); + + Route::resource('companies', 'CompaniesController', [ 'names' =>