Files
selima-perl/lib/perl5/Selima/Processor/LinkCat.pm
2026-03-10 21:31:43 +08:00

220 lines
7.4 KiB
Perl

# Selima Website Content Management System
# LinkCat.pm: The related-link category data processor.
# Copyright (c) 2006-2018 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 <imacat@mail.imacat.idv.tw>
# First written: 2006-03-17
package Selima::Processor::LinkCat;
use 5.008;
use strict;
use warnings;
use base qw(Selima::Processor::Category);
use Selima::DataVars qw($DBH :addcol :scptconf);
use Selima::Guest;
use Selima::Links;
# new: Initialize the processor
sub new : method {
local ($_, %_);
my ($self, $class);
($class, @_) = @_;
$_[1] = "linkcat" if @_ < 2;
$self = $class->SUPER::new(@_);
return $self;
}
# _save_cols: Save the column deposit
sub _save_cols : method {
local ($_, %_);
my ($self, $form, $cur);
$self = $_[0];
($form, $cur) = ($self->{"form"}, $self->{"cur"});
if ($self->{"type"} ne "del") {
# Set the "topmost" parent
$form->delete("parent") if defined $form->param("topmost")
&& $form->param("topmost") eq "true";
}
# A form to create a new item
if ($self->{"type"} eq "new") {
$self->{"sn"} = $self->_new_sn;
$self->{"cols"} = new Selima::AddCol($self->{"table"}, ADDCOL_INSERT);
$self->{"cols"}->addnum("sn", $self->{"sn"});
$self->{"cols"}->addnum("parent", $self->_form("parent"));
$self->{"cols"}->addstr("id", $self->_form("id"));
$self->{"cols"}->addnum("ord", $self->_form("ord"));
$self->{"cols"}->addstr("title", $self->_form("title"));
$self->{"cols"}->addstr("kw", $self->_form("kw"));
$self->{"cols"}->addbool("hid", $self->_form("hid"));
# Automatic Traditional Chinese to Simplified Chinese conversion
$self->_zhsync;
# A form to edit a current item
} elsif ($self->{"type"} eq "cur") {
$self->{"cols"} = new Selima::AddCol($self->{"table"}, ADDCOL_UPDATE);
$self->{"cols"}->addnum("parent", $self->_form("parent"), scalar $cur->param("parent"));
$self->{"cols"}->addstr("id", $self->_form("id"), scalar $cur->param("id"));
$self->{"cols"}->addnum("ord", $self->_form("ord"), scalar $cur->param("ord"));
$self->{"cols"}->addstr("title", $self->_form("title"), scalar $cur->param("title"));
$self->{"cols"}->addstr("kw", $self->_form("kw"), scalar $cur->param("kw"));
$self->{"cols"}->addbool("hid", $self->_form("hid"), scalar $cur->param("hid"));
# Automatic Traditional Chinese to Simplified Chinese conversion
$self->_zhsync;
}
return;
}
# _update_cols: Update the columns
sub _update_cols : method {
local ($_, %_);
my $self;
($self, @_) = @_;
$self->{"curshown"} = $self->_shown_parts;
$self->SUPER::_update_cols(@_);
return;
}
# _actlog: Log the activity
sub _actlog : method {
local ($_, %_);
my ($self, $form, $cur);
$self = $_[0];
($form, $cur) = ($self->{"form"}, $self->{"cur"});
# A form to create a new item
return gactlog "Create a link category " . $form->param("id")
. " with s/n " . $self->{"sn"} . "."
if $self->{"type"} eq "new";
# A form to edit a current item
return gactlog "Update the link category " . $form->param("id")
. " with s/n " . $self->{"sn"} . "."
if $self->{"type"} eq "cur";
# A form to delete a current item
return gactlog "Delete the link category " . $cur->param("id")
. " with s/n " . $self->{"sn"} . "."
if $self->{"type"} eq "del";
}
# _rebuild_partial_pages: Rebuild a limited part of pages
sub _rebuild_partial_pages : method {
local ($_, %_);
my ($self, $form, $cur);
my ($sql, @parents, $build_myself, $cond);
$self = $_[0];
($form, $cur) = ($self->{"form"}, $self->{"cur"});
$self->{"newshown"} = $self->_shown_parts;
# Remove the unwanted pages
$self->_remove_curfile;
# Page was not shown, and is still not shown now
return unless ${$self->{"curshown"}}{"self"}
|| ${$self->{"newshown"}}{"self"};
# Find the affected parents
@parents = qw();
@_ = qw();
%_ = map { $_ => 1 } @{${$self->{"curshown"}}{"cats"}};
push @_, grep !exists $_{$_}, @{${$self->{"newshown"}}{"cats"}};
%_ = map { $_ => 1 } @{${$self->{"newshown"}}{"cats"}};
push @_, grep !exists $_{$_}, @{${$self->{"curshown"}}{"cats"}};
if (@_ > 0) {
my ($sql, $sth, $count, $row);
$sql = "SELECT parent FROM " . $DBH->quote_identifier($self->{"table"})
. " WHERE " . join(" OR ", map "sn=$_", @_)
. " GROUP BY parent;\n";
$sth = $DBH->prepare($sql);
$sth->execute;
$count = $sth->rows;
for ($_ = 0; $_ < $count; $_++) {
push @parents, ${$sth->fetch}[0];
}
}
# Add myself and my parents
$build_myself = 0;
# A form to edit a current item
if ($self->{"type"} eq "cur") {
push @parents, $cur->param("parent")
if !$cur->param("hid");
if (!defined $form->param("hid")) {
push @parents, $form->param("parent");
$build_myself = 1;
}
# A form to delete a current item
} elsif ($self->{"type"} eq "del") {
push @parents, $cur->param("parent")
if !$cur->param("hid");
}
# Nothing to rebuild
return if @parents == 0 && !$build_myself;
# Compose the SQL statement
@_ = qw();
push @_, "sn=" . $self->{"sn"} if $build_myself;
foreach (@parents) {
# The parent page and those share the same parent
if (defined $_) {
push @_, "sn=" . $_;
push @_, "parent=" . $_;
# The topmost pages
} else {
push @_, "parent IS NULL";
}
}
$cond = join " OR ", @_;
$cond = "($cond)" if @_ > 1;
@_ = $DBH->cols($self->{"table"});
push @_, $DBH->strcat("'/links'", "linkcat_path(sn, id, parent)")
. " AS path";
$sql = "SELECT " . join(", ", @_) . " FROM " . $DBH->quote_identifier($self->{"table"})
. " WHERE $cond"
. " AND linkcat_isshown(sn, hid, parent);\n";
# Rebuild the pages
$_ = $MAIN->can("rebuild_links");
&$_($sql);
return;
}
# _remove_curfile: Remove the unwanted page
sub _remove_curfile : method {
local ($_, %_);
my $self;
$self = $_[0];
# Remove the unwanted category files
%_ = map { $_ => 1 } @{${$self->{"newshown"}}{"catspath"}};
grmoldpage $_
foreach grep !exists $_{$_}, @{${$self->{"curshown"}}{"catspath"}};
return;
}
# _shown_parts: Obtain the shown parts
sub _shown_parts : method {
local ($_, %_);
my ($self, $shown, $sql, $sth);
$self = $_[0];
$shown = links_shown_parts;
# Check if myself is shown
$sql = "SELECT sn FROM " . $DBH->quote_identifier($self->{"table"})
. " WHERE sn=" . $self->{"sn"}
. " AND linkcat_isshown(sn, hid, parent);\n";
$sth = $DBH->prepare($sql);
$sth->execute;
$$shown{"self"} = ($sth->rows > 0);
return $shown;
}
return 1;