Skip to content

Commit

Permalink
recording default values form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hinanaya committed Jun 14, 2024
1 parent 9bfd088 commit e18f9c2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
60 changes: 59 additions & 1 deletion controllers/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'];
}

Expand Down
15 changes: 15 additions & 0 deletions models/mediacountries_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions models/medialanguages_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e18f9c2

Please sign in to comment.