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.config.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 XML in the Solr format
58 function &generateSolrXML($values) {
60 foreach ($values as $cid => $tokens) {
67 <field name="id">$cid</field>\n
70 foreach ($tokens as $t) {
72 <field name="$t[0]">$t[1]</field>\n
76 $result .= " </doc>\n";
78 $result .= "</add>\n";
85 * Given a set of contact IDs get the values
90 function getValues(&$contactIDs, &$values) {
93 foreach ($contactIDs as $cid) {
97 getContactInfo($contactIDs, $values);
98 getLocationInfo($contactIDs, $values);
109 * @param null $additionalWhereCond
111 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
112 $selectString = implode(',', array_keys($fields));
113 $idString = implode(',', $contactIDs);
116 SELECT $selectString, $whereField as contact_id
118 WHERE $whereField IN ( $idString )
121 if ($additionalWhereCond) {
122 $sql .= " AND $additionalWhereCond";
125 $dao = CRM_Core_DAO
::executeQuery($sql);
126 while ($dao->fetch()) {
127 foreach ($fields as $fld => $name) {
128 if (empty($dao->$fld)) {
134 $values[$dao->contact_id
][] = [$name, $dao->$fld];
143 function getContactInfo(&$contactIDs, &$values) {
146 'display_name' => NULL,
147 'contact_type' => NULL,
148 'legal_identifier' => NULL,
149 'external_identifier' => NULL,
150 'source' => 'contact_source',
152 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
155 'first_name' => NULL,
157 'middle_name' => NULL,
160 getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id');
162 $fields = ['household_name' => NULL];
163 getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id');
166 'organization_name' => NULL,
167 'legal_name' => NULL,
170 getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id');
173 'note' => 'note_body',
174 'subject' => 'note_subject',
176 getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
183 function getLocationInfo(&$contactIDs, &$values) {
184 $ids = implode(',', $contactIDs);
188 l.entity_id as contact_id, l.name as location_name,
189 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
190 a.supplemental_address_3,
191 a.city, a.postal_code,
192 co.name as county, s.name as state, c.name as country,
193 e.email, p.phone, i.name as im
196 LEFT JOIN civicrm_address a ON a.location_id = l.id
197 LEFT JOIN civicrm_email e ON e.location_id = l.id
198 LEFT JOIN civicrm_phone p ON p.location_id = l.id
199 LEFT JOIN civicrm_im i ON i.location_id = l.id
200 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
201 LEFT JOIN civicrm_country c ON a.country_id = c.id
202 LEFT JOIN civicrm_county co ON a.county_id = co.id
203 WHERE l.entity_table = 'civicrm_contact'
204 AND l.entity_id IN ( $ids )
210 'supplemental_address_1',
211 'supplemental_address_2',
212 'supplemental_address_3',
222 $dao = CRM_Core_DAO
::executeQuery($sql);
223 while ($dao->fetch()) {
224 foreach ($fields as $fld) {
225 if (empty($dao->$fld)) {
228 $values[$dao->contact_id
][] = [$fld, $dao->$fld];
236 function run(&$contactIDs) {
237 $chunks = &splitContactIDs($contactIDs);
239 foreach ($chunks as $chunk) {
241 getValues($chunk, $values);
242 $xml = &generateSolrXML($values);
247 $config = CRM_Core_Config
::singleton();
248 $config->userFramework
= 'Soap';
249 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
250 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
256 $dao = CRM_Core_DAO
::executeQuery($sql);
259 while ($dao->fetch()) {
260 $contactIDs[] = $dao->id
;