Initial commit.
This commit is contained in:
187
lib/perl5/Selima/Page.pm
Normal file
187
lib/perl5/Selima/Page.pm
Normal file
@@ -0,0 +1,187 @@
|
||||
# Selima Website Content Management System
|
||||
# Page.pm: A web page.
|
||||
|
||||
# Copyright (c) 2005-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: 2005-02-26
|
||||
|
||||
package Selima::Page;
|
||||
use 5.008;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Selima::HTTP;
|
||||
|
||||
#use Selima::PageList;
|
||||
|
||||
# new: Initialize the web page
|
||||
sub new : method {
|
||||
local ($_, %_);
|
||||
my ($class, %args, $self);
|
||||
($class, %args) = @_;
|
||||
$self = bless {}, $class;
|
||||
# Set the title
|
||||
http_500 "Please specify the page title."
|
||||
if !exists $args{"title"};
|
||||
$self->{"title"} = $args{"title"};
|
||||
# Set the page path
|
||||
http_500 "Please specify the page path."
|
||||
if !exists $args{"path"};
|
||||
$self->{"path"} = $args{"path"};
|
||||
# Set the order
|
||||
$self->{"ord"} = exists $args{"ord"}? $args{"ord"}: 5000;
|
||||
return $self;
|
||||
}
|
||||
|
||||
# title: Set/return the page title
|
||||
sub title : method {
|
||||
local ($_, %_);
|
||||
my ($self, $title);
|
||||
($self, $title) = @_;
|
||||
if (@_ > 1) {
|
||||
http_500 "Please specify the page title."
|
||||
unless defined $title;
|
||||
$self->{"title"} = $title;
|
||||
}
|
||||
return $self->{"title"};
|
||||
}
|
||||
|
||||
# path: Set/return the page path
|
||||
sub path : method {
|
||||
local ($_, %_);
|
||||
my ($self, $path);
|
||||
($self, $path) = @_;
|
||||
if (@_ > 1) {
|
||||
http_500 "Please specify the page path."
|
||||
unless defined $path;
|
||||
$self->{"path"} = $path;
|
||||
}
|
||||
return $self->{"path"};
|
||||
}
|
||||
|
||||
# ord: Set/return the page order
|
||||
sub ord : method {
|
||||
local ($_, %_);
|
||||
my ($self, $ord);
|
||||
($self, $ord) = @_;
|
||||
if (@_ > 1) {
|
||||
http_500 "Please specify the page order."
|
||||
unless defined $ord;
|
||||
$self->{"ord"} = $ord;
|
||||
}
|
||||
return $self->{"ord"};
|
||||
}
|
||||
|
||||
# addsub: Add the page under this page
|
||||
sub addsub : method {
|
||||
local ($_, %_);
|
||||
my ($self, @pages);
|
||||
($self, @pages) = @_;
|
||||
http_500 "Cannot add sub pages under pages that are not directory indices."
|
||||
unless $self->{"path"} =~ /\/$/;
|
||||
if (!exists $self->{"sub"}) {
|
||||
$self->{"sub"} = new Selima::PageList();
|
||||
$self->{"sub"}->index($self);
|
||||
}
|
||||
$self->{"sub"}->add($_) foreach @pages;
|
||||
return;
|
||||
}
|
||||
|
||||
# compare_to: Compare the order with another page
|
||||
sub compare_to : method {
|
||||
local ($_, %_);
|
||||
my ($self, $another);
|
||||
($self, $another) = @_;
|
||||
# We only compare to another Selima::Page object
|
||||
http_500 "The compare_to() method of a Selima::Page object only accepts another Selima::Page object."
|
||||
if ref $another ne "Selima::Page";
|
||||
# Check the page order
|
||||
return ($self->ord < $another->ord? -1: 1)
|
||||
if $self->ord != $another->ord;
|
||||
# Check the page path
|
||||
return $self->path cmp $another->path
|
||||
if $self->path ne $another->path;
|
||||
# Check the page title (should not)
|
||||
return $self->title cmp $another->title
|
||||
if $self->title ne $another->title;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
# Selima::PageList: A list of pages
|
||||
package Selima::PageList;
|
||||
use 5.008;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Selima::HTTP;
|
||||
|
||||
# new: Initialize the handler
|
||||
sub new : method {
|
||||
local ($_, %_);
|
||||
my ($class, @pages, $self);
|
||||
($class, @pages) = @_;
|
||||
$self = bless {}, $class;
|
||||
$self->{"pages"} = [];
|
||||
$self->add(@pages);
|
||||
return $self;
|
||||
}
|
||||
|
||||
# add: Add web pages to the page list
|
||||
sub add : method {
|
||||
local ($_, %_);
|
||||
my ($self, @pages);
|
||||
($self, @pages) = @_;
|
||||
# Add all the pages
|
||||
foreach my $page (@pages) {
|
||||
# We only accept Selima::Page objects
|
||||
http_500 "The add() method of a Selima::PageList object only accepts Selima::Page objects."
|
||||
if ref $page ne "Selima::Page";
|
||||
# Find the page after the added page
|
||||
for ($_ = 0; $_ < @{$self->{"pages"}}; $_++) {
|
||||
last if $page->compare_to(${$self->{"pages"}}[$_]) < 0;
|
||||
}
|
||||
# Insert the page
|
||||
$self->{"pages"} = [
|
||||
@{$self->{"pages"}}[0..$_-1],
|
||||
$page,
|
||||
@{$self->{"pages"}}[$_..$#{$self->{"pages"}}]
|
||||
];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
# index: Set/return the index page
|
||||
sub index : method {
|
||||
local ($_, %_);
|
||||
my ($self, $page);
|
||||
($self, $page) = @_;
|
||||
# We only accept a Selima::Page object
|
||||
if (@_ > 1) {
|
||||
# Delete it
|
||||
if (!defined $page) {
|
||||
delete $self->{"index"};
|
||||
# Set it
|
||||
} else {
|
||||
http_500 "The index() method of a Selima::PageList object only accepts a Selima::Page object."
|
||||
if ref $page ne "Selima::Page";
|
||||
$self->{"index"} = $page;
|
||||
}
|
||||
}
|
||||
return exists $self->{"index"}? $self->{"index"}: undef;
|
||||
}
|
||||
|
||||
return 1;
|
||||
Reference in New Issue
Block a user