summaryrefslogtreecommitdiff
path: root/rt/html/REST/1.0/Forms/queue/default
blob: ce54088467e27400fc5c28b32f413fb922d33b32 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
%# REST/1.0/Forms/queue/default
%#
<%ARGS>
$id
$format => 's'
$changes => {}
</%ARGS>
<%perl>
my @comments;
my ($c, $o, $k, $e) = ("", [], {}, 0);
my %data = %$changes;
my $queue = new RT::Queue $session{CurrentUser};
my @fields = qw(Name Description CorrespondAddress CommentAddress
                InitialPriority FinalPriority DefaultDueIn);
my %fields = map { lc $_ => $_ } @fields;

if ($id ne 'new') {
    $queue->Load($id);
    if (!$queue->Id) {
        return [ "# Queue $id does not exist.", [], {}, 1 ];
    }
}
else {
    if (%data == 0) {
        return [
            "# Required: Name",
            [ "id", @fields ],
            {
                id => 'queue/new',
                Name => '<queue name>',
                Description => "",
                CommentAddress => "",
                CorrespondAddress => "",
                InitialPriority => "",
                FinalPriority => "",
                DefaultDueIn => "",
            },
            0
        ];
    }
    else {
        my %v;
        my %create = %fields;

        foreach my $k (keys %data) {
            if (exists $create{lc $k}) {
                $v{$create{lc $k}} = delete $data{$k};
            }
        }

        if ($v{Name} eq '<queue name>') {
            my %o = keys %$changes;
            delete @o{"id", @fields};
            return [
                "# Please set the queue name.",
                [ "id", @fields, keys %o ], $changes, 1
            ];
        }

        $queue->Create(%v);
        unless ($queue->Id) {
            return [ "# Could not create queue.", [], {}, 1 ];
        }

        delete $data{id};
        $id = $queue->Id;
        push(@comments, "# Queue $id created.");
        goto DONE if %data == 0;
    }
}

if (%data == 0) {
    my @data;

    push @data, [ id => "queue/".$queue->Id ];
    foreach my $key (@fields) {
        push @data, [ $key => $queue->$key ];
    }

    my %k = map {@$_} @data;
    $o = [ map {$_->[0]} @data ];
    $k = \%k;
}
else {
    my ($get, $set, $key, $val, $n, $s);

    foreach $key (keys %data) {
        $val = $data{$key};
        $key = lc $key;
        $n = 1;

        if (exists $fields{$key}) {
            $key = $fields{$key};
            $set = "Set$key";

            next if $val eq $queue->$key;
            ($n, $s) = $queue->$set($val);
        }
        elsif ($key ne 'id') {
            $n = 0;
            $s = "Unknown field.";
        }

    SET:
        if ($n == 0) {
            $e = 1;
            push @comments, "# $key: $s";
            unless (@$o) {
                my %o = keys %$changes;
                delete @o{"id", @fields};
                @$o = ("id", @fields, keys %o);
                $k = $changes;
            }
        }
    }

    push(@comments, "# Queue $id updated.") unless $n == 0;
}

DONE:
$c ||= join("\n", @comments) if @comments;
return [ $c, $o, $k, $e ];
</%perl>