1
|
|
|
<?php |
2
|
|
|
namespace Wabel\Zoho\CRM\Copy; |
3
|
|
|
|
4
|
|
|
use Doctrine\DBAL\Connection; |
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
6
|
|
|
use Doctrine\DBAL\Schema\SchemaDiff; |
7
|
|
|
use Wabel\Zoho\CRM\AbstractZohoDao; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is in charge of synchronizing one table of your database with Zoho records. |
11
|
|
|
*/ |
12
|
|
|
class ZohoDatabaseCopier |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Connection |
17
|
|
|
*/ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
private $prefix; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ZohoDatabaseCopier constructor. |
24
|
|
|
* @param Connection $connection |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Connection $connection, $prefix = "zoho_") |
27
|
|
|
{ |
28
|
|
|
$this->connection = $connection; |
29
|
|
|
$this->prefix = $prefix; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function copy(AbstractZohoDao $dao) |
34
|
|
|
{ |
35
|
|
|
$this->synchronizeDbModel($dao); |
36
|
|
|
$this->copyData($dao); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Synchronizes the DB model with Zoho. |
42
|
|
|
* @param AbstractZohoDao $dao |
43
|
|
|
* @throws \Doctrine\DBAL\DBALException |
44
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
45
|
|
|
*/ |
46
|
|
|
private function synchronizeDbModel(AbstractZohoDao $dao) { |
47
|
|
|
$tableName = $this->prefix.$dao->getModule(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$schema = new Schema(); |
50
|
|
|
$table = $schema->createTable($tableName); |
51
|
|
|
|
52
|
|
|
$flatFields = $this->getFlatFields($dao->getFields()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$table->addColumn("id", "string", ['length'=>100]); |
55
|
|
|
$table->setPrimaryKey(['id']); |
56
|
|
|
|
57
|
|
|
foreach ($flatFields as $field) { |
58
|
|
|
$columnName = $field['name']; |
59
|
|
|
|
60
|
|
|
$length = null; |
61
|
|
|
$index = false; |
62
|
|
|
|
63
|
|
|
// Note: full list of types available here: https://www.zoho.com/crm/help/customization/custom-fields.html |
64
|
|
|
switch ($field['type']) { |
65
|
|
|
case 'Lookup ID': |
66
|
|
|
case 'Lookup': |
67
|
|
|
$type = "string"; |
68
|
|
|
$length = 100; |
69
|
|
|
$index = true; |
70
|
|
|
break; |
71
|
|
|
case 'OwnerLookup': |
72
|
|
|
$type = "string"; |
73
|
|
|
$index = true; |
74
|
|
|
$length = 25; |
75
|
|
|
break; |
76
|
|
|
case 'DateTime': |
77
|
|
|
$type = "datetime"; |
78
|
|
|
break; |
79
|
|
|
case 'Date': |
80
|
|
|
$type = "date"; |
81
|
|
|
break; |
82
|
|
|
case 'DateTime': |
83
|
|
|
$type = "datetime"; |
84
|
|
|
break; |
85
|
|
|
case 'Boolean': |
86
|
|
|
$type = "boolean"; |
87
|
|
|
break; |
88
|
|
|
case 'TextArea': |
89
|
|
|
$type = "text"; |
90
|
|
|
break; |
91
|
|
|
case 'BigInt': |
92
|
|
|
$type = "bigint"; |
93
|
|
|
break; |
94
|
|
|
case 'Phone': |
95
|
|
|
case 'Auto Number': |
96
|
|
|
case 'Text': |
97
|
|
|
case 'URL': |
98
|
|
|
case 'Email': |
99
|
|
|
case 'Website': |
100
|
|
|
case 'Pick List': |
101
|
|
|
case 'Multiselect Pick List': |
102
|
|
|
$type = "string"; |
103
|
|
|
$length = $field['maxlength']; |
104
|
|
|
break; |
105
|
|
|
case 'Double': |
106
|
|
|
case 'Percent': |
107
|
|
|
$type = "float"; |
108
|
|
|
break; |
109
|
|
|
case 'Integer': |
110
|
|
|
$type = "integer"; |
111
|
|
|
break; |
112
|
|
|
case 'Currency': |
113
|
|
|
case 'Decimal': |
114
|
|
|
$type = "decimal"; |
115
|
|
|
break; |
116
|
|
|
default: |
117
|
|
|
throw new \RuntimeException('Unknown type "'.$field['type'].'"'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$options = []; |
121
|
|
|
|
122
|
|
|
if ($length) { |
123
|
|
|
$options['length'] = $length; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//$options['notnull'] = $field['req']; |
|
|
|
|
127
|
|
|
$options['notnull'] = false; |
128
|
|
|
|
129
|
|
|
$table->addColumn($columnName, $type, $options); |
130
|
|
|
|
131
|
|
|
if ($index) { |
132
|
|
|
$table->addIndex([ $columnName ]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$dbSchema = $this->connection->getSchemaManager()->createSchema(); |
137
|
|
|
if ($this->connection->getSchemaManager()->tablesExist($tableName)) { |
|
|
|
|
138
|
|
|
$dbTable = $dbSchema->getTable($tableName); |
139
|
|
|
|
140
|
|
|
$comparator = new \Doctrine\DBAL\Schema\Comparator(); |
141
|
|
|
$tableDiff = $comparator->diffTable($dbTable, $table); |
142
|
|
|
|
143
|
|
View Code Duplication |
if ($tableDiff !== false) { |
|
|
|
|
144
|
|
|
$diff = new SchemaDiff(); |
145
|
|
|
$diff->fromSchema = $dbSchema; |
146
|
|
|
$diff->changedTables[$tableName] = $tableDiff; |
147
|
|
|
$statements = $diff->toSaveSql($this->connection->getDatabasePlatform()); |
148
|
|
|
foreach ($statements as $sql) { |
149
|
|
|
$this->connection->exec($sql); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
View Code Duplication |
} else { |
|
|
|
|
153
|
|
|
$diff = new SchemaDiff(); |
154
|
|
|
$diff->fromSchema = $dbSchema; |
155
|
|
|
$diff->newTables[$tableName] = $table; |
156
|
|
|
$statements = $diff->toSaveSql($this->connection->getDatabasePlatform()); |
157
|
|
|
foreach ($statements as $sql) { |
158
|
|
|
$this->connection->exec($sql); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function copyData(AbstractZohoDao $dao) { |
165
|
|
|
$records = $dao->getRecords(); |
166
|
|
|
$tableName = $this->prefix.$dao->getModule(); |
|
|
|
|
167
|
|
|
|
168
|
|
|
$table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
$flatFields = $this->getFlatFields($dao->getFields()); |
|
|
|
|
172
|
|
|
$fieldsByName = []; |
173
|
|
|
foreach ($flatFields as $field) { |
174
|
|
|
$fieldsByName[$field['name']] = $field; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
178
|
|
|
|
179
|
|
|
foreach ($records as $record) { |
180
|
|
|
$data = []; |
181
|
|
|
$types = []; |
182
|
|
|
foreach ($table->getColumns() as $column) { |
183
|
|
|
if ($column->getName() === 'id') { |
184
|
|
|
continue; |
185
|
|
|
} else { |
186
|
|
|
$field = $fieldsByName[$column->getName()]; |
187
|
|
|
$getterName = $field['getter']; |
188
|
|
|
$data[$column->getName()] = $record->$getterName(); |
189
|
|
|
$types[$column->getName()] = $column->getType()->getName(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$select->execute([ 'id' => $record->getZohoId() ]); |
194
|
|
|
$result = $select->fetch(\PDO::FETCH_ASSOC); |
195
|
|
|
if ($result === false) { |
196
|
|
|
$data['id'] = $record->getZohoId(); |
197
|
|
|
$types['id'] = 'string'; |
198
|
|
|
|
199
|
|
|
$this->connection->insert($tableName, $data, $types); |
200
|
|
|
} else { |
201
|
|
|
$identifier = ['id' => $record->getZohoId() ]; |
202
|
|
|
$types['id'] = 'string'; |
203
|
|
|
|
204
|
|
|
$this->connection->update($tableName, $data, $identifier, $types); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function getFlatFields(array $fields) |
210
|
|
|
{ |
211
|
|
|
$flatFields = []; |
212
|
|
|
foreach ($fields as $cat) { |
213
|
|
|
$flatFields = array_merge($flatFields, $cat); |
214
|
|
|
} |
215
|
|
|
return $flatFields; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.