• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Importer not importing

Mythotical

Well-known member
#1
I finally got an importer to work, the problem I ran into is when I run the importer I get a return of 0 entries imported. Not sure where I am going wrong but this is my first attempt at an importer and I am getting a headache from it already.

Here is my importer file:
PHP:
<?php
error_reporting (E_ALL ^ E_NOTICE);

class MyShop_Importer_bdBank extends XenForo_Importer_Abstract
{
    protected $_sourceDb;

    protected $_prefix;

    protected $_config;

    protected $_charset = 'windows-1252';

    public static function getName()
    {
        return '[bd] Banking 0.9.9.1';
    }

public function configure(XenForo_ControllerAdmin_Abstract $controller, array &$config)
    {
        if ($config)
        {
            if ($errors = $this->validateConfiguration($config))
                return $controller->responseError($errors);

            $this->_bootstrap($config);

            return true;
        }

        $configPath = getcwd() . '/includes/config.php';
        if (file_exists($configPath) && is_readable($configPath))
        {
            $config = array();
            include($configPath);

            $viewParams = array('input' => $config);
        }
        else
        {
            $viewParams = array('input' => array
            (
                'MasterServer' => array
                (
                    'servername' => 'localhost',
                    'port' => 3306,
                    'username' => '',
                    'password' => '',
                ),
                'Database' => array
                (
                    'dbname' => '',
                    'tableprefix' => ''
                ),
                'Mysqli' => array
                (
                    'charset' => ''
                ),
            ));
        }
            
            return $controller->responseView('XenForo_ViewAdmin_Import_bdBank_Config', 'import_bdbank_config');
    }

    public function validateConfiguration(array &$config)
    {
        $errors = array();

        $config['db']['prefix'] = preg_replace('/[^a-z0-9_]/i', '', $config['db']['prefix']);

        try
        {
            $db = Zend_Db::factory('mysqli',
                array(
                    'host' => $config['db']['host'],
                    'port' => $config['db']['port'],
                    'username' => $config['db']['username'],
                    'password' => $config['db']['password'],
                    'dbname' => $config['db']['dbname']
                )
            );
            $db->getConnection();
        }
        catch (Zend_Db_Exception $e)
        {
            $errors[] = new XenForo_Phrase('source_database_connection_details_not_correct_x', array('error' => $e->getMessage()));
        }

        if ($errors)
        {
            return $errors;
        }

        try
        {
            $db->query('
                SELECT user_id, bdbank_money
                FROM ' . $config['db']['prefix'] . 'user
                LIMIT 1
            ');
        }
        catch (Zend_Db_Exception $e)
        {
            if ($config['db']['dbname'] === '')
            {
                $errors[] = new XenForo_Phrase('please_enter_database_name');
            }
            else
            {
                $errors[] = new XenForo_Phrase('table_prefix_or_database_name_is_not_correct');
            }
        }

        return $errors;
    }

    public function getSteps()
    {
        return array(
            'bdbank' => array(
                'title' => new XenForo_Phrase('import_bdbank_money')
            )
        );
    }

    protected function _bootstrap(array $config)
    {
        if ($this->_sourceDb)
        {
            // already run
            return;
        }

        @set_time_limit(0);

        $this->_config = $config;

        $this->_sourceDb = Zend_Db::factory('mysqli',
            array(
                'host' => $config['db']['host'],
                'port' => $config['db']['port'],
                'username' => $config['db']['username'],
                'password' => $config['db']['password'],
                'dbname' => $config['db']['dbname']
            )
        );

        $this->_prefix = preg_replace('/[^a-z0-9_]/i', '', $config['db']['prefix']);

        if (!empty($config['charset']))
        {
            $this->_charset = $config['charset'];
        }
    }

    public function stepBdbank($start, array $options)
    {
        
        $options = array_merge(array(
            'limit' => 50,
            'max' => false
        ), $options);

        $sDb = $this->_sourceDb;
        $prefix = $this->_prefix;

        /* @var $model XenForo_Model_Import */
        $importModel = $this->_importModel;

        if ($options['max'] === false)
        {
            $options['max'] = $sDb->fetchOne('
                SELECT MAX(user_id)
                FROM ' . $prefix . 'user
            ');
        }

        $money = $sDb->fetchAll($sDb->limit(
            '
                SELECT *
                FROM ' . $prefix . 'user
                WHERE user_id > ' . $sDb->quote($start) . '
                ORDER BY user_id
            ', $options['limit']
        ));
        
        $myshop = $sDb->fetchAll('SELECT *
            FROM myshop_points
            WHERE user_id = ?
            ', $money['user_id']);
            
        if ($myshop){
            $points_total = $myshop['points_total'] + $money['bdbank_money'];
            $points_earned = $myshop['points_earned'] + $money['bdbank_money'];
        } else {
            $points_total = $money['bdbank_money'];
            $points_earned = $money['bdbank_money'];
        }

        $total = 0;

        XenForo_Db::beginTransaction();
        
        foreach ($money AS $points)
        {
            switch ($points['user_id'])
            {

                default:
                    $import = array(
                        'user_id'            => $points['user_id'],
                        'points_total'        => $points_total,
                        'points_earned'     => $points_earned
            );
            if ($importModel->importBdbankMoney($points['user_id'], $import))
            {
            $total++;
            }
        }
        }
        
        XenForo_Db::commit();
        
        $this->_session->incrementStepImportTotal($total);

        return true;
    }
}
Here is my datawriter for importing of points:
PHP:
<?php

class MyShop_DataWriter_Points extends XenForo_DataWriter
{
    protected function _getFields()
    {
        return array(
            'myshop_points' => array(
                'points_id' => array('type' => self::TYPE_UINT, 'autoIncrement' => true),
                'user_id' => array('type' => self::TYPE_UINT),
                'points_total' => array('type' => self::TYPE_UINT, 'default' => 0),
                'points_earned' => array('type' => self::TYPE_UINT, 'default' => 0)
            )
        );
    }

    protected function _getExistingData($data)
    {
        if (!$pointsid = $this->_getExistingPrimaryKey($data, 'points_id'))
        {
            return false;
        }
        return array('myshop_points' => $this->getModelFromCache('MyShop_Model_Points')->getPointsId($pointsid));
    }

    protected function _getUpdateCondition($tableName)
    {
        return 'points_id = ' . $this->_db->quote($this->getExisting('points_id'));
    }
    protected function _postDelete()
    {
        $model = $this->getModelFromCache('MyShop_Model_points');

        $deletedId = $this->_existingData['myshop_points']['points_id'];
        if ($model->hasData($deletedId))
        {
            $dw->delete();
        }
    }
}
And finally my import model:
PHP:
<?php

class MyShop_Model_Import extends XFCP_MyShop_Model_Import
{
    public function __construct()
    {
        parent::__construct();
        self::$extraImporters[] = 'MyShop_Importer_bdBank';
    }
    public function importBdbankMoney($oldId, array $import)
    {
        // I'm doing this a little hacky by not using _importData() since I want postSave() to run so users are updated without needing to do a second import
        // So this is basically _importData but without setting the import mode

        $errorHandler = XenForo_DataWriter::ERROR_ARRAY;

        XenForo_Db::beginTransaction();

        $dw = XenForo_DataWriter::create('MyShop_DataWriter_Points', $errorHandler);

        $dw->bulkSet($import);

        $newId = false;
        if ($dw->save())
        {
            $newId = $dw->get('point_id');
            if ($oldId !== 0 && $oldId !== '')
            {
                $this->logImportData('points', $oldId, $newId);
            }
        }

        XenForo_Db::commit();

        return $newId;
    }
}
If you need to see templates just let me know and I'll get the main template posted.