161 lines
4.7 KiB
Perl
161 lines
4.7 KiB
Perl
# Mandy Wu's Website
|
|
# Material.pm: The historical material form checker.
|
|
|
|
# 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-11-23
|
|
|
|
package Selima::emandy::Checker::Material;
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use base qw(Selima::Checker);
|
|
|
|
use Selima::ChkFunc;
|
|
use Selima::ShortCut;
|
|
|
|
# new: Initialize the checker
|
|
sub new : method {
|
|
local ($_, %_);
|
|
my ($class, $self);
|
|
($class, @_) = @_;
|
|
$_[1] = "material" if scalar(@_) < 2 || !defined $_[1];
|
|
$self = $class->SUPER::new(@_);
|
|
return $self;
|
|
}
|
|
|
|
# _check_type: Check the type
|
|
sub _check_type : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("type");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trim("type");
|
|
# Skip if it is not filled
|
|
return if $form->param("type") eq "";
|
|
# Check if the type exists
|
|
return {"msg"=>N_("This type does not exist anymore. Please select another one.")}
|
|
if !check_sn_in ${$form->param_fetch("type")}[0], "mtrltype";
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# _check_year: Check the year
|
|
sub _check_year : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("year");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trim("year");
|
|
# Skip if it is not filled
|
|
return if $form->param("year") eq "";
|
|
# Check the length
|
|
return {"msg"=>N_("This year is too long. (Max. length [#,_1])"),
|
|
"margs"=>[${$self->{"maxlens"}}{"year"}]}
|
|
if length $form->param("year") > ${$self->{"maxlens"}}{"year"};
|
|
# Check if it is a valid positive integer
|
|
return {"msg"=>N_("Please fill in a positive integer year.")}
|
|
unless $form->param("year") =~ /^\d+$/;
|
|
# Set to an integer
|
|
$_ = $form->param("year");
|
|
$_ += 0;
|
|
$form->param("year", $_);
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# _check_title: Check the title
|
|
# Use the default title checker
|
|
|
|
# _check_body: Check the content
|
|
# Use the default content checker
|
|
|
|
# _check_source: Check the source
|
|
sub _check_source : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("source");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trim("source");
|
|
# Check if it is filled
|
|
return {"msg"=>N_("Please fill in the source.")}
|
|
if $form->param("source") eq "";
|
|
# Check the length
|
|
return {"msg"=>N_("This source is too long. (Max. length [#,_1])"),
|
|
"margs"=>[${$self->{"maxlens"}}{"source"}]}
|
|
if length $form->param("source") > ${$self->{"maxlens"}}{"source"};
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# _check_author: Check the author
|
|
sub _check_author : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("author");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trim("author");
|
|
# Skip if it is not filled
|
|
return if $form->param("author") eq "";
|
|
# Check the length
|
|
return {"msg"=>N_("This author is too long. (Max. length [#,_1])"),
|
|
"margs"=>[${$self->{"maxlens"}}{"author"}]}
|
|
if length $form->param("author") > ${$self->{"maxlens"}}{"author"};
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# _check_notes: Check the notes
|
|
sub _check_notes : method {
|
|
local ($_, %_);
|
|
my ($self, $form, $error);
|
|
$self = $_[0];
|
|
$form = $self->{"form"};
|
|
# Check if it exists
|
|
$error = $self->_missing("notes");
|
|
return $error if defined $error;
|
|
# Regularize it
|
|
$self->_trimtext("notes");
|
|
# Skip if it is not filled
|
|
$form->param("notes", "")
|
|
if $form->param("notes") eq __("Fill in the notes here.");
|
|
return if $form->param("notes") eq "";
|
|
# Check the length
|
|
return {"msg"=>N_("This notes is too long. (Max. length [#,_1])"),
|
|
"margs"=>[${$self->{"maxlens"}}{"notes"}]}
|
|
if length $form->param("notes") > ${$self->{"maxlens"}}{"notes"};
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
return 1;
|