3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
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, ",");
108 function escapeJsonString($value) {
109 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
110 $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
111 return str_replace($escapers, $replacements, $value);
115 * Given a set of contact IDs get the values
117 function getValues(&$contactIDs, &$values) {
120 foreach ($contactIDs as $cid) {
121 $values[$cid] = array();
124 getContactInfo($contactIDs, $values);
125 getAddressInfo($contactIDs, $values);
126 getPhoneInfo($contactIDs, $values);
127 getEmailInfo($contactIDs, $values);
128 getNoteInfo($contactIDs, $values);
133 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
134 $selectString = implode(',', array_keys($fields));
135 $idString = implode(',', $contactIDs);
138 SELECT $selectString, $whereField as contact_id
140 WHERE $whereField IN ( $idString )
143 if ($additionalWhereCond) {
144 $sql .= " AND $additionalWhereCond";
147 $dao = &CRM_Core_DAO
::executeQuery($sql);
148 while ($dao->fetch()) {
149 foreach ($fields as $fld => $name) {
150 $name = $name ?
$name : $fld;
151 appendValue($values, $dao->contact_id
, $name, $dao->$fld);
156 function getContactInfo(&$contactIDs, &$values) {
157 $fields = array('sort_name' => NULL,
158 'display_name' => NULL,
159 'contact_type' => NULL,
160 'legal_identifier' => NULL,
161 'external_identifier' => NULL,
162 'first_name' => NULL,
164 'middle_name' => NULL,
165 'household_name' => NULL,
166 'organization_name' => NULL,
167 'legal_name' => NULL,
170 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
173 function getNoteInfo(&$contactIDs, &$values) {
174 $ids = implode(',', $contactIDs);
178 entity_id as contact_id,
179 note as note, subject as subject
181 WHERE entity_id IN ( $ids )
182 AND entity_table = 'civicrm_contact'
185 $dao = &CRM_Core_DAO
::executeQuery($sql);
186 while ($dao->fetch()) {
187 $note = empty($dao->subject
) ?
'' : "{$dao->subject}: ";
188 $note .= empty($dao->note
) ?
'' : $dao->note
;
190 appendValue($values, $dao->contact_id
, 'note', $note);
194 function getPhoneInfo(&$contactIDs, &$values) {
195 $ids = implode(',', $contactIDs);
200 l.name as location_type,
202 v.label as phone_type
203 FROM civicrm_contact c
204 INNER JOIN civicrm_phone p ON p.contact_id = c.id
205 LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
206 LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
207 LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
208 WHERE c.id IN ( $ids )
209 AND p.phone IS NOT NULL
212 $dao = &CRM_Core_DAO
::executeQuery($sql);
213 while ($dao->fetch()) {
216 if (!empty($dao->location_type
)) {
217 $phone = "{$dao->location_type}: ";
220 $phone .= $dao->phone
;
222 if (!empty($dao->phone_type
)) {
223 $phone .= " ({$dao->phone_type})";
226 appendValue($values, $dao->contact_id
, 'phone', $phone);
230 function getEmailInfo(&$contactIDs, &$values) {
231 $ids = implode(',', $contactIDs);
236 l.name as location_type,
238 FROM civicrm_contact c
239 INNER JOIN civicrm_email e ON e.contact_id = c.id
240 LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
241 WHERE c.id IN ( $ids )
242 AND e.email IS NOT NULL
245 $dao = &CRM_Core_DAO
::executeQuery($sql);
246 while ($dao->fetch()) {
249 if (!empty($dao->location_type
)) {
250 $email = "{$dao->location_type}: ";
253 $email .= $dao->email
;
254 appendValue($values, $dao->contact_id
, 'email', $email);
258 function getAddressInfo(&$contactIDs, &$values) {
259 $ids = implode(',', $contactIDs);
262 SELECT c.id as contact_id, l.name as location_type,
263 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
264 a.city, a.postal_code,
265 s.name as state, co.name as country
266 FROM civicrm_contact c
267 INNER JOIN civicrm_address a ON a.contact_id = c.id
268 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
269 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
270 LEFT JOIN civicrm_country co ON a.country_id = co.id
271 WHERE c.id IN ( $ids )
274 $fields = array('location_type', 'street_address', 'supplemental_address_1',
275 'supplemental_address_2', 'city', 'postal_code',
278 $dao = &CRM_Core_DAO
::executeQuery($sql);
279 while ($dao->fetch()) {
281 foreach ($fields as $fld) {
282 if (empty($dao->$fld)) {
286 $address .= ($fld == 'location_type') ?
"{$dao->$fld}: " : " {$dao->$fld},";
287 appendValue($values, $dao->contact_id
, $fld, $dao->$fld);
290 if (!empty($address)) {
291 $address = rtrim($address, ",");
292 appendValue($values, $dao->contact_id
, 'address', $address);
297 function appendValue(&$values, $contactID, $name, $value) {
302 if (!isset($values[$contactID][$name])) {
303 $values[$contactID][$name] = $value;
306 if (!is_array($values[$contactID][$name])) {
307 $save = $values[$contactID][$name];
308 $values[$contactID][$name] = array();
309 $values[$contactID][$name][] = $save;
311 $values[$contactID][$name][] = $value;
315 function run(&$contactIDs) {
316 $chunks = &splitContactIDs($contactIDs);
318 foreach ($chunks as $chunk) {
320 getValues($chunk, $values);
321 $xml = &generateSolrJSON($values);
326 $config = &CRM_Core_Config
::singleton();
327 $config->userFramework
= 'Soap';
328 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
329 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
335 $dao = &CRM_Core_DAO
::executeQuery($sql);
338 $contactIDs = array();
339 while ($dao->fetch()) {
340 $contactIDs[] = $dao->id
;