# Selima Website Content Management System # LinkCat.pm: The related-link category form checker. # Copyright (c) 2004-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 # First written: 2004-10-24 package Selima::Checker::LinkCat; use 5.008; use strict; use warnings; use base qw(Selima::Checker); use Selima::CallForm; use Selima::ChkFunc; use Selima::DataVars qw($DBH :forms); use Selima::ShortCut; # new: Initialize the checker sub new : method { local ($_, %_); my ($class, $self); ($class, @_) = @_; $_[1] = "linkcat" if scalar(@_) < 2 || !defined $_[1]; $self = $class->SUPER::new(@_); ${$self->{"maxlens"}}{"ord"} = 2; ${$self->{"minlens"}}{"id"} = 2; return $self; } # _check_id: Check the ID. sub _check_id : method { local ($_, %_); my ($self, $form, $error, $sth, $sql); $self = $_[0]; $form = $self->{"form"}; # Run the default ID. checker $error = $self->SUPER::_check_id; return $error if defined $error; # ID. cannot be "index" to avoid overriding index.html return {"msg"=>N_("\"index\" is dedicated to the index file index.html. You cannot set the ID. as \"index\".")} if $form->param("id") eq "index"; # Check if this item is duplicated if (!$self->_missing("topmost", "parent")) { @_ = qw(); push @_, "id=" . $DBH->quote($form->param("id")); push @_, "sn!=" . $self->{"sn"} if $self->{"iscur"}; if ($form->param("topmost") eq "true") { push @_, "parent IS NULL"; } else { push @_, "parent=" . $DBH->quote($form->param("parent")); } $sql = "SELECT * FROM " . $DBH->quote_identifier($self->{"table"}) . " WHERE " . join(" AND ", @_) . ";\n"; $sth = $DBH->prepare($sql); $sth->execute; return {"msg"=>N_("This category already exists. You cannot create a duplicated one.")} if $sth->rows > 0; } # OK return; } # _check_parent: Check the parent category sub _check_parent : method { local ($_, %_); my ($self, $form, $error, $sth, $sql); $self = $_[0]; $form = $self->{"form"}; # "topmost not set" has a different form context return {"msg"=>N_("Please select a parent category.")} if $self->_missing("topmost"); # Regularize it $self->_trim("topmost"); # Check the option value return {"msg"=>N_("This option is invalid. Please select a proper parent category.")} unless $form->param("topmost") =~ /^(?:true|false)$/; # Check the parent category if not a topmost category if ($form->param("topmost") eq "false") { # Check if it exists $error = $self->_missing("parent"); return $error if defined $error; # Regularize it $self->_trim("parent"); # Check if it is filled return {"msg"=>N_("Please select a parent category.")} if $form->param("parent") eq ""; # Check if this category exists return {"msg"=>N_("This parent category does not exist anymore. Please select another one.")} if !check_sn_in ${$form->param_fetch("parent")}[0], "linkcat"; if ($self->{"iscur"}) { # Check if the parent category is itself return {"msg"=>N_("A category cannot belong to itself. Please select another one.")} if $form->param("parent") == $self->{"sn"}; # Check if the parent directory is its descendant $sql = "SELECT linkcat_ischild(" . $self->{"sn"} . ", " . $form->param("parent") . ") AS is_child;\n"; $sth = $DBH->prepare($sql); $sth->execute; return {"msg"=>N_("A category cannot belong to its descendant. Please select another one.")} if ${$sth->fetchrow_hashref}{"is_child"}; } } # OK return; } return 1;