Remove ?last? instances of ->free() dev/core#562
[civicrm-core.git] / tools / scripts / solr / createSyncJSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
5b71fd5f 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
25*/
26
27/**
28 * Create a xml file for a set of contact ID's in a format digestible
29 * by our Sync scripts
30 */
31
32require_once '../../../civicrm.settings.php';
33require_once 'CRM/Core/Config.php';
34
35define('CHUNK_SIZE', 128);
36
37/**
38 * Split a large array of contactIDs into more manageable smaller chunks
d7c8cf03
EM
39 * @param $contactIDs
40 * @return array
6a488035
TO
41 */
42function &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
b7c0a88f 45 $chunks = [];
46 $current = 0;
47 $chunks[$current] = [];
48 $count = 0;
6a488035
TO
49
50 foreach ($contactIDs as $k => $v) {
51 $chunks[$current][$k] = $v;
52 $count++;
53
54 if ($count == CHUNK_SIZE) {
55 $current++;
b7c0a88f 56 $chunks[$current] = [];
6a488035
TO
57 $count = 0;
58 }
59 }
60
61 if (empty($chunks[$current])) {
62 unset($chunks[$current]);
63 }
64
65 return $chunks;
66}
67
68/**
69 * Given a set of contact IDs get the values
d7c8cf03
EM
70 * @param $contactIDs
71 * @param $values
72 * @param $allContactIDs
73 * @param $addditionalContactIDs
74 * @return array
6a488035
TO
75 */
76function getValues(&$contactIDs, &$values, &$allContactIDs, &$addditionalContactIDs) {
b7c0a88f 77 $values = [];
6a488035
TO
78
79 getContactInfo($contactIDs, $values);
80 getAddressInfo($contactIDs, $values);
81 getPhoneInfo($contactIDs, $values);
82 getEmailInfo($contactIDs, $values);
83 getNoteInfo($contactIDs, $values);
84
85 getRelationshipInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
86
87 getActivityInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
88
89 // got to do groups, tags
90
91 // got to do meta data
92
93 return $values;
94}
95
a1a55b61
EM
96/**
97 * @param $contactIDs
98 * @param $values
99 * @param $tableName
100 * @param $fields
101 * @param $whereField
102 * @param null $additionalWhereCond
103 * @param bool $flat
104 */
6a488035 105function getTableInfo(&$contactIDs, &$values, $tableName, &$fields,
b7c0a88f 106 $whereField, $additionalWhereCond = NULL,
107 $flat = FALSE
6a488035
TO
108) {
109 $selectString = implode(',', array_keys($fields));
110 $idString = implode(',', $contactIDs);
111
112 $sql = "
113SELECT $selectString, $whereField as contact_id
114 FROM $tableName
115 WHERE $whereField IN ( $idString )
116";
117
118 if ($additionalWhereCond) {
119 $sql .= " AND $additionalWhereCond";
120 }
121
122 $dao = &CRM_Core_DAO::executeQuery($sql);
123 while ($dao->fetch()) {
b7c0a88f 124 $contact = [];
6a488035
TO
125 foreach ($fields as $fld => $name) {
126 $name = $name ? $name : $fld;
127 if (empty($dao->$fld)) {
128 $contact[$name] = NULL;
129 }
130 else {
131 $contact[$name] = $dao->$fld;
132 }
133 }
134 appendValue($values, $dao->contact_id, 'contact', $contact, $flat);
135 }
6a488035
TO
136}
137
a1a55b61
EM
138/**
139 * @param $contactIDs
140 * @param $values
141 */
6a488035 142function getContactInfo(&$contactIDs, &$values) {
b7c0a88f 143 $fields = [
144 'id' => NULL,
6a488035
TO
145 'sort_name' => NULL,
146 'display_name' => NULL,
147 'contact_type' => NULL,
148 'legal_identifier' => NULL,
149 'external_identifier' => NULL,
150 'first_name' => NULL,
151 'last_name' => NULL,
152 'middle_name' => NULL,
153 'household_name' => NULL,
154 'organization_name' => NULL,
155 'legal_name' => NULL,
156 'job_title' => NULL,
b7c0a88f 157 ];
6a488035
TO
158 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id', NULL, TRUE);
159}
160
a1a55b61
EM
161/**
162 * @param $contactIDs
163 * @param $values
164 */
6a488035
TO
165function getNoteInfo(&$contactIDs, &$values) {
166 $ids = implode(',', $contactIDs);
167
168 $sql = "
169SELECT
170 id,
171 entity_id as contact_id,
172 note as note, subject as subject
173FROM civicrm_note
174WHERE entity_id IN ( $ids )
175AND entity_table = 'civicrm_contact'
176";
177
178 $dao = &CRM_Core_DAO::executeQuery($sql);
179 while ($dao->fetch()) {
b7c0a88f 180 $note = [
181 'id' => $dao->id,
6a488035
TO
182 'contact_id' => $dao->contact_id,
183 'subject' => empty($dao->subject) ? NULL : $dao->subject,
184 'note' => empty($dao->note) ? NULL : $dao->note,
b7c0a88f 185 ];
6a488035
TO
186
187 appendValue($values, $dao->id, 'note', $note);
188 }
6a488035
TO
189}
190
a1a55b61
EM
191/**
192 * @param $contactIDs
193 * @param $values
194 */
6a488035
TO
195function getPhoneInfo(&$contactIDs, &$values) {
196 $ids = implode(',', $contactIDs);
197
198 $sql = "
199SELECT
200 p.id as id,
201 c.id as contact_id,
202 l.name as location_type,
203 p.phone as phone,
204 v.label as phone_type
205FROM civicrm_contact c
206INNER JOIN civicrm_phone p ON p.contact_id = c.id
207LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
208LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
209LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
a1a55b61 210WHERE c.id IN ( $ids )
6a488035
TO
211AND p.phone IS NOT NULL
212";
213
214 $dao = &CRM_Core_DAO::executeQuery($sql);
215 while ($dao->fetch()) {
b7c0a88f 216 $phone = [
217 'id' => $dao->id,
6a488035
TO
218 'contact_id' => $dao->contact_id,
219 'location_type' => empty($dao->location_type) ? NULL : $dao->location_type,
220 'phone' => $dao->phone,
221 'phone_type' => empty($dao->phone_type) ? NULL : $dao->phone_type,
b7c0a88f 222 ];
6a488035
TO
223
224 appendValue($values, $dao->id, 'phone', $phone);
225 }
6a488035
TO
226}
227
a1a55b61
EM
228/**
229 * @param $contactIDs
230 * @param $values
231 */
6a488035
TO
232function getEmailInfo(&$contactIDs, &$values) {
233 $ids = implode(',', $contactIDs);
234
235 $sql = "
236SELECT
237 e.id as id,
238 c.id as contact_id,
239 l.name as location_type,
240 e.email as email
241FROM civicrm_contact c
242INNER JOIN civicrm_email e ON e.contact_id = c.id
243LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
a1a55b61 244WHERE c.id IN ( $ids )
6a488035
TO
245AND e.email IS NOT NULL
246";
247
248 $dao = &CRM_Core_DAO::executeQuery($sql);
249 while ($dao->fetch()) {
b7c0a88f 250 $email = [
251 'id' => $dao->id,
6a488035
TO
252 'contact_id' => $dao->contact_id,
253 'location_type' => empty($dao->location_type) ? NULL : $dao->location_type,
254 'email' => $dao->email,
b7c0a88f 255 ];
6a488035
TO
256 appendValue($values, $dao->id, 'email', $email);
257 }
6a488035
TO
258}
259
a1a55b61
EM
260/**
261 * @param $contactIDs
262 * @param $values
263 */
6a488035
TO
264function getAddressInfo(&$contactIDs, &$values) {
265 $ids = implode(',', $contactIDs);
266
267 $sql = "
268SELECT a.id as id,
269 c.id as contact_id, l.name as location_type,
270 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
207f62c6 271 a.supplemental_address_3,
a1a55b61 272 a.city, a.postal_code,
6a488035
TO
273 s.name as state, co.name as country
274FROM civicrm_contact c
275INNER JOIN civicrm_address a ON a.contact_id = c.id
276LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
277LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
278LEFT JOIN civicrm_country co ON a.country_id = co.id
279WHERE c.id IN ( $ids )
280";
281
b7c0a88f 282 $fields = [
283 'id',
284 'contact_id',
285 'location_type',
286 'street_address',
287 'supplemental_address_1',
288 'supplemental_address_2',
289 'supplemental_address_3',
290 'city',
291 'postal_code',
292 'state',
293 'country',
294 ];
6a488035
TO
295 $dao = &CRM_Core_DAO::executeQuery($sql);
296 while ($dao->fetch()) {
b7c0a88f 297 $address = [];
6a488035
TO
298 foreach ($fields as $fld) {
299 if (empty($dao->$fld)) {
300 $address[$fld] = NULL;
301 }
302 else {
303 $address[$fld] = $dao->$fld;
304 }
305 }
306 appendValue($values, $dao->id, 'address', $address);
307 }
6a488035
TO
308}
309
a1a55b61
EM
310/**
311 * @param $contactIDs
312 * @param $values
313 * @param $allContactIDs
314 * @param $additionalContacts
315 */
6a488035
TO
316function getRelationshipInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
317 // handle relationships only once
b7c0a88f 318 static $_relationshipsHandled = [];
6a488035
TO
319
320 $ids = implode(',', $contactIDs);
321
322 $sql = "(
323 SELECT r.*
324 FROM civicrm_relationship r
325 WHERE r.contact_id_a IN ( $ids )
326) UNION (
327 SELECT r.*
328 FROM civicrm_relationship r
329 WHERE r.contact_id_b IN ( $ids )
330)
331";
332
333 $relationshipFields = getDBFields('CRM_Contact_DAO_Relationship');
b7c0a88f 334 $fields = array_keys($relationshipFields);
335 $dao = &CRM_Core_DAO::executeQuery($sql);
6a488035
TO
336 while ($dao->fetch()) {
337 if (isset($_relationshipsHandled[$dao->id])) {
338 continue;
339 }
340 $_relationshipsHandled[$dao->id] = $dao->id;
341
b7c0a88f 342 $relationship = [];
6a488035
TO
343 foreach ($fields as $fld) {
344 if (empty($dao->$fld)) {
345 $relationship[$fld] = NULL;
346 }
347 else {
348 $relationship[$fld] = $dao->$fld;
349 }
350 }
351 appendValue($values, $dao->id, 'relationship', $relationship);
352
b7c0a88f 353 addAdditionalContacts([
354 $dao->contact_id_a,
355 $dao->contact_id_b,
356 ],
6a488035
TO
357 $allContactIDs, $additionalContacts
358 );
359 }
6a488035
TO
360}
361
a1a55b61
EM
362/**
363 * @param $contactIDs
364 * @param $values
365 * @param $allContactIDs
366 * @param $additionalContacts
367 */
6a488035 368function getActivityInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
b7c0a88f 369 static $_activitiesHandled = [];
6a488035
TO
370
371 $ids = implode(',', $contactIDs);
372
373 $sql = "(
374 SELECT a.*
375 FROM civicrm_activity a
376 INNER JOIN civicrm_activity_assignment aa ON aa.activity_id = a.id
377 WHERE aa.assignee_contact_id IN ( $ids )
378 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
379) UNION (
380 SELECT a.*
381 FROM civicrm_activity a
382 INNER JOIN civicrm_activity_target at ON at.activity_id = a.id
383 WHERE at.target_contact_id IN ( $ids )
384 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
385)
386";
387
388 $activityFields = &getDBFields('CRM_Activity_DAO_Activity');
389 $fields = array_keys($activityFields);
390
b7c0a88f 391 $activityIDs = [];
6a488035
TO
392 $dao = &CRM_Core_DAO::executeQuery($sql);
393 while ($dao->fetch()) {
394 if (isset($_activitiesHandled[$dao->id])) {
395 continue;
396 }
397 $_activitiesHandled[$dao->id] = $dao->id;
398 $activityIDs[] = $dao->id;
399
b7c0a88f 400 $activity = [];
6a488035
TO
401 foreach ($fields as $fld) {
402 if (empty($dao->$fld)) {
403 $activity[$fld] = NULL;
404 }
405 else {
406 $activity[$fld] = $dao->$fld;
407 }
408 }
409
410 appendValue($values, $dao->id, 'activity', $activity);
b7c0a88f 411 addAdditionalContacts([$dao->source_contact_id],
6a488035
TO
412 $allContactIDs, $additionalContacts
413 );
414 }
6a488035
TO
415
416 if (empty($activityIDs)) {
417 return;
418 }
419
420 $activityIDString = implode(",", $activityIDs);
421
422 // now get all assignee contact ids and target contact ids for this activity
b7c0a88f 423 $sql = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)";
424 $aaDAO = &CRM_Core_DAO::executeQuery($sql);
425 $activityContacts = [];
6a488035 426 while ($aaDAO->fetch()) {
b7c0a88f 427 $activityAssignee = [
428 'id' => $aaDAO->id,
6a488035
TO
429 'assignee_contact_id' => $aaDAO->assignee_contact_id,
430 'activity_id' => $aaDAO->activity_id,
b7c0a88f 431 ];
6a488035
TO
432 appendValue($values, $aaDAO->id, 'activity_assignment', $activityAssignee);
433 $activityContacts[] = $aaDAO->assignee_contact_id;
434 }
6a488035
TO
435
436 $sql = "SELECT * FROM civicrm_activity_target WHERE activity_id IN ($activityIDString)";
437 $atDAO = &CRM_Core_DAO::executeQuery($sql);
438 while ($atDAO->fetch()) {
b7c0a88f 439 $activityTarget = [
440 'id' => $atDAO->id,
6a488035
TO
441 'target_contact_id' => $atDAO->target_contact_id,
442 'activity_id' => $atDAO->activity_id,
b7c0a88f 443 ];
6a488035
TO
444 appendValue($values, $atDAO->id, 'activity_target', $activityTarget);
445 $activityContacts[] = $atDAO->target_contact_id;
446 }
6a488035
TO
447
448 addAdditionalContacts($activityContacts, $allContactIDs, $additionalContacts);
449}
450
a1a55b61
EM
451/**
452 * @param $values
453 * @param $id
454 * @param $name
455 * @param $value
456 * @param bool $ignored
457 */
6a488035
TO
458function appendValue(&$values, $id, $name, $value, $ignored = FALSE) {
459 if (empty($value)) {
460 return;
461 }
462
463 if (!isset($values[$name])) {
b7c0a88f 464 $values[$name] = [];
6a488035
TO
465 $values[$name][] = array_keys($value);
466 }
467 $values[$name][] = array_values($value);
468}
469
a1a55b61 470/**
c490a46a 471 * @param string $daoName
a1a55b61
EM
472 *
473 * @return mixed
474 */
6a488035 475function getDBFields($daoName) {
b7c0a88f 476 static $_fieldsRetrieved = [];
6a488035
TO
477
478 if (!isset($_fieldsRetrieved[$daoName])) {
b7c0a88f 479 $_fieldsRetrieved[$daoName] = [];
6a488035 480 $daoFile = str_replace('_',
b7c0a88f 481 DIRECTORY_SEPARATOR,
482 $daoName
483 ) . '.php';
484 include_once($daoFile);
6a488035
TO
485
486 $daoFields = &$daoName::fields();
487 require_once 'CRM/Utils/Array.php';
488
489 foreach ($daoFields as $key => & $value) {
b7c0a88f 490 $_fieldsRetrieved[$daoName][$value['name']] = [
491 'uniqueName' => $key,
6a488035
TO
492 'type' => $value['type'],
493 'title' => CRM_Utils_Array::value('title', $value, NULL),
b7c0a88f 494 ];
6a488035
TO
495 }
496 }
497 return $_fieldsRetrieved[$daoName];
498}
499
a1a55b61
EM
500/**
501 * @param $contactIDs
502 * @param $allContactIDs
503 * @param $additionalContacts
504 */
6a488035
TO
505function addAdditionalContacts($contactIDs, &$allContactIDs, &$additionalContacts) {
506 foreach ($contactIDs as $cid) {
507 if ($cid &&
508 !isset($allContactIDs[$cid]) &&
509 !isset($additionalContacts[$cid])
510 ) {
511 $additionalContacts[$cid] = $cid;
512 }
513 }
514}
515
a1a55b61
EM
516/**
517 * @param $values
518 * @param $contactIDs
519 * @param $allContactIDs
520 */
6a488035
TO
521function run(&$values, &$contactIDs, &$allContactIDs) {
522 $chunks = &splitContactIDs($contactIDs);
523
b7c0a88f 524 $additionalContactIDs = [];
6a488035
TO
525
526 foreach ($chunks as $chunk) {
527 getValues($chunk, $values, $allContactIDs, $additionalContactIDs);
528 }
529
530 if (!empty($additionalContactIDs)) {
531 $allContactIDs = $allContactIDs + $additionalContactIDs;
532 run($values, $additionalContactIDs, $allContactIDs);
533 }
534}
535
f3a87cf4 536$config = CRM_Core_Config::singleton();
6a488035
TO
537$config->userFramework = 'Soap';
538$config->userFrameworkClass = 'CRM_Utils_System_Soap';
539$config->userHookClass = 'CRM_Utils_Hook_Soap';
540
541$sql = "
a1a55b61 542SELECT id
6a488035
TO
543FROM civicrm_contact
544LIMIT 10
545";
546$dao = &CRM_Core_DAO::executeQuery($sql);
547
548
b7c0a88f 549$contactIDs = [];
6a488035
TO
550while ($dao->fetch()) {
551 $contactIDs[$dao->id] = $dao->id;
552}
553
b7c0a88f 554$values = [];
6a488035
TO
555run($values, $contactIDs, $contactIDs);
556
557$json = json_encode($values);
558echo $json;
559// print_r( json_decode( $json ) );
560