]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Changes.pm
ids_to_dbdump is still needed by the mailbot
[pciids.git] / PciIds / Html / Changes.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Html::Changes;
20 use strict;
21 use PciIds::Html::Users;
22 use PciIds::Html::List;
23 use PciIds::Html::Util;
24 use PciIds::Html::Forms;
25 use PciIds::Notifications;
26 use PciIds::Log;
27 use PciIds::Address;
28 use Apache2::Const qw(:common :http);
29
30 sub genNewItemForm( $$$$$$ ) {
31         my( $req, $args, $auth, $tables, $error, $values ) = @_;
32         my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
33         return NOT_FOUND unless( $ok );
34         genHtmlHead( $req, "Add new item", undef );
35         genCustomHead( $req, $args, $address, "Add new item", [ $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'newitem' ], [ 'ID syntax', 'help', $address->helpName() ] ], [ logItem( $auth ), [ 'Notifications', 'notifications' ] ] );
36         print "<div class='error'>$error</div>\n" if( defined $error );
37         print "<form name='newitem' id='newitem' method='POST' action=''>\n<table>";
38         genFormEx( [ [ 'input', 'ID:', 'text', 'id', 'maxlength="'.$address->subIdSize().'"' ],
39                 [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
40                 [ 'input', 'Note:', 'text', 'note', 'maxlength="1024"' ],
41                 [ 'textarea', 'Discussion:', undef, 'discussion', 'rows="5" cols="50"' ],
42                 [ 'input', 'Subscribe:', 'checkbox', 'subscribe', 'value="subscribe" checked="checked"' ],
43                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
44         print '</table></form>';
45         print '
46 <p>
47         Please enter only accurate information. Descriptions like "Unknown modem device" are only of a little use to anybody.
48         Real chip names and numbers are preferred over marketing names. In case you know both, enclose the marketing name in square brackets like in
49         "3c595 100BaseTX [Vortex]". Do not include names of superitems in the name (like vendor name in device name).
50         Check information specific to this <a href="'.buildExcept( 'action', $args ).'?action=help?help='.$address->helpName().'">ID type</a>.
51 <p>
52         If there is something you want to clarify about the item, you can use note (like the ID does not belong to people using it).
53         Discussion is for things more relevant to history of the item than the real device (like information source).
54         Both note and discussion is optional.';
55         genHtmlFooter( 1, $req, $args );
56         return OK;
57 }
58
59 sub newItemForm( $$$$ ) {
60         my( $req, $args, $tables, $auth ) = @_;
61         if( defined $auth->{'authid'} ) {#Logged in alright
62                 return genNewItemForm( $req, $args, $auth, $tables, undef, {} );
63         } else {
64                 return notLoggedComplaint( $req, $args, $auth );
65         }
66 }
67
68 sub newItemSubmit( $$$$ ) {
69         my( $req, $args, $tables, $auth ) = @_;
70         if( defined $auth->{'authid'} ) {
71                 my( $pok, $parent, $pname, $pnote, $paddress ) = loadItem( $tables, $req->uri() );
72                 return NOT_FOUND unless( $pok );
73                 my( $data, $error ) = getForm( {
74                         'id' => sub{ return ( length shift ) ? undef : 'Please, provide the ID'; }, #Checked at the bottom and added as address
75                         'name' => sub {
76                                 my( $name ) = @_;
77                                 return 'Too short for a name' if( length $name < 3 );
78                                 return 'Lenght limit of the name is 200 characters' if( length $name > 200 );
79                                 return undef;
80                         },
81                         'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
82                         'discussion' => sub { return ( length shift > 1024 ) ? 'Discussion can not be longer than 1024 characters' : undef; },
83                         'subscribe' => undef
84                 }, [ sub { my( $data ) = @_;
85                         my $errstr;
86                         return undef unless( length $data->{'id'} );#No address, so let it for the first check
87                         ( $data->{'address'}, $errstr ) = $paddress->append( $data->{'id'} );
88                         return $errstr;
89                 }, sub { return $paddress->canAddItem() ? undef : 'Can not add items here'; } ] );
90                 return genNewItemForm( $req, $args, $auth, $tables, $error, $data ) if( defined $error );
91                 my( $result, $comName ) = $tables->submitItem( $data, $auth );
92                 if( $result eq 'exists' ) {
93                         genHtmlHead( $req, 'ID collision', undef );
94                         my $addr = PciIds::Address::new( $req->uri() );
95                         genCustomHead( $req, $args, $addr, 'ID collision', [ [ 'Add other item', 'newitem' ], $addr->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), ], [ logItem( $auth ) ] );
96                         print '<p>Sorry, this ID already exists.';
97                         genHtmlFooter( 0, undef, undef );
98                         return OK;
99                 } elsif( $result ) {
100                         return genNewItemForm( $req, $args, $auth, $tables, $result, $data );
101                 }
102                 notify( $tables, $data->{'address'}->parent()->get(), $comName, 2, 0 );#Notify the parent (parent gets new items)
103                 $tables->submitNotification( $auth->{'authid'}, $data->{'address'}->get(), { 'recursive' => 0, 'notification' => 0, 'way' => 0 } ) if( defined $data->{'subscribe'} && $data->{'subscribe'} eq 'subscribe' );
104                 tulog( $auth->{'authid'}, "Item created ".$data->{'address'}->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'note'} )." ".logEscape( $data->{'discussion'} )." $comName" );
105                 return HTTPRedirect( $req, '/read/'.$data->{'address'}->get().'?action=list' );
106         } else {
107                 return notLoggedComplaint( $req, $args, $auth );
108         }
109 }
110
111 sub genNewHistoryForm( $$$$$$ ) {
112         my( $req, $args, $tables, $auth, $error, $values ) = @_;
113         my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
114         return NOT_FOUND unless( $ok );
115         genHtmlHead( $req, "Discuss", undef );
116         genCustomHead( $req, $args, $address, "Discuss", [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), [ 'Help', 'help', 'newhistory' ] ], [ logItem( $auth ),  [ 'Notifications', 'notifications' ] ] );
117         print "<div class='error'>$error</div>\n" if( defined $error );
118         print "<form name='newhistory' id='newhistory' method='POST' action=''>\n<table>";
119         genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
120                 [ 'input', 'Request deletion', 'checkbox', 'delete', 'value="delete"' ],
121                 [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
122                 [ 'input', 'Note:', 'text', 'note', 'maxlength="1024"' ],
123                 !$tables->notifExists( $auth->{'authid'}, $address->get() ) ? [ 'input', 'Subscribe:', 'checkbox', 'subscribe', "value='subscribe' checked='checked'" ] : (),
124                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
125         print '</table></form>';
126         print '
127 <p>
128         Please enter only accurate information. Descriptions like "Unknown modem device" are only of a little use to anybody.
129         Real chip names and numbers are preferred over marketing names. In case you know both, enclose the marketing name in square brackets like in
130         "3c595 100BaseTX [Vortex]". Do not include names of superitems in the name (like vendor name in device name).
131         Check information specific to this <a href="'.buildExcept( 'action', $args ).'?action=help?help='.$address->helpName().'">ID type</a>.
132 <p>
133         You may provide just discussion, request deletion or enter a new name and note.
134         Note is for clarification of the device information, discussion is for reasons, why you change it and like that.
135 <p>
136         You may add discussion note to name change or deletion request too.
137         You must provide at last name or discussion or deletion request.
138 <p>
139         If you provide note, you must provide name too.';
140         genHtmlFooter( 1, $req, $args );
141         return OK;
142 }
143
144 sub newHistoryForm( $$$$ ) {
145         my( $req, $args, $tables, $auth ) = @_;
146         if( defined $auth->{'authid'} ) {
147                 return genNewHistoryForm( $req, $args, $tables, $auth, undef, {} );
148         } else {
149                 return notLoggedComplaint( $req, $args, $auth );
150         }
151 }
152
153 sub newHistorySubmit( $$$$ ) {
154         my( $req, $args, $tables, $auth ) = @_;
155         if( defined $auth->{'authid'} ) {
156                 my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
157                 return NOT_FOUND unless( $ok );
158                 my( $data, $error ) = getForm( {
159                         'name' => sub { return ( length shift > 200 ) ? 'Lenght limit of the name is 200 characters' : undef; },
160                         'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
161                         'text' => sub {
162                                 my( $expl ) = @_;
163                                 return 'Text can not be longer than 1024 characters' if ( length $expl > 1024 );
164                                 return undef;
165                         },
166                         'delete' => sub {
167                                 my( $delete ) = @_;
168                                 return ( undef, '0' ) unless defined $delete;
169                                 return undef if $delete eq 'delete';
170                                 return 'Invalid form value';
171                                 return undef;
172                         },
173                         'subscribe' => undef
174                 }, [ sub { my( $data ) = @_;
175                         return 'You must provide either name, text or request a deletion' if( ! length $data->{'name'} && ! length $data->{'text'} && ! $data->{'delete'} );
176                         return undef;
177                 }, sub { my( $data ) = @_;
178                         return 'You can not set name and request deletion at the same time' if( length $data->{'name'} && $data->{'delete'} );
179                         return undef;
180                 }, sub { my( $data ) = @_;
181                         return 'You must provide name too' if( ( length $data->{'note'} ) && ( ! length $data->{'name'} ) );
182                         return undef;
183                 }, sub { return $address->canDiscuss() ? undef : 'You can not discuss this item'; } ] );
184                 return genNewHistoryForm( $req, $args, $tables, $auth, $error, $data ) if( defined $error );
185                 my $hid = $tables->submitHistory( $data, $auth, $address );
186                 tulog( $auth->{'authid'}, "Discussion created $hid ".$address->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'description'} )." ".logEscape( $data->{'text'} ) );
187                 notify( $tables, $address->get(), $hid, ( defined $name && ( $name ne '' ) ) ? 1 : 0, 1 );
188                 $tables->submitNotification( $auth->{'authid'}, $address->get(), { 'recursive' => 0, 'notification' => 1, 'way' => 0 } ) if( defined $data->{'subscribe'} && $data->{'subscribe'} eq 'subscribe' );
189                 return HTTPRedirect( $req, '/read/'.$address->get().'?action=list' );
190         } else {
191                 return notLoggedComplaint( $req, $args, $auth );
192         }
193 }
194
195 1;