| Line # | Revision | Author | |
|---|---|---|---|
| 1 | 609 | ahitrov | <% $json %> |
| 2 | <%once> | ||
| 3 | |||
| 4 | use JSON::XS; | ||
| 5 | |||
| 6 | </%once> | ||
| 7 | <%args> | ||
| 8 | |||
| 9 | $id => undef | ||
| 10 | $aid => undef | ||
| 11 | $s => undef | ||
| 12 | $move => undef | ||
| 13 | |||
| 14 | </%args> | ||
| 15 | <%init> | ||
| 16 | |||
| 17 | my %result; | ||
| 18 | 843 | ahitrov | warn Dumper(\%ARGS); |
| 19 | 609 | ahitrov | |
| 20 | 843 | ahitrov | if ( $id && $s && $s =~ /^\d+$/ && $move && $move =~ /^(up|down|first|last|before|after)$/ && (!$aid || $aid && $aid =~ /^\d+$/) ) { |
| 21 | my $check_id = 1; | ||
| 22 | my @ids; | ||
| 23 | if ( ref $id eq 'ARRAY' ) { | ||
| 24 | foreach my $data ( @$id ) { | ||
| 25 | if ( $data =~ /^\d+$/ ) { | ||
| 26 | push @ids, $data; | ||
| 27 | } else { | ||
| 28 | $check_id = 0; | ||
| 29 | last; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } elsif ( $id =~ /^[\d,]+$/ ) { | ||
| 33 | @ids = split(/,/, $id); | ||
| 34 | } else { | ||
| 35 | $check_id = 0; | ||
| 36 | } | ||
| 37 | if ( $check_id ) { | ||
| 38 | my $section = $keeper->get_section_by_id( $s ); | ||
| 39 | if ( ref $section ) { | ||
| 40 | if ( $section->_sorted ) { | ||
| 41 | if ( $section->dmove( \@ids, $move, $aid ) ) { | ||
| 42 | my $position = $section->_get_document_pos( $ids[0] ); | ||
| 43 | warn Dumper( $position ); | ||
| 44 | if ( ref $position && exists $position->{index} ) { | ||
| 45 | if ( exists $position->{before} ) { | ||
| 46 | $result{before} = $position->{before}; | ||
| 47 | $result{success} = 1; | ||
| 48 | } | ||
| 49 | if ( exists $position->{after} ) { | ||
| 50 | $result{after} = $position->{after}; | ||
| 51 | $result{success} = 1; | ||
| 52 | } | ||
| 53 | if ( exists $position->{first} ) { | ||
| 54 | $result{first} = 1; | ||
| 55 | $result{success} = 1; | ||
| 56 | } | ||
| 57 | if ( exists $position->{last} ) { | ||
| 58 | $result{last} = 1; | ||
| 59 | $result{success} = 1; | ||
| 60 | } | ||
| 61 | unless ( exists $result{success} ) { | ||
| 62 | $result{error} = Encode::decode('utf-8', 'Ошибка индексации. Для разбора полетов обратитесь к разработчикам'); | ||
| 63 | } | ||
| 64 | } else { | ||
| 65 | 609 | ahitrov | $result{error} = Encode::decode('utf-8', 'Ошибка индексации. Для разбора полетов обратитесь к разработчикам'); |
| 66 | } | ||
| 67 | } else { | ||
| 68 | 843 | ahitrov | $result{error} = Encode::decode('utf-8', 'Ошибка перемещения. Для разбора полетов обратитесь к разработчикам'); |
| 69 | 609 | ahitrov | } |
| 70 | } else { | ||
| 71 | 843 | ahitrov | $result{error} = Encode::decode('utf-8', 'В данной секции отключен режим ручной сортировки'); |
| 72 | 609 | ahitrov | } |
| 73 | } else { | ||
| 74 | 843 | ahitrov | $result{error} = Encode::decode('utf-8', 'Секция не найдена'); |
| 75 | 609 | ahitrov | } |
| 76 | } else { | ||
| 77 | 843 | ahitrov | $result{error} = Encode::decode('utf-8', 'Переданы неверные параметры'); |
| 78 | 609 | ahitrov | } |
| 79 | } else { | ||
| 80 | $result{error} = Encode::decode('utf-8', 'Переданы неверные параметры'); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $json = encode_json \%result; | ||
| 84 | |||
| 85 | </%init> |