|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wabel\Zoho\CRM\Copy; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
use Wabel\Zoho\CRM\AbstractZohoDao; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class is in charge of synchronizing one table MODEL with Zoho. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ZohoDatabaseModelSync |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Connection |
|
18
|
|
|
*/ |
|
19
|
|
|
private $connection; |
|
20
|
|
|
|
|
21
|
|
|
private $prefix; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var LoggerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $logger; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var LocalChangesTracker |
|
30
|
|
|
*/ |
|
31
|
|
|
private $localChangesTracker; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ZohoDatabaseCopier constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Connection $connection |
|
37
|
|
|
* @param string $prefix Prefix for the table name in DB |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Connection $connection, $prefix = 'zoho_', LoggerInterface $logger = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->connection = $connection; |
|
42
|
|
|
$this->prefix = $prefix; |
|
43
|
|
|
if ($logger === null) { |
|
44
|
|
|
$this->logger = new NullLogger(); |
|
45
|
|
|
} else { |
|
46
|
|
|
$this->logger = $logger; |
|
47
|
|
|
} |
|
48
|
|
|
$this->localChangesTracker = new LocalChangesTracker($connection, $this->logger); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param LoggerInterface $logger |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setLogger(LoggerInterface $logger) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->logger = $logger; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Synchronizes the DB model with Zoho. |
|
61
|
|
|
* |
|
62
|
|
|
* @param AbstractZohoDao $dao |
|
63
|
|
|
* @param bool $twoWaysSync |
|
64
|
|
|
* @param bool $skipCreateTrigger |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
67
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function synchronizeDbModel(AbstractZohoDao $dao, $twoWaysSync, $skipCreateTrigger = false) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($twoWaysSync === true) { |
|
72
|
|
|
$this->localChangesTracker->createTrackingTables(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($dao, $this->prefix); |
|
76
|
|
|
$this->logger->info('Synchronizing DB Model for '.$tableName); |
|
77
|
|
|
|
|
78
|
|
|
$schema = new Schema(); |
|
79
|
|
|
$table = $schema->createTable($tableName); |
|
80
|
|
|
|
|
81
|
|
|
$flatFields = ZohoDatabaseHelper::getFlatFields($dao->getFields()); |
|
|
|
|
|
|
82
|
|
|
$table->addColumn('uid', 'integer', ['autoincrement' => true]); |
|
83
|
|
|
$table->addColumn('id', 'string', ['length' => 100]); |
|
84
|
|
|
$table->addUniqueIndex(['id']); |
|
85
|
|
|
$table->setPrimaryKey(['uid']); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($flatFields as $field) { |
|
88
|
|
|
$columnName = $field['name']; |
|
89
|
|
|
|
|
90
|
|
|
$length = null; |
|
91
|
|
|
$index = false; |
|
92
|
|
|
|
|
93
|
|
|
// Note: full list of types available here: https://www.zoho.com/crm/help/customization/custom-fields.html |
|
94
|
|
|
switch ($field['type']) { |
|
95
|
|
|
case 'Lookup ID': |
|
96
|
|
|
case 'Lookup': |
|
97
|
|
|
$type = 'string'; |
|
98
|
|
|
$length = 100; |
|
99
|
|
|
$index = true; |
|
100
|
|
|
break; |
|
101
|
|
|
case 'OwnerLookup': |
|
102
|
|
|
$type = 'string'; |
|
103
|
|
|
$index = true; |
|
104
|
|
|
$length = 25; |
|
105
|
|
|
break; |
|
106
|
|
|
case 'Formula': |
|
107
|
|
|
// Note: a Formula can return any type, but we have no way to know which type it returns... |
|
108
|
|
|
$type = 'string'; |
|
109
|
|
|
$length = 100; |
|
110
|
|
|
break; |
|
111
|
|
|
case 'DateTime': |
|
112
|
|
|
$type = 'datetime'; |
|
113
|
|
|
break; |
|
114
|
|
|
case 'Date': |
|
115
|
|
|
$type = 'date'; |
|
116
|
|
|
break; |
|
117
|
|
|
case 'DateTime': |
|
118
|
|
|
$type = 'datetime'; |
|
119
|
|
|
break; |
|
120
|
|
|
case 'Boolean': |
|
121
|
|
|
$type = 'boolean'; |
|
122
|
|
|
break; |
|
123
|
|
|
case 'TextArea': |
|
124
|
|
|
$type = 'text'; |
|
125
|
|
|
break; |
|
126
|
|
|
case 'BigInt': |
|
127
|
|
|
$type = 'bigint'; |
|
128
|
|
|
break; |
|
129
|
|
|
case 'Phone': |
|
130
|
|
|
case 'Auto Number': |
|
131
|
|
|
case 'Text': |
|
132
|
|
|
case 'URL': |
|
133
|
|
|
case 'Email': |
|
134
|
|
|
case 'Website': |
|
135
|
|
|
case 'Pick List': |
|
136
|
|
|
case 'Multiselect Pick List': |
|
137
|
|
|
$type = 'string'; |
|
138
|
|
|
$length = $field['maxlength']; |
|
139
|
|
|
break; |
|
140
|
|
|
case 'Double': |
|
141
|
|
|
case 'Percent': |
|
142
|
|
|
$type = 'float'; |
|
143
|
|
|
break; |
|
144
|
|
|
case 'Integer': |
|
145
|
|
|
$type = 'integer'; |
|
146
|
|
|
break; |
|
147
|
|
|
case 'Currency': |
|
148
|
|
|
case 'Decimal': |
|
149
|
|
|
$type = 'decimal'; |
|
150
|
|
|
break; |
|
151
|
|
|
default: |
|
152
|
|
|
throw new \RuntimeException('Unknown type "'.$field['type'].'"'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$options = []; |
|
156
|
|
|
|
|
157
|
|
|
if ($length) { |
|
158
|
|
|
$options['length'] = $length; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
//$options['notnull'] = $field['req']; |
|
|
|
|
|
|
162
|
|
|
$options['notnull'] = false; |
|
163
|
|
|
|
|
164
|
|
|
$table->addColumn($columnName, $type, $options); |
|
165
|
|
|
|
|
166
|
|
|
if ($index) { |
|
167
|
|
|
$table->addIndex([$columnName]); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$dbalTableDiffService = new DbalTableDiffService($this->connection, $this->logger); |
|
172
|
|
|
$hasChanges = $dbalTableDiffService->createOrUpdateTable($table); |
|
173
|
|
|
if ($twoWaysSync && ($hasChanges || !$skipCreateTrigger)) { |
|
174
|
|
|
$this->localChangesTracker->createInsertTrigger($table); |
|
175
|
|
|
$this->localChangesTracker->createDeleteTrigger($table); |
|
176
|
|
|
$this->localChangesTracker->createUpdateTrigger($table); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
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.