]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Changes.pm
Remove useless directories
[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 Apache2::Const qw(:common :http);
10
11 sub genNewItemForm( $$$$$ ) {
12         my( $req, $args, $tables, $error, $values ) = @_;
13         my( $ok, $parent, $name, $description, $address ) = loadItem( $tables, $req->uri() );
14         return NOT_FOUND unless( $ok );
15         my $prettyAddr = encode( $address->pretty() );
16         genHtmlHead( $req, "$prettyAddr - add new item", undef );
17         print "<h1>$prettyAddr - add new item</h1>\n";
18         print "<div class='error'>$error</div>\n" if( defined $error );
19         print "<form name='newitem' id='newitem' method='POST' action='".setAddrPrefix( $req->uri(), "mods" ).buildExcept( 'action', $args )."?action=newitem'>\n<table>";
20         genFormEx( [ [ 'input', 'Id:', 'text', 'id', 'maxlength="50"' ],
21                 [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
22                 [ 'input', 'Description*:', 'text', 'description', 'maxlength="1024"' ],
23                 [ 'textarea', 'Text*:', undef, 'text', 'rows="5" cols="50"' ],
24                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
25         print '</table></form>';
26         print '<p>Items marked with * are optional.';
27         genHtmlTail();
28         return OK;
29 }
30
31 sub newItemForm( $$$$ ) {
32         my( $req, $args, $tables, $auth ) = @_;
33         if( defined $auth->{'authid'} ) {#Logged in alright
34                 return genNewItemForm( $req, $args, $tables, undef, {} );
35         } else {
36                 return notLoggedComplaint( $req, $args, $auth );
37         }
38 }
39
40 sub newItemSubmit( $$$$ ) {
41         my( $req, $args, $tables, $auth ) = @_;
42         if( defined $auth->{'authid'} ) {
43                 my( $pok, $parent, $pname, $pdescription, $paddress ) = loadItem( $tables, $req->uri() );
44                 return NOT_FOUND unless( $pok );
45                 my( $data, $error ) = getForm( {
46                         'id' => sub{ return ( length shift ) ? undef : 'Please, provide the ID'; }, #Checked at the bottom and added as address
47                         'name' => sub {
48                                 my( $name ) = @_;
49                                 return 'Too short for a name' if( length $name < 3 );
50                                 return 'Lenght limit of the name is 200 characters' if( length $name > 200 );
51                                 return undef;
52                         },
53                         'description' => sub { return ( length shift > 1024 ) ? 'Description can not be longer than 1024 characters' : undef; },
54                         'text' => sub { return ( length shift > 1024 ) ? 'Text can not be longer than 1024 characters' : undef; }
55                 }, [ sub { my( $data ) = @_;
56                         my $errstr;
57                         return undef unless( length $data->{'id'} );#No address, so let it for the first check
58                         ( $data->{'address'}, $errstr ) = $paddress->append( $data->{'id'} );
59                         return $errstr;
60                 }, sub { return $paddress->canAddItem() ? undef : 'Can not add items here'; } ] );
61                 return genNewItemForm( $req, $args, $tables, $error, $data ) if( defined $error );
62                 my( $result, $comName ) = $tables->submitItem( $data, $auth );
63                 if( $result eq 'exists' ) {
64                         genHtmlHead( $req, 'ID collision', undef );
65                         print '<h1>ID collision</h1>';
66                         print '<p>This ID already exists. Have a look <a href="/read/'.$data->{'address'}->get().'?action=list">at it</a>';
67                         genHtmlTail();
68                         return OK;
69                 } elsif( $result ) {
70                         die "Failed to submit new item: $result\n";
71                 }
72                 notify( $tables, $data->{'address'}->get(), $comName, 2, 0 );
73                 tulog( $auth->{'authid'}, "Item created ".$data->{'address'}->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'description'} )." ".logEscape( $data->{'text'} )." $comName" );
74                 return HTTPRedirect( $req, '/read/'.$data->{'address'}->get().'?action=list' );
75         } else {
76                 return notLoggedComplaint( $req, $args, $auth );
77         }
78 }
79
80 sub genNewCommentForm( $$$$$ ) {
81         my( $req, $args, $tables, $error, $values ) = @_;
82         my( $ok, $parent, $name, $description, $address ) = loadItem( $tables, $req->uri() );
83         return NOT_FOUND unless( $ok );
84         my $prettyAddr = encode( $address->pretty() );
85         genHtmlHead( $req, "$prettyAddr - add a comment to discussion", undef );
86         print "<h1>$prettyAddr - add a comment to discussion</h1>\n";
87         print "<div class='error'>$error</div>\n" if( defined $error );
88         print "<form name='newcomment' id='newitem' method='POST' action='".setAddrPrefix( $req->uri(), "mods" ).buildExcept( 'action', $args )."?action=newcomment'>\n<table>";
89         genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
90                 [ 'input', 'Name*:', 'text', 'name', 'maxlength="200"' ],
91                 [ 'input', 'Description*:', 'text', 'description', 'maxlength="1024"' ],
92                 [ 'input', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
93         print '</table></form>';
94         print '<p>Items marked with * are optional, use them only if you want to change the name and description.';
95         print '<p>If you specify description must include name too.';
96         genHtmlTail();
97         return OK;
98 }
99
100 sub newCommentForm( $$$$ ) {
101         my( $req, $args, $tables, $auth ) = @_;
102         if( defined $auth->{'authid'} ) {
103                 return genNewCommentForm( $req, $args, $tables, undef, {} );
104         } else {
105                 return notLoggedComplaint( $req, $args, $auth );
106         }
107 }
108
109 sub newCommentSubmit( $$$$ ) {
110         my( $req, $args, $tables, $auth ) = @_;
111         if( defined $auth->{'authid'} ) {
112                 my( $ok, $parent, $name, $description, $address ) = loadItem( $tables, $req->uri() );
113                 return NOT_FOUND unless( $ok );
114                 my( $data, $error ) = getForm( {
115                         'name' => sub { return ( length shift > 200 ) ? 'Lenght limit of the name is 200 characters' : undef; },
116                         'description' => sub { return ( length shift > 1024 ) ? 'Description can not be longer than 1024 characters' : undef; },
117                         'text' => sub {
118                                 my( $expl ) = @_;
119                                 return 'Text can not be longer than 1024 characters' if ( length $expl > 1024 );
120                                 return 'You must provide the text of comment' unless( length $expl );
121                                 return undef;
122                         }
123                 }, [ sub { my( $data ) = @_;
124                         return 'You must provide name too' if( ( length $data->{'description'} ) && ( ! length $data->{'name'} ) );
125                         return undef;
126                 }, sub { return $address->canAddComment() ? undef : 'You can not discuss this item'; } ] );
127                 return genNewCommentForm( $req, $args, $tables, $error, $data ) if( defined $error );
128                 my $hid = $tables->submitComment( $data, $auth, $address );
129                 tulog( $auth->{'authid'}, "Comment created $hid ".$address->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'description'} )." ".logEscape( $data->{'text'} ) );
130                 notify( $tables, $address->get(), $hid, ( defined $name && ( $name ne '' ) ) ? 1 : 0, 1 );
131                 return HTTPRedirect( $req, '/read/'.$address->get().'?action=list' );
132         } else {
133                 return notLoggedComplaint( $req, $args, $auth );
134         }
135 }
136
137 1;