1 package PciIds::Html::Changes;
3 use PciIds::Html::Users;
4 use PciIds::Html::List;
5 use PciIds::Html::Util;
6 use PciIds::Html::Forms;
7 use PciIds::Notifications;
9 use Apache2::Const qw(:common :http);
11 sub genNewItemForm( $$$$$ ) {
12 my( $req, $args, $tables, $error, $values ) = @_;
13 my( $ok, $parent, $name, $note, $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=''>\n<table>";
20 genFormEx( [ [ 'input', 'Id:', 'text', 'id', 'maxlength="50"' ],
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', '', 'submit', 'submit', 'value="Submit"' ] ], $values );
25 print '</table></form>';
26 print '<p>Items marked with * are optional.';
31 sub newItemForm( $$$$ ) {
32 my( $req, $args, $tables, $auth ) = @_;
33 if( defined $auth->{'authid'} ) {#Logged in alright
34 return genNewItemForm( $req, $args, $tables, undef, {} );
36 return notLoggedComplaint( $req, $args, $auth );
40 sub newItemSubmit( $$$$ ) {
41 my( $req, $args, $tables, $auth ) = @_;
42 if( defined $auth->{'authid'} ) {
43 my( $pok, $parent, $pname, $pnote, $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
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 );
53 'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
54 'discussion' => sub { return ( length shift > 1024 ) ? 'Discussion can not be longer than 1024 characters' : undef; }
55 }, [ sub { my( $data ) = @_;
57 return undef unless( length $data->{'id'} );#No address, so let it for the first check
58 ( $data->{'address'}, $errstr ) = $paddress->append( $data->{'id'} );
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>';
70 die "Failed to submit new item: $result\n";
72 notify( $tables, $data->{'address'}->get(), $comName, 2, 0 );
73 tulog( $auth->{'authid'}, "Item created ".$data->{'address'}->get()." ".logEscape( $data->{'name'} )." ".logEscape( $data->{'note'} )." ".logEscape( $data->{'discussion'} )." $comName" );
74 return HTTPRedirect( $req, '/read/'.$data->{'address'}->get().'?action=list' );
76 return notLoggedComplaint( $req, $args, $auth );
80 sub genNewHistoryForm( $$$$$ ) {
81 my( $req, $args, $tables, $error, $values ) = @_;
82 my( $ok, $parent, $name, $note, $address ) = loadItem( $tables, $req->uri() );
83 return NOT_FOUND unless( $ok );
84 my $prettyAddr = encode( $address->pretty() );
85 genHtmlHead( $req, "$prettyAddr - discuss", undef );
86 print "<h1>$prettyAddr - discuss</h1>\n";
87 print "<div class='error'>$error</div>\n" if( defined $error );
88 print "<form name='newhistory' id='newhistory' method='POST' action=''>\n<table>";
89 genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
90 [ 'input', 'Name*:', 'text', 'name', 'maxlength="200"' ],
91 [ 'input', 'Note*:', 'text', 'note', '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 note.';
95 print '<p>If you specify note, you must include name too.';
100 sub newHistoryForm( $$$$ ) {
101 my( $req, $args, $tables, $auth ) = @_;
102 if( defined $auth->{'authid'} ) {
103 return genNewHistoryForm( $req, $args, $tables, undef, {} );
105 return notLoggedComplaint( $req, $args, $auth );
109 sub newHistorySubmit( $$$$ ) {
110 my( $req, $args, $tables, $auth ) = @_;
111 if( defined $auth->{'authid'} ) {
112 my( $ok, $parent, $name, $note, $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 'note' => sub { return ( length shift > 1024 ) ? 'Note can not be longer than 1024 characters' : undef; },
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 );
123 }, [ sub { my( $data ) = @_;
124 return 'You must provide name too' if( ( length $data->{'note'} ) && ( ! length $data->{'name'} ) );
126 }, sub { return $address->canDiscuss() ? undef : 'You can not discuss this item'; } ] );
127 return genNewHistoryForm( $req, $args, $tables, $error, $data ) if( defined $error );
128 my $hid = $tables->submitHistory( $data, $auth, $address );
129 tulog( $auth->{'authid'}, "Discussion 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' );
133 return notLoggedComplaint( $req, $args, $auth );