#! /usr/bin/perl -w # History: Theory and Culture # newslets.cgi: The newsletter administration. # Copyright (c) 2006-2021 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Author: imacat # First written: 2006-04-28 use 5.008; use strict; use warnings; use lib $ENV{"DOCUMENT_ROOT"} . qw(/magicat/lib/perl5); use Selima::htc; local $SIG{"__DIE__"} = \&http_500; my $d = new Selima::Destroy; # Prototype declaration sub main(); sub check_get(); sub check_post(); sub html_page($); sub fetch_curitem(); sub fetch_index($$$); use Date::Parse qw(str2time); initenv(-restricted => 1, -this_table => "newslets", -dbi_lock => {"newslets" => LOCK_EX, "nlindex" => LOCK_EX, "nlarts" => LOCK_SH}, -lastmod => 1, -page_param => {"keywords" => N_("newsletters")}); main; exit 0; sub main() { local ($_, %_); my ($error, $success, $processor); # If the request is a GET query if ($ENV{"REQUEST_METHOD"} ne "POST") { $error = check_get; # If an error occurs if (defined $error) { html_page $error; # Display the page } else { html_page retrieve_status; } # If a form was POSTed from the client } else { $error = check_post; # If an error occurs if (defined $error) { error_redirect $error; # Else, save the data } else { $processor = new Selima::htc::Processor::Newslet($POST); $success = $processor->process; success_redirect $success; } } return; } # check_get: Check the GET arguments sub check_get() { local ($_, %_); my $error; # A form is requested if (is_form) { $_ = form_type; # A form to create a new item if ($_ eq "new") { # A form to edit a current item } elsif ($_ eq "cur") { # Check at fetch_curitem() $error = fetch_curitem; return $error if defined $error; # A form to delete a current item } elsif ($_ eq "del") { # Check at fetch_curitem() $error = fetch_curitem; return $error if defined $error; # A form to preview a submitted item } elsif ($_ eq "preview") { my ($sql, $sth, $count, $row, @allno); # Check at fetch_preview() $error = fetch_preview; return $error if defined $error; $PREVIEW{"path"} = sprintf "/newsletters/%03d/", $PREVIEW{"no"}; $PREVIEW{"date"} = str2time $PREVIEW{"date"}; $PREVIEW{"title"} = newslet_textno($PREVIEW{"no"}) . " " . $PREVIEW{"title"}; # Obtain all the pages @_ = qw(); push @_, "sn!=" . $PREVIEW{"sn"} if exists $PREVIEW{"sn"}; push @_, "NOT hid"; $sql = "SELECT no FROM newslets" . " WHERE " . join(" AND ", @_) . " ORDER BY no;\n"; $sth = $DBH->prepare($sql); $sth->execute; $count = $sth->rows; for (my $i = 0, @allno = qw(); $i < $count; $i++) { push @allno, ${$sth->fetch}[0]; } undef $sth; # Insert this page for ($_ = 0; $_ < @allno; $_++) { last if $PREVIEW{"no"} < $allno[$_]; } @allno = ( @allno[0..$_-1], $PREVIEW{"no"}, @allno[$_..$#allno]); $PREVIEW{"allno"} = [@allno]; # Not a valid form } else { return {"msg"=>N_("Incorrect form: [_1]."), "margs"=>[$_], "isform"=>0}; } } # List handler handles its own error # OK return; } # check_post: Check the POSTed form sub check_post() { local ($_, %_); my ($checker, $error); $_ = form_type; # A form to create a new item if ($_ eq "new") { # Run the checker $checker = new Selima::htc::Checker::Newslet(curform); $checker->redir(qw(selndxart delndxart)); $error = $checker->check(qw(no date title credits kw)); return $error if defined $error; # A form to edit a current item } elsif ($_ eq "cur") { # Check at fetch_curitem() $error = fetch_curitem; return $error if defined $error; # Run the checker $checker = new Selima::htc::Checker::Newslet(curform); $checker->redir(qw(del selndxart delndxart)); $error = $checker->check(qw(no date title credits kw)); return $error if defined $error; # A form to delete a current item } elsif ($_ eq "del") { # Check at fetch_curitem() $error = fetch_curitem; return $error if defined $error;; # Run the checker $checker = new Selima::htc::Checker::Newslet(curform); $checker->redir(qw(cancel)); # Not a valid form } else { return {"msg"=>N_("Incorrect form: [_1]."), "margs"=>[$_], "isform"=>0}; } # OK return; } # html_page: Display the page sub html_page($) { local ($_, %_); my ($status, $LIST, $FORM); $status = $_[0]; # A form is requested if (is_form $status) { # A form to preview a submitted item if (form_type eq "preview") { html_preview; } else { $FORM = new Selima::htc::Form::Newslet($status); html_header $FORM->{"title"}; html_errmsg $status; $FORM->html; html_footer; } # List the available items } else { $LIST = new Selima::htc::List::Newslets; html_header $LIST->{"title"}, $LIST->page_param; html_errmsg $status; $LIST->html; html_footer; } return; } ################################## # Subroutines to manage the data # ################################## # fetch_curitem: Fetch the current item sub fetch_curitem() { local ($_, %_); my ($sn, $FORM, $sth, $sql, $row); # Return if fetched before return if scalar(keys %CURRENT) > 0; # Obtain the current form $FORM = curform; # No item specified return {"msg"=>N_("Please select the newsletter."), "isform"=>0} if !defined $FORM->param("sn"); $sn = $FORM->param("sn"); # Find the record %CURRENT = fetchrec $sn, $THIS_TABLE; # If this record exist return {"msg"=>N_("This newsletter does not exist anymore. Please select another one."), "isform"=>0} if scalar(keys %CURRENT) == 0; # Obtain the date $CURRENT{"date"} = fmtdate $CURRENT{"date"}; # Obtain the belonging index items list @_ = qw(); push @_, "sn AS sn"; push @_, "art AS art"; push @_, "title AS title"; $sql = "SELECT " . join(", ", @_) . " FROM nlindex" . " WHERE newslet=$sn" . " AND parent IS NULL" . " ORDER BY ord;\n"; $sth = $DBH->prepare($sql); $sth->execute; $CURRENT{"ndxcount"} = $sth->rows; for ($_ = 0; $_ < $CURRENT{"ndxcount"}; $_++) { $row = $sth->fetchrow_hashref; $CURRENT{"ndx$_"} = 1; $CURRENT{"ndx$_" . "sn"} = $$row{"sn"}; $CURRENT{"ndx$_" . "art"} = $$row{"art"}; $CURRENT{"ndx$_" . "title"} = $$row{"title"}; } for ($_ = 0; $_ < $CURRENT{"ndxcount"}; $_++) { fetch_index $sn, $CURRENT{"ndx$_" . "sn"}, "ndx$_" . "sub"; } # Obtain the belonging articles list @_ = qw(); push @_, "sn AS sn"; push @_, "title AS title"; $sql = "SELECT " . join(", ", @_) . " FROM nlarts" . " WHERE newslet=$sn" . " ORDER BY ord;\n"; $sth = $DBH->prepare($sql); $sth->execute; $CURRENT{"artcount"} = $sth->rows; for ($_ = 0; $_ < $CURRENT{"artcount"}; $_++) { $row = $sth->fetchrow_hashref; $CURRENT{"art$_" . "sn"} = $$row{"sn"}; $CURRENT{"art$_" . "title"} = $$row{"title"}; } # OK return; } # fetch_index: Fetch the index of the current item sub fetch_index($$$) { local ($_, %_); my ($sn, $parent, $prefix, $sql, $sth, $count, $row); ($sn, $parent, $prefix) = @_; # Find the items @_ = qw(); push @_, "sn AS sn"; push @_, "art AS art"; push @_, "title AS title"; $sql = "SELECT " . join(", ", @_) . " FROM nlindex" . " WHERE newslet=$sn" . " AND parent=$parent" . " ORDER BY ord;\n"; $sth = $DBH->prepare($sql); $sth->execute; $count = $sth->rows; return if $count == 0; $CURRENT{$prefix} = 1; $CURRENT{$prefix . "count"} = $count; for ($_ = 0; $_ < $CURRENT{$prefix . "count"}; $_++) { $row = $sth->fetchrow_hashref; $CURRENT{"$prefix$_"} = 1; $CURRENT{"$prefix$_" . "sn"} = $$row{"sn"}; $CURRENT{"$prefix$_" . "art"} = $$row{"art"}; $CURRENT{"$prefix$_" . "title"} = $$row{"title"}; } undef $sth; # Find the subitems for ($_ = 0; $_ < $CURRENT{$prefix . "count"}; $_++) { fetch_index $sn, $CURRENT{"$prefix$_" . "sn"}, "$prefix$_" . "sub"; } return; } # import_selndxart: Import the selected index item article into the retrieved form sub import_selndxart($) { local ($_, %_); my $FORM; $FORM = $_[0]; $FORM->param($FORM->param("caller_index"), $GET->param("selsn")) if defined $FORM->param("caller_index") && defined $GET->param("selsn") && check_sn_in ${$GET->param_fetch("selsn")}[0], "nlarts"; return; }