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