#!/usr/bin/perl
use strict;
use warnings;

package API::StaticPage;

use MIME::Entity;
use MIME::Base64;
use MIME::Lite;
use Encode;

use SOAP::Lite ();
use base 'SOAP::Server::Parameters';

our $Config;
BEGIN{
	use API::Config;
	$Config = $API::Config::Config;
}
use lib ($Config->{Website_libs});

use ECS::SQL;
use ECS::Site;

my $DBH = ECS::SQL::connect($ECS::dbConfig::db{'tms'});

sub add {

	my $ref = shift;
	my $input = shift;	
	$input->{'dateCreated'} = strftime("%Y-%m-%d %H:%M:%S",localtime(time));
	
    my $errMessage;
    my $result;
	my $site    = ECS::Site->fromName($input->{Sitename});
	my $dbh     = $site->site_dbh;
    if($input->{entities_contentHtml}=~(/src\s*=\s*("|')\s*http:.*/isg)){
        $errMessage = "HTML contains non-secure image sources.";
    }
    elsif($input->{entities_contentHtml}=~(/<script[\s\S]*?>[\s\S]*?<\/script\s*>/isg)){
        $errMessage="HTML contains script tags.";
    }
	
	$input->{content} = Encode::decode_utf8(delete $input->{entities_contentHtml});	
	my @columns    = qw(content url title shortDesc location parentBlockID dateCreated);
	my @updateCols = grep {exists $input->{$_} && ($input->{$_} ne '')} @columns;
	my %rows       = map { $_ => $input->{$_} } grep {(exists $input->{$_}) && ($input->{$_} ne '')} @columns;
	$result = {
		'success'	=> 1,
		'error'		=> $errMessage,
		'update_DB'	=> $Config->{update_DB},
		'row'	    => 0,
	};
    
    return $result if ($errMessage);
	# IF we want to write to DB (Determined by API::Config)
	# --------------------------------------------------
	if ($Config->{update_DB}){
		use POSIX qw(strftime);		
	    my $row = ECS::SQL::insert({
	              		DBH     => $dbh,
	                    table   => 'html_block',
	                    key     => 'blockID',
	                    columns => \@updateCols,
	                    hash    => \%rows
	                });
		if(!$row){
			$result->{success} = 0;
			$result->{error} = "Unable to add header to DB.";
		}
		$result->{row} = $row;
	}
	return $result;
}

sub update {
	
	my $ref = shift;
	my $input = shift;
    my $errMessage;
    my $result;
	my $site    = ECS::Site->fromName($input->{Sitename});
	my $dbh     = $site->site_dbh;
    if($input->{entities_contentHtml}=~(/src\s*=\s*("|')\s*http:.*/isg)){
        $errMessage = "HTML contains non-secure image sources.";
    }
    elsif($input->{entities_contentHtml}=~(/<script[\s\S]*?>[\s\S]*?<\/script\s*>/isg)){
        $errMessage="HTML contains script tags.";
    }
    
	$input->{content} = Encode::decode_utf8(delete $input->{entities_contentHtml});	
	my @columns    = qw(content url title shortDesc location parentBlockID);
	my @updateCols = grep {exists $input->{$_} && ($input->{$_} ne '')} @columns;
	my %rows       = map { $_ => $input->{$_} } grep {exists $input->{$_} && ($input->{$_} ne '')} @columns;
	
	##pushing primary key
	$rows{blockID}= $input->{blockID};
	
	$result = {
		'success'	=> 1,
		'error'		=> $errMessage,
		'update_DB'	=> $Config->{update_DB}
	};
    return $result if ($errMessage);
	# IF we want to write to DB (Determined by API::Config)
	# --------------------------------------------------
	if ($Config->{update_DB}){
		if(!ECS::SQL::update({
	         DBH => $dbh,
	         table => 'html_block',
	         key => 'blockID',
	         columns => \@updateCols,
	         hash => \%rows
			 })){
	     		$result->{success} = 0;
				$result->{error} = "Unable to update header.";
	     };
	}
    return $result;
}

sub delete {
	my $ref = shift;
	my $input = shift;
	my $result = {
		'success'	=> 1,
		'error'		=> undef,
		'update_DB'	=> $Config->{update_DB}
	};
	
	my $site = ECS::Site->fromID($input->{SiteID});
	my $dbh  = $site->site_dbh;
	my $tmp;	
	($tmp = $input->{removeStaticPage}) =~ s/^a_//;
	ECS::SQL::delete({
		DBH => $dbh, 
		table => 'html_block',
		columns => [qw(blockID)],
		hash => {blockID=>$tmp}
	});
	
	return $result;
}

1;
