1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | la-sql-edit-form - Tools to edit forms (accreq) |
---|
12 | |
---|
13 | =head1 SYNOPSIS |
---|
14 | |
---|
15 | la-sql-edit-form formid |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | GetOptions( |
---|
20 | 'c|config=s' => \my $config, |
---|
21 | 'b|base=s' => \my $base, |
---|
22 | 'f=s' => \my $inputfile, |
---|
23 | 'help' => sub { pod2usage(0) }, |
---|
24 | ) or pod2usage(); |
---|
25 | |
---|
26 | =head1 OPTIONS |
---|
27 | |
---|
28 | =over 4 |
---|
29 | |
---|
30 | =item -c|--config configdir |
---|
31 | |
---|
32 | Use this configuration directory instead of the default one. |
---|
33 | |
---|
34 | =item -b|--base basename |
---|
35 | |
---|
36 | Query this specific base instead of the default one. |
---|
37 | |
---|
38 | =item -f FILE |
---|
39 | |
---|
40 | Use this file to get attributes (- use stdin) |
---|
41 | If this option is not present, you'll have a file opened in your prefered editor to edit. |
---|
42 | |
---|
43 | =back |
---|
44 | |
---|
45 | =cut |
---|
46 | |
---|
47 | if (!$ARGV[0]) { warn "You must specify 'obj_id', aborting\n"; pod2usage(); } |
---|
48 | |
---|
49 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
50 | my $labase = $LA->base($base); |
---|
51 | $labase && $labase->load or die "Cannot load base"; |
---|
52 | $labase->wexported(1); |
---|
53 | |
---|
54 | sub input_from_handle { |
---|
55 | my ($obj, $fh) = @_; |
---|
56 | my $yaml = join('', <$fh>); |
---|
57 | my $res = $obj->set_c_fields(form => $yaml); |
---|
58 | if($res) { |
---|
59 | print "Changes applied\n"; |
---|
60 | $labase->commit; |
---|
61 | $LA->call_batch_sync; |
---|
62 | } |
---|
63 | return |
---|
64 | $res ? 1 : 0; |
---|
65 | } |
---|
66 | |
---|
67 | if (my $ouid = shift(@ARGV)) { |
---|
68 | my $obj = $labase->get_object('accreq', $ouid) or do { |
---|
69 | die "Object accreq $ouid not found\n"; |
---|
70 | }; |
---|
71 | |
---|
72 | if ($inputfile) { |
---|
73 | my $handle; |
---|
74 | if ($inputfile eq '-') { |
---|
75 | $handle = *STDIN; |
---|
76 | } else { |
---|
77 | open($handle, '<', $inputfile) or die |
---|
78 | "Cannot open input file $@\n"; |
---|
79 | } |
---|
80 | my $res = input_from_handle($obj, $handle); |
---|
81 | close($handle); |
---|
82 | exit(!$res); |
---|
83 | } else { |
---|
84 | exit ! LATMOS::Accounts::Utils::dump_read_temp_file( |
---|
85 | sub { |
---|
86 | my ($fh) = @_; |
---|
87 | print $fh $obj->get_attributes('form'); |
---|
88 | }, |
---|
89 | sub { |
---|
90 | my ($fh) = @_; |
---|
91 | return input_from_handle($obj, $fh); |
---|
92 | } |
---|
93 | ); |
---|
94 | } |
---|
95 | } else { |
---|
96 | pod2usage(); |
---|
97 | } |
---|
98 | |
---|
99 | =head1 FORM FORMAT |
---|
100 | |
---|
101 | =head2 EXAMPLE |
---|
102 | |
---|
103 | --- |
---|
104 | attrs: |
---|
105 | - formtype: textarea |
---|
106 | label: Please explain why your name must changed |
---|
107 | name: reason |
---|
108 | - sn |
---|
109 | - givenName |
---|
110 | description: Change my name |
---|
111 | notifyMail: sysadmin@nomail |
---|
112 | |
---|
113 | =head2 SYNTAX |
---|
114 | |
---|
115 | The form must be describe in YAML format (L<http://en.wikipedia.org/wiki/YAML>). |
---|
116 | |
---|
117 | =head2 OPTIONS |
---|
118 | |
---|
119 | =over 4 |
---|
120 | |
---|
121 | =item description |
---|
122 | |
---|
123 | A short description about this form |
---|
124 | |
---|
125 | =item notifyMail |
---|
126 | |
---|
127 | If set a mail is sent to this address when someone submit the form |
---|
128 | |
---|
129 | =item attrs |
---|
130 | |
---|
131 | The list of attributes the form must contains. See below. |
---|
132 | |
---|
133 | =back |
---|
134 | |
---|
135 | =head2 ATTRIBUTES DESCRIPTION |
---|
136 | |
---|
137 | Each attributes is either: |
---|
138 | |
---|
139 | =over 4 |
---|
140 | |
---|
141 | =item a name |
---|
142 | |
---|
143 | Just the name of an object's attribute the is related (C<sn>, C<givenName> for user objects) |
---|
144 | |
---|
145 | =item an option list |
---|
146 | |
---|
147 | A list of options describing how the attribute must appear: |
---|
148 | |
---|
149 | =over 4 |
---|
150 | |
---|
151 | =item name |
---|
152 | |
---|
153 | The internal short name of attribute |
---|
154 | |
---|
155 | =item label |
---|
156 | |
---|
157 | The text displayed to user |
---|
158 | |
---|
159 | =item formtype |
---|
160 | |
---|
161 | The type of field to diaply (text, textarea, date, checkbox or list) |
---|
162 | |
---|
163 | =back |
---|
164 | |
---|
165 | =back |
---|
166 | |
---|
167 | =cut |
---|