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
|
|
|
use function Stringy\create as s; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class is in charge of synchronizing one table of your database with Zoho records. |
12
|
|
|
*/ |
13
|
|
|
class ZohoDatabaseCopier |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Connection |
18
|
|
|
*/ |
19
|
|
|
private $connection; |
20
|
|
|
|
21
|
|
|
private $prefix; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ZohoChangeListener[] |
25
|
|
|
*/ |
26
|
|
|
private $listeners; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ZohoDatabaseCopier constructor. |
30
|
|
|
* @param Connection $connection |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Connection $connection, $prefix = "zoho_", array $listeners = []) |
33
|
|
|
{ |
34
|
|
|
$this->connection = $connection; |
35
|
|
|
$this->prefix = $prefix; |
36
|
|
|
$this->listeners = $listeners; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param AbstractZohoDao $dao |
41
|
|
|
* @param bool $incrementalSync Whether we synchronize only the modified files or everything. |
42
|
|
|
*/ |
43
|
|
|
public function copy(AbstractZohoDao $dao, $incrementalSync = true) |
44
|
|
|
{ |
45
|
|
|
$this->synchronizeDbModel($dao); |
46
|
|
|
$this->copyData($dao, $incrementalSync); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Synchronizes the DB model with Zoho. |
52
|
|
|
* @param AbstractZohoDao $dao |
53
|
|
|
* @throws \Doctrine\DBAL\DBALException |
54
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
55
|
|
|
*/ |
56
|
|
|
private function synchronizeDbModel(AbstractZohoDao $dao) { |
57
|
|
|
$tableName = $this->getTableName($dao); |
58
|
|
|
|
59
|
|
|
$schema = new Schema(); |
60
|
|
|
$table = $schema->createTable($tableName); |
61
|
|
|
|
62
|
|
|
$flatFields = $this->getFlatFields($dao->getFields()); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$table->addColumn("id", "string", ['length'=>100]); |
65
|
|
|
$table->setPrimaryKey(['id']); |
66
|
|
|
|
67
|
|
|
foreach ($flatFields as $field) { |
68
|
|
|
$columnName = $field['name']; |
69
|
|
|
|
70
|
|
|
$length = null; |
71
|
|
|
$index = false; |
72
|
|
|
|
73
|
|
|
// Note: full list of types available here: https://www.zoho.com/crm/help/customization/custom-fields.html |
74
|
|
|
switch ($field['type']) { |
75
|
|
|
case 'Lookup ID': |
76
|
|
|
case 'Lookup': |
77
|
|
|
$type = "string"; |
78
|
|
|
$length = 100; |
79
|
|
|
$index = true; |
80
|
|
|
break; |
81
|
|
|
case 'OwnerLookup': |
82
|
|
|
$type = "string"; |
83
|
|
|
$index = true; |
84
|
|
|
$length = 25; |
85
|
|
|
break; |
86
|
|
|
case 'Formula': |
87
|
|
|
// Note: a Formula can return any type, but we have no way to know which type it returns... |
88
|
|
|
$type = "string"; |
89
|
|
|
$length = 100; |
90
|
|
|
break; |
91
|
|
|
case 'DateTime': |
92
|
|
|
$type = "datetime"; |
93
|
|
|
break; |
94
|
|
|
case 'Date': |
95
|
|
|
$type = "date"; |
96
|
|
|
break; |
97
|
|
|
case 'DateTime': |
98
|
|
|
$type = "datetime"; |
99
|
|
|
break; |
100
|
|
|
case 'Boolean': |
101
|
|
|
$type = "boolean"; |
102
|
|
|
break; |
103
|
|
|
case 'TextArea': |
104
|
|
|
$type = "text"; |
105
|
|
|
break; |
106
|
|
|
case 'BigInt': |
107
|
|
|
$type = "bigint"; |
108
|
|
|
break; |
109
|
|
|
case 'Phone': |
110
|
|
|
case 'Auto Number': |
111
|
|
|
case 'Text': |
112
|
|
|
case 'URL': |
113
|
|
|
case 'Email': |
114
|
|
|
case 'Website': |
115
|
|
|
case 'Pick List': |
116
|
|
|
case 'Multiselect Pick List': |
117
|
|
|
$type = "string"; |
118
|
|
|
$length = $field['maxlength']; |
119
|
|
|
break; |
120
|
|
|
case 'Double': |
121
|
|
|
case 'Percent': |
122
|
|
|
$type = "float"; |
123
|
|
|
break; |
124
|
|
|
case 'Integer': |
125
|
|
|
$type = "integer"; |
126
|
|
|
break; |
127
|
|
|
case 'Currency': |
128
|
|
|
case 'Decimal': |
129
|
|
|
$type = "decimal"; |
130
|
|
|
break; |
131
|
|
|
default: |
132
|
|
|
throw new \RuntimeException('Unknown type "'.$field['type'].'"'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$options = []; |
136
|
|
|
|
137
|
|
|
if ($length) { |
138
|
|
|
$options['length'] = $length; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
//$options['notnull'] = $field['req']; |
|
|
|
|
142
|
|
|
$options['notnull'] = false; |
143
|
|
|
|
144
|
|
|
$table->addColumn($columnName, $type, $options); |
145
|
|
|
|
146
|
|
|
if ($index) { |
147
|
|
|
$table->addIndex([ $columnName ]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$dbSchema = $this->connection->getSchemaManager()->createSchema(); |
152
|
|
|
if ($this->connection->getSchemaManager()->tablesExist($tableName)) { |
|
|
|
|
153
|
|
|
$dbTable = $dbSchema->getTable($tableName); |
154
|
|
|
|
155
|
|
|
$comparator = new \Doctrine\DBAL\Schema\Comparator(); |
156
|
|
|
$tableDiff = $comparator->diffTable($dbTable, $table); |
157
|
|
|
|
158
|
|
View Code Duplication |
if ($tableDiff !== false) { |
|
|
|
|
159
|
|
|
$diff = new SchemaDiff(); |
160
|
|
|
$diff->fromSchema = $dbSchema; |
161
|
|
|
$diff->changedTables[$tableName] = $tableDiff; |
162
|
|
|
$statements = $diff->toSaveSql($this->connection->getDatabasePlatform()); |
163
|
|
|
foreach ($statements as $sql) { |
164
|
|
|
$this->connection->exec($sql); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
View Code Duplication |
} else { |
|
|
|
|
168
|
|
|
$diff = new SchemaDiff(); |
169
|
|
|
$diff->fromSchema = $dbSchema; |
170
|
|
|
$diff->newTables[$tableName] = $table; |
171
|
|
|
$statements = $diff->toSaveSql($this->connection->getDatabasePlatform()); |
172
|
|
|
foreach ($statements as $sql) { |
173
|
|
|
$this->connection->exec($sql); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param AbstractZohoDao $dao |
181
|
|
|
* @param bool $incrementalSync Whether we synchronize only the modified files or everything. |
182
|
|
|
* @throws \Doctrine\DBAL\DBALException |
183
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
184
|
|
|
* @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException |
185
|
|
|
*/ |
186
|
|
|
private function copyData(AbstractZohoDao $dao, $incrementalSync = true) { |
187
|
|
|
$tableName = $this->getTableName($dao); |
188
|
|
|
|
189
|
|
|
if ($incrementalSync) { |
190
|
|
|
// Let's get the last modification date: |
191
|
|
|
$lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName); |
192
|
|
|
if ($lastActivityTime !== null) { |
193
|
|
|
$lastActivityTime = new \DateTime($lastActivityTime); |
194
|
|
|
} |
195
|
|
|
$records = $dao->getRecords(null, null, $lastActivityTime); |
196
|
|
|
} else { |
197
|
|
|
$records = $dao->getRecords(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
$table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
$flatFields = $this->getFlatFields($dao->getFields()); |
|
|
|
|
205
|
|
|
$fieldsByName = []; |
206
|
|
|
foreach ($flatFields as $field) { |
207
|
|
|
$fieldsByName[$field['name']] = $field; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
211
|
|
|
|
212
|
|
|
$this->connection->beginTransaction(); |
213
|
|
|
|
214
|
|
|
foreach ($records as $record) { |
215
|
|
|
$data = []; |
216
|
|
|
$types = []; |
217
|
|
|
foreach ($table->getColumns() as $column) { |
218
|
|
|
if ($column->getName() === 'id') { |
219
|
|
|
continue; |
220
|
|
|
} else { |
221
|
|
|
$field = $fieldsByName[$column->getName()]; |
222
|
|
|
$getterName = $field['getter']; |
223
|
|
|
$data[$column->getName()] = $record->$getterName(); |
224
|
|
|
$types[$column->getName()] = $column->getType()->getName(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$select->execute([ 'id' => $record->getZohoId() ]); |
229
|
|
|
$result = $select->fetch(\PDO::FETCH_ASSOC); |
230
|
|
|
if ($result === false) { |
231
|
|
|
$data['id'] = $record->getZohoId(); |
232
|
|
|
$types['id'] = 'string'; |
233
|
|
|
|
234
|
|
|
$this->connection->insert($tableName, $data, $types); |
235
|
|
|
|
236
|
|
|
foreach ($this->listeners as $listener) { |
237
|
|
|
$listener->onInsert($data, $dao); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
} else { |
240
|
|
|
$identifier = ['id' => $record->getZohoId() ]; |
241
|
|
|
$types['id'] = 'string'; |
242
|
|
|
|
243
|
|
|
$this->connection->update($tableName, $data, $identifier, $types); |
244
|
|
|
|
245
|
|
|
// Let's add the id for the update trigger |
246
|
|
|
$data['id'] = $record->getZohoId(); |
247
|
|
|
foreach ($this->listeners as $listener) { |
248
|
|
|
$listener->onUpdate($data, $result, $dao); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->connection->commit(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function getFlatFields(array $fields) |
257
|
|
|
{ |
258
|
|
|
$flatFields = []; |
259
|
|
|
foreach ($fields as $cat) { |
260
|
|
|
$flatFields = array_merge($flatFields, $cat); |
261
|
|
|
} |
262
|
|
|
return $flatFields; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Computes the name of the table based on the DAO plural module name. |
267
|
|
|
* |
268
|
|
|
* @param AbstractZohoDao $dao |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
private function getTableName(AbstractZohoDao $dao) { |
272
|
|
|
$tableName = $this->prefix.$dao->getPluralModuleName(); |
|
|
|
|
273
|
|
|
$tableName= s($tableName)->upperCamelize()->underscored(); |
274
|
|
|
return (string) $tableName; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
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.