package Whostmgr::Thirdparty::edit_com;

# cpanel12 - cpanel/Whostmgr/Thirdparty/edit_com.pm
#                                                 Copyright(c) 2007 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

#WHMADDON:edit_com:Website Maintenance Service by Edit.com
#ACLS:thirdparty

use strict;
use warnings;
use Cpanel::SafeDir         ();
use Cpanel::DataStore       ();
use Whostmgr::ACLS          ();
use Whostmgr::HTMLInterface ();

my $service       = 'Edit.com';
my $service_short = 'edit_com';
my $lang          = $main::lang ? $main::lang : 'english';

# Add to ACLs hash
$Whostmgr::ACLS::default{'edit_com'} = 0;

# Load saved values for ACLs
Whostmgr::ACLS::init_acls();

sub saveservice {
    return if !Whostmgr::ACLS::checkacl('thirdparty');
    my $reseller = $ENV{'REMOTE_USER'};
    my $conf_ref;
    if ( -e $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller . '/' . $service_short ) {
        $conf_ref = Cpanel::DataStore::fetch_ref( $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller . '/' . $service_short );
    }
    if ( !exists $conf_ref->{'serviceid'} ) {
        $conf_ref->{'serviceid'} = 0;
    }

    foreach my $key ( keys %{$conf_ref} ) {
        if ( exists $main::FORM{$key} ) {
            $conf_ref->{$key} = $main::FORM{$key};
        }
        else {
            delete $conf_ref->{$key};
        }
    }

    my $theme    = Whostmgr::Theme::gettheme();
    my %template = (
        'file'             => '/usr/local/cpanel/whostmgr/docroot/themes/' . $theme . '/templates/saveservice_' . $service_short,
        'theme'            => $theme,
        'service'          => $service,
        'serviceid'        => $conf_ref->{'serviceid'} ? $conf_ref->{'serviceid'} : 'NA',
        'manage_with_edit' => $Cpanel::Lang::LANG{$lang}{'edit_com-manage_with_edit'},
    );

    Whostmgr::HTMLInterface::defheader( $Cpanel::Lang::LANG{$lang}{'edit_com-page_title'} );

    Cpanel::SafeDir::safemkdir( $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller );
    if ( !Cpanel::DataStore::store_ref( $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller . '/' . $service_short, $conf_ref ) ) {
        $template{'message'} = "Failed to save $service configuration";
    }
    else {
        $template{'message'} = "Successfully saved $service configuration";
    }
    print Whostmgr::Template::process( \%template );
}

sub showservice {
    return if !Whostmgr::ACLS::checkacl('thirdparty');
    my $reseller = $ENV{'REMOTE_USER'};
    my %template;
    if ( -e $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller . '/' . $service_short ) {
        %template = %{ Cpanel::DataStore::fetch_ref( $Whostmgr::Thirdparty::reseller_conf_dir . '/' . $reseller . '/' . $service_short ) };
    }

    my $theme = Whostmgr::Theme::gettheme();
    $template{'theme'}                 = $theme;
    $template{'file'}                  = '/usr/local/cpanel/whostmgr/docroot/themes/' . $theme . '/templates/showservice_' . $service_short;
    $template{'service'}               = $service;
    $template{'activate'}              = $Cpanel::Lang::LANG{$lang}{'edit_com-activate'};
    $template{'action'}                = '/scripts2/saveservice?service=' . $service_short;
    $template{'settings-hasserviceid'} = $template{'serviceid'} ? 1 : 0;
    $template{'overview_desc'}         = $Cpanel::Lang::LANG{$lang}{'edit_com-overview_desc'};
    $template{'overview'}              = $Cpanel::Lang::LANG{$lang}{'edit_com-overview'};
    $template{'overview_link'}         = 'http://cpanel.edit.com/';
    $template{'overview_link_text'}    = $Cpanel::Lang::LANG{$lang}{'edit_com-overview_link_text'};
    $template{'activation'}            = $Cpanel::Lang::LANG{$lang}{'edit_com-activation'};
    $template{'activation_desc'}       = $Cpanel::Lang::LANG{$lang}{'edit_com-activations_desc'};
    $template{'disable'}               = $Cpanel::Lang::LANG{$lang}{'edit_com-disable'};
    $template{'disable_desc'}          = $Cpanel::Lang::LANG{$lang}{'edit_com-disable_desc'};

    if ( $template{'serviceid'} ) {
        $template{'management'}      = $Cpanel::Lang::LANG{$lang}{'edit_com-management'};
        $template{'management_link'} = 'http://cpanel.edit.com/?kid=' . $template{'serviceid'};
        $template{'management_link_text'} = $Cpanel::Lang::LANG{$lang}{'edit_com-management_link_text'};
        $template{'serviceid'}       = $template{'serviceid'};
    }

    Whostmgr::HTMLInterface::defheader( $Cpanel::Lang::LANG{$lang}{'edit_com-page_title'} );
    print Whostmgr::Template::process( \%template );
}

1;
