<?php
/**
* @package XenCentral Feedback System
* @author DNF Technology
* @copyright Drnoyan & Nalyan LDA, Portugal, EU
* @license http://dnf.technology/terms/
* @link http://customers.dnf.technology
* @version 2.0.0 Beta 10
* @revision 12
*/
namespace XenCentral\Feedback\Repository;
use XF\Db\Exception;
use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;
class Import extends Repository
{
use FeedbackAbstractRepository;
/**
* Source database connection.
*
* @var Zend_Db_Adapter_Abstract
*/
protected $_sourceDb;
protected $_config;
protected $_prefix;
protected $_charset = 'windows-1252';
protected $_successFeedback=0;
protected $_successComments=0;
protected $_failFeedback=0;
protected $_failComments=0;
protected $_errors=array();
protected $_validConnection;
public function init($config)
{
if ($this->_sourceDb)
{
// already run
}
@set_time_limit(0);
$this->_config = $config;
$sourceDbConfig ['db'] =
array (
'host' => $config['host'],
'port' => $config['port'],
'username' => $config['username'],
'password' => $config['password'],
'dbname' => $config['dbname'],
'charset' => $config['charset']
);
if ($sourceDbConfig['db']['host']) {
$this->_validConnection = false;
try {
$this->_sourceDb = new \XF\Db\Mysqli\Adapter( $sourceDbConfig['db'], false );
$this->_sourceDb->getConnection();
$this->_validConnection = true;
} catch ( \XF\Db\Exception $e ) {
$errors[] = \XF::phrase( 'source_database_connection_details_not_correct_x', array( 'error' => $e->getMessage() ) );
}
}
$this->_prefix = preg_replace('/[^a-z0-9_]/i', '', $config['prefix']);
if (!empty($config['charset']))
{
$this->_charset = $config['charset'];
}
$this->setSuccessFeedback($config['successFeedback']);
$this->setSuccessComments($config['successComments']);
$this->setFailFeedback($config['failFeedback']);
$this->setFailComments($config['failComments']);
}
public function validateConfiguration(array &$config)
{
$errors = array();
$config['prefix'] = preg_replace('/[^a-z0-9_]/i', '', $config['prefix']);
// validate charset
if ($config['charset'])
{
$config['charset']=strtolower($config['charset']);
if ($config['charset']=='utf-8')
{
$config['charset']='utf8';
}
}
$sourceDbConfig['db'] =
array (
'host' => $config['host'],
'port' => $config['port'],
'username' => $config['username'],
'password' => $config['password'],
'dbname' => $config['dbname'],
'charset' => $config['charset']
);
if ($sourceDbConfig['db']['host']) {
$validDbConnection = false;
try {
$_sourceDb = new \XF\Db\Mysqli\Adapter( $sourceDbConfig['db'], false );
$_sourceDb->getConnection();
$validDbConnection = true;
} catch ( \XF\Db\Exception $e ) {
$errors[] = \XF::phrase( 'source_database_connection_details_not_correct_x', array( 'error' => $e->getMessage() ) );
}
}
$importers=$this->getImporterList();
if(!isset($importers[$config['importer']])) {
$errors[] = \XF::phrase('xcfs_please_choose_importer');
}
if ($errors)
{
return $errors;
}
if ($validDbConnection) {
try {
$_sourceDb = new \XF\Db\Mysqli\Adapter( $sourceDbConfig['db'], false );
$this->_getImporter( $config['importer'] )->assertTablePrefix( $_sourceDb, $config['prefix'] );
} catch ( \XF\Db\Exception $e ) {
if ( $config['dbname'] === '' ) {
$errors[] = \XF::phrase( 'please_enter_database_name' );
} else {
$errors[] = \XF::phrase( 'table_prefix_or_database_name_is_not_correct' );
}
}
if ( ! $errors ) {
$this->_getImporter( $config['importer'] )->preImport( $_sourceDb, $config, $errors );
}
}
return $errors;
}
public function restart()
{
$this->db()->query("
DELETE FROM xf_xc_feedback_feedback
WHERE importid!=0
");
$this->db()->query("
DELETE FROM xf_xc_feedback_comment
WHERE importid!=0
");
return true;
}
public function import($start, $perpage)
{
$importer=$this->_getImporter($this->_config['importer']);
$importer->setSourceDb($this->_sourceDb);
$all_feedback=$importer->getAllFeedback($start, $perpage);
if (count($all_feedback)==0)
{
return false;
}
foreach ($all_feedback AS $feedback)
{
if(!empty($importer->getForUserId($feedback)) && !empty($importer->getFromUserId($feedback))) {
$feedbackWriter = $this->em->create('XenCentral\Feedback:Feedback');
$feedbackWriter->set('foruserid', $importer->getForUserId($feedback));
$feedbackWriter->set('fromuserid', $importer->getFromUserId($feedback));
$feedbackWriter->set('amount', $importer->getAmount($feedback));
$feedbackWriter->set('type', $importer->getType($feedback));
$feedbackWriter->set('dealurl', $this->_convertToUtf8($importer->getDealUrl($feedback)));
$feedbackWriter->set('threadid', $importer->getThreadId($feedback));
$feedbackWriter->set('dateline', $importer->getDateline($feedback));
$feedbackWriter->set('importid', $importer->getOriginalId($feedback));
$feedbackWriter->set('review', $this->_convertToUtf8($importer->getFeedbackText($feedback)));
$feedbackWriter->setOption('saveip', $importer->getIPAddress($feedback));
if ($feedbackWriter->save()) {
$this->_successFeedback++;
$fb_id = $feedbackWriter->get('fb_id');
// get comments
$comments = $importer->getAllComments($feedbackWriter->get('importid'));
foreach ($comments AS $comment) {
$commentWriter = $this->em->create('XenCentral\Feedback:FeedbackComment');
$commentWriter->set('fb_id', $fb_id);
$commentWriter->set('user_id', $importer->getCommentUserId($comment));
$commentWriter->set('message', $this->_convertToUtf8($importer->getCommentText($comment)));
$commentWriter->set('dateline', $importer->getCommentDateline($comment));
$commentWriter->preSave();
if ($commentWriter->save()) {
$this->_successComments++;
} else {
$this->_errors[] = $commentWriter->getErrors();
$this->_failComments++;
}
}
} else {
$this->_errors[] = $feedbackWriter->getErrors();
$this->_failFeedback++;
}
}
}
return true;
}
public function getImporterList()
{
$importers=array();
$importerDirectoryResource=opendir(dirname(__FILE__).'/ImportSource');
while($importer=readdir($importerDirectoryResource)) {
if($importer=='ImportAbstract.php') {
continue;
}
if(is_file(dirname(__FILE__) . '/ImportSource/'.$importer)) {
try {
$importerModel = $this->_getImporter(basename($importer, '.php'));
} catch (Exception $ex) {
$importers[]=$ex->getMessage();
continue;
}
$importers[basename($importer, '.php')]= $importerModel->getImporterTitle();
}
}
return $importers;
}
/**
* @param $name
* @return XenCentral_Feedback_Model_ImportSource_Abstract
* @throws Exception
*/
protected function _getImporter($name)
{
if(!class_exists('XenCentral\Feedback\Repository\ImportSource\\'.$name)) {
throw new \Exception('Importer class not found - '. $name);
}
return \XF::repository('XenCentral\Feedback:ImportSource\\' . $name);
}
/**
* @return the $_successFeedback
*/
public function getSuccessFeedback()
{
return $this->_successFeedback;
}
/**
* @return the $_successComments
*/
public function getSuccessComments()
{
return $this->_successComments;
}
/**
* @return the $_failFeedback
*/
public function getFailFeedback()
{
return $this->_failFeedback;
}
/**
* @return the $_failComments
*/
public function getFailComments()
{
return $this->_failComments;
}
/**
* @param field_type $_successFeedback
*/
public function setSuccessFeedback($_successFeedback)
{
$this->_successFeedback = $_successFeedback;
}
/**
* @param field_type $_successComments
*/
public function setSuccessComments($_successComments)
{
$this->_successComments = $_successComments;
}
/**
* @param field_type $_failFeedback
*/
public function setFailFeedback($_failFeedback)
{
$this->_failFeedback = $_failFeedback;
}
/**
* @param field_type $_failComments
*/
public function setFailComments($_failComments)
{
$this->_failComments = $_failComments;
}
public function getErrors()
{
$errorString=array();
foreach ($this->_errors AS $error)
{
if (is_array($error))
{
foreach ($error AS $e)
{
if (!in_array($e, $errorString))
{
$errorString[]=$e;
}
}
}
else
{
if (!in_array($error, $errorString))
{
$errorString[]=$error;
}
}
}
return implode('<br />', $errorString);
}
protected function _convertToUtf8($string, $entities = null)
{
// note: assumes charset is ascii compatible
if (preg_match('/[\x80-\xff]/', $string)) {
$newString = false;
if (function_exists('iconv')) {
$newString = @iconv($this->_charset, 'utf-8//IGNORE', $string);
}
if (!$newString && function_exists('mb_convert_encoding')) {
$newString = @mb_convert_encoding($string, 'utf-8', $this->_charset);
}
$string = ($newString ? $newString : preg_replace('/[\x80-\xff]/', '', $string));
}
$string = utf8_unhtml($string, $entities);
$string = preg_replace('/[\xF0-\xF7].../', '', $string);
$string = preg_replace('/[\xF8-\xFB]..../', '', $string);
return $string;
}
}