forked from eileenmcnaughton/civimigrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civimigrate.class.inc
187 lines (170 loc) · 6.04 KB
/
civimigrate.class.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
abstract class Civimigration extends Migration {
protected $base_table;
protected $base_table_id;
protected $base_table_alias;
protected $query;
protected $_db = 'default';
protected $entity = 'contact';
protected $debug = 0;
protected $autoquery= 1;
protected $matchexternalidentifier = 1;// if set then for contacts the external_identifier will be used to look up existing contacts
public function __construct(array $arguments) {
parent::__construct($arguments);
$this->destination = new MigrateDestinationCivicrmApi($this->entity);
if($this->debug ==1){
$this->destination->debug=1;
}
if(!empty($this->base_table) && !empty($this->base_table_id)){
$column = $this->gettableField($this->base_table, $this->base_table_id);
// 'type' is returned as 'int(11)' - need to separated if there is a length
if(isset($column->type)){
$type = explode('(', $column->type);
}
else{
$type = explode('(', $column->Type);
}
$length = array();
if (array_key_exists(1, $type)) { //length key
$length = array(
'length' => substr($type[1], 0, - 1)
);
}
if(isset($column->null)){
$null = $column->null;
}
else{
$null = $column->Null;
}
$this->map = new MigrateSQLMap($this->machineName, array(
$this->base_table_id => array(
'type' => $type[0],
'not null' => $null,
'alias' => empty($this->base_table_alias) ? $this->base_table : $this->base_table_alias
) + $length
), MigrateDestinationCivicrmApi::getKeySchema());
if($this->autoquery == 1){
$query = $this->getQuery($this->base_table,$this->base_table_alias,$this->base_table_id);
$this->source = new MigrateSourceSQL($query);
}
}
$mappings = $this->getmappings();
foreach ($mappings as $mapping) {
$destField = empty($mapping->mappings_destination_field) ? null : $mapping->mappings_destination_field;
$sourceField = empty($mapping->mappings_source_field) ? null : $mapping->mappings_source_field;
if(empty($sourceField)){
$this->addFieldMapping($destField)
->description($mapping->mappings_description)
->issueGroup($mapping->mappings_issuegroup)
->defaultValue($mapping->mappings_default) ;
}else{
$this->addFieldMapping($destField, $sourceField)
->description($mapping->mappings_description)
->issueGroup($mapping->mappings_issuegroup)
->defaultValue($mapping->mappings_default) ;
}
}
}
/*
* You can override this but the default function does a lookup on external identifier
* and does a match on that if present
*/
public function prepare(&$entity, &$row) {
if($this->matchexternalidentifier && strtolower($this->entity) == 'contact' && empty($entity->id) && !empty($entity->external_identifier) ){
$existingID = (civicrm_api('contact','getvalue',array('version' => 3,'return' => 'id', 'external_identifier' => $entity->external_identifier )));
if(!empty($existingID) && !is_array($existingID)){
$entity->id = $existingID;
}
}
}
/*
* Get started DB object based on table
*/
protected function getQuery($table,$alias, $idKey =""){
$query = Database::getConnection('default', $this->_db)
->select($table, $alias);
$this->addFieldsFromtable($query,$table,$alias, $idKey);
return $query;
}
/*
* Add all fields to function (there is a function to do this as a * but
* by adding them by name they become visible in the source fields in migrateUI
*/
protected function addFieldsFromtable(&$query, $table, $alias, $idKey = null){
$fields = $this->gettableFields($table, $alias);
foreach ($fields as $field) {
if(strtolower($field) == strtolower($idKey)){
$query->addField($alias, $field, strtolower($field));
}else{
$query->addField($alias, $field, strtolower($alias . "_" . $field));
}
}
return $query;
}
/*
* Get specific field in table
*/
protected function gettableField($table, $column){
$columns = Database::getConnection('default', $this->_db)
->query("SHOW columns from {$this->base_table} WHERE field = :column",array( 'column' => $column ));
foreach ($columns as $column ) {
return $column;
}
}
/*
* Get specific field in table
*/
protected function getmappings(){
// Force the mapping query to be executed in the default database, as the
// civimigrate_mappings are stored there.
$origdb = $this->_db;
$this->_db = 'default';
$mappingquery = $this->getQuery('civimigrate_mappings','mappings');
$mappingquery->condition('migration', $this->machineName);
$mappings = $mappingquery->execute();
$this->_db = $origdb;
return $mappings;
}
/*
* Get all fields in table
*/
protected function gettableFields($table, $alias = ""){
$fields = array();
$result = Database::getConnection('default', $this->_db)
->query("SHOW COLUMNS FROM {$table}");
foreach ($result as $record) {
$fields[] = $record->Field;
}
return $fields;
}
/*
* Get all fields in table
*/
protected function addtableFields(&$query,$table, $alias = ""){
if(empty($alias)){
$alias = $table;
}
$fields = array();
$result = Database::getConnection('default', $this->_db)
->query("SHOW COLUMNS FROM {$table}");
foreach ($result as $record) {
$query->addField($alias, $record->field, $alias . "_" . strtolower($record->field));
}
}
/*
* Convert Microsoft Timestamp to mysql friendly date.
* @param string $ms_date MS date string
* @param Bool $force Definitely needs conversion - by default function tests if it is already a valid timestamp
* (this is in case data is inconsistently being sent through)
*
*/
function mssql2mysql($ms_date, $force = 0)
{
if(empty($ms_date)){
return null;
}
if(!strtotime($ms_date) || $force)
$timestamp = ($ms_date - 25569) * 86400;
return date('Y-m-d',$timestamp);
}
}