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