-
Notifications
You must be signed in to change notification settings - Fork 0
/
armeniaregions.php
178 lines (177 loc) · 4.67 KB
/
armeniaregions.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
<?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 armeniaregions_stateConfig() {
$config = array(
// CAUTION: only use `overwrite` on fresh databases.
'overwrite' => TRUE,
'countryIso' => 'AM',
'states' => array(
// 'state name' => 'abbreviation',
'Ararat' => '1',
'Armavir' => '2',
'Aragatsotn' => '3',
'Gegharkunik' => '4',
'Kotayk' => '5',
'Lori' => '6',
'Shirak' => '7',
'Syunik' => '8',
'Tavush' => '9',
'Vayots Dzor' => '10',
'Yerevan' => '11',
),
'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 armeniaregions_loadProvinces() {
$stateConfig = armeniaregions_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.armeniaregions',
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 armeniaregions_civicrm_install() {
armeniaregions_loadProvinces();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function armeniaregions_civicrm_enable() {
armeniaregions_loadProvinces();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function armeniaregions_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
armeniaregions_loadProvinces();
}