Commit | Line | Data |
---|---|---|
2d3e3c7b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
2d3e3c7b | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
2d3e3c7b | 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 and the CiviCRM Licensing Exception. | | |
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 and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
2d3e3c7b | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the common functionality for sending email to | |
38 | * one or a group of contact ids. This class is reused by all the search | |
39 | * components in CiviCRM (since they all have send email as a task) | |
40 | */ | |
41 | class CRM_Contact_Form_Task_LabelCommon { | |
42 | /** | |
43 | * Check for presence of tokens to be swapped out | |
77b97be7 | 44 | * |
2d3e3c7b | 45 | * @param array $contact |
46 | * @param array $mailingFormatProperties | |
47 | * @param array $tokenFields | |
77b97be7 EM |
48 | * |
49 | * @return bool | |
2d3e3c7b | 50 | */ |
51 | function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { | |
52 | foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { | |
53 | //we should not consider addressee for data exists, CRM-6025 | |
8cc574cf | 54 | if ($key != 'addressee' && !empty($contact[$key])) { |
2d3e3c7b | 55 | return TRUE; |
56 | } | |
57 | } | |
58 | return FALSE; | |
59 | } | |
60 | /** | |
61 | * function to create labels (pdf) | |
62 | * | |
63 | * @param array $contactRows assciated array of contact data | |
64 | * @param string $format format in which labels needs to be printed | |
65 | * @param string $fileName The name of the file to save the label in | |
66 | * | |
67 | * @return null | |
68 | * @access public | |
69 | */ | |
70 | static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { | |
71 | $pdf = new CRM_Utils_PDF_Label($format, 'mm'); | |
72 | $pdf->Open(); | |
73 | $pdf->AddPage(); | |
74 | ||
75 | //build contact string that needs to be printed | |
76 | $val = NULL; | |
77 | foreach ($contactRows as $row => $value) { | |
78 | foreach ($value as $k => $v) { | |
79 | $val .= "$v\n"; | |
80 | } | |
81 | ||
82 | $pdf->AddPdfLabel($val); | |
83 | $val = ''; | |
84 | } | |
85 | $pdf->Output($fileName, 'D'); | |
86 | } | |
87 | ||
88 | ||
89 | /** | |
90 | * function to get the rows for the labels | |
91 | * | |
6c8f6e67 | 92 | * @param $contactIDs |
2d3e3c7b | 93 | * @param integer $locationTypeID |
94 | * @param boolean $respectDoNotMail | |
6c8f6e67 EM |
95 | * @param $mergeSameAddress |
96 | * @param $mergeSameHousehold | |
97 | * | |
98 | * @internal param array $contactIds Contact IDS to do labels for | |
2d3e3c7b | 99 | * @return array of rows for labels |
100 | * @access public | |
101 | */ | |
102 | ||
103 | static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) { | |
104 | $locName = NULL; | |
105 | //get the address format sequence from the config file | |
106 | $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties(); | |
107 | ||
108 | //build the returnproperties | |
ec1edc63 | 109 | $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1); |
2d3e3c7b | 110 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
111 | 'mailing_format' | |
112 | ); | |
113 | ||
114 | $mailingFormatProperties = array(); | |
115 | if ($mailingFormat) { | |
116 | $mailingFormatProperties = CRM_Contact_Form_Task_LabelCommon::regexReturnProperties($mailingFormat); | |
117 | $returnProperties = array_merge($returnProperties, $mailingFormatProperties); | |
118 | } | |
119 | ||
120 | $customFormatProperties = array(); | |
121 | if (stristr($mailingFormat, 'custom_')) { | |
122 | foreach ($mailingFormatProperties as $token => $true) { | |
123 | if (substr($token, 0, 7) == 'custom_') { | |
a7488080 | 124 | if (empty($customFormatProperties[$token])) { |
2d3e3c7b | 125 | $customFormatProperties[$token] = $mailingFormatProperties[$token]; |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | $returnProperties = array_merge($returnProperties, $customFormatProperties); | |
131 | ||
132 | if ($mergeSameAddress) { | |
133 | // we need first name/last name for summarising to avoid spillage | |
134 | $returnProperties['first_name'] = 1; | |
135 | $returnProperties['last_name'] = 1; | |
136 | } | |
137 | ||
138 | //get the contacts information | |
139 | $params = $custom = array(); | |
140 | foreach ($contactIDs as $key => $contactID) { | |
141 | $params[] = array( | |
142 | CRM_Core_Form::CB_PREFIX . $contactID, | |
143 | '=', 1, 0, 0, | |
144 | ); | |
145 | } | |
146 | ||
147 | // fix for CRM-2651 | |
a7488080 | 148 | if (!empty($respectDoNotMail['do_not_mail'])) { |
2d3e3c7b | 149 | $params[] = array('do_not_mail', '=', 0, 0, 0); |
150 | } | |
151 | // fix for CRM-2613 | |
152 | $params[] = array('is_deceased', '=', 0, 0, 0); | |
153 | ||
154 | if ($locationTypeID) { | |
155 | $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); | |
156 | $locName = $locType[$locationTypeID]; | |
157 | $location = array('location' => array("{$locName}" => $addressReturnProperties)); | |
158 | $returnProperties = array_merge($returnProperties, $location); | |
159 | $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0); | |
160 | } | |
161 | else { | |
162 | $returnProperties = array_merge($returnProperties, $addressReturnProperties); | |
163 | } | |
164 | ||
165 | foreach ($returnProperties as $name) { | |
166 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
167 | if ($cfID) { | |
168 | $custom[] = $cfID; | |
169 | } | |
170 | } | |
171 | ||
172 | //get the total number of contacts to fetch from database. | |
173 | $numberofContacts = count($contactIDs); | |
174 | //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds) | |
175 | // except it also handles multiple locations | |
176 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); | |
177 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
178 | ||
179 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); | |
180 | $details = $details[0]; | |
181 | $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details); | |
182 | ||
183 | foreach ($contactIDs as $value) { | |
184 | foreach ($custom as $cfID) { | |
185 | if (isset($details[$value]["custom_{$cfID}"])) { | |
186 | $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]); | |
187 | } | |
188 | } | |
189 | $contact = CRM_Utils_Array::value($value, $details); | |
190 | ||
191 | if (is_a($contact, 'CRM_Core_Error')) { | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | // we need to remove all the "_id" | |
196 | unset($contact['contact_id']); | |
197 | ||
8cc574cf | 198 | if ($locName && !empty($contact[$locName])) { |
2d3e3c7b | 199 | // If location type is not primary, $contact contains |
200 | // one more array as "$contact[$locName] = array( values... )" | |
201 | ||
202 | if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { | |
203 | continue; | |
204 | } | |
205 | ||
206 | unset($contact[$locName]); | |
207 | ||
a7488080 | 208 | if (!empty($contact['county_id'])) { |
2d3e3c7b | 209 | unset($contact['county_id']); |
210 | } | |
211 | ||
212 | foreach ($contact as $field => $fieldValue) { | |
213 | $rows[$value][$field] = $fieldValue; | |
214 | } | |
215 | ||
216 | $valuesothers = array(); | |
217 | $paramsothers = array('contact_id' => $value); | |
218 | $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); | |
219 | if ($locationTypeID) { | |
220 | foreach ($valuesothers as $vals) { | |
221 | if ( CRM_Utils_Array::value('location_type_id', $vals) == | |
222 | $locationTypeID) { | |
223 | foreach ($vals as $k => $v) { | |
224 | if (in_array($k, array( | |
225 | 'email', 'phone', 'im', 'openid'))) { | |
226 | if ($k == 'im') { | |
227 | $rows[$value][$k] = $v['1']['name']; | |
228 | } | |
229 | else { | |
230 | $rows[$value][$k] = $v['1'][$k]; | |
231 | } | |
232 | $rows[$value][$k . '_id'] = $v['1']['id']; | |
233 | } | |
234 | } | |
235 | } | |
236 | } | |
237 | } | |
238 | } | |
239 | else { | |
240 | if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { | |
241 | continue; | |
242 | } | |
243 | ||
a7488080 | 244 | if (!empty($contact['addressee_display'])) { |
2d3e3c7b | 245 | $contact['addressee_display'] = trim($contact['addressee_display']); |
246 | } | |
a7488080 | 247 | if (!empty($contact['addressee'])) { |
2d3e3c7b | 248 | $contact['addressee'] = $contact['addressee_display']; |
249 | } | |
250 | ||
251 | // now create the rows for generating mailing labels | |
252 | foreach ($contact as $field => $fieldValue) { | |
253 | $rows[$value][$field] = $fieldValue; | |
254 | } | |
255 | } | |
256 | } | |
257 | // sigh couldn't extract out tokenfields yet | |
258 | return array($rows, $tokenFields); | |
259 | } | |
260 | ||
261 | /** | |
262 | * function to extract the return properties from the mailing format | |
263 | * @todo I'm placing bets this is a duplicate of code elsewhere - find & merge | |
264 | * @param unknown_type $format | |
265 | * @return multitype:number | |
266 | */ | |
267 | function regexReturnProperties(&$format) { | |
268 | $returnProperties = array(); | |
269 | $matches = array(); | |
270 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
271 | $format, | |
272 | $matches, | |
273 | PREG_PATTERN_ORDER | |
274 | ); | |
275 | if ($matches[1]) { | |
276 | foreach ($matches[1] as $token) { | |
277 | list($type, $name) = preg_split('/\./', $token, 2); | |
278 | if ($name) { | |
279 | $returnProperties["{$name}"] = 1; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | return $returnProperties; | |
285 | } | |
286 | ||
287 | /** | |
288 | * Get array of return properties for address fields required for mailing label | |
289 | * @return array return properites for address e.g | |
290 | * array ( | |
291 | * - [street_address] => 1, | |
292 | * - [supplemental_address_1] => 1, | |
293 | * - [supplemental_address_2] => 1 | |
294 | * ) | |
295 | */ | |
296 | function getAddressReturnProperties() { | |
297 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
298 | 'mailing_format' | |
299 | ); | |
300 | ||
301 | $addressFields = CRM_Utils_Address::sequence($mailingFormat); | |
302 | $addressReturnProperties = array_fill_keys($addressFields, 1); | |
303 | ||
304 | if (array_key_exists('postal_code', $addressReturnProperties)) { | |
305 | $addressReturnProperties['postal_code_suffix'] = 1; | |
306 | } | |
307 | return $addressReturnProperties; | |
308 | } | |
309 | ||
310 | /** | |
311 | * Get token list from mailing format & contacts | |
312 | * @param unknown_type $contacts | |
313 | * @return unknown | |
314 | */ | |
315 | function getTokenData(&$contacts) { | |
316 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
317 | 'mailing_format' | |
318 | ); | |
319 | $tokens = $tokenFields = array(); | |
320 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); | |
321 | ||
322 | // also get all token values | |
323 | CRM_Utils_Hook::tokenValues($contacts, | |
324 | array_keys($contacts), | |
325 | NULL, | |
326 | $messageToken, | |
327 | 'CRM_Contact_Form_Task_LabelCommon' | |
328 | ); | |
329 | ||
330 | CRM_Utils_Hook::tokens($tokens); | |
331 | ||
332 | foreach ($tokens as $category => $catTokens) { | |
333 | foreach ($catTokens as $token => $tokenName) { | |
334 | $tokenFields[] = $token; | |
335 | } | |
336 | } | |
337 | return $tokenFields; | |
338 | ||
339 | } | |
340 | /** | |
341 | * Merge contacts with the Same address to get one shared label | |
342 | * @param unknown_type $rows | |
343 | */ | |
344 | function mergeSameAddress(&$rows) { | |
345 | $uniqueAddress = array(); | |
346 | foreach (array_keys($rows) as $rowID) { | |
347 | // load complete address as array key | |
348 | $address = | |
349 | trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); | |
350 | if (isset($rows[$rowID]['last_name'])) { | |
351 | $name = $rows[$rowID]['last_name']; | |
352 | } | |
353 | else { | |
354 | $name = $rows[$rowID]['display_name']; | |
355 | } | |
ec1edc63 RK |
356 | $formatted = array( |
357 | 'first_name' => $rows[$rowID]['first_name'], | |
358 | 'individual_prefix' => $rows[$rowID]['individual_prefix'] | |
359 | ); | |
360 | $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'display_name_format'); | |
361 | $firstNameWithPrefix = CRM_Utils_Address::format($formatted, $format, FALSE, FALSE, TRUE); | |
362 | $firstNameWithPrefix = trim($firstNameWithPrefix); | |
363 | ||
2d3e3c7b | 364 | // fill uniqueAddress array with last/first name tree |
365 | if (isset($uniqueAddress[$address])) { | |
f456d4f1 RK |
366 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; |
367 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
2d3e3c7b | 368 | // drop unnecessary rows |
369 | unset($rows[$rowID]); | |
370 | // this is the first listing at this address | |
371 | } | |
372 | else { | |
373 | $uniqueAddress[$address]['ID'] = $rowID; | |
f456d4f1 RK |
374 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; |
375 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
2d3e3c7b | 376 | } |
377 | } | |
378 | foreach ($uniqueAddress as $address => $data) { | |
379 | // copy data back to $rows | |
380 | $count = 0; | |
381 | // one last name list per row | |
382 | foreach ($data['names'] as $last_name => $first_names) { | |
383 | // too many to list | |
384 | if ($count > 2) { | |
385 | break; | |
386 | } | |
f456d4f1 RK |
387 | if(count($first_names) == 1) { |
388 | $family = $first_names[current(array_keys($first_names))]['addressee_display']; | |
389 | } | |
390 | else { | |
391 | // collapse the tree to summarize | |
392 | $family = trim(implode(" & ", $first_names) . " " . $last_name); | |
393 | } | |
2d3e3c7b | 394 | if ($count) { |
395 | $processedNames .= "\n" . $family; | |
396 | } | |
397 | else { | |
398 | // build display_name string | |
399 | $processedNames = $family; | |
400 | } | |
401 | $count++; | |
402 | } | |
403 | $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; | |
404 | } | |
405 | } | |
406 | ||
86538308 EM |
407 | /** |
408 | * @param $rows | |
409 | * | |
410 | * @return array | |
411 | */ | |
2d3e3c7b | 412 | function mergeSameHousehold(&$rows) { |
413 | # group selected contacts by type | |
414 | $individuals = array(); | |
415 | $households = array(); | |
416 | foreach ($rows as $contact_id => $row) { | |
417 | if ($row['contact_type'] == 'Household') { | |
418 | $households[$contact_id] = $row; | |
419 | } | |
420 | elseif ($row['contact_type'] == 'Individual') { | |
421 | $individuals[$contact_id] = $row; | |
422 | } | |
423 | } | |
424 | ||
425 | # exclude individuals belonging to selected households | |
426 | foreach ($households as $household_id => $row) { | |
427 | $dao = new CRM_Contact_DAO_Relationship(); | |
428 | $dao->contact_id_b = $household_id; | |
429 | $dao->find(); | |
430 | while ($dao->fetch()) { | |
431 | $individual_id = $dao->contact_id_a; | |
432 | if (array_key_exists($individual_id, $individuals)) { | |
433 | unset($individuals[$individual_id]); | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
438 | # merge back individuals and households | |
439 | $rows = array_merge($individuals, $households); | |
440 | return $rows; | |
441 | } | |
442 | } |