diff --git a/lib/Tuba/DB/Mixin/Object/Generic.pm b/lib/Tuba/DB/Mixin/Object/Generic.pm index 4331e96d..0118ab96 100644 --- a/lib/Tuba/DB/Mixin/Object/Generic.pm +++ b/lib/Tuba/DB/Mixin/Object/Generic.pm @@ -45,5 +45,37 @@ sub type { return shift->attrs->{reftype}; } +sub set_attr { + my $self = shift; + my $args = shift; + my $new_attrs = $args->{new_attrs}; + my $audit_user = $args->{audit_user}; + my $audit_note = $args->{audit_note}; + my $attr = $self->attrs; + foreach my $key ( keys %$new_attrs ) { + $attr->{ $key } = $new_attrs->{$key}; + } + $self->attrs( $attr ); + $self->save(audit_user => $audit_user, audit_note => $audit_note); + return 1; +} + +sub delete_attr { + my $self = shift; + my $args = shift; + my $del_attr = $args->{del_attr}; + my $audit_user = $args->{audit_user}; + my $audit_note = $args->{audit_note}; + + my $attr = $self->attrs; + delete $attr->{ $del_attr }; + $self->attrs( $attr ); + $self->save(audit_user => $audit_user, audit_note => $audit_note); + + return 1; +} + + + 1; diff --git a/lib/Tuba/Generic.pm b/lib/Tuba/Generic.pm index 9fccda04..1449d31e 100644 --- a/lib/Tuba/Generic.pm +++ b/lib/Tuba/Generic.pm @@ -28,5 +28,51 @@ sub list { $c->SUPER::list(@_); } +sub update { + my $c = shift; + $c->SUPER::update(@_) if (my $json = $c->req->json); + + my $generic = $c->_this_object or return $c->reply->not_found; + + if (my $params = $c->req->params->to_hash ) { + if ( $params->{delete_pub_attr} ) { + $generic->delete_attr( { del_attr => $params->{delete_pub_attr}, audit_user => $c->user, audit_note => "Setting attributes" }); + $c->redirect_without_error('update_form'); + } + elsif ( exists $params->{new_attr_key} || /^attribute/ ~~ %$params ) { + my $new_attributes = _collect_attributes($params); + $generic->set_attr( { new_attrs => $new_attributes, audit_user => $c->user, audit_note => "Setting attributes" }); + $c->redirect_without_error('update_form'); + } + else { + $c->SUPER::update(@_); + } + } +} + +sub _collect_attributes { + my ( $params ) = @_; + my $attrs; + + # any newly entered key should overwrite existing of that name. + my $new_attr_flag = 0; + foreach my $key ( keys %$params ) { + next if $key eq 'new_attr_value'; + if ( $key eq 'new_attr_key') { + $new_attr_flag = 1; + } + if ( $key =~ /^attribute_(.*)/ && $params->{ $key } ) { + $attrs->{"$1"} = $params->{$key}; + } + } + if ( $new_attr_flag ) { + $attrs->{ $params->{'new_attr_key'} } = $params->{'new_attr_value'} + } + + return $attrs; +} + + + 1; diff --git a/lib/Tuba/files/templates/generic/update_form.html.ep b/lib/Tuba/files/templates/generic/update_form.html.ep new file mode 100644 index 00000000..9e4b100c --- /dev/null +++ b/lib/Tuba/files/templates/generic/update_form.html.ep @@ -0,0 +1,81 @@ +% layout 'default'; +
+

<%= $object->meta->table %> : <%= $object->stringify %>

+
+ +% my $controls = stash 'controls' || {}; + +% my $textfield = begin + % my $column = shift; + % my $object = shift; + % my $accessor = $column->accessor_method_name; +
+ +
+% end + +% my $textarea = begin + % my $column = shift; + % my $object = shift; + % my $accessor = $column->accessor_method_name; +
+ +
+% end + +%= include 'update_tabs', active => 'fields'; +
+
+

To edit this generic publication, please choose a valid tab from above.

+
+ +%= hidden_field 'delete_pub_attr'; +%= form_for obj_uri_for($object, 'update') => class => 'form-horizontal' => method => 'POST' => begin + +
+ + + + + + + + + % for my $k (sort keys %{ $object->attrs }) { + + + + + + + % } +
Attributes
DeleteKeyCurrent ValueReplace Value
<%= $k %><%= $object->attrs->{$k} %><%= text_field 'attribute_'.$k, class => "form-control input-small squeezevert" %>
+

New Attribute

+
+
+ %= label_for 'new_attr_key', class => 'required' => begin + Key + %= end +
+
+ <%= text_field 'new_attr_key', class => "form-control input-small squeezevert" %> +
+
+ %= label_for 'new_attr_value', class => 'required' => begin + Value + %= end +
+
+ <%= text_field 'new_attr_value', class => "form-control input-small squeezevert" %> +
+
+ +
+
+ +
+
+
+ +%= end +