2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Settings;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Http\Controller;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Http\Requests\Setting\Setting as Request;
|
2018-06-10 02:48:51 +03:00
|
|
|
use App\Models\Common\Company;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Module\Module;
|
2020-02-28 17:54:01 +03:00
|
|
|
use App\Models\Setting\Currency;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Traits\DateTime;
|
|
|
|
use App\Traits\Uploads;
|
2018-02-20 18:47:10 +03:00
|
|
|
use App\Utilities\Installer;
|
2019-11-16 10:21:14 +03:00
|
|
|
use Illuminate\Support\Str;
|
2017-10-11 15:47:07 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
class Settings extends Controller
|
|
|
|
{
|
|
|
|
use DateTime, Uploads;
|
|
|
|
|
2021-05-29 17:01:22 +03:00
|
|
|
public $skip_keys = ['company_id', '_method', '_token', '_prefix'];
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
public $file_keys = ['company.logo', 'invoice.logo'];
|
|
|
|
|
2021-05-29 17:01:22 +03:00
|
|
|
public $uploaded_file_keys = ['company.uploaded_logo', 'invoice.uploaded_logo'];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2019-11-16 10:21:14 +03:00
|
|
|
public function index()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$modules = new \stdClass();
|
|
|
|
$modules->settings = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Get enabled modules
|
|
|
|
$enabled_modules = Module::enabled()->get();
|
2017-10-11 15:47:07 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
foreach ($enabled_modules as $module) {
|
|
|
|
$m = module($module->alias);
|
2018-10-17 20:58:46 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Check if the module exists and has settings
|
|
|
|
if (!$m || empty($m->get('settings'))) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 20:58:46 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$modules->settings[$m->getAlias()] = [
|
|
|
|
'name' => $m->getName(),
|
|
|
|
'description' => $m->getDescription(),
|
2020-01-06 15:01:38 +03:00
|
|
|
'url' => route('settings.module.edit', ['alias' => $m->getAlias()]),
|
2019-11-16 10:21:14 +03:00
|
|
|
'icon' => $m->get('icon', 'fa fa-cog'),
|
|
|
|
];
|
|
|
|
}
|
2018-04-16 19:01:29 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
event(new \App\Events\Module\SettingShowing($modules));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-01-06 14:42:14 +03:00
|
|
|
$settings = [];
|
|
|
|
|
|
|
|
foreach ($modules->settings as $alias => $setting) {
|
2020-09-07 12:25:55 +03:00
|
|
|
$permission = !empty($setting['permission']) ? $setting['permission'] : 'read-' . $alias . '-settings';
|
|
|
|
|
|
|
|
if (!user()->can($permission)) {
|
2020-01-06 14:42:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings[$alias] = $setting;
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
return $this->response('settings.settings.index', ['modules' => $settings]);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param Request $request
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request)
|
|
|
|
{
|
|
|
|
$fields = $request->all();
|
2019-11-16 10:21:14 +03:00
|
|
|
$prefix = $request->get('_prefix', 'general');
|
2017-09-14 22:21:00 +03:00
|
|
|
$company_id = $request->get('company_id');
|
2018-01-03 14:02:27 +03:00
|
|
|
|
|
|
|
if (empty($company_id)) {
|
2021-04-16 00:59:43 +03:00
|
|
|
$company_id = company_id();
|
2018-01-03 14:02:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$company = Company::find($company_id);
|
|
|
|
|
2020-02-12 18:48:31 +03:00
|
|
|
$total_companies = Company::count();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
foreach ($fields as $key => $value) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$real_key = $prefix . '.' . $key;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Don't process unwanted keys
|
2019-11-16 10:21:14 +03:00
|
|
|
if (in_array($key, $this->skip_keys)) {
|
2017-09-14 22:21:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-29 17:01:22 +03:00
|
|
|
// change dropzone middleware already uploaded file
|
|
|
|
if (in_array($real_key, $this->uploaded_file_keys)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Process file uploads
|
2019-11-16 10:21:14 +03:00
|
|
|
if (in_array($real_key, $this->file_keys)) {
|
2018-01-03 14:02:27 +03:00
|
|
|
// Upload attachment
|
|
|
|
if ($request->file($key)) {
|
|
|
|
$media = $this->getMedia($request->file($key), 'settings');
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$company->attachMedia($media, Str::snake($real_key));
|
2018-01-03 14:02:27 +03:00
|
|
|
|
|
|
|
$value = $media->id;
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
// Prevent reset
|
|
|
|
if (empty($value)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($real_key == 'default.locale') {
|
2021-05-29 17:01:22 +03:00
|
|
|
if (!in_array($value, config('language.allowed'))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
user()->setAttribute('locale', $value)->save();
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:54:01 +03:00
|
|
|
if ($real_key == 'default.currency') {
|
|
|
|
$currency = Currency::code($value)->first();
|
|
|
|
$currency->rate = '1';
|
|
|
|
$currency->save();
|
|
|
|
}
|
|
|
|
|
2018-06-09 15:37:28 +03:00
|
|
|
// If only 1 company
|
2020-02-12 18:48:31 +03:00
|
|
|
if ($total_companies == 1) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$this->oneCompany($real_key, $value);
|
2017-12-06 14:06:02 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
setting()->set($real_key, $value);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save all settings
|
|
|
|
setting()->save();
|
|
|
|
|
|
|
|
$message = trans('messages.success.updated', ['type' => trans_choice('general.settings', 2)]);
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = [
|
|
|
|
'status' => null,
|
|
|
|
'success' => true,
|
|
|
|
'error' => false,
|
|
|
|
'message' => $message,
|
|
|
|
'data' => null,
|
|
|
|
'redirect' => route('settings.index'),
|
|
|
|
];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
flash($message)->success();
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2018-06-09 15:37:28 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
protected function oneCompany($real_key, $value)
|
2018-06-09 15:37:28 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
switch ($real_key) {
|
|
|
|
case 'company.name':
|
2018-11-06 14:00:30 +03:00
|
|
|
Installer::updateEnv(['MAIL_FROM_NAME' => '"' . $value . '"']);
|
|
|
|
break;
|
2019-11-16 10:21:14 +03:00
|
|
|
case 'company.email':
|
2021-05-29 17:01:22 +03:00
|
|
|
Installer::updateEnv(['MAIL_FROM_ADDRESS' => '"' . $value . '"']);
|
2018-11-06 14:00:30 +03:00
|
|
|
break;
|
2019-11-16 10:21:14 +03:00
|
|
|
case 'default.locale':
|
2021-05-29 17:01:22 +03:00
|
|
|
Installer::updateEnv(['APP_LOCALE' => '"' . $value . '"']);
|
2018-06-09 15:37:28 +03:00
|
|
|
break;
|
2019-11-16 10:21:14 +03:00
|
|
|
case 'schedule.time':
|
2019-03-07 16:41:14 +03:00
|
|
|
Installer::updateEnv(['APP_SCHEDULE_TIME' => '"' . $value . '"']);
|
|
|
|
break;
|
2018-06-09 15:37:28 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-29 17:01:22 +03:00
|
|
|
}
|