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