<?php

require_once 'classes/SnapinPlugin.php';

class PluginMergeclient extends SnapinPlugin
{
    function getVariables()
    {
        $variables = array(
            'Plugin Name'       => array(
                'type'        => 'hidden',
                'description' => '',
                'value'       => 'Merge Client',
            ),
        );

        return $variables;
    }

    function view()
    {
        if (isset($_POST['merge'])) {
            return $this->_processdata();
        } else {
            return $this->_showform();
        }
    }

    function _showform($message = '') {
        if (!NE_Lib::isPublicDir()) {
            $this->tpl->define(array('layout' => '../../plugins/snapins/mergeclient/MergeClient.tpl'));
            if (isset($_SESSION['customerId']) && $_SESSION['customerId'] > 0) {
                $this->tpl->parse('trash', 'mergeClient');
            } else {
                $this->tpl->parse('trash', 'selectClient');
            }
            if ($message != '') {
                $this->tpl->assign('Message', $message);
                $this->tpl->parse('trash', 'message');
            }
        } else {
            $this->tpl->define(array('layout' => '../../plugins/snapins/mergeclient/PublicSection_MergeClient.tpl'));
        }
        $this->tpl->parse('output', 'layout');
        return $this->tpl->fetch('output');
    }

    function _processdata() {
        if (!isset($_POST['clientID']) && $_POST['clientID'] == '') {
            return $this->_showform('Destination Client ID must be specified');
        }
        $oldClient = $_SESSION['customerId'];
        $newClient = $_POST['clientID'];
        $deleteClient = $this->_checked('delete');
        $allData = $this->_checked('mergeall');
        $packages = $this->_checked('mergepackages');
        $domains = $this->_checked('mergedomains');
        $recurring = $this->_checked('mergerecurring');
        $invoices = $this->_checked('mergeinvoices');
        $tickets = $this->_checked('mergetickets');
        $email = $this->_checked('mergeemail');
        
        // check that the new id is valid
        $query = "SELECT `id` FROM `users` WHERE `id` = ?";
        $result = $this->db->query($query, $newClient);
        if ($result->getNumRows() < 1) {
            return $this->_showform('Destination Client ID is invalid');
        }
        
        if ($allData) {
            $this->_moveAllData($oldClient, $newClient);
        } else {
            if ($packages) $this->_movePackages($oldClient, $newClient);
            if ($domains) $this->_moveDomains($oldClient, $newClient);
            if ($recurring) $this->_moveRecurring($oldClient, $newClient);
            if ($invoices) $this->_moveInvoices($oldClient, $newClient);
            if ($tickets) $this->_moveTickets($oldClient, $newClient);
            if ($email) $this->_moveEmails($oldClient, $newClient);
        }
        
        if ($deleteClient) {
            $user =& new User($oldClient);
            $user->delete();
            unset($_SESSION['customerId']);
        }
        
        return $this->_showform('Merge completed successfully');
    }
    
    function _movePackages($old, $new)
    {
        $query = 'UPDATE `domains` SET `CustomerID` = ? WHERE `CustomerID` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveDomains($old, $new)
    {
        $query = 'UPDATE `users_domains` SET `userid` = ? WHERE `userid` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveRecurring($old, $new)
    {
        $query = 'UPDATE `recurringfee` SET `CustomerID` = ? WHERE `CustomerID` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveInvoices($old, $new)
    {
        $query = 'UPDATE `invoice` SET `CustomerID` = ? WHERE `CustomerID` = ?';
        $result = $this->db->query($query, $new, $old);
        
        $query = 'UPDATE `invoiceentry` SET `CustomerID` = ? WHERE `CustomerID` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveTickets($old, $new)
    {
        $query = 'UPDATE `troubleticket` SET `userid` = ? WHERE `userid` = ?';
        $result = $this->db->query($query, $new, $old);
        
        $query = 'UPDATE `troubleticket_files` SET `userid` = ? WHERE `userid` = ?';
        $result = $this->db->query($query, $new, $old);
        
        $query = 'UPDATE `troubleticket_log` SET `userid` = ? WHERE `userid` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveEmails($old, $new)
    {
        $query = 'UPDATE `altuseremail` SET `userid` = ? WHERE `userid` = ?';
        $result = $this->db->query($query, $new, $old);
    }
    
    function _moveAllData($oldClient, $newClient)
    {
        $this->_movePackages($oldClient, $newClient);
        $this->_moveDomains($oldClient, $newClient);
        $this->_moveRecurring($oldClient, $newClient);
        $this->_moveInvoices($oldClient, $newClient);
        $this->_moveTickets($oldClient, $newClient);
        $this->_moveEmails($oldClient, $newClient);
    }
    
    function _checked($name)
    {
        if (isset($_POST[$name]) && $_POST[$name] == 'on') 
            return true;
        return false;
    }
}

?>
