3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
28 * Create a xml file for a set of contact ID's in a format digestible
32 require_once '../../civicrm.config.php';
33 require_once 'CRM/Core/Config.php';
35 define('CHUNK_SIZE', 128);
38 * Split a large array of contactIDs into more manageable smaller chunks
42 function &splitContactIDs(&$contactIDs) {
43 // contactIDs could be a real large array, so we split it up into
44 // smaller chunks and then general xml for each chunk
47 $chunks[$current] = [];
50 foreach ($contactIDs as $cid) {
51 $chunks[$current][] = $cid;
54 if ($count == CHUNK_SIZE
) {
56 $chunks[$current] = [];
61 if (empty($chunks[$current])) {
62 unset($chunks[$current]);
69 * Given an array of values, generate the XML in the Solr format
73 function &generateSolrXML($values) {
75 foreach ($values as $cid => $tokens) {
82 <field name="id">$cid</field>\n
85 foreach ($tokens as $t) {
87 <field name="$t[0]">$t[1]</field>\n
91 $result .= " </doc>\n";
93 $result .= "</add>\n";
100 * Given a set of contact IDs get the values
105 function getValues(&$contactIDs, &$values) {
108 foreach ($contactIDs as $cid) {
112 getContactInfo($contactIDs, $values);
113 getLocationInfo($contactIDs, $values);
124 * @param null $additionalWhereCond
126 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
127 $selectString = implode(',', array_keys($fields));
128 $idString = implode(',', $contactIDs);
131 SELECT $selectString, $whereField as contact_id
133 WHERE $whereField IN ( $idString )
136 if ($additionalWhereCond) {
137 $sql .= " AND $additionalWhereCond";
140 $dao = CRM_Core_DAO
::executeQuery($sql);
141 while ($dao->fetch()) {
142 foreach ($fields as $fld => $name) {
143 if (empty($dao->$fld)) {
149 $values[$dao->contact_id
][] = [$name, $dao->$fld];
158 function getContactInfo(&$contactIDs, &$values) {
161 'display_name' => NULL,
162 'contact_type' => NULL,
163 'legal_identifier' => NULL,
164 'external_identifier' => NULL,
165 'source' => 'contact_source',
167 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
170 'first_name' => NULL,
172 'middle_name' => NULL,
175 getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id');
177 $fields = ['household_name' => NULL];
178 getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id');
181 'organization_name' => NULL,
182 'legal_name' => NULL,
185 getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id');
188 'note' => 'note_body',
189 'subject' => 'note_subject',
191 getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
198 function getLocationInfo(&$contactIDs, &$values) {
199 $ids = implode(',', $contactIDs);
203 l.entity_id as contact_id, l.name as location_name,
204 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
205 a.supplemental_address_3,
206 a.city, a.postal_code,
207 co.name as county, s.name as state, c.name as country,
208 e.email, p.phone, i.name as im
211 LEFT JOIN civicrm_address a ON a.location_id = l.id
212 LEFT JOIN civicrm_email e ON e.location_id = l.id
213 LEFT JOIN civicrm_phone p ON p.location_id = l.id
214 LEFT JOIN civicrm_im i ON i.location_id = l.id
215 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
216 LEFT JOIN civicrm_country c ON a.country_id = c.id
217 LEFT JOIN civicrm_county co ON a.county_id = co.id
218 WHERE l.entity_table = 'civicrm_contact'
219 AND l.entity_id IN ( $ids )
225 'supplemental_address_1',
226 'supplemental_address_2',
227 'supplemental_address_3',
237 $dao = CRM_Core_DAO
::executeQuery($sql);
238 while ($dao->fetch()) {
239 foreach ($fields as $fld) {
240 if (empty($dao->$fld)) {
243 $values[$dao->contact_id
][] = [$fld, $dao->$fld];
251 function run(&$contactIDs) {
252 $chunks = &splitContactIDs($contactIDs);
254 foreach ($chunks as $chunk) {
256 getValues($chunk, $values);
257 $xml = &generateSolrXML($values);
262 $config = CRM_Core_Config
::singleton();
263 $config->userFramework
= 'Soap';
264 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
265 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
271 $dao = CRM_Core_DAO
::executeQuery($sql);
274 while ($dao->fetch()) {
275 $contactIDs[] = $dao->id
;