1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-16 23:11:07 +03:00

Update on issue #2: Smartresponder import

This commit is contained in:
Alexander Yakovlev 2016-10-15 13:39:02 +07:00
parent 85d02681af
commit 3e14176c1f
4 changed files with 128 additions and 42 deletions

View file

@ -12,7 +12,3 @@ trim_trailing_whitespace = true
[*.bat]
end_of_line = crlf
[*.yml]
indent_style = space
indent_size = 2

View file

@ -1,4 +1,7 @@
<?php defined('SYSPATH') OR die('No direct script access.');
use Ddeboer\Transcoder\Transcoder;
/**
* This is a task to migrate client database from SmartResponder csv dump
* It can accept -csv option.
@ -11,12 +14,14 @@ class Task_Migrate_Smartresponder extends Minion_Task
{
protected $_options = array(
'csv' => NULL,
'group' => NULL,
);
public function build_validation(Validation $validation)
{
return parent::build_validation($validation)
->rule('csv', 'not_empty'); // Require this param
->rule('csv', 'not_empty') // Require this param
->rule('group', 'not_empty'); // Require this param
}
/**
@ -24,37 +29,58 @@ class Task_Migrate_Smartresponder extends Minion_Task
*
* @return null
*/
protected function _execute(array $params)
protected function _execute()
{
$params = $this->get_options();
$path = $params['csv'];
if (file_exists($path))
if (!file_exists($path))
{
$db = Database::instance();
$db->begin();
try
{
$csv = file($path);
foreach ($csv as $line_num => $line)
{
$arr = explode(',',$line);
DB::insert('clients', array('email', 'name'))->values(array(trim($arr[0], '"'), trim($arr[1], '"')))->execute();
echo "ERROR: File not found.";
return;
}
$db = Database::instance();
$db->begin();
$transcoder = Transcoder::create();
$group = ORM::factory('Group')->where('name', '=', $params['group'])->find();
if ( ! $group->loaded()) {
echo "No group with name " . $params['group'] . " found.\n";
return;
}
try
{
if (($handle = fopen($path, "r")) !== FALSE) {
echo "File opened.\n";
$query = DB::query(
Database::INSERT,
'INSERT INTO clients (email, name, group_id)
VALUES (:email, :name, :group_id)'
)
->bind(':email', $email)
->bind(':name', $name)
->bind(':group_id', $group->id);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$email = $data[0];
$name = $transcoder->transcode($data[1] . ' ' . $data[2], 'cp1251');
$sex = $data[5];
$referrer = $data[13];
$city = $transcoder->transcode($data[7], 'cp1251');
$country = $data[6];
echo "Importing client " . $name . ".\n";
$query->execute();
}
/*
$csv = fopen($path, 'r');
while (!feof($csv)) {
$string = fgets($csv, 1024);
$arr = explode(',',$string);
echo $string;
DB::insert('clients', array('name', 'email'))->values(array(trim($arr[0], '"'), trim($arr[1], '"')))->execute();
}
fclose($csv);
*/
$db->commit();
} else {
echo "Could not open the file.";
}
catch(Database_Exception $e)
{
$db->rollback();
}
}
catch(Database_Exception $e)
{
$db->rollback();
}
}
}

View file

@ -37,7 +37,8 @@
"zombor/kostache": "^4.0",
"tscms/email": "dev-3.3/master",
"twbs/bootstrap-sass": "^3.3",
"zer0pants/kohana-migrations": "dev-3.4/develop"
"zer0pants/kohana-migrations": "dev-3.4/develop",
"ddeboer/transcoder": "^1.0"
},
"require-dev": {
"phing/phing": "dev-master",

83
composer.lock generated
View file

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "cdd5d55f92469dea3bcd533ffcd17fde",
"content-hash": "05d74ec93f60bdb8b4c933cbc4b857f6",
"hash": "5da8cc0c49d54bcb6c979650e290f395",
"content-hash": "74aefb055ab354ad7516e9b43041c3c8",
"packages": [
{
"name": "composer/installers",
@ -114,6 +114,69 @@
],
"time": "2016-08-13 20:53:52"
},
{
"name": "ddeboer/transcoder",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/ddeboer/transcoder.git",
"reference": "e56652fe3b97908def6f53d97498952bbf13a9f6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ddeboer/transcoder/zipball/e56652fe3b97908def6f53d97498952bbf13a9f6",
"reference": "e56652fe3b97908def6f53d97498952bbf13a9f6",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"suggest": {
"ext-iconv": "For using the IconvTranscoder",
"ext-mbstring": "For using the MbTranscoder"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Ddeboer\\Transcoder\\": "src/",
"Ddeboer\\Transcoder\\Tests\\": "tests/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "David de Boer",
"email": "david@ddeboer.nl"
},
{
"name": "Community contributors",
"homepage": "https://github.com/ddeboer/transcoder/graphs/contributors"
}
],
"description": "Better encoding conversion for PHP",
"keywords": [
"charset",
"encoding",
"iconv",
"iso",
"mb",
"mb_convert_encoding",
"multibyte",
"utf-8"
],
"time": "2015-01-23 17:55:34"
},
{
"name": "kohana/auth",
"version": "dev-3.4/develop",
@ -1198,12 +1261,12 @@
"source": {
"type": "git",
"url": "https://github.com/phingofficial/phing.git",
"reference": "74af0362988dfd6c53eaf7f6ade823d8e7d40348"
"reference": "5de3dcc4effa2753485b3d64a5a64987faea6353"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phingofficial/phing/zipball/74af0362988dfd6c53eaf7f6ade823d8e7d40348",
"reference": "74af0362988dfd6c53eaf7f6ade823d8e7d40348",
"url": "https://api.github.com/repos/phingofficial/phing/zipball/5de3dcc4effa2753485b3d64a5a64987faea6353",
"reference": "5de3dcc4effa2753485b3d64a5a64987faea6353",
"shasum": ""
},
"require": {
@ -1283,7 +1346,7 @@
"task",
"tool"
],
"time": "2016-10-10 11:51:22"
"time": "2016-10-12 13:07:40"
},
{
"name": "phpdocumentor/reflection-common",
@ -2373,12 +2436,12 @@
"source": {
"type": "git",
"url": "https://github.com/webmozart/assert.git",
"reference": "1a6cc8d09dd675802ca93de290ac483c10ef95e0"
"reference": "9350cea5bf343b657f24e68980490021791935d8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webmozart/assert/zipball/1a6cc8d09dd675802ca93de290ac483c10ef95e0",
"reference": "1a6cc8d09dd675802ca93de290ac483c10ef95e0",
"url": "https://api.github.com/repos/webmozart/assert/zipball/9350cea5bf343b657f24e68980490021791935d8",
"reference": "9350cea5bf343b657f24e68980490021791935d8",
"shasum": ""
},
"require": {
@ -2415,7 +2478,7 @@
"check",
"validate"
],
"time": "2016-10-10 07:45:47"
"time": "2016-10-14 13:57:37"
}
],
"aliases": [],