Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 Solr | |
30 | */ | |
31 | ||
32 | require_once '../../civicrm.config.php'; | |
33 | require_once 'CRM/Core/Config.php'; | |
34 | ||
35 | define('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 | */ |
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 | |
45 | $chunks = array(); | |
46 | $current = 0; | |
47 | $chunks[$current] = array(); | |
48 | $count = 0; | |
49 | ||
50 | foreach ($contactIDs as $cid) { | |
51 | $chunks[$current][] = $cid; | |
52 | $count++; | |
53 | ||
54 | if ($count == CHUNK_SIZE) { | |
55 | $current++; | |
56 | $chunks[$current] = array(); | |
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 an array of values, generate the XML in the Solr format | |
d7c8cf03 EM |
70 | * @param $values |
71 | * @return string | |
6a488035 TO |
72 | */ |
73 | function &generateSolrXML($values) { | |
74 | $result = "<add>\n"; | |
75 | foreach ($values as $cid => $tokens) { | |
76 | if (empty($tokens)) { | |
77 | continue; | |
78 | } | |
79 | ||
80 | $result .= <<<EOT | |
81 | <doc> | |
82 | <field name="id">$cid</field>\n | |
83 | EOT; | |
84 | ||
85 | foreach ($tokens as $t) { | |
86 | $result .= <<<EOT | |
87 | <field name="$t[0]">$t[1]</field>\n | |
88 | EOT; | |
89 | } | |
90 | ||
91 | $result .= " </doc>\n"; | |
92 | } | |
93 | $result .= "</add>\n"; | |
94 | ||
95 | ||
96 | return $result; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Given a set of contact IDs get the values | |
d7c8cf03 EM |
101 | * @param $contactIDs |
102 | * @param $values | |
103 | * @return array | |
6a488035 TO |
104 | */ |
105 | function getValues(&$contactIDs, &$values) { | |
106 | $values = array(); | |
107 | ||
108 | foreach ($contactIDs as $cid) { | |
109 | $values[$cid] = array(); | |
110 | } | |
111 | ||
112 | getContactInfo($contactIDs, $values); | |
113 | getLocationInfo($contactIDs, $values); | |
114 | ||
115 | return $values; | |
116 | } | |
117 | ||
a1a55b61 EM |
118 | /** |
119 | * @param $contactIDs | |
120 | * @param $values | |
121 | * @param $tableName | |
122 | * @param $fields | |
123 | * @param $whereField | |
124 | * @param null $additionalWhereCond | |
125 | */ | |
6a488035 TO |
126 | function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) { |
127 | $selectString = implode(',', array_keys($fields)); | |
128 | $idString = implode(',', $contactIDs); | |
129 | ||
130 | $sql = " | |
131 | SELECT $selectString, $whereField as contact_id | |
132 | FROM $tableName | |
133 | WHERE $whereField IN ( $idString ) | |
134 | "; | |
135 | ||
136 | if ($additionalWhereCond) { | |
137 | $sql .= " AND $additionalWhereCond"; | |
138 | } | |
139 | ||
140 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
141 | while ($dao->fetch()) { | |
142 | foreach ($fields as $fld => $name) { | |
143 | if (empty($dao->$fld)) { | |
144 | continue; | |
145 | } | |
146 | if (!$name) { | |
147 | $name = $fld; | |
148 | } | |
149 | $values[$dao->contact_id][] = array($name, $dao->$fld); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
a1a55b61 EM |
154 | /** |
155 | * @param $contactIDs | |
156 | * @param $values | |
157 | */ | |
6a488035 TO |
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', | |
165 | ); | |
166 | getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id'); | |
167 | ||
168 | $fields = array('first_name' => NULL, | |
169 | 'last_name' => NULL, | |
170 | 'middle_name' => NULL, | |
171 | 'job_title' => NULL, | |
172 | ); | |
173 | getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id'); | |
174 | ||
175 | $fields = array('household_name' => NULL); | |
176 | getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id'); | |
177 | ||
178 | $fields = array('organization_name' => NULL, | |
179 | 'legal_name' => NULL, | |
180 | 'sic_code' => NULL, | |
181 | ); | |
182 | getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id'); | |
183 | ||
184 | $fields = array('note' => 'note_body', | |
185 | 'subject' => 'note_subject', | |
186 | ); | |
187 | getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'"); | |
188 | } | |
189 | ||
a1a55b61 EM |
190 | /** |
191 | * @param $contactIDs | |
192 | * @param $values | |
193 | */ | |
6a488035 TO |
194 | function getLocationInfo(&$contactIDs, &$values) { |
195 | $ids = implode(',', $contactIDs); | |
196 | ||
197 | $sql = " | |
198 | SELECT | |
199 | l.entity_id as contact_id, l.name as location_name, | |
200 | a.street_address, a.supplemental_address_1, a.supplemental_address_2, | |
a1a55b61 | 201 | a.city, a.postal_code, |
6a488035 TO |
202 | co.name as county, s.name as state, c.name as country, |
203 | e.email, p.phone, i.name as im | |
204 | FROM | |
205 | civicrm_location l | |
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 ) | |
215 | "; | |
216 | ||
217 | $fields = array('location_name', 'street_address', 'supplemental_address_1', | |
218 | 'supplemental_address_2', 'city', 'postal_code', 'county', 'state', | |
219 | 'country', 'email', 'phone', 'im', | |
220 | ); | |
221 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
222 | while ($dao->fetch()) { | |
223 | foreach ($fields as $fld) { | |
224 | if (empty($dao->$fld)) { | |
225 | continue; | |
226 | } | |
227 | $values[$dao->contact_id][] = array($fld, $dao->$fld); | |
228 | } | |
229 | } | |
230 | } | |
231 | ||
a1a55b61 EM |
232 | /** |
233 | * @param $contactIDs | |
234 | */ | |
6a488035 TO |
235 | function run(&$contactIDs) { |
236 | $chunks = &splitContactIDs($contactIDs); | |
237 | ||
238 | foreach ($chunks as $chunk) { | |
239 | $values = array(); | |
240 | getValues($chunk, $values); | |
241 | $xml = &generateSolrXML($values); | |
242 | echo $xml; | |
243 | } | |
244 | } | |
245 | ||
246 | $config = &CRM_Core_Config::singleton(); | |
247 | $config->userFramework = 'Soap'; | |
248 | $config->userFrameworkClass = 'CRM_Utils_System_Soap'; | |
249 | $config->userHookClass = 'CRM_Utils_Hook_Soap'; | |
250 | ||
251 | $sql = <<<EOT | |
a1a55b61 | 252 | SELECT id |
6a488035 TO |
253 | FROM civicrm_contact |
254 | EOT; | |
255 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
256 | ||
257 | $contactIDs = array(); | |
258 | while ($dao->fetch()) { | |
259 | $contactIDs[] = $dao->id; | |
260 | } | |
261 | ||
262 | run($contactIDs); | |
263 |