#!/usr/bin/perl # MimerDesk # Web based groupware and eLearning environment # www.mimerdesk.org # # Copyright (C) 2001 Ionstream Ltd. # www.ionstream.fi # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed with a hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # $Id: resources.html,v 1.9 2002/07/17 14:25:19 inf Exp $ #"""""""""""""""""""""""""""""""""""""""""# # # # MimerDesk: Home - Resources # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # (c) Ionstream Oy 1999 - 2001 # # # # Programmed by: Teemu Arina # # # #_________________________________________# #"""""""""""""""""""""""# # Config # #_______________________# use strict; use vars qw ($APPLICATION $ACTIVEGRP $buttons $USER $IP $LAST_ACT $RESCAT $RESINDEX $FORWARDED $TIME_USED $IDLE $form $ref $sth $GROUPACTIVOR $trans); use lib::MimerDesk; $APPLICATION = 'Resources - Links'; $RESCAT = 'rescat'; $RESINDEX = 'resindex'; sub print_template; sub add_category; sub create_link_columns; sub add_link; sub link_edit; sub link_remove; sub cat_edit; sub cat_remove; sub html_generator; #""""""""""""""""""""# # Program # #____________________# my $admin_status; read_config('../config/mimerdesk.cfg'); initialize(); $form = decode_multipart(); $form->{'ID'} =~ tr/0-9//cd; $form->{'auth'} =~ tr/0-9a-z//cd; ($USER, $IP, $LAST_ACT, $FORWARDED, $TIME_USED, $IDLE, $ACTIVEGRP) = authenticate($form->{'ID'}, $form->{'auth'}, $form->{'changeGroup'}); $trans = lib::MimerDesk->new_gettext(program => 'links',language => $config{'language'}); $APPLICATION = $trans->gettext('Resources - Links'); if ($form->{'category'} || $form->{'search'}) { $buttons = tagged('a',{href => "resources.html?ID=$form->{'ID'}&auth=$form->{'auth'}&grouptool=$form->{'grouptool'}&go=resources", onMouseOver => "doClock();return true", content => image(src => "$config{'loc_pictures'}/showcategories.gif", alt => $trans->gettext("Show categories") )}); } if ($form->{'grouptool'} =~ /^\d+$/) { my ($test); lock_tables('READ', 'groupusers'); db_list("SELECT RIGHTS,USER FROM groupusers where USER = '$USER' and GID = '$form->{'grouptool'}'"); while (my $ref = $sth->fetchrow_hashref()) { $admin_status = $ref->{'RIGHTS'}; $GROUPACTIVOR = $ref->{'USER'}; } db_end(); unlock_tables(); if (!$GROUPACTIVOR) { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); } else { $RESCAT = "$form->{'grouptool'}_$RESCAT"; $RESINDEX = "$form->{'grouptool'}_$RESINDEX"; } } else { lock_tables('READ', 'users'); db_list("SELECT * FROM users where nimi = '$USER'"); while (my $ref = $sth->fetchrow_hashref()) {$admin_status = $ref->{'flags'};} db_end(); unlock_tables(); } if ($form->{'quit'}) {redirect("$config{'loc_server'}$config{'bin_dir'}/index.html?ID=$form->{'ID'}&auth=$form->{'auth'}&quit=quit");} elsif ($admin_status =~ /L0/ && $form->{'mode'}) { if ($form->{'mode'} eq 'link_add') {add_link();} elsif ($form->{'mode'} eq 'cat_add') {add_category();} elsif ($form->{'mode'} eq 'link_remove') {link_remove();} elsif ($form->{'mode'} eq 'cat_remove') {cat_remove();} elsif ($form->{'mode'} eq 'link_edit') {link_edit();} elsif ($form->{'mode'} eq 'cat_edit') {cat_edit();} } elsif ($form->{'search'}) {print_template("$config{'theme'}_resources", 'search', $form->{'keyword'});} elsif ($form->{'category'}) {print_template("$config{'theme'}_resources", 'show_category', $form->{'category'});} else {print_template("$config{'theme'}_resources", 'show_categories');} #""""""""""""""""""""""""""""""""# # Print template # #________________________________# # # 1. Get list of categories and descriptions # 2. Count categories and total number of links # 3. If search is selected... # 3.1 Create SQL search by form->type # 3.2 Get links from database # 3.3 If links found, generate columns # 4. If category is selected... # 4.1 test if category exists # 4.2 Get links from database # 4.3 If links found, generate columns # 4.4 If admin, add buttons # 5. If categories is selected... # 5.1 Get categories from database # 5.2 If categories found, generate columns # 6. Print template sub print_template { my( $ref,$fulltemplate, $template, @category, @desc, %resindex_count, %categories, $search_type, @in_desc, @in_url, @in_link, $count_index, $information, $state, $html ); my ($template, $type ,$parameter) = @_; print_header('pragma'); lock_tables('READ', $RESCAT , $RESINDEX); db_list("SELECT category,description FROM $RESCAT group by category"); while (my $ref = $sth->fetchrow_hashref()) { $ref->{'category'} =~ s/,/\&comma\;/g; $ref->{'description'} =~ s/,/\&comma\;/g; $categories{$ref->{'category'}} = $ref->{'description'}; } db_end(); my $totals = row_count($RESINDEX); my $count_cat = keys %categories; unlock_tables(); if ($type eq 'search') { my $search_category = prepare_fordb($parameter); if ($form->{'type'} eq 'link') {$search_type = "SELECT * FROM $RESINDEX where link_name like '%$search_category%' group by link_name";} elsif ($form->{'type'} eq 'desc') {$search_type = "SELECT * FROM $RESINDEX where description like '%$search_category%' group by link_name";} elsif ($form->{'type'} eq 'url') {$search_type = "SELECT * FROM $RESINDEX where link_url like '%$search_category%' group by link_name";} else {$search_type = "SELECT * FROM $RESINDEX where link_name like '%$search_category%' or description like '%$search_category%' group by link_name";} db_list($search_type); while (my $ref = $sth->fetchrow_hashref()) { $ref->{'description'} =~ s/,/\&comma\;/g; $ref->{'link_url'} =~ s/,/\&comma\;/g; $ref->{'link_name'} =~ s/,/\&comma\;/g; push @in_desc, $ref->{'description'}; push @in_url, $ref->{'link_url'}; push @in_link, $ref->{'link_name'}; } db_end(); unlock_tables(); $count_index = @in_link; $information = "Links found: $count_index"; $state = "Results for search '$parameter'"; if ($count_index > 0) { my ($in_link1, $in_url1, $in_desc1, $in_link, $in_url, $in_desc, $half) = create_link_columns($count_index, \@in_link,\@in_url,\@in_desc); for (my $n = 0;$n < $half;$n++) { if ($form->{'type'} ne 'url') {s/($parameter)/$1<\/U>/gi foreach ($in_link->[$n],$in_link1->[$n],$in_desc->[$n],$in_desc1->[$n]);} my $cat1 = qq!$in_link->[$n]
$in_desc->[$n]

!; my $cat2 = qq!$in_link1->[$n]
$in_desc1->[$n]

!; foreach ("start", "left,$cat1,no", "left,$cat2,no", "end") { my @list = split /,/, $_; s/\&comma\;/,/g foreach (@list); $html .= html_generator(@list); } } } else { my $nothing_message = $trans->gettext('No matches found'); $html .= html_generator(split /,/, $_) foreach ("start", "left,
$nothing_message

,no", "end"); } } elsif ($type eq 'show_category') { if (!(grep {$_ eq $parameter} keys %categories)) { write_log("$USER tried to submit unknown categories!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } my $search_category = prepare_fordb($parameter); lock_tables('READ', $RESINDEX); db_list("SELECT * FROM $RESINDEX where category = '$search_category' group by link_name"); while (my $ref = $sth->fetchrow_hashref()) { $ref->{'description'} =~ s/,/\&comma\;/g; $ref->{'link_url'} =~ s/,/\&comma\;/g; $ref->{'link_name'} =~ s/,/\&comma\;/g; push @in_desc, $ref->{'description'}; push @in_url, $ref->{'link_url'}; push @in_link, $ref->{'link_name'}; } db_end(); unlock_tables(); $count_index = @in_link; $information = $trans->gettext('Links').": $count_index"; $state = $parameter; if ($count_index > 0) { my ($in_link1, $in_url1, $in_desc1, $in_link, $in_url, $in_desc, $half) = create_link_columns($count_index, \@in_link,\@in_url,\@in_desc); for (my $n = 0;$n < $half;$n++) { my $cat1 = qq!$in_link->[$n]
$in_desc->[$n]

!; my $cat2 = qq!$in_link1->[$n]
$in_desc1->[$n]

!; foreach ("start", "left,$cat1,no", "left,$cat2,no", "end") { my @list = split /,/, $_; s/\&comma\;/,/g foreach (@list); $html .= html_generator(@list); } } } else { my $nothing_message = $trans->gettext('No links available in this category'); $html .= html_generator(split /,/, $_) foreach ("start", "left,
$nothing_message

,no", "end"); } if ($admin_status =~ /L0/) { my $category = encodeurl($parameter); $buttons .= qq[ ].
			            $trans->gettext(]; $buttons .= qq[ ].
			            $trans->gettext(]; $buttons .= qq[ ].
			            $trans->gettext(]; } } elsif ($type eq 'show_categories') { $state = $trans->gettext('Select a category'); $information = $trans->gettext('Categories').": $count_cat ".$trans->gettext('Links').": $totals"; if ($count_cat == 0) { $html .= html_generator(split /,/) foreach ("start", "left,
".$trans->gettext("No categories available.")."

,no", "end"); } else { lock_tables('READ', $RESCAT,$RESINDEX); db_list("SELECT $RESCAT.category,$RESCAT.description,COUNT($RESINDEX.category) as count FROM $RESCAT LEFT JOIN $RESINDEX on $RESINDEX.category = $RESCAT.category group by $RESCAT.category"); while (my $ref = $sth->fetchrow_hashref()) { $ref->{'category'} =~ s/,/\&comma\;/g; $ref->{'description'} =~ s/,/\&comma\;/g; push @category, $ref->{'category'}; push @desc, $ref->{'description'}; $resindex_count{$ref->{'category'}} = $ref->{'count'}; } db_end(); unlock_tables(); my $half = int($count_cat / 2); my ($addit, $reduc); if (($count_cat % 2) == 0) {$addit = '0';$reduc = '1';} else {$addit = '1';$reduc = '0';} my @category2 = @category[($half+$addit)..$count_cat]; my @desc2 = @desc[($half+$addit)..$count_cat]; my @category3 = @category[0..($half-$reduc)]; my @desc3 = @desc[0..($half-$reduc)]; $half++ if $addit == '1'; $information = $trans->gettext('Categories').": $count_cat ".$trans->gettext('Links').": $totals"; for (my $n = 0;$n < $half;$n++) { my $category2 = encodeurl($category2[$n]); my $category3 = encodeurl($category3[$n]); my $cat1 = "{'ID'}&grouptool=$form->{'grouptool'}&auth=$form->{'auth'}&category=$category3\" onMouseOver=\"doClock(); return true\" class=\"secTitleLink\">$category3[$n]   [ $resindex_count{$category3[$n]} ]
$desc3[$n]

"; my $cat2 = "{'ID'}&grouptool=$form->{'grouptool'}&auth=$form->{'auth'}&category=$category2\" onMouseOver=\"doClock(); return true\" class=\"secTitleLink\">$category2[$n]   [ $resindex_count{$category2[$n]} ]
$desc2[$n]

"; if (!$category2[$n]) {$cat2 = '';} foreach ("start", "left,$cat1,no", "left,$cat2,no", "end") { my @list = split /,/; s/\&comma\;/,/g foreach (@list); $html .= html_generator(@list); } } } if ($admin_status =~ /L0/) { $parameter = encodeurl($parameter); $buttons .= " {'grouptool'}&category=$parameter\',600,350,'Links')\" onMouseOver=\"doClock(); return true\">\"".gettext("Add new category")."\" border=\"0\">"; $buttons .= " {'grouptool'}&category=$parameter\',600,500,'Links')\" onMouseOver=\"doClock(); return true\">\"".gettext("Remove a category")."\" border=\"0\">"; $buttons .= " {'grouptool'}&category=$parameter\',600,350,'Links')\" onMouseOver=\"doClock(); return true\">\"".gettext("Edit a category")."\" border=\"0\">"; } } $ref = get_template('maintemplate',$template); $ref->{'maintemplate'} =~ s/<>/$ref->{$template}/m; $fulltemplate = $ref->{'maintemplate'}; $ref = get_template('js_doClock', 'js_help', 'js_resources', 'js_gotosite'); my $javascripts = $ref->{'js_doClock'}.$ref->{'js_help'}.$ref->{'js_resources'}.$ref->{'js_gotosite'}; $fulltemplate =~ s/<>/minea/gm; $fulltemplate =~ s/<>/$javascripts/m; $fulltemplate =~ s/<>/MimerDesk\: $APPLICATION/m; $fulltemplate = replace_tags($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $fulltemplate = add_popups($fulltemplate, $USER, $form->{'auth'}, $form->{'ID'}); if ($GROUPACTIVOR) {$fulltemplate = create_buttons($fulltemplate, 'Group', 'Links', $form);} else {$fulltemplate = create_buttons($fulltemplate, 'Resources', 'Links', $form);} my ($dropdown); lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT group by category"); while (my $ref = $sth->fetchrow_hashref()) { if ($ref->{'category'} eq $form->{'category'}) {$dropdown .= "\n";} else {$dropdown .= "\n";} } db_end(); unlock_tables(); $fulltemplate =~ s/<>/$dropdown/m; $fulltemplate =~ s/<>/$state/m; $fulltemplate =~ s/<>/$buttons/m; $fulltemplate =~ s/<>/$html/m; $fulltemplate =~ s/<>/index.html/m; $fulltemplate =~ s/<>/$information/m; $fulltemplate =~ s/<>/$form->{'grouptool'}/mg; print $fulltemplate; db_end('disconnect'); exit; } #""""""""""""""""""""""""""""""""""# # Create link columns # #----------------------------------# # Creates two columns, sorts # # horizontally # # # #__________________________________# sub create_link_columns { my ($reduc, $addit); my ($count_index, $in_link, $in_url, $in_desc) = @_; my $half = int($count_index / 2); if (!($count_index % 2)) {$addit = '0';$reduc = '1';} else {$addit = '1';$reduc = '0';} my @in_link1 = @$in_link[($half+$addit)..$count_index]; my @in_desc1 = @$in_desc[($half+$addit)..$count_index]; my @in_url1 = @$in_url[($half+$addit)..$count_index]; my @in_link = @$in_link[0..($half-$reduc)]; my @in_desc = @$in_desc[0..($half-$reduc)]; my @in_url = @$in_url[0..$half]; $half++ if $addit == '1'; return (\@in_link1,\@in_url1,\@in_desc1,\@in_link,\@in_url,\@in_desc, $half); } #""""""""""# # ADD link # #__________# # # 1. Remove tags from link # 2. If add pressed... # 2.1 Check link # 2.2 Prepare link for database # 2.3 Check if link exists # 2.4 Add link to database # 3. Print template sub add_link { my($num, $message, $ref, @category2, $onload); print_header('pragma'); $form->{'link_name'} =~ s/<.*?>//gm; $form->{'link_desc'} =~ s/<.*?>//gm; $form->{'link_url'} =~ s/<.*?>//gm; if ($form->{'add'}) { my $test = $form->{'link_name'}; my $test2 = $form->{'link_url'}; $test2 =~ s/ //g; $test =~ s/ //g; if ($form->{'link_url'} !~ /:\/\//) {$form->{'link_url'} =~ s/(.*)/http:\/\/$1/;} if ($form->{'link_name'} eq '' || $test eq '') {$message = "You must specify a link name!";} elsif ($form->{'link_url'} eq '' || $test2 eq '') {$message = "You must specify a link URL!";} else { ($form->{'link_name'},$form->{'link_desc'},$form->{'link_url'},$form->{'category'}) = prepare_fordb($form->{'link_name'},$form->{'link_desc'},$form->{'link_url'},$form->{'category'}); lock_tables('READ', $RESINDEX); db_list("SELECT link_name FROM $RESINDEX where link_name = '$form->{'link_name'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) {$message = $trans->gettext("ERROR: Link already exists.");} else { lock_tables('WRITE', $RESINDEX); db_list("insert into $RESINDEX (number, category, link_url, link_name, description) values ('NULL', '$form->{'category'}', '$form->{'link_url'}', '$form->{'link_name'}', '$form->{'link_desc'}')"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Link %s has been successfully added."),tagged('i',{content => $form->{'link_name'}})); $onload = "top.opener.location.reload();"; write_log("$USER added link named $form->{'link_name'}", 'notify'); } } } $ref = get_template('add_link'); $ref->{'add_link'} = replace_tags($ref->{'add_link'}, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $ref->{'add_link'} =~ s/<>/$message/m; $ref->{'add_link'} =~ s/<>/$form->{'category'}/m; $ref->{'add_link'} =~ s/<>/$onload/m; $ref->{'add_link'} =~ s/<>/$form->{'grouptool'}/mg; print $ref->{'add_link'}; db_end('disconnect'); exit; } #"""""""""""""# # Remove link # #_____________# # # 1. If link selected... # 1.1 Check if link exists # 1.2 Remove link # 2. Generate link selection # 3. Print template sub link_remove { my($num, $message, $ref, $onload, $rivi, $template); print_header('pragma'); ($form->{'category'}, $form->{'links'}) = prepare_fordb($form->{'category'}, $form->{'links'}); lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT where category = '$form->{'category'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) { if ($form->{'remove'}) { if ($form->{'links'} eq '') {$message = $trans->gettext("ERROR: You must select a link before trying to remove anything!");} else { lock_tables('READ', $RESINDEX); db_list("SELECT link_name FROM $RESINDEX where link_name='$form->{'links'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) { lock_tables('WRITE', $RESINDEX); db_list("delete from $RESINDEX where link_name='$form->{'links'}'"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Link %s has been successfully removed."),tagged('i',{content => $form->{'links'}})); $onload = "top.opener.location.reload();"; write_log("$USER removed link named $form->{'link_name'}", 'notify'); } else { write_log("$USER tried to remove unknown links!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } } } } else { write_log("$USER tried to submit unknown categories!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } $ref = get_template('remove_link'); $template = $ref->{'remove_link'}; $template = replace_tags($template, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); lock_tables('READ', $RESINDEX); db_list("SELECT link_name,category FROM $RESINDEX where category='$form->{'category'}' group by link_name"); while (my $ref = $sth->fetchrow_hashref()) {$rivi .= "\n";} db_end(); unlock_tables(); $template =~ s/<>/$rivi/m; $template =~ s/<>/$message/m; $template =~ s/<>/$form->{'category'}/m; $template =~ s/<>/$onload/m; $template =~ s/<>/$form->{'grouptool'}/mg; print $template; db_end('disconnect'); exit; } #"""""""""""# # Edit link # #___________# # # 1. If edit button pressed... # 1.1 Check if link selected # 1.2 Check if link exists # 1.3 Check if new link name exists # 1.4 Add link # 2. Generate link selection # 3. Print template sub link_edit { my($num, $message, $ref, $onload, $template, $rivi); print_header('pragma'); $form->{'link_name'} =~ s/<.*?>//gm; $form->{'link_desc'} =~ s/<.*?>//gm; $form->{'link_url'} =~ s/<.*?>//gm; ($form->{'category'}, $form->{'links'}, $form->{'link_name'}, $form->{'link_url'}, $form->{'link_desc'}) = prepare_fordb($form->{'category'}, $form->{'links'}, $form->{'link_name'}, $form->{'link_url'}, $form->{'link_desc'}); if ($form->{'edit_link'}) { if ($form->{'links'} eq '') {$message = $trans->gettext("ERROR: You must select a link before trying to edit anything!");} else { if ($form->{'link_url'} !~ /:\/\//) {$form->{'link_url'} =~ s/(.*)/http:\/\/$1/;} lock_tables('READ', $RESINDEX); db_list("SELECT link_name FROM $RESINDEX where link_name='$form->{'links'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) { lock_tables('READ', $RESCAT , $RESINDEX); db_list("SELECT link_name FROM $RESINDEX where link_name='$form->{'link_name'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0 && $form->{'link_name'} ne $form->{'links'}) {$message = $trans->gettext("ERROR: Link already exists.");} else { lock_tables('WRITE', $RESINDEX); db_list("update $RESINDEX set link_name = '$form->{'link_name'}', link_url = '$form->{'link_url'}', description = '$form->{'link_desc'}' where link_name = '$form->{'links'}'"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Link %s has been successfully edited."),tagged('i',{content => $form->{'links'}})); $onload = "top.opener.location.reload();"; write_log("$USER edited link named $form->{'links'}", 'notify'); } } else { write_log("$USER tried to edit unknown links!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } } } $ref = get_template('edit_link'); $template = $ref->{'edit_link'}; $template = replace_tags($template, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); lock_tables('READ', $RESINDEX); db_list("SELECT link_name,category FROM $RESINDEX where category='$form->{'category'}' group by link_name"); while (my $ref = $sth->fetchrow_hashref()) { if ($ref->{'link_name'} eq $form->{'links'}) {$rivi .= "\n";} else {$rivi .= "\n";} } db_end(); $template =~ s/<>/$rivi/m; db_list("SELECT link_name,link_url,description FROM $RESINDEX where link_name='$form->{'links'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); $template =~ s/<>/$ref->{'link_name'}/m; $template =~ s/<>/$ref->{'link_url'}/m; $template =~ s/<>/$ref->{'description'}/m; $template =~ s/<>/$message/m; $template =~ s/<>/$form->{'category'}/m; $template =~ s/<>/$form->{'grouptool'}/mg; $template =~ s/<>/$onload/m; print $template; db_end('disconnect'); exit; } #"""""""""""""""# # Edit category # #_______________# # # 1. If edit category pressed... # 1.1 Check if a category is selected # 1.2 Check if category exists # 1.3 Check if new category name exists # 1.4 Add category # 2. Generate category selection # 3. Print template sub cat_edit { my($num, $message, $ref, $onload, $template, $rivi); print_header('pragma'); $form->{'cat_name'} =~ s/<.*?>//g; $form->{'cat_desc'} =~ s/<.*?>//g; ($form->{'category'}, $form->{'links'}, $form->{'cat_name'}, $form->{'cat_desc'}) = prepare_fordb($form->{'category'}, $form->{'links'}, $form->{'cat_name'}, $form->{'cat_desc'}); if ($form->{'edit_cat'}) { if ($form->{'links'} eq '') {$message = $trans->gettext("ERROR: You must select a category before trying to edit anything!");} else { lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT where category = '$form->{'links'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) { lock_tables('READ', $RESCAT , $RESINDEX); db_list("SELECT category FROM $RESCAT where category = '$form->{'cat_name'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0 && $form->{'cat_name'} ne $form->{'links'}) {$message = $trans->gettext("ERROR: Category already exists.");} else { lock_tables('WRITE', $RESCAT, $RESINDEX); db_list("update $RESCAT set category = '$form->{'cat_name'}', description = '$form->{'cat_desc'}' where category = '$form->{'links'}'"); db_end(); db_list("update $RESINDEX set category = '$form->{'cat_name'}' where category = '$form->{'links'}'"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Category %s has been successfully edited."),tagged('i',{content => $form->{'links'}})); $onload = "top.opener.location.reload();"; write_log("$USER edited category named $form->{'links'}", 'notify'); } } else { write_log("$USER tried to edit an unknown category!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } } } $ref = get_template('edit_category'); $template = $ref->{'edit_category'}; $template = replace_tags($template, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT group by category"); while (my $ref = $sth->fetchrow_hashref()) { if ($ref->{'category'} eq $form->{'links'}) {$rivi .= "\n";} else {$rivi .= "\n";} } db_end(); $template =~ s/<>/$rivi/m; db_list("SELECT category,description FROM $RESCAT where category='$form->{'links'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); $template =~ s/<>/$ref->{'category'}/m; $template =~ s/<>/$ref->{'description'}/m; $template =~ s/<>/$message/m; $template =~ s/<>/$form->{'grouptool'}/mg; $template =~ s/<>/$onload/m; print $template; db_end('disconnect'); exit; } #"""""""""""""""""""# # Remove a category # #___________________# # # 1. If remove pressed... # 1.1 Check if a category is selected # 1.2 Check if category exists # 1.3 Remove category # 2. Generate category selection # 3. Print template sub cat_remove { my($num, $message, $ref, $onload, $template, $rivi); print_header('pragma'); if ($form->{'remove'}) { ($form->{'categories'}) = prepare_fordb($form->{'categories'}); if ($form->{'categories'} eq '') {$message = $trans->gettext("ERROR: You must select a category before trying to remove anything!");} else { lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT where category = '$form->{'categories'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) { lock_tables('WRITE', $RESCAT, $RESINDEX); db_list("delete from $RESCAT where category='$form->{'categories'}'"); db_end(); db_list("delete from $RESINDEX where category='$form->{'categories'}'"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Category %s has been successfully removed."),tagged('i',{content => $form->{'categories'}})); $onload = "top.opener.location.reload();"; write_log("$USER removed category named $form->{'categories'}", 'notify'); } else { write_log("$USER tried to remove unknown categories!", 'warning'); print $trans->gettext("Don't even try it!"); exit; } } } $ref = get_template('remove_category'); $template = $ref->{'remove_category'}; $template = replace_tags($template, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); lock_tables('READ', $RESCAT); db_list("SELECT category FROM $RESCAT group by category"); while (my $ref = $sth->fetchrow_hashref()) {$rivi .= "\n";} db_end(); unlock_tables(); $template =~ s/<>/$rivi/g; $template =~ s/<>/$form->{'grouptool'}/mg; $template =~ s/<>/$message/m; $template =~ s/<>/$onload/m; print $template; db_end('disconnect'); exit; } #""""""""""""""""# # ADD a category # #________________# # # 1. If add button pressed... # 1.1 Check if category exists # 1.2 Add category # 2. Print template sub add_category { my($num, $message, $onload, $template); print_header('pragma'); $form->{'cat_name'} =~ s/<.*?>//gm; $form->{'cat_desc'} =~ s/<.*?>//gm; ($form->{'category'}, $form->{'links'}, $form->{'cat_name'}, $form->{'cat_desc'}) = prepare_fordb($form->{'category'}, $form->{'links'}, $form->{'cat_name'}, $form->{'cat_desc'}); if ($form->{'add'}) { my $test = $form->{'cat_name'}; $test =~ s/ //g; if ($form->{'cat_name'} eq '' || $test eq '') {$message = $trans->gettext("You must specify a category name!");} else { lock_tables('READ', $RESCAT , $RESINDEX); db_list("SELECT category FROM $RESCAT where category = '$form->{'cat_name'}'"); $ref = $sth->fetchrow_hashref(); db_end(); unlock_tables(); if (keys %$ref > 0) {$message = $trans->gettext("ERROR: Category already exists.");} else { lock_tables('WRITE', $RESCAT); db_list("insert into $RESCAT (category, description) values ('$form->{'cat_name'}', '$form->{'cat_desc'}')"); db_end(); unlock_tables(); $message = sprintf($trans->gettext("Category %s has been successfully added."),tagged('i',{content => $form->{'cat_name'}})); $onload = "parentReload();"; write_log("$USER added category named $form->{'cat_name'}", 'notify'); } } } $ref = get_template('add_category'); $template = $ref->{'add_category'}; $template = replace_tags($template, $USER, $form->{'auth'}, $form->{'ID'}, $TIME_USED); $template =~ s/<>/$message/m; $template =~ s/<>/$onload/m; $template =~ s/<>/$form->{'grouptool'}/mg; print $template; db_end('disconnect'); exit; } ################## # HTML generator # ################## sub html_generator { my ($white_space); if ($_[0] eq 'start'){return "\n";} elsif ($_[0] eq 'end'){return "\n";} else {$white_space = "$_[1]";} return "$white_space\n"; }