allow new values to be added on the product option edit page
authorTony Cook <tony@develop-help.com>
Mon, 17 Oct 2011 08:48:07 +0000 (19:48 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 17 Oct 2011 08:48:07 +0000 (19:48 +1100)
MANIFEST
site/cgi-bin/modules/BSE/Edit/Product.pm
site/htdocs/js/admin_editprodopt.js [new file with mode: 0644]
site/templates/admin/prodopt_edit.tmpl

index dcdbfc2be211a02cf23e80689efa6d3239535000..4104b358f18e51884fc2b410928d2fa3c7594b08 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -463,6 +463,7 @@ site/htdocs/js/admin-ui/debug.js
 site/htdocs/js/admin-ui/menu.js
 site/htdocs/js/admin.js
 site/htdocs/js/admin_edit.js
+site/htdocs/js/admin_editprodopt.js
 site/htdocs/js/admin_messages.js
 site/htdocs/js/admin_prodopts.js
 site/htdocs/js/admin_siteusers.js
index 7c4d327ca223d28bbff1a3e71f1d6a3efa2364b6..853db250c595255c5660add598674004a40ef63d 100644 (file)
@@ -9,7 +9,7 @@ use BSE::Util::HTML;
 use BSE::CfgInfo 'product_options';
 use BSE::Util::Tags qw(tag_hash);
 
-our $VERSION = "1.006";
+our $VERSION = "1.007";
 
 =head1 NAME
 
@@ -749,7 +749,6 @@ sub req_add_option {
   my $order = time;
   my @values;
   for my $value_key (sort grep /^value/, keys %option_fields) {
-    print STDERR "fetching $value_key\n";
     my ($value) = $cgi->param($value_key);
     if (defined $value && $value =~ /\S/) {
       my $entry = BSE::TB::ProductOptionValues->make
@@ -965,7 +964,7 @@ sub req_save_option {
   my %errors;
   my $option = $self->_get_option($req, $article, \%errors);
   keys %errors
-    and return $self->_service_error($req, $article, $articles, undef, \%errors);
+    and return $self->_service_error($req, $article, $articles, undef, \%errors, 'FIELD', "req_edit_option");
   $req->validate(fields => \%option_name,
                 errors => \%errors);
   my @values = $option->values;
@@ -978,8 +977,22 @@ sub req_save_option {
     grep $_->{id} == $default_value, @values
       or $errors{default_value} = "Unknown value selected as default";
   }
+
+  $DB::single = 1;
+  my @new_values;
+  my $index = 1;
+  while ($index < 10 && defined $cgi->param("newvalue$index")) {
+    my $field = "newvalue$index";
+    my $value = $cgi->param($field);
+    $req->validate(fields => { $field => \%option_value },
+                  errors => \%errors);
+    push @new_values, $value;
+
+    ++$index;
+  }
+
   keys %errors
-    and return $self->_service_error($req, $article, $articles, undef, \%errors);
+    and return $self->_service_error($req, $article, $articles, undef, \%errors, "FIELD", "req_edit_option");
 
   my $name = $cgi->param("name");
   defined $name
@@ -998,6 +1011,15 @@ sub req_save_option {
       $value->save;
     }
   }
+  my $order = @values ? $values[-1]->display_order : time;
+  for my $value (@new_values) {
+    BSE::TB::ProductOptionValues->make
+       (
+        product_option_id => $option->id,
+        value => $value,
+        display_order => ++$order,
+       );
+  }
 
   $req->is_ajax
     and return $req->json_content
@@ -1007,7 +1029,8 @@ sub req_save_option {
        values => [ map $_->data_only, @values ],
       );
 
-  return $self->refresh($article, $req->cgi, undef, "Option saved");
+  return $self->refresh($article, $req->cgi, undef,
+                       "Option '" . $option->name . "' saved");
 }
 
 =item a_delconf_option
diff --git a/site/htdocs/js/admin_editprodopt.js b/site/htdocs/js/admin_editprodopt.js
new file mode 100644 (file)
index 0000000..8c901d7
--- /dev/null
@@ -0,0 +1,22 @@
+document.observe("dom:loaded", function() {
+  var td = $("product_option_values");
+  var add_link = new Element("a", { href: "#" });
+  add_link.update("Add new value");
+  td.appendChild(add_link);
+  var index = parseInt($("newvaluecount").value);
+  add_link.observe("click", function(ev) {
+    ev.stop();
+
+    ++index;
+
+    var div = new Element("div");
+    var name = "newvalue" + index;
+    var label = new Element("label", { for: name });
+    label.update("Value:");
+    div.appendChild(label);
+    var input = new Element("input", { type: "text", name: name, id: name });
+    div.appendChild(input);
+    td.insertBefore(div, add_link);
+    $("newvaluecount").value = index;
+  });
+});
\ No newline at end of file
index 8636acd555465814f94285b382eb15980dbb6c11..4d7ef5a8b268dc8b27d4f589ca3922f2d9c3da9b 100644 (file)
@@ -1,9 +1,10 @@
-<:wrap admin/base.tmpl title => "Edit Product Option", menuitem=>"none", showtitle=>"1" :>
+<:wrap admin/base.tmpl title => "Edit Product Option", menuitem=>"none", showtitle=>"1", js => "admin_editprodopt.js" :>
 <:include admin/product_menu.tmpl:>
 <form action="<:script:>" method="post">
 <input type="hidden" name="id" value="<:article id:>" />
 <input type="hidden" name="option_id" value="<:option id:>" />
 <input type="hidden" name="_t" value="prodopts" />
+<input type="hidden" name="newvaluecount" id="newvaluecount" value="<:ifCgi newvaluecount:><:cgi newvaluecount:><:or:>0<:eif:>" />
 <:csrfp admin_save_option hidden:>
 <input type="hidden" name="save_enabled" value="1" />
 <table class="editform editformsmall">
@@ -19,7 +20,7 @@
 </tr>
 <tr>
   <th>Values:</th>
-<td>
+<td id="product_option_values">
 <table class="editform">
 <tr>
   <td colspan="2"></td>
 </tr>
 <:iterator end dboptionvalues:>
 </table>
+<:if Cgi newvaluecount:>
+<:iterator begin repeats [cgi newvaluecount]:>
+<div><label for="newvalue<:repeat value:>">Value:</label>
+<input type="text" name="newvalue<:repeat value:>" value="<:cgi [cat newvalue [repeat value]]:>" /><:error_img [cat newvalue [repeat value]]:></div>
+<:iterator end repeats:>
+<:or Cgi:><:eif Cgi:>
 </td>
 <td class="help"></td>
 </tr>