1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-05-05 02:28:17 +03:00

Smartresponder migration with all the fields

This commit is contained in:
Alexander Yakovlev 2016-10-28 12:55:33 +07:00
parent c5f5d0de3d
commit 34237c1d2f
2 changed files with 33 additions and 21 deletions

View file

@ -34,7 +34,6 @@ class Model_Client extends ORM {
),
'name' => array(
array('not_empty'),
array('min_length', array(':value', 5)),
),
'sex' => array(
array('max_length', array(':value', 1)),

View file

@ -14,14 +14,14 @@ class Task_Migrate_Smartresponder extends Minion_Task
{
protected $_options = array(
'csv' => NULL,
'group' => NULL,
'group_id' => NULL,
);
public function build_validation(Validation $validation)
{
return parent::build_validation($validation)
->rule('csv', 'not_empty') // Require this param
->rule('group', 'not_empty'); // Require this param
->rule('group_id', 'not_empty'); // Require this param
}
/**
@ -44,9 +44,9 @@ class Task_Migrate_Smartresponder extends Minion_Task
$db->begin();
$transcoder = Transcoder::create();
$group = ORM::factory('Group')->where('name', '=', $params['group'])->find();
$group = ORM::factory('Group')->where('id', '=', $params['group_id'])->find();
if ( ! $group->loaded()) {
echo "No group with name " . $params['group'] . " found.\n";
echo "No group with id " . $params['group_id'] . " found.\n";
return;
}
@ -54,24 +54,37 @@ class Task_Migrate_Smartresponder extends Minion_Task
{
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];
$client = ORM::factory('Client')->where('email', '=', $data[0]);
if (!$client->loaded()) {
$client = ORM::factory('Client');
$client->email = $data[0];
}
$name = trim($transcoder->transcode($data[1] . ' ' . $data[2], 'cp1251'));
$client->name = $name;
$client->sex = $data[5];
if (!empty($data[13]))
{
$client->referrer = $data[13];
}
if (!empty($data[7]))
{
$client->city = $transcoder->transcode($data[7], 'cp1251');
}
if (!empty($data[6]))
{
$client->country = $data[6];
}
echo "Importing client " . $name . ".\n";
$query->execute();
try
{
$client->customize();
$client->save();
} catch (ORM_Validation_Exception $e) {
continue;
var_dump($client->object());
}
$client->add('group', $group->id);
}
$db->commit();
} else {