-
Notifications
You must be signed in to change notification settings - Fork 0
/
mexicoregions.php
203 lines (203 loc) · 5.44 KB
/
mexicoregions.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Provide config options for the extension.
*
* @return array
* Array of:
* - whether to remove existing states/provinces,
* - ISO abbreviation of the country,
* - list of states/provinces with abbreviation,
* - list of states/provinces to rename,
*/
function mexicoregions_stateConfig() {
$config = array(
// CAUTION: only use `overwrite` on fresh databases.
'overwrite' => TRUE,
'countryIso' => 'MX',
'states' => array(
// 'state name' => 'abbreviation',
'Abia State' => '1',
'Adamawa State' => '2',
'Akwa Ibom State' => '3',
'Anambra State' => '4',
'Bauchi State' => '5',
'Bayelsa State' => '6',
'Benue State' => '7',
'Borno State' => '8',
'Cross River State' => '9',
'Delta State' => '10',
'Ebonyi State' => '11',
'Edo State' => '12',
'Ekiti State' => '13',
'Enugu State' => '14',
'FCT' => '15',
'Gombe State' => '16',
'Imo State' => '17',
'Jigawa State' => '18',
'Kaduna State' => '19',
'Kano State' => '20',
'Katsina State' => '21',
'Kebbi State' => '22',
'Kogi State' => '23',
'Kwara State' => '24',
'Lagos State' => '25',
'Nasarawa State' => '26',
'Niger State' => '27',
'Ogun State' => '28',
'Ondo State' => '29',
'Osun State' => '30',
'Oyo State' => '31',
'Plateau State' => '32',
'Rivers State' => '33',
'Sokoto State' => '34',
'Taraba State' => '35',
'Yobe State' => '36',
'Zamfara State' => '37',
),
'rewrites' => array(
// List states to rewrite in the format:
// 'Default State Name' => 'Corrected State Name',
),
);
return $config;
}
/**
* Check and load states/provinces.
*
* @return bool
* Success true/false.
*/
function mexicoregions_loadProvinces() {
$stateConfig = mexicoregions_stateConfig();
if (empty($stateConfig['states']) || empty($stateConfig['countryIso'])) {
return FALSE;
}
static $dao = NULL;
if (!$dao) {
$dao = new CRM_Core_DAO();
}
$statesToAdd = $stateConfig['states'];
try {
$countryId = civicrm_api3('Country', 'getvalue', array(
'return' => 'id',
'iso_code' => $stateConfig['countryIso'],
));
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error: %1', array(
'domain' => 'org.ndi.mexicoregions',
1 => $error,
)));
return FALSE;
}
// Rewrite states.
if (!empty($stateConfig['rewrites'])) {
foreach ($stateConfig['rewrites'] as $old => $new) {
$sql = 'UPDATE civicrm_state_province SET name = %1 WHERE name = %2 and country_id = %3';
$stateParams = array(
1 => array(
$new,
'String',
),
2 => array(
$old,
'String',
),
3 => array(
$countryId,
'Integer',
),
);
CRM_Core_DAO::executeQuery($sql, $stateParams);
}
}
// Find states that are already there.
$stateIdsToKeep = array();
foreach ($statesToAdd as $state => $abbr) {
$sql = 'SELECT id FROM civicrm_state_province WHERE name = %1 AND country_id = %2 LIMIT 1';
$stateParams = array(
1 => array(
$state,
'String',
),
2 => array(
$countryId,
'Integer',
),
);
$foundState = CRM_Core_DAO::singleValueQuery($sql, $stateParams);
if ($foundState) {
unset($statesToAdd[$state]);
$stateIdsToKeep[] = $foundState;
continue;
}
}
// Wipe out states to remove.
if (!empty($stateConfig['overwrite'])) {
$sql = 'SELECT id FROM civicrm_state_province WHERE country_id = %1';
$params = array(
1 => array(
$countryId,
'Integer',
),
);
$dbStates = CRM_Core_DAO::executeQuery($sql, $params);
$deleteIds = array();
while ($dbStates->fetch()) {
if (!in_array($dbStates->id, $stateIdsToKeep)) {
$deleteIds[] = $dbStates->id;
}
}
// Go delete the remaining old ones.
foreach ($deleteIds as $id) {
$sql = "DELETE FROM civicrm_state_province WHERE id = %1";
$params = array(
1 => array(
$id,
'Integer',
),
);
CRM_Core_DAO::executeQuery($sql, $params);
}
}
// Add new states.
$insert = array();
foreach ($statesToAdd as $state => $abbr) {
$stateE = $dao->escape($state);
$abbrE = $dao->escape($abbr);
$insert[] = "('$stateE', '$abbrE', $countryId)";
}
// Put it into queries of 50 states each.
for ($i = 0; $i < count($insert); $i = $i + 50) {
$inserts = array_slice($insert, $i, 50);
$query = "INSERT INTO civicrm_state_province (name, abbreviation, country_id) VALUES ";
$query .= implode(', ', $inserts);
CRM_Core_DAO::executeQuery($query);
}
return TRUE;
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function mexicoregions_civicrm_install() {
mexicoregions_loadProvinces();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function mexicoregions_civicrm_enable() {
mexicoregions_loadProvinces();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function mexicoregions_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
mexicoregions_loadProvinces();
}