Skip to content

A free open source IT asset/license management system

Added companies ajax select2 endpoint

snipebdb95e4e3d13
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] [<[email protected]>]
+ * @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 @@
+<!-- Company -->
+<div id="{{ $fieldname }}" class="form-group{{ $errors->has($fieldname) ? ' has-error' : '' }}">
+ {{ Form::label($fieldname, $translated_name, array('class' => 'col-md-3 control-label')) }}
+ <div class="col-md-7">
+ <select class="js-data-ajax" data-endpoint="companies" name="{{ $fieldname }}" style="width: 100%" id="company_select">
+ </select>
+ </div>
+
+
+ {!! $errors->first($fieldname, '<div class="col-md-8 col-md-offset-3"><span class="alert-msg"><i class="fa fa-times"></i> :message</span></div>') !!}
+
+</div>
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' =>