App-Office-CMS
view release on metacpan or search on metacpan
lib/App/Office/CMS/Database/Content.pm view on Meta::CPAN
# --------------------------------------------------
# TODO. We assume the page has 1 content record, so we get it and not an arrayref.
sub get_content_by_page_id
{
my($self, $id) = @_;
return $self -> db -> simple -> query('select * from contents where page_id = ?', $id) -> hash;
} # End of get_content_by_page_id.
# --------------------------------------------------
sub get_content_id_by_page_id
{
my($self, $id) = @_;
my($hash) = $self -> db -> simple -> query('select id from contents where page_id = ?', $id) -> hash;
return $hash && $$hash{id} ? $$hash{id} : 0;
} # End of get_content_id_by_page_id.
# --------------------------------------------------
sub save_content_record
{
my($self, $context, $page, $content) = @_;
$self -> log(debug => "save_content_record($context, ...)");
my($table_name) = 'contents';
my(@field) = (qw/
body_text
design_id
head_text
page_id
site_id
/);
my($data) = {};
for (@field)
{
$$data{$_} = $$content{$_};
}
if ($context eq 'add')
{
$$content{id} = $self -> db -> insert_hash_get_id($table_name, $data);
}
else
{
$self -> db -> simple -> update($table_name, $data, {id => $$content{id} });
}
$self -> log(debug => "Saved ($context) content for page '$$page{name}' with id $$content{id}");
} # End of save_content_record.
# --------------------------------------------------
sub update
{
my($self, $page, $content) = @_;
$self -> log(debug => "update($$page{name}, ...)");
$self -> log(debug => '-' x 50);
$self -> log(debug => "$_ => $$content{$_}") for sort grep{! /^(?:body_text|head_text)$/} keys %$content;
$self -> log(debug => 'head_text => ' . length($$content{head_text}) . ' bytes');
$self -> log(debug => 'body_text => ' . length($$content{body_text}) . ' bytes');
$self -> log(debug => '-' x 50);
# Now determine if this is an add or an update.
$$content{id} = $self -> get_content_id_by_page_id($$content{page_id});
my($action) = $$content{id} ? 'update' : 'add';
$self -> save_content_record($action, $page, $content);
return "Saved ($action) content";
} # End of update.
# --------------------------------------------------
1;
( run in 0.959 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )