]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Changes.pm
2fa3d2c4a242e04a8e600d8b171b6024c30dfad4
[pciids.git] / PciIds / Html / Changes.pm
1 package PciIds::Html::Changes;
2 use strict;
3 use PciIds::Html::Users;
4 use PciIds::Html::List;
5 use PciIds::Html::Util;
6 use PciIds::Html::Forms;
7 use PciIds::Notifications;
8 use PciIds::Log;
9 use PciIds::Address;
10 use Apache2::Const qw(:common :http);
11
12 sub genNewItemForm( $$$$$$ ) {
13         my( $req, $args, $auth, $tables, $error, $values ) = @_;
14         my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
15         return NOT_FOUND unless( $ok );
16         my $prettyAddr = encode( $address->pretty() );
17         genHtmlHead( $req, "$prettyAddr - add new item", undef );
18         print "<h1>$prettyAddr - add new item</h1>\n";
19         genLocMenu( $req, $args, [ logItem( $auth ), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newitem' ] ] );
20         print "<div class='error'>$error</div>\n" if( defined $error );
21         print "<form name='newitem' id='newitem' method='POST' action=''>\n<table>";
22         genFormEx( [ [ 'input', 'Id:', 'text', 'id', 'maxlength="50"' ],
23                 [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
24                 [ 'input', 'Note*:', 'text', 'note', 'maxlength="1024"' ],
25                 [ 'textarea', 'Discussion*:', undef, 'discussion', 'rows="5" cols="50"' ],
26                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
27         print '</table></form>';
28         print '<p>Items marked with * are optional.';
29         genHtmlTail();
30         return OK;
31 }
32
33 sub newItemForm( $$$$ ) {
34         my( $req, $args, $tables, $auth ) = @_;
35         if( defined $auth->{'authid'} ) {#Logged in alright
36                 return genNewItemForm( $req, $args, $auth, $tables, undef, {} );
37         } else {
38                 return notLoggedComplaint( $req, $args, $auth );
39         }
40 }
41
42 sub newItemSubmit( $$$$ ) {
43         my( $req, $args, $tables, $auth ) = @_;
44         if( defined $auth->{'authid'} ) {
45                 my( $pok, $parent, $pname, $pnote, $paddress ) = loadItem( $tables, $req->uri() );
46                 return NOT_FOUND unless( $pok );
47                 my( $data, $error ) = getForm( {
48                         'id' => sub{ return ( length shift ) ? undef : 'Please, provide the ID'; }, #Checked at the bottom and added as address
49                         'name' => sub {
50                                 my( $name ) = @_;
51                                 return 'Too short for a name' if( length $name < 3 );
52                                 return 'Lenght limit of the name is 200 characters' if( length $name > 200 );
53                                 return undef;
54                         },
55                         'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
56                         'discussion' => sub { return ( length shift > 1024 ) ? 'Discussion can not be longer than 1024 characters' : undef; }
57                 }, [ sub { my( $data ) = @_;
58                         my $errstr;
59                         return undef unless( length $data->{'id'} );#No address, so let it for the first check
60                         ( $data->{'address'}, $errstr ) = $paddress->append( $data->{'id'} );
61                         return $errstr;
62                 }, sub { return $paddress->canAddItem() ? undef : 'Can not add items here'; } ] );
63                 return genNewItemForm( $req, $args, $auth, $tables, $error, $data ) if( defined $error );
64                 my( $result, $comName ) = $tables->submitItem( $data, $auth );
65                 if( $result eq 'exists' ) {
66                         genHtmlHead( $req, 'ID collision', undef );
67                         print '<h1>ID collision</h1>';
68                         my $addr = PciIds::Address::new( $req->uri() );
69                         genCustomMenu( $req, $addr, $args, [ logItem( $auth ), [ 'Add other item', 'newitem' ], $addr->canDiscuss() ? [ 'Discuss', 'newhistory' ] : () ] );
70                         genPath( $req, $data->{'address'}, 1 );
71                         print '<p>Sorry, this ID already exists.';
72                         genHtmlTail();
73                         return OK;
74                 } elsif( $result ) {
75                         die "Failed to submit new item: $result\n";
76                 }
77                 notify( $tables, $data->{'address'}->get(), $comName, 2, 0 );
78                 tulog( $auth->{'authid'}, "Item created ".$data->{'address'}->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'note'} )." ".logEscape( $data->{'discussion'} )." $comName" );
79                 return HTTPRedirect( $req, '/read/'.$data->{'address'}->get().'?action=list' );
80         } else {
81                 return notLoggedComplaint( $req, $args, $auth );
82         }
83 }
84
85 sub genNewHistoryForm( $$$$$$ ) {
86         my( $req, $args, $tables, $auth, $error, $values ) = @_;
87         my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
88         return NOT_FOUND unless( $ok );
89         my $prettyAddr = encode( $address->pretty() );
90         genHtmlHead( $req, "$prettyAddr - discuss", undef );
91         print "<h1>$prettyAddr - discuss</h1>\n";
92         genLocMenu( $req, $args, [ logItem( $auth ), $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newhistory' ] ] );
93         print "<div class='error'>$error</div>\n" if( defined $error );
94         print "<form name='newhistory' id='newhistory' method='POST' action=''>\n<table>";
95         genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
96                 [ 'input', 'Name*:', 'text', 'name', 'maxlength="200"' ],
97                 [ 'input', 'Note*:', 'text', 'note', 'maxlength="1024"' ],
98                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
99         print '</table></form>';
100         print '<p>Items marked with * are optional, use them only if you want to change the name and note.';
101         print '<p>If you specify note, you must include name too.';
102         genHtmlTail();
103         return OK;
104 }
105
106 sub newHistoryForm( $$$$ ) {
107         my( $req, $args, $tables, $auth ) = @_;
108         if( defined $auth->{'authid'} ) {
109                 return genNewHistoryForm( $req, $args, $tables, $auth, undef, {} );
110         } else {
111                 return notLoggedComplaint( $req, $args, $auth );
112         }
113 }
114
115 sub newHistorySubmit( $$$$ ) {
116         my( $req, $args, $tables, $auth ) = @_;
117         if( defined $auth->{'authid'} ) {
118                 my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
119                 return NOT_FOUND unless( $ok );
120                 my( $data, $error ) = getForm( {
121                         'name' => sub { return ( length shift > 200 ) ? 'Lenght limit of the name is 200 characters' : undef; },
122                         'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
123                         'text' => sub {
124                                 my( $expl ) = @_;
125                                 return 'Text can not be longer than 1024 characters' if ( length $expl > 1024 );
126                                 return 'You must provide the text of comment' unless( length $expl );
127                                 return undef;
128                         }
129                 }, [ sub { my( $data ) = @_;
130                         return 'You must provide name too' if( ( length $data->{'note'} ) && ( ! length $data->{'name'} ) );
131                         return undef;
132                 }, sub { return $address->canDiscuss() ? undef : 'You can not discuss this item'; } ] );
133                 return genNewHistoryForm( $req, $args, $tables, $auth, $error, $data ) if( defined $error );
134                 my $hid = $tables->submitHistory( $data, $auth, $address );
135                 tulog( $auth->{'authid'}, "Discussion created $hid ".$address->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'description'} )." ".logEscape( $data->{'text'} ) );
136                 notify( $tables, $address->get(), $hid, ( defined $name && ( $name ne '' ) ) ? 1 : 0, 1 );
137                 return HTTPRedirect( $req, '/read/'.$address->get().'?action=list' );
138         } else {
139                 return notLoggedComplaint( $req, $args, $auth );
140         }
141 }
142
143 1;