102 lines
2.9 KiB
Perl
102 lines
2.9 KiB
Perl
# Selima Website Content Management System
|
|
# Country.pm: The subroutines to query the country name from the database.
|
|
|
|
# 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-17
|
|
|
|
package Selima::Country;
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use base qw(Exporter);
|
|
use vars qw(@EXPORT @EXPORT_OK);
|
|
BEGIN {
|
|
@EXPORT = qw(ctname ctname_zhtw);
|
|
@EXPORT_OK = @EXPORT;
|
|
# Prototype declaration
|
|
sub ctname($);
|
|
sub ctname_zhtw($);
|
|
}
|
|
|
|
use Selima::Cache qw(:country);
|
|
use Selima::CommText;
|
|
use Selima::DataVars qw($DBH :lninfo);
|
|
use Selima::GetLang;
|
|
use Selima::LnInfo;
|
|
|
|
# The default language here is always English
|
|
use constant DEFAULT_LANG => "en";
|
|
use constant TRAD_CHINESE => "zh-tw";
|
|
|
|
# ctname: Obtain a country name
|
|
sub ctname($) {
|
|
local ($_, %_);
|
|
my ($id, $name, $col, $defcol, $sql, $sth);
|
|
$id = $_[0];
|
|
# Bounce if there is any problem with $id
|
|
return t_notset unless defined $id;
|
|
# Return the cache
|
|
return $Country_ctname{$id} if exists $Country_ctname{$id};
|
|
|
|
# Default language
|
|
if (getlang eq DEFAULT_LANG) {
|
|
$name = "name_" . getlang(LN_DATABASE) . " AS name";
|
|
# Fall back to the default language
|
|
} else {
|
|
$col = "name_" . getlang LN_DATABASE;
|
|
$defcol = "name_" . ln DEFAULT_LANG, LN_DATABASE;
|
|
$name= "COALESCE($col, $defcol) AS name";
|
|
}
|
|
# Query
|
|
$sql = "SELECT $name FROM country"
|
|
. " WHERE id=" . $DBH->quote($id) . ";\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
|
|
# Found
|
|
return ($Country_ctname{$id} = ${$sth->fetch}[0])
|
|
if $sth->rows == 1;
|
|
# Not found
|
|
return ($Country_ctname{$id} = t_na);
|
|
}
|
|
|
|
# ctname_zhtw: Obtain a country name in Traditional Chinese
|
|
sub ctname_zhtw($) {
|
|
local ($_, %_);
|
|
my ($id, $name, $col, $defcol, $sql, $sth);
|
|
$id = $_[0];
|
|
# Bounce if there is any problem with $id
|
|
return t_notset unless defined $id;
|
|
|
|
# Fall back to the default language
|
|
$col = "name_" . ln TRAD_CHINESE, LN_DATABASE;
|
|
$defcol = "name_" . ln DEFAULT_LANG, LN_DATABASE;
|
|
$name= "COALESCE($col, $defcol) AS name";
|
|
# Query
|
|
$sql = "SELECT $name FROM country"
|
|
. " WHERE id=" . $DBH->quote($id) . ";\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
|
|
# Found
|
|
return ${$sth->fetch}[0] if $sth->rows == 1;
|
|
# Not found
|
|
return t_na;
|
|
}
|
|
|
|
return 1;
|