<?php
class SlotUserUpgrade_UserUpgradeModel extends XFCP_SlotUserUpgrade_UserUpgradeModel
{
    public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
    {
        $retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);
       
        $options = XenForo_Application::get('options');
       
        // before we begin, lets check if the upgrade is allowed a slot on a server
        if(!in_array($upgrade['user_upgrade_id'], $options->slotuserupgrade_checkedupgrades))
        {
            return;
        }
       
        $user = $this->getModelFromCache('XenForo_Model_User')->getFullUserById($userId);
        $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
       
        try
        {
            $slotdb = new Zend_Db_Adapter_Pdo_Mysql(array(
                'host'     => $options->slotuserupgrade_host,
                'username' => $options->slotuserupgrade_username,
                'password' => $options->slotuserupgrade_password,
                'dbname'   => $options->slotuserupgrade_dbname
            ));
           
            $slotdb->getConnection();
        }
        catch (Exception $e)
        {
            // error must have happened, lets log this to XenForo
            XenForo_Error::logException($e, false);
        }
       
        $field = (!empty($user['customFields']['originid']) ? $user['customFields']['originid'] : $user['username']);
        try
        {
            $field = $slotdb->select()->from(array('playerdata' => 'tbl_playerdata'), array('PlayerID'))->where('SoldierName = ?', $field);
            $field = $slotdb->fetchAll($field);
           
            $field = (!empty($field[0]['PlayerID']) ? (int)$field[0]['PlayerID'] : (string)$user['username']);
           
            if(ctype_digit($field)) {
                $data = array('player_group' => 'slot_reserved', 'player_id'  =>  $field);
            } else {
                $data = array('player_group' => 'slot_reserved', 'player_identifier'  =>  $field);
            }
           
            $slotdb->insert('adkats_specialplayers', $data);
        }
        catch (Exception $e)
        {
            // error must have happened, lets log this to XenForo
            XenForo_Error::logException($e, false);
        }
       
        return $retval;
    }
   
    public function downgradeUserUpgrades(array $upgrades, $sendAlert = true)
    {
        if (!$upgrades)
        {
            return;
        }
       
        $retval = parent::downgradeUserUpgrades($upgrades);
       
        $options = XenForo_Application::get('options');
        try
        {
            $slotdb = new Zend_Db_Adapter_Pdo_Mysql(array(
                'host'     => $options->slotuserupgrade_host,
                'username' => $options->slotuserupgrade_username,
                'password' => $options->slotuserupgrade_password,
                'dbname'   => $options->slotuserupgrade_dbname
            ));
           
            $slotdb->getConnection();
        }
        catch (Exception $e)
        {
            // error must have happened, lets log this to XenForo
            XenForo_Error::logException($e, false);
        }
       
        $playerRecors = array();
        foreach ($upgrades AS $upgrade)
        {
            if(!in_array($upgrade['user_upgrade_id'], $options->slotuserupgrade_checkedupgrades))
            {
                continue;
            }
       
            $user = $this->getModelFromCache('XenForo_Model_User')->getFullUserById($upgrade['user_id']);
            $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
            $field = (!empty($user['customFields']['originid']) ? $user['customFields']['originid'] : $user['username']);
           
            try
            {
                $field = $slotdb->select()->from(array('playerdata' => 'tbl_playerdata'), array('PlayerID'))->where('SoldierName = ?', $field);
                $field = $slotdb->fetchAll($field);
               
                $field = (!empty($field[0]['PlayerID']) ? (int)$field[0]['PlayerID'] : (string)$user['username']);
               
                if(ctype_digit($field)) {
                    $data = array('player_id' => $field);
                } else {
                    $data = array('player_identifier'  =>  $field);
                }
                $playerRecors[] = $data;
               
            }
            catch (Exception $e)
            {
                // error must have happened, lets log this to XenForo
                XenForo_Error::logException($e, false);
            }
           
            $slotdb->delete('adkats_specialplayers', 'player_id IN (' . $slotdb->quote($playerRecors) . ') OR player_identifier IN (' . $slotdb->quote($playerRecors) . ')');
        }
        return $retval;
       
    }
}