Initial commit.
This commit is contained in:
143
lib/perl5/Selima/Checker/GroupMem.pm
Normal file
143
lib/perl5/Selima/Checker/GroupMem.pm
Normal file
@@ -0,0 +1,143 @@
|
||||
# Selima Website Content Management System
|
||||
# GroupMem.pm: The group-to-group membership 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 <imacat@mail.imacat.idv.tw>
|
||||
# First written: 2004-10-13
|
||||
|
||||
package Selima::Checker::GroupMem;
|
||||
use 5.008;
|
||||
use strict;
|
||||
use warnings;
|
||||
use base qw(Selima::Checker);
|
||||
|
||||
use Selima::ChkFunc;
|
||||
use Selima::CallForm;
|
||||
use Selima::DataVars qw($DBH :forms);
|
||||
use Selima::ShortCut;
|
||||
|
||||
# new: Initialize the checker
|
||||
sub new : method {
|
||||
local ($_, %_);
|
||||
my $class;
|
||||
($class, @_) = @_;
|
||||
$_[1] = "groupmem" if scalar(@_) < 2 || !defined $_[1];
|
||||
return $class->SUPER::new(@_);
|
||||
}
|
||||
|
||||
# check: Run a list of checks
|
||||
sub check : method {
|
||||
local ($_, %_);
|
||||
my ($self, $error, @cols);
|
||||
($self, @cols) = @_;
|
||||
# Run the parent method first
|
||||
$error = $self->SUPER::check(@cols);
|
||||
return $error if defined $error;
|
||||
# See if we need to check the duplicates. Check it in the end.
|
||||
%_ = map { $_ => 1 } @cols;
|
||||
if (exists $_{"grp"} && exists $_{"member"}) {
|
||||
$error = $self->__check_dup();
|
||||
return $error if defined $error;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
# _check_grp: Check the group
|
||||
sub _check_grp : method {
|
||||
local ($_, %_);
|
||||
my ($self, $form, $error);
|
||||
$self = $_[0];
|
||||
$form = $self->{"form"};
|
||||
# Run the default group checker
|
||||
$error = $self->SUPER::_check_grp;
|
||||
return $error if defined $error;
|
||||
# Check if the group and the member are different
|
||||
return {"msg"=>N_("Please select a different belonging group.")}
|
||||
if !$self->_missing("member")
|
||||
&& $form->param("member") ne ""
|
||||
&& $form->param("grp") == $form->param("member");
|
||||
# OK
|
||||
return;
|
||||
}
|
||||
|
||||
# _check_member: Check the member
|
||||
sub _check_member : method {
|
||||
local ($_, %_);
|
||||
my ($self, $form, $error);
|
||||
$self = $_[0];
|
||||
$form = $self->{"form"};
|
||||
# Check if it exists
|
||||
$error = $self->_missing("member");
|
||||
return $error if defined $error;
|
||||
# Regularize it
|
||||
$self->_trim("member");
|
||||
# Check if it is filled
|
||||
return {"msg"=>N_("Please select a member.")}
|
||||
if $form->param("member") eq "";
|
||||
# Check if this group exists
|
||||
return {"msg"=>N_("This member does not exist anymore. Please select another one.")}
|
||||
if !check_sn_in ${$form->param_fetch("member")}[0], "groups AS grpmembers";
|
||||
# Check if the group and the member are different
|
||||
return {"msg"=>N_("Please select a different group member.")}
|
||||
if !$self->_missing("grp")
|
||||
&& $form->param("grp") ne ""
|
||||
&& $form->param("grp") == $form->param("member");
|
||||
# OK
|
||||
return;
|
||||
}
|
||||
|
||||
# __check_dup: Check if this item is duplicated
|
||||
sub __check_dup : method {
|
||||
local ($_, %_);
|
||||
my ($self, $form, $error, $sth, $sql);
|
||||
$self = $_[0];
|
||||
$form = $self->{"form"};
|
||||
@_ = qw();
|
||||
push @_, "grp=" . $form->param("grp");
|
||||
push @_, "member=" . $form->param("member");
|
||||
push @_, "sn!=" . $self->{"sn"}
|
||||
if $self->{"iscur"};
|
||||
$sql = "SELECT * FROM " . $DBH->quote_identifier($self->{"table"})
|
||||
. " WHERE " . join(" AND ", @_) . ";\n";
|
||||
$sth = $DBH->prepare($sql);
|
||||
$sth->execute;
|
||||
return {"msg"=>N_("This membership record already exists. You cannot create a duplicated one.")}
|
||||
if $sth->rows > 0;
|
||||
return;
|
||||
}
|
||||
|
||||
# _redir_selmember: Suspend and move to the member selection form
|
||||
sub _redir_selmember : method {
|
||||
local ($_, %_);
|
||||
my $self;
|
||||
$self = $_[0];
|
||||
# Skip if not requested
|
||||
return if $self->_missing("selmember");
|
||||
call_form FORM_GROUPS, undef, "import_selmember";
|
||||
}
|
||||
|
||||
# _redir_delmember: Remove the member
|
||||
sub _redir_delmember : method {
|
||||
local ($_, %_);
|
||||
my $self;
|
||||
$self = $_[0];
|
||||
# Skip if not requested
|
||||
return if $self->_missing("delmember");
|
||||
$self->{"form"}->delete("member");
|
||||
success_redirect undef;
|
||||
}
|
||||
|
||||
return 1;
|
||||
Reference in New Issue
Block a user