From e18f9c2686abc004cba8ab2e3e6a996226fb0108 Mon Sep 17 00:00:00 2001 From: Sanae Date: Fri, 14 Jun 2024 20:29:22 +0000 Subject: [PATCH] recording default values form validation --- controllers/metadata.php | 60 ++++++++++++++++++++++++++++++++- models/mediacountries_model.php | 15 +++++++++ models/medialanguages_model.php | 15 +++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/controllers/metadata.php b/controllers/metadata.php index 3c236275..ab3ddff9 100644 --- a/controllers/metadata.php +++ b/controllers/metadata.php @@ -233,8 +233,66 @@ public function recording_default_values_save() { $this->user->require_permission('manage_media_settings'); - $this->models->settings('setting_set', 'recording_defaults', json_encode($this->data)); + $coreFields = $this->models->mediametadata('get_fields')[2]; + $customFields = $this->models->mediametadata('get_all'); + + if ($coreFields['album'] === 'required' && empty($this->data['album'])) { + //T No default album provided. + return [false, 'No default album provided.']; + } + + if ($coreFields['year'] === 'required' && ! ctype_digit($this->data['year'])) { + //T No default year provided. + return [false, 'No default year provided.']; + } + + $genre = $this->models->mediagenres('get_by_id', $this->data['genre'] ?? 0); + $category = $genre['media_category_id'] === $this->data['category']; + if ($coreFields['category_id'] === 'required' && (! $genre || ! $category)) { + //T No default category provided. + return [false, 'No default category provided.']; + } + + $country = $this->models->mediacountries('get_by_id', $this->data['country'] ?? 0); + if ($coreFields['country'] === 'required' && ! $country) { + //T No default country provided. + return [false, 'No default country provided.']; + } + $language = $this->models->medialanguages('get_by_id', $this->data['language'] ?? 0); + if ($coreFields['language'] === 'required' && ! $language) { + //T No default language provided. + return [false, 'No default language provided.']; + } + + if ($coreFields['comments'] === 'required' && empty($this->data['comments'])) { + //T No default comments provided. + return [false, 'No default comments provided.']; + } + + foreach ($customFields as $field) { + if (isset($field['settings']->mode) && $field['settings']->mode === 'required') { + $value = $this->data['custom_metadata'][$field['name']] ?? null; + + if ($value === '' || $value === null) { + return [false, 'No default value provided for required custom field `' . $field['name'] . '`']; + } + + if ( + $field['type'] === 'select' && + (! in_array($value, $field['settings']->options) && + (! ctype_digit($value) || count($field['settings']->options) < intval($value) || intval($value) < 0)) + ) { + return [false, 'Selected value not in allowed options for custom field `' . $field['name'] . '`']; + } + + if ($field['type'] === 'integer' && ! ctype_digit($value)) { + return [false, 'Value for custom field `' . $field['name'] . '` must be an integer']; + } + } + } + + $this->models->settings('setting_set', 'recording_defaults', json_encode($this->data)); return [true, 'Successfully saved default recording metadata values.']; } diff --git a/models/mediacountries_model.php b/models/mediacountries_model.php index f5e4962c..8783e32e 100644 --- a/models/mediacountries_model.php +++ b/models/mediacountries_model.php @@ -38,4 +38,19 @@ public function get_all() return $types; } + + /** + * Get country by ID. + * + * @param id + * + * @return country + */ + public function get_by_id($id) + { + $this->db->where('country_id', $id); + $country = $this->db->get('countries'); + + return $country; + } } diff --git a/models/medialanguages_model.php b/models/medialanguages_model.php index b0380fc1..127ba32a 100644 --- a/models/medialanguages_model.php +++ b/models/medialanguages_model.php @@ -80,4 +80,19 @@ public function get_top($args = []) return $languages; } + + /** + * Get language by ID. + * + * @param id + * + * @return language + */ + public function get_by_id($id) + { + $this->db->where('language_id', $id); + $language = $this->db->get('languages'); + + return $language; + } }