2018-10-23 18:47:55 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Wizard;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Http\Controller;
|
2018-10-24 12:07:28 +03:00
|
|
|
use App\Http\Requests\Wizard\Company as Request;
|
2018-10-23 18:47:55 +03:00
|
|
|
use App\Models\Common\Company;
|
2018-10-24 12:07:28 +03:00
|
|
|
use App\Traits\Uploads;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Support\Str;
|
2018-10-23 18:47:55 +03:00
|
|
|
|
|
|
|
class Companies extends Controller
|
|
|
|
{
|
2018-10-24 12:07:28 +03:00
|
|
|
use Uploads;
|
|
|
|
|
2020-01-08 11:33:39 +03:00
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Add CRUD permission check
|
2020-06-21 18:00:31 +03:00
|
|
|
$this->middleware('permission:create-common-companies')->only('create', 'store', 'duplicate', 'import');
|
|
|
|
$this->middleware('permission:read-common-companies')->only('index', 'show', 'edit', 'export');
|
|
|
|
$this->middleware('permission:update-common-companies')->only('update', 'enable', 'disable');
|
2020-01-08 11:33:39 +03:00
|
|
|
$this->middleware('permission:delete-common-companies')->only('destroy');
|
|
|
|
}
|
|
|
|
|
2018-10-23 18:47:55 +03:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit()
|
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
$company = Company::find(company_id());
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2021-05-20 15:18:43 +03:00
|
|
|
return $this->response('wizard.companies.edit', compact('company'));
|
2018-10-23 18:47:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2018-10-24 12:07:28 +03:00
|
|
|
public function update(Request $request)
|
2018-10-23 18:47:55 +03:00
|
|
|
{
|
2018-10-24 12:07:28 +03:00
|
|
|
// Company
|
2021-04-16 00:59:43 +03:00
|
|
|
$company = Company::find(company_id());
|
2018-10-24 12:07:28 +03:00
|
|
|
|
|
|
|
$fields = $request->all();
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2022-03-01 17:09:22 +03:00
|
|
|
$skip_keys = ['company_id', '_method', '_token', '_prefix'];
|
2019-11-16 10:21:14 +03:00
|
|
|
$file_keys = ['company.logo'];
|
2021-05-28 11:34:42 +03:00
|
|
|
$uploaded_file_keys = ['company.uploaded_logo'];
|
2018-10-24 12:07:28 +03:00
|
|
|
|
2022-08-05 19:08:03 +03:00
|
|
|
// Clear setting media
|
|
|
|
foreach ($file_keys as $file_key) {
|
|
|
|
$keys = explode('.', $file_key);
|
|
|
|
|
|
|
|
if (! setting($file_key, false)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_old_key = 'uploaded_' . $keys[1];
|
|
|
|
if (array_key_exists($file_old_key, $fields)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
setting()->forget($file_key);
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:07:28 +03:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
// Don't process unwanted keys
|
|
|
|
if (in_array($key, $skip_keys)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
switch ($key) {
|
|
|
|
case 'api_key':
|
|
|
|
$real_key = 'apps.' . $key;
|
|
|
|
break;
|
|
|
|
case 'financial_start':
|
|
|
|
$real_key = 'localisation.' . $key;
|
|
|
|
break;
|
2021-09-03 11:43:55 +03:00
|
|
|
case 'country':
|
|
|
|
$real_key = 'company.' . $key;
|
|
|
|
break;
|
2019-11-16 10:21:14 +03:00
|
|
|
default:
|
|
|
|
$real_key = 'company.' . $key;
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:34:42 +03:00
|
|
|
// change dropzone middleware already uploaded file
|
|
|
|
if (in_array($real_key, $uploaded_file_keys)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:31:17 +03:00
|
|
|
// Process file uploads
|
|
|
|
if (in_array($real_key, $file_keys)) {
|
2018-10-24 12:07:28 +03:00
|
|
|
// Upload attachment
|
|
|
|
if ($request->file($key)) {
|
|
|
|
$media = $this->getMedia($request->file($key), 'settings');
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2021-05-26 18:31:17 +03:00
|
|
|
$company->attachMedia($media, Str::snake($real_key));
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2018-10-24 12:07:28 +03:00
|
|
|
$value = $media->id;
|
|
|
|
}
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2018-10-24 12:07:28 +03:00
|
|
|
// Prevent reset
|
|
|
|
if (empty($value)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-23 18:47:55 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 18:29:44 +03:00
|
|
|
setting()->set($real_key, $value);
|
2018-10-24 12:07:28 +03:00
|
|
|
}
|
2018-10-23 18:47:55 +03:00
|
|
|
|
2018-10-24 12:07:28 +03:00
|
|
|
// Save all settings
|
2018-10-23 18:47:55 +03:00
|
|
|
setting()->save();
|
|
|
|
|
2021-05-25 19:52:39 +03:00
|
|
|
return response()->json([
|
2019-11-16 10:21:14 +03:00
|
|
|
'status' => null,
|
|
|
|
'success' => true,
|
|
|
|
'error' => false,
|
2021-05-25 19:52:39 +03:00
|
|
|
'message' => trans('messages.success.updated', ['type' => trans_choice('general.companies', 2)]),
|
2019-11-16 10:21:14 +03:00
|
|
|
'data' => null,
|
2021-05-25 19:52:39 +03:00
|
|
|
]);
|
2018-10-24 12:07:28 +03:00
|
|
|
}
|
2018-10-23 18:47:55 +03:00
|
|
|
}
|