-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Collection.pm
109 lines (89 loc) · 3.01 KB
/
Collection.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package MusicBrainz::Server::Form::Collection;
use HTML::FormHandler::Moose;
use MusicBrainz::Server::Form::Utils qw( select_options_tree );
use MusicBrainz::Server::Translation qw( l );
extends 'MusicBrainz::Server::Form';
has '+name' => ( default => 'edit-list' );
has_field 'name' => (
type => '+MusicBrainz::Server::Form::Field::Text',
required => 1,
);
has_field 'type_id' => (
type => 'Select',
required => 1,
);
has_field 'description' => (
type => 'TextArea',
required => 0,
not_nullable => 1,
);
has_field 'public' => (
type => 'Boolean',
);
has_field 'collaborators' => (
type => 'Repeatable',
);
has_field 'collaborators.contains' => (
type => '+MusicBrainz::Server::Form::Field::Editor',
);
sub edit_field_names
{
return qw( name description public type_id collaborators );
}
sub options_type_id {
my $self = shift;
my $types = select_options_tree($self->ctx, 'CollectionType');
my $collection = $self->init_object;
if ($collection && blessed $collection) {
my $entity_type = $collection->type->item_entity_type;
unless ($self->ctx->model('Collection')->is_empty($entity_type, $collection->{id})) {
my %valid_types =
map { $_->id => 1 }
$self->ctx->model('CollectionType')->find_by_entity_type($entity_type);
$types = [grep {$valid_types{$_->{value}}} @$types];
}
}
return $types;
}
sub validate_type_id {
my $self = shift;
my $collection = $self->init_object;
return unless $collection && blessed $collection;
my $entity_type = $collection->type->item_entity_type;
if (!$self->ctx->model('Collection')->is_empty($entity_type, $collection->id)) {
my $new_type = $self->ctx->model('CollectionType')->get_by_id(
$self->field('type_id')->value
);
if ($entity_type ne $new_type->item_entity_type) {
return $self->field('type_id')->add_error(
l('The collection type must match the type of entities it contains.')
);
}
}
}
sub validate_collaborators {
my $self = shift;
my @collaborators = $self->field('collaborators')->fields;
my $is_valid = 1;
for my $collaborator (@collaborators) {
my $id_field = $collaborator->field('id');
my $name_field = $collaborator->field('name');
if (defined $name_field->value && !(defined $id_field->value)) {
my $editor = $self->ctx->model('Editor')->get_by_name($name_field->value);
if (defined $editor) {
$id_field->add_error(
l('To add “{editor}” as a collaborator, please select them from the dropdown.',
{editor => $name_field->value})
);
} else {
$id_field->add_error(
l('Editor “{editor}” does not exist.',
{editor => $name_field->value})
);
}
$is_valid = 0;
}
}
return $is_valid;
}
1;