Add missing field filtering and the optional model method

This commit is contained in:
Alex Tselegidis 2023-01-14 09:38:49 +01:00
parent 71fa900ea7
commit 2e3e766f73
3 changed files with 85 additions and 6 deletions

View File

@ -155,6 +155,17 @@ class Providers extends EA_Controller {
'services',
]);
$this->providers_model->only($provider['settings'], [
'username',
'password',
'notifications',
'calendar_view'
]);
$this->providers_model->optional($provider, [
'services' => [],
]);
$provider_id = $this->providers_model->save($provider);
$provider = $this->providers_model->find($provider_id);
@ -206,6 +217,10 @@ class Providers extends EA_Controller {
'services',
]);
$this->providers_model->optional($provider, [
'services' => [],
]);
$provider_id = $this->providers_model->save($provider);
$provider = $this->providers_model->find($provider_id);

View File

@ -136,6 +136,36 @@ class Secretaries extends EA_Controller {
$secretary = request('secretary');
$this->secretaries_model->only($secretary, [
'first_name',
'last_name',
'email',
'alt_number',
'phone_number',
'address',
'city',
'state',
'zip_code',
'notes',
'timezone',
'language',
'is_private',
'id_roles',
'settings',
'secretaries',
]);
$this->secretaries_model->only($secretary['settings'], [
'username',
'password',
'notifications',
'calendar_view'
]);
$this->secretaries_model->optional($secretary, [
'providers' => [],
]);
$secretary_id = $this->secretaries_model->save($secretary);
$secretary = $this->secretaries_model->find($secretary_id);
@ -167,6 +197,38 @@ class Secretaries extends EA_Controller {
$secretary = request('secretary');
$this->secretaries_model->only($secretary, [
'id',
'first_name',
'last_name',
'email',
'alt_number',
'phone_number',
'address',
'city',
'state',
'zip_code',
'notes',
'timezone',
'language',
'is_private',
'id_roles',
'settings',
'secretaries',
]);
$this->secretaries_model->only($secretary['settings'], [
'username',
'password',
'notifications',
'calendar_view'
]);
$this->secretaries_model->optional($secretary, [
'providers' => [],
]);
$secretary_id = $this->secretaries_model->save($secretary);
$secretary = $this->secretaries_model->find($secretary_id);

View File

@ -195,19 +195,21 @@ class EA_Model extends CI_Model {
*/
public function optional(array &$record, array $fields)
{
$sanitize = function ($field) use (&$record) {
return $record[$field] ?? NULL;
};
if (is_assoc($record))
{
$record = array_map($sanitize, $fields);
foreach ($fields as $field => $default)
{
$record[$field] = $record[$field] ?? $default;
}
}
else
{
foreach ($record as &$record_item)
{
$record_item = array_map($sanitize, $fields);
foreach ($fields as $field => $default)
{
$record_item[$field] = $record_item[$field] ?? $default;
}
}
}
}