]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Changes.pm
fe50ec7a67504f27c7f5ab8c62af761dd7ae3fdc
[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         genHtmlHead( $req, "Add new item", undef );
17         genCustomHead( $req, $args, $address, "Add new item", [ $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'newitem' ], [ 'ID syntax', 'help', $address->helpName() ], [ '', 'jump' ] ], [ logItem( $auth ), [ 'Notifications', 'notifications' ] ] );
18         print "<div class='error'>$error</div>\n" if( defined $error );
19         print "<form name='newitem' id='newitem' method='POST' action=''>\n<table>";
20         genFormEx( [ [ 'input', 'ID:', 'text', 'id', 'maxlength="'.$address->subIdSize().'"' ],
21                 [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
22                 [ 'input', 'Note:', 'text', 'note', 'maxlength="1024"' ],
23                 [ 'textarea', 'Discussion:', undef, 'discussion', 'rows="5" cols="50"' ],
24                 [ 'input', 'Subscribe:', 'checkbox', 'subscribe', 'value="subscribe" checked="checked"' ],
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                         'subscribe' => undef
66                 }, [ sub { my( $data ) = @_;
67                         my $errstr;
68                         return undef unless( length $data->{'id'} );#No address, so let it for the first check
69                         ( $data->{'address'}, $errstr ) = $paddress->append( $data->{'id'} );
70                         return $errstr;
71                 }, sub { return $paddress->canAddItem() ? undef : 'Can not add items here'; } ] );
72                 return genNewItemForm( $req, $args, $auth, $tables, $error, $data ) if( defined $error );
73                 my( $result, $comName ) = $tables->submitItem( $data, $auth );
74                 if( $result eq 'exists' ) {
75                         genHtmlHead( $req, 'ID collision', undef );
76                         my $addr = PciIds::Address::new( $req->uri() );
77                         genCustomHead( $req, $args, $addr, 'ID collision', [ [ 'Add other item', 'newitem' ], $addr->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ '', 'jump' ] ], [ logItem( $auth ) ] );
78                         print '<p>Sorry, this ID already exists.';
79                         genHtmlTail();
80                         return OK;
81                 } elsif( $result ) {
82                         return genNewItemForm( $req, $args, $auth, $tables, $result, $data );
83                 }
84                 notify( $tables, $data->{'address'}->parent()->get(), $comName, 2, 0 );#Notify the parent (parent gets new items)
85                 $tables->submitNotification( $auth->{'authid'}, $data->{'address'}->get(), { 'recursive' => 0, 'notification' => 1, 'way' => 0 } ) if( defined $data->{'subscribe'} && $data->{'subscribe'} eq 'subscribe' );
86                 tulog( $auth->{'authid'}, "Item created ".$data->{'address'}->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'note'} )." ".logEscape( $data->{'discussion'} )." $comName" );
87                 return HTTPRedirect( $req, '/read/'.$data->{'address'}->get().'?action=list' );
88         } else {
89                 return notLoggedComplaint( $req, $args, $auth );
90         }
91 }
92
93 sub genNewHistoryForm( $$$$$$ ) {
94         my( $req, $args, $tables, $auth, $error, $values ) = @_;
95         my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
96         return NOT_FOUND unless( $ok );
97         genHtmlHead( $req, "Discuss", undef );
98         genCustomHead( $req, $args, $address, "Discuss", [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), [ 'Help', 'help', 'newhistory' ], [ '', 'jump' ] ], [ logItem( $auth ),  [ 'Notifications', 'notifications' ] ] );
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                 !$tables->notifExists( $auth->{'authid'}, $address->get() ) ? [ 'input', 'Subscribe:', 'checkbox', 'subscribe', "value='subscribe' checked='checked'" ] : (),
106                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
107         print '</table></form>';
108         genHtmlTail();
109         return OK;
110 }
111
112 sub newHistoryForm( $$$$ ) {
113         my( $req, $args, $tables, $auth ) = @_;
114         if( defined $auth->{'authid'} ) {
115                 return genNewHistoryForm( $req, $args, $tables, $auth, undef, {} );
116         } else {
117                 return notLoggedComplaint( $req, $args, $auth );
118         }
119 }
120
121 sub newHistorySubmit( $$$$ ) {
122         my( $req, $args, $tables, $auth ) = @_;
123         if( defined $auth->{'authid'} ) {
124                 my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
125                 return NOT_FOUND unless( $ok );
126                 my( $data, $error ) = getForm( {
127                         'name' => sub { return ( length shift > 200 ) ? 'Lenght limit of the name is 200 characters' : undef; },
128                         'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
129                         'text' => sub {
130                                 my( $expl ) = @_;
131                                 return 'Text can not be longer than 1024 characters' if ( length $expl > 1024 );
132                                 return undef;
133                         },
134                         'delete' => sub {
135                                 my( $delete ) = @_;
136                                 return ( undef, '0' ) unless defined $delete;
137                                 return undef if $delete eq 'delete';
138                                 return 'Invalid form value';
139                                 return undef;
140                         },
141                         'subscribe' => undef
142                 }, [ sub { my( $data ) = @_;
143                         return 'You must provide either name, text or request a deletion' if( ! length $data->{'name'} && ! length $data->{'text'} && ! $data->{'delete'} );
144                         return undef;
145                 }, sub { my( $data ) = @_;
146                         return 'You can not set name and request deletion at the same time' if( length $data->{'name'} && $data->{'delete'} );
147                         return undef;
148                 }, sub { my( $data ) = @_;
149                         return 'You must provide name too' if( ( length $data->{'note'} ) && ( ! length $data->{'name'} ) );
150                         return undef;
151                 }, sub { return $address->canDiscuss() ? undef : 'You can not discuss this item'; } ] );
152                 return genNewHistoryForm( $req, $args, $tables, $auth, $error, $data ) if( defined $error );
153                 my $hid = $tables->submitHistory( $data, $auth, $address );
154                 tulog( $auth->{'authid'}, "Discussion created $hid ".$address->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'description'} )." ".logEscape( $data->{'text'} ) );
155                 notify( $tables, $address->get(), $hid, ( defined $name && ( $name ne '' ) ) ? 1 : 0, 1 );
156                 $tables->submitNotification( $auth->{'authid'}, $address->get(), { 'recursive' => 0, 'notification' => 1, 'way' => 0 } ) if( defined $data->{'subscribe'} && $data->{'subscribe'} eq 'subscribe' );
157                 return HTTPRedirect( $req, '/read/'.$address->get().'?action=list' );
158         } else {
159                 return notLoggedComplaint( $req, $args, $auth );
160         }
161 }
162
163 1;