3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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] = 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 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) {
109 $values[$cid] = array();
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
][] = array($name, $dao->$fld);
158 function getContactInfo(&$contactIDs, &$values) {
159 $fields = array('sort_name' => NULL,
160 'display_name' => NULL,
161 'contact_type' => NULL,
162 'legal_identifier' => NULL,
163 'external_identifier' => NULL,
164 'source' => 'contact_source',
166 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
168 $fields = array('first_name' => NULL,
170 'middle_name' => NULL,
173 getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id');
175 $fields = array('household_name' => NULL);
176 getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id');
178 $fields = array('organization_name' => NULL,
179 'legal_name' => NULL,
182 getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id');
184 $fields = array('note' => 'note_body',
185 'subject' => 'note_subject',
187 getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
194 function getLocationInfo(&$contactIDs, &$values) {
195 $ids = implode(',', $contactIDs);
199 l.entity_id as contact_id, l.name as location_name,
200 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
201 a.city, a.postal_code,
202 co.name as county, s.name as state, c.name as country,
203 e.email, p.phone, i.name as im
206 LEFT JOIN civicrm_address a ON a.location_id = l.id
207 LEFT JOIN civicrm_email e ON e.location_id = l.id
208 LEFT JOIN civicrm_phone p ON p.location_id = l.id
209 LEFT JOIN civicrm_im i ON i.location_id = l.id
210 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
211 LEFT JOIN civicrm_country c ON a.country_id = c.id
212 LEFT JOIN civicrm_county co ON a.county_id = co.id
213 WHERE l.entity_table = 'civicrm_contact'
214 AND l.entity_id IN ( $ids )
217 $fields = array('location_name', 'street_address', 'supplemental_address_1',
218 'supplemental_address_2', 'city', 'postal_code', 'county', 'state',
219 'country', 'email', 'phone', 'im',
221 $dao = CRM_Core_DAO
::executeQuery($sql);
222 while ($dao->fetch()) {
223 foreach ($fields as $fld) {
224 if (empty($dao->$fld)) {
227 $values[$dao->contact_id
][] = array($fld, $dao->$fld);
235 function run(&$contactIDs) {
236 $chunks = &splitContactIDs($contactIDs);
238 foreach ($chunks as $chunk) {
240 getValues($chunk, $values);
241 $xml = &generateSolrXML($values);
246 $config = &CRM_Core_Config
::singleton();
247 $config->userFramework
= 'Soap';
248 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
249 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
255 $dao = CRM_Core_DAO
::executeQuery($sql);
257 $contactIDs = array();
258 while ($dao->fetch()) {
259 $contactIDs[] = $dao->id
;