3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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.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
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 XML in the Solr format
69 function &generateSolrXML($values) {
71 foreach ($values as $cid => $tokens) {
78 <field name="id">$cid</field>\n
81 foreach ($tokens as $t) {
83 <field name="$t[0]">$t[1]</field>\n
87 $result .= " </doc>\n";
89 $result .= "</add>\n";
96 * Given a set of contact IDs get the values
98 function getValues(&$contactIDs, &$values) {
101 foreach ($contactIDs as $cid) {
102 $values[$cid] = array();
105 getContactInfo($contactIDs, $values);
106 getLocationInfo($contactIDs, $values);
117 * @param null $additionalWhereCond
119 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
120 $selectString = implode(',', array_keys($fields));
121 $idString = implode(',', $contactIDs);
124 SELECT $selectString, $whereField as contact_id
126 WHERE $whereField IN ( $idString )
129 if ($additionalWhereCond) {
130 $sql .= " AND $additionalWhereCond";
133 $dao = &CRM_Core_DAO
::executeQuery($sql, CRM_Core_DAO
::$_nullArray);
134 while ($dao->fetch()) {
135 foreach ($fields as $fld => $name) {
136 if (empty($dao->$fld)) {
142 $values[$dao->contact_id
][] = array($name, $dao->$fld);
151 function getContactInfo(&$contactIDs, &$values) {
152 $fields = array('sort_name' => NULL,
153 'display_name' => NULL,
154 'contact_type' => NULL,
155 'legal_identifier' => NULL,
156 'external_identifier' => NULL,
157 'source' => 'contact_source',
159 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
161 $fields = array('first_name' => NULL,
163 'middle_name' => NULL,
166 getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id');
168 $fields = array('household_name' => NULL);
169 getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id');
171 $fields = array('organization_name' => NULL,
172 'legal_name' => NULL,
175 getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id');
177 $fields = array('note' => 'note_body',
178 'subject' => 'note_subject',
180 getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
187 function getLocationInfo(&$contactIDs, &$values) {
188 $ids = implode(',', $contactIDs);
192 l.entity_id as contact_id, l.name as location_name,
193 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
194 a.city, a.postal_code,
195 co.name as county, s.name as state, c.name as country,
196 e.email, p.phone, i.name as im
199 LEFT JOIN civicrm_address a ON a.location_id = l.id
200 LEFT JOIN civicrm_email e ON e.location_id = l.id
201 LEFT JOIN civicrm_phone p ON p.location_id = l.id
202 LEFT JOIN civicrm_im i ON i.location_id = l.id
203 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
204 LEFT JOIN civicrm_country c ON a.country_id = c.id
205 LEFT JOIN civicrm_county co ON a.county_id = co.id
206 WHERE l.entity_table = 'civicrm_contact'
207 AND l.entity_id IN ( $ids )
210 $fields = array('location_name', 'street_address', 'supplemental_address_1',
211 'supplemental_address_2', 'city', 'postal_code', 'county', 'state',
212 'country', 'email', 'phone', 'im',
214 $dao = &CRM_Core_DAO
::executeQuery($sql, CRM_Core_DAO
::$_nullArray);
215 while ($dao->fetch()) {
216 foreach ($fields as $fld) {
217 if (empty($dao->$fld)) {
220 $values[$dao->contact_id
][] = array($fld, $dao->$fld);
228 function run(&$contactIDs) {
229 $chunks = &splitContactIDs($contactIDs);
231 foreach ($chunks as $chunk) {
233 getValues($chunk, $values);
234 $xml = &generateSolrXML($values);
239 $config = &CRM_Core_Config
::singleton();
240 $config->userFramework
= 'Soap';
241 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
242 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
248 $dao = &CRM_Core_DAO
::executeQuery($sql, CRM_Core_DAO
::$_nullArray);
250 $contactIDs = array();
251 while ($dao->fetch()) {
252 $contactIDs[] = $dao->id
;