Fixes for group settings parser

This commit is contained in:
David Bomba 2023-09-05 15:12:49 +10:00
parent 3fcce909f5
commit ddc949c260
3 changed files with 33 additions and 7 deletions

View File

@ -24,16 +24,19 @@ class UpdateGroupSettingRequest extends Request
*/ */
public function authorize() : bool public function authorize() : bool
{ {
return auth()->user()->can('edit', $this->group_setting); /** @var \App\Models\User $user */
$user = auth()->user();
return $user->can('edit', $this->group_setting);
} }
public function rules() public function rules()
{ {
$rules['settings'] = new ValidClientGroupSettingsRule();
// $rules['name'] = 'unique:group_settings,name,'.$this->id.',id,company_id,'.$this->group_setting->company_id; return [
'settings' => [new ValidClientGroupSettingsRule()],
];
return $rules;
} }
public function prepareForValidation() public function prepareForValidation()

View File

@ -18,6 +18,15 @@ class GroupSettingRepository extends BaseRepository
{ {
public function save($data, GroupSetting $group_setting) :?GroupSetting public function save($data, GroupSetting $group_setting) :?GroupSetting
{ {
if(isset($data['settings']['translations'])) {
unset($data['settings']['translations']);
}
if(isset($data['settings']['pdf_variables'])) {
unset($data['settings']['pdf_variables']);
}
$group_setting->fill($data); $group_setting->fill($data);
$group_setting->save(); $group_setting->save();
@ -25,16 +34,16 @@ class GroupSettingRepository extends BaseRepository
$settings = $group_setting->settings; $settings = $group_setting->settings;
unset($settings->company_logo); unset($settings->company_logo);
$group_setting->settings = $settings; $group_setting->settings = $settings;
$group_setting->save();
} }
if (! array_key_exists('settings', $data) || count((array) $data['settings']) == 0) { if (! array_key_exists('settings', $data) || count((array) $data['settings']) == 0) {
$settings = new \stdClass; $settings = new \stdClass;
$settings->entity = Client::class; $settings->entity = Client::class;
$group_setting->settings = $settings; $group_setting->settings = $settings;
$group_setting->save();
} }
$group_setting->save();
return $group_setting; return $group_setting;
} }
} }

View File

@ -46,6 +46,14 @@ trait ClientGroupSettingsSaver
unset($settings[$field]); unset($settings[$field]);
} }
foreach(['translations','pdf_variables'] as $field) {
if (isset($settings[$field])) {
unset($settings[$field]);
}
}
/* /*
* for clients and group settings, if a field is not set or is set to a blank value, * for clients and group settings, if a field is not set or is set to a blank value,
* we unset it from the settings object * we unset it from the settings object
@ -86,6 +94,12 @@ trait ClientGroupSettingsSaver
unset($settings->translations); unset($settings->translations);
} }
foreach(['translations','pdf_variables'] as $key){
if (property_exists($settings, $key)) {
unset($settings->{$key});
}
}
//18-07-2022 removed || empty($settings->{$key}) from this check to allow "0" values to persist //18-07-2022 removed || empty($settings->{$key}) from this check to allow "0" values to persist
foreach ($settings as $key => $value) { foreach ($settings as $key => $value) {
if (! isset($settings->{$key}) || (! is_object($settings->{$key}) && strlen($settings->{$key}) == 0)) { if (! isset($settings->{$key}) || (! is_object($settings->{$key}) && strlen($settings->{$key}) == 0)) {