1 package BSE::Edit::Product;
3 use base 'BSE::Edit::Article';
7 use BSE::Util::Iterate;
9 use BSE::CfgInfo 'product_options';
10 use BSE::Util::Tags qw(tag_hash tag_article);
11 use constant PRODUCT_CUSTOM_FIELDS_CFG => "product custom fields";
13 our $VERSION = "1.016";
17 BSE::Edit::Product - tags and actions for editing BSE products
21 http://www.example.com/cgi-bin/admin/add.pl ...
25 Article editor subclass for editing Products.
31 retailPrice => "Retail price",
32 wholesalePrice => "Wholesale price",
36 sub generator { 'BSE::Generate::Product' }
38 sub _make_dummy_article {
39 my ($self, $article) = @_;
41 require BSE::DummyProduct;
42 return bless $article, "BSE::DummyProduct";
45 sub base_template_dirs {
46 return ( "products" );
50 my ($self, $article) = @_;
52 my @extras = $self->SUPER::extra_templates($article);
53 push @extras, 'shopitem.tmpl'
54 if grep -f "$_/shopitem.tmpl",
55 BSE::Template->template_dirs($self->{cfg});
57 my $extras = $self->{cfg}->entry('products', 'extra_templates');
58 push @extras, grep /\.(tmpl|html)$/i, split /,/, $extras
65 my ($article, $arg) = @_;
67 my $value = $article->{$arg};
68 defined $value or $value = '';
69 if ($value =~ /\cJ/ && $value =~ /\cM/) {
73 return encode_entities($value);
77 require BSE::TB::Subscriptions;
78 BSE::TB::Subscriptions->all;
81 sub iter_option_values {
82 my ($self, $rcurrent_option, $args) = @_;
87 return $$rcurrent_option->values;
91 my ($object, $args) = @_;
93 my $value = $object->{$args};
94 defined $value or $value = '';
95 if ($value =~ /\cJ/ && $value =~ /\cM/) {
98 escape_html($value, '<>&"');
101 sub tag_dboptionvalue_move {
102 my ($self, $req, $article, $rvalues, $rindex, $args) = @_;
104 $$rindex >= 0 && $$rindex < @$rvalues
105 or return "** dboptionvalue_move only in dboption_values iterator **";
107 my $my_id = $rvalues->[$$rindex]{id};
108 my $base_url = "$ENV{SCRIPT_NAME}?id=$article->{id}&value_id=$my_id&_csrfp=".$req->get_csrf_token("admin_move_option_value") . "&";
110 my $t = $req->cgi->param('_t');
112 and $base_url .= "_t=$t&";
116 $up_url = $base_url . "a_option_value_moveup=1";
119 if ($$rindex < $#$rvalues) {
120 $down_url = $base_url . "a_option_value_movedown=1";
123 my $refresh = $self->refresh_url($article, $req->cgi);
126 return BSE::Arrows::make_arrows($req->cfg, $down_url, $up_url, $refresh, $args, id => $my_id, id_prefix => "prodoptvaluemove");
129 sub tag_dboption_move {
130 my ($self, $req, $article, $roptions, $rindex, $args) = @_;
132 $$rindex >= 0 && $$rindex < @$roptions
133 or return "** dboption_move only in dboptions iterator **";
135 my $my_id = $roptions->[$$rindex]{id};
136 my $base_url = "$ENV{SCRIPT_NAME}?id=$article->{id}&option_id=$my_id&_csrfp=".$req->get_csrf_token("admin_move_option") . "&";
138 my $t = $req->cgi->param('_t');
140 and $base_url .= "_t=$t&";
144 $up_url = $base_url . "a_option_moveup=1";
147 if ($$rindex < $#$roptions) {
148 $down_url = $base_url . "a_option_movedown=1";
151 my $refresh = $self->refresh_url($article, $req->cgi);
154 return BSE::Arrows::make_arrows($req->cfg, $down_url, $up_url, $refresh, $args, id => $my_id, id_prefix => "prodoptmove");
158 my ($self, $rtier, $rprices, $product) = @_;
160 unless ($rprices->{loaded}) {
161 %$rprices = map { $_->tier_id => $_ } $product->prices
163 $rprices->{loaded} = 1;
166 $$rtier or return '** no current tier **';
168 exists $rprices->{$$rtier->id}
171 return $rprices->{$$rtier->id}->retailPrice;
175 my ($self, $req, $article, $data) = @_;
177 $self->_save_price_tiers($req, $article, $data);
178 $self->SUPER::save_more($req, $article, $data);
182 my ($self, $req, $article, $data) = @_;
184 $self->_save_price_tiers($req, $article, $data);
185 $self->SUPER::save_new_more($req, $article, $data);
188 sub _save_price_tiers {
189 my ($self, $req, $article, $data) = @_;
191 $data->{save_pricing_tiers}
194 $req->user_can('edit_field_edit_retailPrice', $article)
197 my @tiers = BSE::TB::Products->pricing_tiers;
199 for my $tier (@tiers) {
200 my $key = "tier_price_" . $tier->id;
201 if (exists $data->{$key} && $data->{$key} =~ /\S/) {
202 $prices{$tier->id} = $data->{$key} * 100;
205 $article->set_prices(\%prices);
209 my ($self, $table_object) = @_;
211 my @cols = $self->SUPER::save_columns($table_object);
212 my @tiers = BSE::TB::Products->pricing_tiers;
214 push @cols, "save_pricing_tiers";
215 push @cols, map { "tier_price_" . $_->id } @tiers;
222 my ($self, $article) = @_;
227 return $article->db_options;
232 These a tags available on admin/edit_* pages specific to products.
238 product I<field> - display the given field from the product being edited.
242 iterator begin dboptions ... dboption I<field> ... iterator end dboptions
244 - iterate over the existing database stored options for the product
248 dboption_move - display arrows to move the current dboption. The span
249 for the arrows is given an id of "prodoptmoveI<option-id>" by default.
253 iterator begin dboptionvalues ... dboptionvalue I<field> ... iterator end dboptionvalues
255 - iterate over the values for the current dboption
259 dboptionvalue_move - display arrows to move the current dboption. The
260 span for the arrows is given an id of "prodoptvaluemoveI<value-id>"
265 dboptionsjson - returns the product options as JSON.
269 iterator begin price_tiers ... price_tier I<field> ... iterator end price_tiers
271 Iterate over the configured price tiers.
277 Return the price at the current price_tier. Returns an empty string
278 if there's no price at this tier.
285 my ($self, $acts, $req, $article, $articles, $msg, $errors) = @_;
287 my $product_opts = product_options($req->cfg);
290 my $mbcs = $cfg->entry('html', 'mbcs', 0);
291 my $tag_hash = $mbcs ? \&tag_hash_mbcs : \&hash_tag;
296 my $dboption_value_index;
297 my $current_option_value;
298 my $it = BSE::Util::Iterate->new;
302 $req->set_variable(product => $article);
305 product => [ \&tag_article, $article, $cfg ],
306 $self->SUPER::low_edit_tags($acts, $req, $article, $articles, $msg,
308 alloptions => join(",", sort keys %$product_opts),
310 ([ \&iter_subs, $req ], 'subscription', 'subscriptions'),
313 single => "dboption",
314 plural => "dboptions",
315 store => \$current_option,
317 index => \$dboption_index,
318 code => [ iter_dboptions => $self, $article ],
323 $self, $req, $article, \@dboptions, \$dboption_index
327 single => "dboptionvalue",
328 plural => "dboptionvalues",
329 data => \@dboption_values,
330 index => \$dboption_value_index,
331 store => \$current_option_value,
332 code => [ iter_option_values => $self, \$current_option ],
335 dboptionsjson => [ tag_dboptionsjson => $self, $article ],
336 dboptionvalue_move =>
338 tag_dboptionvalue_move =>
339 $self, $req, $article, \@dboption_values, \$dboption_value_index
343 single => "price_tier",
344 plural => "price_tiers",
345 code => [ pricing_tiers => "BSE::TB::Products" ],
347 store => \$price_tier,
349 tier_price => [ tag_tier_price => $self, \$price_tier, \%prices, $article ],
354 my ($self, $article, $cgi) = @_;
356 my $base = 'product';
357 my $t = $cgi->param('_t');
358 if ($t && $t =~ /^\w+$/) {
361 return $self->{cfg}->entry('admin templates', $base,
366 my ($self, $article, $cgi) = @_;
368 return $self->{cfg}->entry('admin templates', 'add_product',
369 'admin/edit_product');
372 sub validate_parent {
373 my ($self, $data, $articles, $parent, $rmsg) = @_;
375 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
377 $parent->{generator} eq 'BSE::Generate::Catalog') {
378 $$rmsg = "Products must be in a catalog (not $parent->{generator})";
382 return $self->SUPER::validate_parent($data, $articles, $parent, $rmsg);
385 sub _validate_common {
386 my ($self, $data, $articles, $errors) = @_;
388 $self->SUPER::_validate_common($data, $articles, $errors);
390 for my $col (keys %money_fields) {
391 my $value = $data->{$col};
392 defined $value or next;
393 unless ($value =~ /^\d+(\.\d{1,2})?\s*/) {
394 $errors->{$col} = "$money_fields{$col} invalid";
398 if (defined $data->{options}) {
399 my $avail_options = product_options($self->{cfg});
401 my @bad_opts = grep !$avail_options->{$_},
402 split /,/, $data->{options};
404 $errors->{options} = "Bad product options '". join(",", @bad_opts)."' entered";
409 for my $sub_field (qw(subscription_id subscription_required)) {
410 my $value = $data->{$sub_field};
411 defined $value or next;
412 if ($value ne '-1') {
413 require BSE::TB::Subscriptions;
414 @subs = BSE::TB::Subscriptions->all unless @subs;
415 unless (grep $_->{subscription_id} == $value, @subs) {
416 $errors->{$sub_field} = "Invalid $sub_field value";
420 if (defined $data->{subscription_period}) {
421 my $sub = $data->{subscription_id};
422 if ($data->{subscription_period} !~ /^\d+$/) {
423 $errors->{subscription_period} = "Invalid subscription period, it must be the number of months to subscribe";
425 elsif ($sub != -1 && $data->{subscription_period} < 1) {
426 $errors->{subscription_period} = "Subscription period must be 1 or more when a subscription is selected";
429 if (defined $data->{subscription_usage}) {
430 unless ($data->{subscription_usage} =~ /^[123]$/) {
431 $errors->{subscription_usage} = "Invalid subscription usage";
435 if ($data->{save_pricing_tiers}) {
436 my @tiers = BSE::TB::Products->pricing_tiers;
437 for my $tier (@tiers) {
438 my $key = "tier_price_" . $tier->id;
439 my $value = $data->{$key};
440 defined $value or next;
441 if ($value =~ /\S/ && $value !~ /^\d+(\.\d{1,2})?\s*/) {
442 $errors->{$key} = 'Pricing tier "' . $tier->description . '" price invalid';
447 return !keys %$errors;
451 my ($self, $data, $articles, $errors) = @_;
453 my $ok = $self->SUPER::validate($data, $articles, $errors);
454 $self->_validate_common($data, $articles, $errors);
456 for my $field (qw(title)) {
457 unless ($data->{$field} =~ /\S/) {
458 $errors->{$field} = "No $field entered";
462 return $ok && !keys %$errors;
466 my ($self, $article, $data, $articles, $errors) = @_;
468 $self->SUPER::validate_old($article, $data, $articles, $errors)
471 return !keys %$errors;
474 sub possible_parents {
475 my ($self, $article, $articles) = @_;
480 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
481 # the parents of a catalog can be other catalogs or the shop
482 my $shop = $articles->getByPkey($shopid);
483 my @work = [ $shopid, $shop->{title} ];
485 my ($id, $title) = @{pop @work};
487 $labels{$id} = $title;
488 push @work, map [ $_->{id}, $title.' / '.$_->{title} ],
489 sort { $b->{displayOrder} <=> $a->{displayOrder} }
490 grep $_->{generator} eq 'BSE::Generate::Catalog',
491 $articles->getBy(parentid=>$id);
493 unless ($shop->{generator} eq 'BSE::Generate::Catalog') {
495 delete $labels{$shopid};
497 return (\@values, \%labels);
501 my ($self, $articles) = @_;
507 my ($self, $articles, $article) = @_;
509 return BSE::TB::Products->getByPkey($article->{id});
512 sub default_link_path {
513 my ($self, $article) = @_;
515 $self->{cfg}->entry('uri', 'shop', '/shop');
519 my ($self, $article) = @_;
526 if ($self->{cfg}->entry('shop', 'secureurl_articles', 1)) {
527 $urlbase = $self->{cfg}->entryVar('site', 'secureurl');
531 if ($article->is_dynamic) {
532 (my $extra = $article->title) =~ tr/A-Za-z0-9/-/sc;
533 return "$urlbase/cgi-bin/page.pl?page=$article->{id}&title=".escape_uri($extra);
536 my $shop_uri = $self->link_path($article);
537 return $urlbase.$shop_uri."/shop$article->{id}.html";
540 sub _fill_product_data {
541 my ($self, $req, $data, $src) = @_;
543 for my $money_col (qw(retailPrice wholesalePrice gst)) {
544 if (exists $src->{$money_col}) {
545 if ($src->{$money_col} =~ /^\d+(\.\d\d)?\s*/) {
546 $data->{$money_col} = 100 * $src->{$money_col};
549 $data->{$money_col} = 0;
553 if (exists $src->{leadTime}) {
554 $src->{leadTime} =~ /^\d+\s*$/
555 or $src->{leadTime} = 0;
556 $data->{leadTime} = $src->{leadTime};
558 if (exists $src->{description} && length $src->{description}) {
560 if ($req->user_can('edit_field_edit_description', $data)) {
561 $data->{description} = $src->{description};
565 if (exists $src->{product_code} && length $src->{product_code}) {
567 if ($req->user_can('edit_field_edit_product_code', $data)) {
568 $data->{product_code} = $src->{product_code};
572 for my $field (qw(options subscription_id subscription_period
573 subscription_usage subscription_required
574 weight length width height)) {
575 if (exists $src->{$field}) {
576 $data->{$field} = $src->{$field};
578 elsif ($data == $src) {
580 $data->{$field} = $self->default_value($req, $data, $field);
586 my ($self, $req, $data, $articles) = @_;
588 $self->_fill_product_data($req, $data, $data);
590 return $self->SUPER::fill_new_data($req, $data, $articles);
594 my ($self, $req, $article, $src) = @_;
596 $self->_fill_product_data($req, $article, $src);
598 return $self->SUPER::fill_old_data($req, $article, $src);
601 sub default_template {
602 my ($self, $article, $cfg, $templates) = @_;
604 my $template = $cfg->entry('products', 'template');
606 if $template && grep $_ eq $template, @$templates;
608 return $self->SUPER::default_template($article, $cfg, $templates);
614 return ( 'product flags', $self->SUPER::flag_sections );
617 sub shop_article { 1 }
623 subscription_id => -1,
624 subscription_required => -1,
625 subscription_period => 1,
626 subscription_usage => 3,
639 my ($self, $req, $article, $col) = @_;
641 my $value = $self->SUPER::default_value($req, $article, $col);
642 defined $value and return $value;
644 exists $defaults{$col} and return $defaults{$col};
649 sub type_default_value {
650 my ($self, $req, $col) = @_;
652 my $value = $req->cfg->entry('product defaults', $col);
653 defined $value and return $value;
655 return $self->SUPER::type_default_value($req, $col);
662 description => "Option name",
664 rules => "dh_one_line",
669 description => "Value 1",
670 rules => "dh_one_line",
677 Actions you can request from add.pl for products.
683 Add a new product option.
685 On failure perform a service error.
687 Requires _csrfp for admin_add_option
689 For Ajax requests (or with a _ parameter) returns JSON like:
693 option: { <option data> },
694 values: [ { value data }, { value data }, ... ]
707 name - Name of the option (required)
711 value1 .. value5 - if any of these are non-blank they are added to the
716 Permission required: bse_edit_prodopt_add
721 my ($self, $req, $article, $articles, $msg, $errors) = @_;
723 $req->check_csrf('admin_add_option')
724 or return $self->csrf_error($req, $article, "admin_add_option", "Add Product Option");
726 $req->user_can(bse_edit_prodopt_add => $article)
727 or return $self->_service_error($req, $article, $articles, "Insufficient product access to add options");
730 my %work_option_fields = \%option_fields;
731 for my $field (grep /^value[1-9][0-9]*$/, $req->cgi->param) {
732 my ($index) = ( $field =~ /([1-9][0-9]*)$/);
733 $work_option_fields{$field} =
735 description => "Value $index",
736 rules => "dh_one_line",
740 $req->validate(fields => \%work_option_fields,
743 and return $self->_service_error($req, $article, $articles, undef,
747 require BSE::TB::ProductOptions;
748 require BSE::TB::ProductOptionValues;
749 my $option = BSE::TB::ProductOptions->make
751 product_id => $article->{id},
752 name => scalar($cgi->param('name')),
753 display_order => time,
759 for my $value_key (sort grep /^value/, keys %work_option_fields) {
760 my ($value) = $cgi->param($value_key);
761 if (defined $value && $value =~ /\S/) {
762 my $entry = BSE::TB::ProductOptionValues->make
764 product_option_id => $option->{id},
766 display_order => $order,
768 push @values, $entry;
769 $value_keys{$value_key} = $entry;
773 my $def = $cgi->param("default");
774 if ($def && $value_keys{$def}) {
775 $option->set_default_value($value_keys{$def}->id);
780 and return $req->json_content
783 option => $option->data_only,
784 values => [ map $_->data_only, @values ]
787 return $self->refresh($article, $cgi, undef, "Option added");
794 rules => "required;positiveint",
799 my ($self, $req, $article, $errors) = @_;
803 $req->validate(fields => \%option_id,
805 my @option_ids = $cgi->param("option_id");
806 unless ($errors->{option_id}) {
808 or $errors->{option_id} = "This request accepts only one option_id";
810 unless ($errors->{option_id}) {
811 require BSE::TB::ProductOptions;
812 $option = BSE::TB::ProductOptions->getByPkey($cgi->param("option_id"));
814 or $errors->{option_id} = "Unknown option id";
816 unless ($errors->{option_id}) {
817 $option->{product_id} = $article->{id}
818 or $errors->{option_id} = "Option doesn't belong to this product";
827 my ($self, $template, $req, $article, $articles, $msg, $errors) = @_;
830 my $option = $self->_get_option($req, $article, \%errors);
832 and return $self->_service_error($req, $article, $articles, undef, \%errors);
834 $req->set_variable(option => $option);
835 $req->messages($errors);
836 my $it = BSE::Util::Iterate->new;
840 $self->low_edit_tags(\%acts, $req, $article, $articles, $msg, $errors),
841 option => [ \&tag_hash, $option ],
844 single => "dboptionvalue",
845 plural => "dboptionvalues",
846 code => [ iter_option_values => $self, \$option ],
850 return $req->dyn_response($template, \%acts);
855 Produce a form to edit the given option.
867 option_id - option id. This must belong to the product identified by
872 Template: admin/prodopt_edit
874 Permission required: bse_edit_prodopt_edit
878 sub req_edit_option {
879 my ($self, $req, $article, $articles, $msg, $errors) = @_;
881 $req->user_can(bse_edit_prodopt_edit => $article)
882 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
884 return $self->_common_option('admin/prodopt_edit', $req, $article,
885 $articles, $msg, $errors);
892 description => "Option name",
893 rules => "required;dh_one_line",
898 description => "Default Value",
899 rules => "positiveint"
905 description => "Value",
906 rules => "required;dh_one_line",
912 Saves changes to an option.
914 On failure perform a service error.
916 Requires _csrfp for admin_save_option
918 For Ajax requests (or with a _ parameter), returns JSON like:
922 option: { <option data> },
923 values: [ { value data, value data, ... } ]
936 option_id - id of the option to save, must belong to the product
941 name - new value for the name field
945 default_value - id of the default value
949 save_enabled - if supplied and true, set enabled from the enabled
954 enabled - If supplied and true, enable the option, otherwise disable
955 it. Ignored unless save_enabled is true.
959 valueI<value-id> - set the displayed value for the value record
960 identified by I<value-id>. If these aren't supplied the values aren't
965 Permission required: bse_edit_prodopt_save
969 sub req_save_option {
970 my ($self, $req, $article, $articles) = @_;
974 $req->check_csrf("admin_save_option")
975 or return $self->csrf_error($req, $article, "admin_save_option", "Save Product Option");
977 $req->user_can(bse_edit_prodopt_edit => $article)
978 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
981 my $option = $self->_get_option($req, $article, \%errors);
983 and return $self->_service_error($req, $article, $articles, undef, \%errors, 'FIELD', "req_edit_option");
984 $req->validate(fields => \%option_name,
986 my @values = $option->values;
987 my %fields = map {; "value$_->{id}" => \%option_value } @values;
988 $req->validate(fields => \%fields,
991 my $default_value = $cgi->param('default_value');
992 if (!$errors{default_value} && $default_value) {
993 grep $_->{id} == $default_value, @values
994 or $errors{default_value} = "Unknown value selected as default";
1000 while ($index < 10 && defined $cgi->param("newvalue$index")) {
1001 my $field = "newvalue$index";
1002 my $value = $cgi->param($field);
1003 $req->validate(fields => { $field => \%option_value },
1004 errors => \%errors);
1005 push @new_values, $value;
1011 and return $self->_service_error($req, $article, $articles, undef, \%errors, "FIELD", "req_edit_option");
1013 my $name = $cgi->param("name");
1015 and $option->set_name($name);
1016 defined $default_value
1017 and $option->set_default_value($default_value);
1018 if ($cgi->param("save_enabled")) {
1019 my $enabled = $cgi->param("enabled") ? 1 : 0;
1020 $option->set_enabled($enabled);
1023 for my $value (@values) {
1024 my $new_value = $cgi->param("value$value->{id}");
1025 if (defined $new_value && $new_value ne $value->value) {
1026 $value->set_value($new_value);
1030 my $order = @values ? $values[-1]->display_order : time;
1031 for my $value (@new_values) {
1032 BSE::TB::ProductOptionValues->make
1034 product_option_id => $option->id,
1036 display_order => ++$order,
1041 and return $req->json_content
1044 option => $option->data_only,
1045 values => [ map $_->data_only, @values ],
1048 return $self->refresh($article, $req->cgi, undef,
1049 "Option '" . $option->name . "' saved");
1052 =item a_delconf_option
1054 Produce a form to confirm deletion of the given option.
1066 option_id - option id. This must belong to the product identified by
1071 Template: admin/prodopt_delete
1075 sub req_delconf_option {
1076 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1078 $req->user_can(bse_edit_prodopt_delete => $article)
1079 or return $self->_service_error($req, $article, $articles, "Insufficient product access to delete options");
1081 return $self->_common_option('admin/prodopt_delete', $req, $article,
1082 $articles, $msg, $errors);
1085 =item a_delete_option
1087 Delete the given option.
1089 On failure perform a service error.
1091 Requires _csrfp for admin_delete_option
1093 For Ajax requests (or with a _ parameter), returns JSON like:
1099 Permission required: bse_edit_prodopt_delete
1103 sub req_delete_option {
1104 my ($self, $req, $article, $articles) = @_;
1106 $req->check_csrf("admin_delete_option")
1107 or return $self->csrf_error($req, $article, "admin_delete_option", "Delete Product Option");
1109 $req->user_can(bse_edit_prodopt_delete => $article)
1110 or return $self->_service_error($req, $article, $articles, "Insufficient product access to delete options");
1113 my $option = $self->_get_option($req, $article, \%errors);
1115 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1116 my @values = $option->values;
1118 for my $value (@values) {
1124 and return $req->json_content
1129 return $self->refresh($article, $req->cgi, undef, "Option deleted");
1133 my %add_option_value_fields =
1137 description => "Option id",
1138 rules => "required;positiveint",
1142 description => "Value",
1143 rules => "required;dh_one_line",
1148 =item a_add_option_value
1150 Add a value to a product option.
1152 On failure perform a service error, see BSE::Edit::Article::_service_error.
1154 Requires _csrfp for admin_add_option_value
1156 For Ajax requests returns JSON like
1158 { success: 1, value: (valueobject) }
1160 Standard redirect on success otherwise.
1172 option_id - id of the option to add the value to
1176 value - text of the value to add.
1180 Permission required: bse_edit_prodopt_edit
1184 sub req_add_option_value {
1185 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1187 $req->check_csrf("admin_add_option_value")
1188 or return $self->csrf_error($req, $article, "admin_add_option_value", "Add Product Option Value");
1190 $req->user_can(bse_edit_prodopt_edit => $article)
1191 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1194 $req->validate(fields => \%add_option_value_fields,
1195 errors => \%errors);
1197 my $cgi = $req->cgi;
1198 unless ($errors{option_id}) {
1199 require BSE::TB::ProductOptions;
1200 $option = BSE::TB::ProductOptions->getByPkey($cgi->param("option_id"));
1201 defined $option && $option->{product_id}
1202 or $errors{option_id} = "Bad option id - either unknown or for a different product";
1205 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1207 my $value = $cgi->param("value");
1208 require BSE::TB::ProductOptionValues;
1209 my $entry = BSE::TB::ProductOptionValues->make
1211 product_option_id => $option->{id},
1213 display_order => time,
1217 and return $req->json_content
1220 value => $entry->data_only
1223 return $self->refresh($article, $cgi, undef, "Value added");
1227 my %option_value_id =
1231 rules => "required;positiveint",
1235 sub _get_option_value {
1236 my ($self, $req, $article, $errors) = @_;
1239 my $cgi = $req->cgi;
1240 $req->validate(fields => \%option_value_id,
1242 unless ($errors->{value_id}) {
1243 require BSE::TB::ProductOptionValues;
1244 $option_value = BSE::TB::ProductOptionValues->getByPkey($cgi->param("value_id"));
1246 or $errors->{value_id} = "Unknown option value id";
1249 unless ($errors->{value_id}) {
1250 $option = $option_value->option;
1251 defined $option && $option->{product_id} == $article->{id}
1252 or $errors->{value_id} = "Value has no option or doesn't belong to the product";
1258 return wantarray ? ( $option_value, $option ) : $option_value ;
1261 sub _common_option_value {
1262 my ($self, $template, $req, $article, $articles, $msg, $errors) = @_;
1265 my ($option_value, $option) = $self->_get_option_value($req, $article, \%errors);
1267 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1269 $req->set_variable(option => $option);
1270 $req->set_variable(option_value => $option_value);
1274 $self->low_edit_tags(\%acts, $req, $article, $articles, $msg, $errors),
1275 option_value => [ \&tag_hash, $option_value ],
1276 option => [ \&tag_hash, $option ],
1279 return $req->dyn_response($template, \%acts);
1282 =item a_edit_option_value
1284 Displays a form to edit the value for a given option.
1292 id - id of the product
1296 value_id - id of he product option value to edit, must belong to the
1301 Template: admin/prodopt_value_edit
1303 Permission required: bse_edit_prodopt_edit
1307 sub req_edit_option_value {
1308 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1310 $req->user_can(bse_edit_prodopt_edit => $article)
1311 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1313 return $self->_common_option_value('admin/prodopt_value_edit', $req,
1314 $article, $articles, $msg, $errors);
1317 my %save_option_value_fields =
1321 rules => "required;dh_one_line",
1326 =item a_save_option_value
1328 Saves changes to an option.
1330 On failure perform a service error.
1332 Requires _csrfp for admin_save_option_value
1334 For Ajax requests (or with a _ parameter), returns JSON like:
1338 value: { value data }
1351 value_id - id of the value to save, must belong to the product
1356 value - new displayed value for the option value.
1360 Permission required: bse_edit_prodopt_edit
1364 sub req_save_option_value {
1365 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1367 $req->check_csrf("admin_save_option_value")
1368 or return $self->csrf_error($req, $article, "admin_save_option_value", "Save Product Option Value");
1370 $req->user_can(bse_edit_prodopt_edit => $article)
1371 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1374 $req->validate(fields => \%save_option_value_fields,
1375 errors => \%errors);
1376 my $option_value = $self->_get_option_value($req, $article, \%errors);
1378 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1380 my $cgi = $req->cgi;
1381 $option_value->{value} = $cgi->param("value");
1382 $option_value->save;
1385 and return $req->json_content
1388 value => $option_value->data_only
1391 return $self->refresh($article, $cgi, undef, "Value saved");
1394 =item a_confdel_option_value
1396 Displays a page confirming deletion of a product option value.
1408 value_id - option value id
1412 Template: admin/prodopt_value_delete
1414 Permission required: bse_edit_prodopt_edit
1418 sub req_confdel_option_value {
1419 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1421 $req->user_can(bse_edit_prodopt_edit => $article)
1422 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1424 return $self->_common_option_value('admin/prodopt_value_delete', $req,
1425 $article, $articles, $msg, $errors);
1428 =item a_delete_option_value
1430 Deletes a product option.
1432 On failure perform a service error.
1434 Requires _csrfp for admin_delete_option_value
1436 For Ajax requests (or with a _ parameter), returns JSON like:
1452 value_id - id of the value to delete, must belong to the product
1457 Permission required: bse_edit_prodopt_edit
1461 sub req_delete_option_value {
1462 my ($self, $req, $article, $articles, $msg, $errors) = @_;
1464 $req->check_csrf("admin_delete_option_value")
1465 or return $self->csrf_error($req, $article, "admin_delete_option_value", "Delete Product Option Value");
1467 $req->user_can(bse_edit_prodopt_edit => $article)
1468 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1471 my $option_value = $self->_get_option_value($req, $article, \%errors);
1473 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1475 $option_value->remove;
1478 and return $req->json_content
1483 return $self->refresh($article, $req->cgi, undef, "Value removed");
1486 sub tag_dboptionsjson {
1487 my ($self, $article) = @_;
1490 my @options = $article->db_options;
1491 my @opt_cols = BSE::TB::ProductOption->columns;
1492 for my $option (@options) {
1493 my $entry = $option->data_only;
1494 $entry->{values} = [ map $_->data_only, $option->values ];
1495 push @result, $entry;
1499 my $json = JSON->new;
1500 return $json->encode(\@result);
1504 my ($self, $req, $article, $articles, $direction) = @_;
1506 $req->check_csrf("admin_move_option")
1507 or return $self->csrf_error($req, $article, "admin_move_option", "Move Product Option");
1509 $req->user_can(bse_edit_prodopt_move => $article)
1510 or return $self->_service_error($req, $article, $articles, "Insufficient product access to move options");
1513 my $option = $self->_get_option($req, $article, \%errors);
1515 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1516 my @options = $article->db_options;
1517 my ($index) = grep $options[$_]{id} == $option->{id}, 0 .. $#options
1518 or return $self->_service_error($req, $article, $articles, "Unknown option id");
1520 $options[$index] = $option;
1522 my $other_index = $index + $direction;
1523 $other_index >= 0 && $other_index < @options
1524 or return $self->_service_error($req, $article, $articles, "Can't move option beyond end");
1526 my $other = $options[$other_index];
1528 ($option->{display_order}, $other->{display_order}) =
1529 ($other->{display_order}, $option->{display_order});
1533 if ($req->is_ajax) {
1534 @options = sort { $a->{display_order} <=> $b->{display_order} } @options;
1535 return return $req->json_content
1538 order => [ map $_->{id}, @options ]
1542 return $self->refresh($article, $req->cgi, undef, "Option moved");
1545 =item a_option_moveup
1547 =item a_option_movedown
1549 Move a product option up/down through the options for a product.
1551 On failure perform a service error.
1553 Requires _csrfp for admin_move_option
1555 For Ajax requests (or with a _ parameter), returns JSON like:
1559 order: [ list of option ids ]
1572 option_id - option id. This must belong to the product identified by
1577 Permission required: bse_edit_prodopt_move
1581 sub req_option_moveup {
1582 my ($self, $req, $article, $articles) = @_;
1584 return $self->_option_move($req, $article, $articles, -1);
1587 sub req_option_movedown {
1588 my ($self, $req, $article, $articles) = @_;
1590 return $self->_option_move($req, $article, $articles, 1);
1593 =item a_option_reorder
1595 Move a product option up/down through the options for a product.
1597 On failure perform a service error.
1599 Requires _csrfp for admin_move_option
1601 For Ajax requests (or with a _ parameter), returns JSON like:
1605 order: [ list of option ids ]
1618 option_ids - option ids separated by commas. These must belong to the
1619 product identified by id.
1623 Permission required: bse_edit_prodopt_move
1627 sub req_option_reorder {
1628 my ($self, $req, $article, $articles) = @_;
1630 $req->check_csrf("admin_move_option")
1631 or return $self->csrf_error($req, $article, "admin_move_option", "Move Product Option");
1633 $req->user_can(bse_edit_prodopt_move => $article)
1634 or return $self->_service_error($req, $article, $articles, "Insufficient product access to move options");
1636 my @options = $article->db_options;
1637 my @order = map { split ',' } $req->cgi->param('option_ids');
1638 my %options = map { $_->{id} => $_ } @options;
1640 for my $id (@order) {
1641 my $option = delete $options{$id}
1643 push @new_options, $option;
1645 push @new_options, sort { $a->{display_order} <=> $b->{display_order} } values %options;
1646 my @display_order = map $_->{display_order}, @options;
1647 for my $index (0 .. $#new_options) {
1648 $new_options[$index]{display_order} = $display_order[$index];
1649 $new_options[$index]->save;
1653 and return $req->json_content
1656 order => [ map $_->{id}, @new_options ]
1659 return $self->refresh($article, $req->cgi, undef, "Options reordered");
1662 sub _option_value_move {
1663 my ($self, $req, $article, $articles, $direction) = @_;
1665 $req->check_csrf("admin_move_option_value")
1666 or return $self->csrf_error($req, $article, "admin_move_option_value", "Move Product Option Value");
1668 $req->user_can(bse_edit_prodopt_edit => $article)
1669 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1672 my ($option_value, $option) = $self->_get_option_value($req, $article, \%errors);
1674 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1675 my @values = $option->values;
1676 my ($index) = grep $values[$_]{id} == $option_value->{id}, 0 .. $#values
1677 or return $self->_service_error($req, $article, $articles, "Unknown option value id");
1679 $values[$index] = $option_value;
1681 my $other_index = $index + $direction;
1682 $other_index >= 0 && $other_index < @values
1683 or return $self->_service_error($req, $article, $articles, "Can't move option value beyond end");
1685 my $other = $values[$other_index];
1687 ($option_value->{display_order}, $other->{display_order}) =
1688 ($other->{display_order}, $option_value->{display_order});
1689 $option_value->save;
1692 # make sure the json gets the new order
1693 @values[$index, $other_index] = @values[$other_index, $index];
1696 and return $req->json_content
1699 order => [ map $_->{id}, @values ]
1702 return $self->refresh($article, $req->cgi, undef, "Value moved");
1705 =item a_option_value_moveup
1707 =item a_option_value_movedown
1709 Move a product option value up/down through the values for a product
1712 On failure perform a service error.
1714 Requires _csrfp for admin_move_option_value
1716 For Ajax requests (or with a _ parameter), returns JSON like:
1720 order: [ list of value ids ]
1733 value_id - option id. This must belong to the product identified by
1738 Permission required: bse_edit_prodopt_edit
1742 sub req_option_value_moveup {
1743 my ($self, $req, $article, $articles) = @_;
1745 return $self->_option_value_move($req, $article, $articles, -1);
1748 sub req_option_value_movedown {
1749 my ($self, $req, $article, $articles) = @_;
1751 return $self->_option_value_move($req, $article, $articles, 1);
1754 =item a_option_value_reorder
1756 Specify a new order for the values belonging to a product option.
1758 On failure perform a service error.
1760 Requires _csrfp for admin_move_option_value
1762 For Ajax requests (or with a _ parameter), returns JSON like:
1766 order: [ list of value ids ]
1779 option_id - the option to reorder values for
1783 value_ids - new order for values specified as value ids separated by
1788 Permission required: bse_edit_prodopt_edit
1792 sub req_option_value_reorder {
1793 my ($self, $req, $article, $articles) = @_;
1795 $req->check_csrf("admin_move_option_value")
1796 or return $self->csrf_error($req, $article, "admin_move_option_value", "Move Product Option Value");
1798 $req->user_can(bse_edit_prodopt_edit => $article)
1799 or return $self->_service_error($req, $article, $articles, "Insufficient product access to edit options");
1802 my $option = $self->_get_option($req, $article, \%errors);
1804 and return $self->_service_error($req, $article, $articles, undef, \%errors);
1805 my @order = map { split ',' } $req->cgi->param('value_ids');
1806 my @values = $option->values;
1807 my %values = map { $_->{id} => $_ } @values;
1809 for my $id (@order) {
1810 my $value = delete $values{$id}
1812 push @new_values, $value;
1814 push @new_values, sort { $a->{display_order} <=> $b->{display_order} } values %values;
1815 my @display_order = map $_->{display_order}, @values;
1816 for my $index (0 .. $#new_values) {
1817 $new_values[$index]{display_order} = $display_order[$index];
1818 $new_values[$index]->save;
1822 and return $req->json_content
1825 option => $option->data_only,
1826 order => [ map $_->{id}, @new_values ]
1829 return $self->refresh($article, $req->cgi, undef, "Values reordered");
1835 my $custom = $self->SUPER::custom_fields();
1837 require DevHelp::Validate;
1838 DevHelp::Validate->import;
1839 return DevHelp::Validate::dh_configure_fields
1843 PRODUCT_CUSTOM_FIELDS_CFG,
1844 BSE::DB->single->dbh,
1848 sub article_actions {
1853 $self->SUPER::article_actions,
1854 a_add_option => 'req_add_option',
1855 a_confdel_option => 'req_confdel_option',
1856 a_del_option => 'req_del_option',
1857 a_edit_option => 'req_edit_option',
1858 a_save_option => 'req_save_option',
1859 a_delconf_option => 'req_delconf_option',
1860 a_delete_option => 'req_delete_option',
1861 a_get_option => 'req_get_option',
1862 a_edit_option_value => 'req_edit_option_value',
1863 a_save_option_value => 'req_save_option_value',
1864 a_confdel_option_value => 'req_confdel_option_value',
1865 a_delete_option_value => 'req_delete_option_value',
1866 a_add_option_value => 'req_add_option_value',
1867 a_option_value_moveup => 'req_option_value_moveup',
1868 a_option_value_movedown => 'req_option_value_movedown',
1869 a_option_value_reorder => 'req_option_value_reorder',
1870 a_option_moveup => 'req_option_moveup',
1871 a_option_movedown => 'req_option_movedown',
1872 a_option_reorder => 'req_option_reorder',
1882 Tony Cook <tony@develop-help.com>