3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
40 function &splitContactIDs(&$contactIDs) {
41 // contactIDs could be a real large array, so we split it up into
42 // smaller chunks and then general xml for each chunk
45 $chunks[$current] = array();
48 foreach ($contactIDs as $cid) {
49 $chunks[$current][] = $cid;
52 if ($count == CHUNK_SIZE
) {
54 $chunks[$current] = array();
59 if (empty($chunks[$current])) {
60 unset($chunks[$current]);
67 * Given an array of values, generate the JSON in the Solr format
69 function &generateSolrJSON($values) {
71 foreach ($values as $cid => $tokens) {
76 $result .= "\n {\n \"contact_id\" : \"$cid\",";
78 foreach ($tokens as $n => $v) {
82 $el = escapeJsonString($el);
85 $string = implode(",", $str);
86 $result .= "\n \"{$n}\" : [$string],";
89 $v = escapeJsonString($v);
90 $result .= "\n \"{$n}\" : \"{$v}\",";
94 // remove the last comma
95 $result = rtrim($result, ",");
99 // remove the last comma
100 $result = rtrim($result, ",");
113 function escapeJsonString($value) {
114 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
115 $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
116 return str_replace($escapers, $replacements, $value);
120 * Given a set of contact IDs get the values
122 function getValues(&$contactIDs, &$values) {
125 foreach ($contactIDs as $cid) {
126 $values[$cid] = array();
129 getContactInfo($contactIDs, $values);
130 getAddressInfo($contactIDs, $values);
131 getPhoneInfo($contactIDs, $values);
132 getEmailInfo($contactIDs, $values);
133 getNoteInfo($contactIDs, $values);
144 * @param null $additionalWhereCond
146 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
147 $selectString = implode(',', array_keys($fields));
148 $idString = implode(',', $contactIDs);
151 SELECT $selectString, $whereField as contact_id
153 WHERE $whereField IN ( $idString )
156 if ($additionalWhereCond) {
157 $sql .= " AND $additionalWhereCond";
160 $dao = &CRM_Core_DAO
::executeQuery($sql);
161 while ($dao->fetch()) {
162 foreach ($fields as $fld => $name) {
163 $name = $name ?
$name : $fld;
164 appendValue($values, $dao->contact_id
, $name, $dao->$fld);
173 function getContactInfo(&$contactIDs, &$values) {
174 $fields = array('sort_name' => NULL,
175 'display_name' => NULL,
176 'contact_type' => NULL,
177 'legal_identifier' => NULL,
178 'external_identifier' => NULL,
179 'first_name' => NULL,
181 'middle_name' => NULL,
182 'household_name' => NULL,
183 'organization_name' => NULL,
184 'legal_name' => NULL,
187 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
194 function getNoteInfo(&$contactIDs, &$values) {
195 $ids = implode(',', $contactIDs);
199 entity_id as contact_id,
200 note as note, subject as subject
202 WHERE entity_id IN ( $ids )
203 AND entity_table = 'civicrm_contact'
206 $dao = &CRM_Core_DAO
::executeQuery($sql);
207 while ($dao->fetch()) {
208 $note = empty($dao->subject
) ?
'' : "{$dao->subject}: ";
209 $note .= empty($dao->note
) ?
'' : $dao->note
;
211 appendValue($values, $dao->contact_id
, 'note', $note);
219 function getPhoneInfo(&$contactIDs, &$values) {
220 $ids = implode(',', $contactIDs);
225 l.name as location_type,
227 v.label as phone_type
228 FROM civicrm_contact c
229 INNER JOIN civicrm_phone p ON p.contact_id = c.id
230 LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
231 LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
232 LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
233 WHERE c.id IN ( $ids )
234 AND p.phone IS NOT NULL
237 $dao = &CRM_Core_DAO
::executeQuery($sql);
238 while ($dao->fetch()) {
241 if (!empty($dao->location_type
)) {
242 $phone = "{$dao->location_type}: ";
245 $phone .= $dao->phone
;
247 if (!empty($dao->phone_type
)) {
248 $phone .= " ({$dao->phone_type})";
251 appendValue($values, $dao->contact_id
, 'phone', $phone);
259 function getEmailInfo(&$contactIDs, &$values) {
260 $ids = implode(',', $contactIDs);
265 l.name as location_type,
267 FROM civicrm_contact c
268 INNER JOIN civicrm_email e ON e.contact_id = c.id
269 LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
270 WHERE c.id IN ( $ids )
271 AND e.email IS NOT NULL
274 $dao = &CRM_Core_DAO
::executeQuery($sql);
275 while ($dao->fetch()) {
278 if (!empty($dao->location_type
)) {
279 $email = "{$dao->location_type}: ";
282 $email .= $dao->email
;
283 appendValue($values, $dao->contact_id
, 'email', $email);
291 function getAddressInfo(&$contactIDs, &$values) {
292 $ids = implode(',', $contactIDs);
295 SELECT c.id as contact_id, l.name as location_type,
296 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
297 a.city, a.postal_code,
298 s.name as state, co.name as country
299 FROM civicrm_contact c
300 INNER JOIN civicrm_address a ON a.contact_id = c.id
301 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
302 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
303 LEFT JOIN civicrm_country co ON a.country_id = co.id
304 WHERE c.id IN ( $ids )
307 $fields = array('location_type', 'street_address', 'supplemental_address_1',
308 'supplemental_address_2', 'city', 'postal_code',
311 $dao = &CRM_Core_DAO
::executeQuery($sql);
312 while ($dao->fetch()) {
314 foreach ($fields as $fld) {
315 if (empty($dao->$fld)) {
319 $address .= ($fld == 'location_type') ?
"{$dao->$fld}: " : " {$dao->$fld},";
320 appendValue($values, $dao->contact_id
, $fld, $dao->$fld);
323 if (!empty($address)) {
324 $address = rtrim($address, ",");
325 appendValue($values, $dao->contact_id
, 'address', $address);
336 function appendValue(&$values, $contactID, $name, $value) {
341 if (!isset($values[$contactID][$name])) {
342 $values[$contactID][$name] = $value;
345 if (!is_array($values[$contactID][$name])) {
346 $save = $values[$contactID][$name];
347 $values[$contactID][$name] = array();
348 $values[$contactID][$name][] = $save;
350 $values[$contactID][$name][] = $value;
357 function run(&$contactIDs) {
358 $chunks = &splitContactIDs($contactIDs);
360 foreach ($chunks as $chunk) {
362 getValues($chunk, $values);
363 $xml = &generateSolrJSON($values);
368 $config = &CRM_Core_Config
::singleton();
369 $config->userFramework
= 'Soap';
370 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
371 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
377 $dao = &CRM_Core_DAO
::executeQuery($sql);
380 $contactIDs = array();
381 while ($dao->fetch()) {
382 $contactIDs[] = $dao->id
;