3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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.settings.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] = array();
50 foreach ($contactIDs as $cid) {
51 $chunks[$current][] = $cid;
54 if ($count == CHUNK_SIZE
) {
56 $chunks[$current] = array();
61 if (empty($chunks[$current])) {
62 unset($chunks[$current]);
69 * Given an array of values, generate the JSON in the Solr format
73 function &generateSolrJSON($values) {
75 foreach ($values as $cid => $tokens) {
80 $result .= "\n {\n \"contact_id\" : \"$cid\",";
82 foreach ($tokens as $n => $v) {
86 $el = escapeJsonString($el);
89 $string = implode(",", $str);
90 $result .= "\n \"{$n}\" : [$string],";
93 $v = escapeJsonString($v);
94 $result .= "\n \"{$n}\" : \"{$v}\",";
98 // remove the last comma
99 $result = rtrim($result, ",");
103 // remove the last comma
104 $result = rtrim($result, ",");
117 function escapeJsonString($value) {
118 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
119 $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
120 return str_replace($escapers, $replacements, $value);
124 * Given a set of contact IDs get the values
129 function getValues(&$contactIDs, &$values) {
132 foreach ($contactIDs as $cid) {
133 $values[$cid] = array();
136 getContactInfo($contactIDs, $values);
137 getAddressInfo($contactIDs, $values);
138 getPhoneInfo($contactIDs, $values);
139 getEmailInfo($contactIDs, $values);
140 getNoteInfo($contactIDs, $values);
151 * @param null $additionalWhereCond
153 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
154 $selectString = implode(',', array_keys($fields));
155 $idString = implode(',', $contactIDs);
158 SELECT $selectString, $whereField as contact_id
160 WHERE $whereField IN ( $idString )
163 if ($additionalWhereCond) {
164 $sql .= " AND $additionalWhereCond";
167 $dao = &CRM_Core_DAO
::executeQuery($sql);
168 while ($dao->fetch()) {
169 foreach ($fields as $fld => $name) {
170 $name = $name ?
$name : $fld;
171 appendValue($values, $dao->contact_id
, $name, $dao->$fld);
180 function getContactInfo(&$contactIDs, &$values) {
181 $fields = array('sort_name' => NULL,
182 'display_name' => NULL,
183 'contact_type' => NULL,
184 'legal_identifier' => NULL,
185 'external_identifier' => NULL,
186 'first_name' => NULL,
188 'middle_name' => NULL,
189 'household_name' => NULL,
190 'organization_name' => NULL,
191 'legal_name' => NULL,
194 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
201 function getNoteInfo(&$contactIDs, &$values) {
202 $ids = implode(',', $contactIDs);
206 entity_id as contact_id,
207 note as note, subject as subject
209 WHERE entity_id IN ( $ids )
210 AND entity_table = 'civicrm_contact'
213 $dao = &CRM_Core_DAO
::executeQuery($sql);
214 while ($dao->fetch()) {
215 $note = empty($dao->subject
) ?
'' : "{$dao->subject}: ";
216 $note .= empty($dao->note
) ?
'' : $dao->note
;
218 appendValue($values, $dao->contact_id
, 'note', $note);
226 function getPhoneInfo(&$contactIDs, &$values) {
227 $ids = implode(',', $contactIDs);
232 l.name as location_type,
234 v.label as phone_type
235 FROM civicrm_contact c
236 INNER JOIN civicrm_phone p ON p.contact_id = c.id
237 LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
238 LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
239 LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
240 WHERE c.id IN ( $ids )
241 AND p.phone IS NOT NULL
244 $dao = &CRM_Core_DAO
::executeQuery($sql);
245 while ($dao->fetch()) {
248 if (!empty($dao->location_type
)) {
249 $phone = "{$dao->location_type}: ";
252 $phone .= $dao->phone
;
254 if (!empty($dao->phone_type
)) {
255 $phone .= " ({$dao->phone_type})";
258 appendValue($values, $dao->contact_id
, 'phone', $phone);
266 function getEmailInfo(&$contactIDs, &$values) {
267 $ids = implode(',', $contactIDs);
272 l.name as location_type,
274 FROM civicrm_contact c
275 INNER JOIN civicrm_email e ON e.contact_id = c.id
276 LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
277 WHERE c.id IN ( $ids )
278 AND e.email IS NOT NULL
281 $dao = &CRM_Core_DAO
::executeQuery($sql);
282 while ($dao->fetch()) {
285 if (!empty($dao->location_type
)) {
286 $email = "{$dao->location_type}: ";
289 $email .= $dao->email
;
290 appendValue($values, $dao->contact_id
, 'email', $email);
298 function getAddressInfo(&$contactIDs, &$values) {
299 $ids = implode(',', $contactIDs);
302 SELECT c.id as contact_id, l.name as location_type,
303 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
304 a.supplemental_address_3,
305 a.city, a.postal_code,
306 s.name as state, co.name as country
307 FROM civicrm_contact c
308 INNER JOIN civicrm_address a ON a.contact_id = c.id
309 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
310 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
311 LEFT JOIN civicrm_country co ON a.country_id = co.id
312 WHERE c.id IN ( $ids )
315 $fields = array('location_type', 'street_address', 'supplemental_address_1',
316 'supplemental_address_2', 'supplemental_address_3', 'city', 'postal_code',
319 $dao = &CRM_Core_DAO
::executeQuery($sql);
320 while ($dao->fetch()) {
322 foreach ($fields as $fld) {
323 if (empty($dao->$fld)) {
327 $address .= ($fld == 'location_type') ?
"{$dao->$fld}: " : " {$dao->$fld},";
328 appendValue($values, $dao->contact_id
, $fld, $dao->$fld);
331 if (!empty($address)) {
332 $address = rtrim($address, ",");
333 appendValue($values, $dao->contact_id
, 'address', $address);
344 function appendValue(&$values, $contactID, $name, $value) {
349 if (!isset($values[$contactID][$name])) {
350 $values[$contactID][$name] = $value;
353 if (!is_array($values[$contactID][$name])) {
354 $save = $values[$contactID][$name];
355 $values[$contactID][$name] = array();
356 $values[$contactID][$name][] = $save;
358 $values[$contactID][$name][] = $value;
365 function run(&$contactIDs) {
366 $chunks = &splitContactIDs($contactIDs);
368 foreach ($chunks as $chunk) {
370 getValues($chunk, $values);
371 $xml = &generateSolrJSON($values);
376 $config = &CRM_Core_Config
::singleton();
377 $config->userFramework
= 'Soap';
378 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
379 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
385 $dao = &CRM_Core_DAO
::executeQuery($sql);
388 $contactIDs = array();
389 while ($dao->fetch()) {
390 $contactIDs[] = $dao->id
;