3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This code is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Create a xml file for a set of contact ID's in a format digestible
17 require_once '../../../civicrm.settings.php';
18 require_once 'CRM/Core/Config.php';
20 define('CHUNK_SIZE', 128);
23 * Split a large array of contactIDs into more manageable smaller chunks
27 function &splitContactIDs(&$contactIDs) {
28 // contactIDs could be a real large array, so we split it up into
29 // smaller chunks and then general xml for each chunk
32 $chunks[$current] = [];
35 foreach ($contactIDs as $cid) {
36 $chunks[$current][] = $cid;
39 if ($count == CHUNK_SIZE
) {
41 $chunks[$current] = [];
46 if (empty($chunks[$current])) {
47 unset($chunks[$current]);
54 * Given an array of values, generate the JSON in the Solr format
58 function &generateSolrJSON($values) {
60 foreach ($values as $cid => $tokens) {
65 $result .= "\n {\n \"contact_id\" : \"$cid\",";
67 foreach ($tokens as $n => $v) {
71 $el = escapeJsonString($el);
74 $string = implode(",", $str);
75 $result .= "\n \"{$n}\" : [$string],";
78 $v = escapeJsonString($v);
79 $result .= "\n \"{$n}\" : \"{$v}\",";
83 // remove the last comma
84 $result = rtrim($result, ",");
88 // remove the last comma
89 $result = rtrim($result, ",");
102 function escapeJsonString($value) {
103 $escapers = ["\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"];
104 $replacements = ["\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"];
105 return str_replace($escapers, $replacements, $value);
109 * Given a set of contact IDs get the values
114 function getValues(&$contactIDs, &$values) {
117 foreach ($contactIDs as $cid) {
121 getContactInfo($contactIDs, $values);
122 getAddressInfo($contactIDs, $values);
123 getPhoneInfo($contactIDs, $values);
124 getEmailInfo($contactIDs, $values);
125 getNoteInfo($contactIDs, $values);
136 * @param null $additionalWhereCond
138 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
139 $selectString = implode(',', array_keys($fields));
140 $idString = implode(',', $contactIDs);
143 SELECT $selectString, $whereField as contact_id
145 WHERE $whereField IN ( $idString )
148 if ($additionalWhereCond) {
149 $sql .= " AND $additionalWhereCond";
152 $dao = &CRM_Core_DAO
::executeQuery($sql);
153 while ($dao->fetch()) {
154 foreach ($fields as $fld => $name) {
155 $name = $name ?
$name : $fld;
156 appendValue($values, $dao->contact_id
, $name, $dao->$fld);
165 function getContactInfo(&$contactIDs, &$values) {
168 'display_name' => NULL,
169 'contact_type' => NULL,
170 'legal_identifier' => NULL,
171 'external_identifier' => NULL,
172 'first_name' => NULL,
174 'middle_name' => NULL,
175 'household_name' => NULL,
176 'organization_name' => NULL,
177 'legal_name' => NULL,
180 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
187 function getNoteInfo(&$contactIDs, &$values) {
188 $ids = implode(',', $contactIDs);
192 entity_id as contact_id,
193 note as note, subject as subject
195 WHERE entity_id IN ( $ids )
196 AND entity_table = 'civicrm_contact'
199 $dao = &CRM_Core_DAO
::executeQuery($sql);
200 while ($dao->fetch()) {
201 $note = empty($dao->subject
) ?
'' : "{$dao->subject}: ";
202 $note .= empty($dao->note
) ?
'' : $dao->note
;
204 appendValue($values, $dao->contact_id
, 'note', $note);
212 function getPhoneInfo(&$contactIDs, &$values) {
213 $ids = implode(',', $contactIDs);
218 l.name as location_type,
220 v.label as phone_type
221 FROM civicrm_contact c
222 INNER JOIN civicrm_phone p ON p.contact_id = c.id
223 LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
224 LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
225 LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
226 WHERE c.id IN ( $ids )
227 AND p.phone IS NOT NULL
230 $dao = &CRM_Core_DAO
::executeQuery($sql);
231 while ($dao->fetch()) {
234 if (!empty($dao->location_type
)) {
235 $phone = "{$dao->location_type}: ";
238 $phone .= $dao->phone
;
240 if (!empty($dao->phone_type
)) {
241 $phone .= " ({$dao->phone_type})";
244 appendValue($values, $dao->contact_id
, 'phone', $phone);
252 function getEmailInfo(&$contactIDs, &$values) {
253 $ids = implode(',', $contactIDs);
258 l.name as location_type,
260 FROM civicrm_contact c
261 INNER JOIN civicrm_email e ON e.contact_id = c.id
262 LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
263 WHERE c.id IN ( $ids )
264 AND e.email IS NOT NULL
267 $dao = &CRM_Core_DAO
::executeQuery($sql);
268 while ($dao->fetch()) {
271 if (!empty($dao->location_type
)) {
272 $email = "{$dao->location_type}: ";
275 $email .= $dao->email
;
276 appendValue($values, $dao->contact_id
, 'email', $email);
284 function getAddressInfo(&$contactIDs, &$values) {
285 $ids = implode(',', $contactIDs);
288 SELECT c.id as contact_id, l.name as location_type,
289 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
290 a.supplemental_address_3,
291 a.city, a.postal_code,
292 s.name as state, co.name as country
293 FROM civicrm_contact c
294 INNER JOIN civicrm_address a ON a.contact_id = c.id
295 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
296 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
297 LEFT JOIN civicrm_country co ON a.country_id = co.id
298 WHERE c.id IN ( $ids )
304 'supplemental_address_1',
305 'supplemental_address_2',
306 'supplemental_address_3',
312 $dao = &CRM_Core_DAO
::executeQuery($sql);
313 while ($dao->fetch()) {
315 foreach ($fields as $fld) {
316 if (empty($dao->$fld)) {
320 $address .= ($fld == 'location_type') ?
"{$dao->$fld}: " : " {$dao->$fld},";
321 appendValue($values, $dao->contact_id
, $fld, $dao->$fld);
324 if (!empty($address)) {
325 $address = rtrim($address, ",");
326 appendValue($values, $dao->contact_id
, 'address', $address);
337 function appendValue(&$values, $contactID, $name, $value) {
342 if (!isset($values[$contactID][$name])) {
343 $values[$contactID][$name] = $value;
346 if (!is_array($values[$contactID][$name])) {
347 $save = $values[$contactID][$name];
348 $values[$contactID][$name] = [];
349 $values[$contactID][$name][] = $save;
351 $values[$contactID][$name][] = $value;
358 function run(&$contactIDs) {
359 $chunks = &splitContactIDs($contactIDs);
361 foreach ($chunks as $chunk) {
363 getValues($chunk, $values);
364 $xml = &generateSolrJSON($values);
369 $config = CRM_Core_Config
::singleton();
370 $config->userFramework
= 'Soap';
371 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
372 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
378 $dao = &CRM_Core_DAO
::executeQuery($sql);
382 while ($dao->fetch()) {
383 $contactIDs[] = $dao->id
;