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 | * | |
92 | * @param array $contactIds Contact IDS to do labels for | |
93 | * @param integer $locationTypeID | |
94 | * @param boolean $respectDoNotMail | |
95 | * @return array of rows for labels | |
96 | * @access public | |
97 | */ | |
98 | ||
99 | static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) { | |
100 | $locName = NULL; | |
101 | //get the address format sequence from the config file | |
102 | $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties(); | |
103 | ||
104 | //build the returnproperties | |
105 | $returnProperties = array('display_name' => 1, 'contact_type' => 1); | |
106 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
107 | 'mailing_format' | |
108 | ); | |
109 | ||
110 | $mailingFormatProperties = array(); | |
111 | if ($mailingFormat) { | |
112 | $mailingFormatProperties = CRM_Contact_Form_Task_LabelCommon::regexReturnProperties($mailingFormat); | |
113 | $returnProperties = array_merge($returnProperties, $mailingFormatProperties); | |
114 | } | |
115 | ||
116 | $customFormatProperties = array(); | |
117 | if (stristr($mailingFormat, 'custom_')) { | |
118 | foreach ($mailingFormatProperties as $token => $true) { | |
119 | if (substr($token, 0, 7) == 'custom_') { | |
a7488080 | 120 | if (empty($customFormatProperties[$token])) { |
2d3e3c7b | 121 | $customFormatProperties[$token] = $mailingFormatProperties[$token]; |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | $returnProperties = array_merge($returnProperties, $customFormatProperties); | |
127 | ||
128 | if ($mergeSameAddress) { | |
129 | // we need first name/last name for summarising to avoid spillage | |
130 | $returnProperties['first_name'] = 1; | |
131 | $returnProperties['last_name'] = 1; | |
132 | } | |
133 | ||
134 | //get the contacts information | |
135 | $params = $custom = array(); | |
136 | foreach ($contactIDs as $key => $contactID) { | |
137 | $params[] = array( | |
138 | CRM_Core_Form::CB_PREFIX . $contactID, | |
139 | '=', 1, 0, 0, | |
140 | ); | |
141 | } | |
142 | ||
143 | // fix for CRM-2651 | |
a7488080 | 144 | if (!empty($respectDoNotMail['do_not_mail'])) { |
2d3e3c7b | 145 | $params[] = array('do_not_mail', '=', 0, 0, 0); |
146 | } | |
147 | // fix for CRM-2613 | |
148 | $params[] = array('is_deceased', '=', 0, 0, 0); | |
149 | ||
150 | if ($locationTypeID) { | |
151 | $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); | |
152 | $locName = $locType[$locationTypeID]; | |
153 | $location = array('location' => array("{$locName}" => $addressReturnProperties)); | |
154 | $returnProperties = array_merge($returnProperties, $location); | |
155 | $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0); | |
156 | } | |
157 | else { | |
158 | $returnProperties = array_merge($returnProperties, $addressReturnProperties); | |
159 | } | |
160 | ||
161 | foreach ($returnProperties as $name) { | |
162 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
163 | if ($cfID) { | |
164 | $custom[] = $cfID; | |
165 | } | |
166 | } | |
167 | ||
168 | //get the total number of contacts to fetch from database. | |
169 | $numberofContacts = count($contactIDs); | |
170 | //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds) | |
171 | // except it also handles multiple locations | |
172 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); | |
173 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
174 | ||
175 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); | |
176 | $details = $details[0]; | |
177 | $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details); | |
178 | ||
179 | foreach ($contactIDs as $value) { | |
180 | foreach ($custom as $cfID) { | |
181 | if (isset($details[$value]["custom_{$cfID}"])) { | |
182 | $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]); | |
183 | } | |
184 | } | |
185 | $contact = CRM_Utils_Array::value($value, $details); | |
186 | ||
187 | if (is_a($contact, 'CRM_Core_Error')) { | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | // we need to remove all the "_id" | |
192 | unset($contact['contact_id']); | |
193 | ||
8cc574cf | 194 | if ($locName && !empty($contact[$locName])) { |
2d3e3c7b | 195 | // If location type is not primary, $contact contains |
196 | // one more array as "$contact[$locName] = array( values... )" | |
197 | ||
198 | if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { | |
199 | continue; | |
200 | } | |
201 | ||
202 | unset($contact[$locName]); | |
203 | ||
a7488080 | 204 | if (!empty($contact['county_id'])) { |
2d3e3c7b | 205 | unset($contact['county_id']); |
206 | } | |
207 | ||
208 | foreach ($contact as $field => $fieldValue) { | |
209 | $rows[$value][$field] = $fieldValue; | |
210 | } | |
211 | ||
212 | $valuesothers = array(); | |
213 | $paramsothers = array('contact_id' => $value); | |
214 | $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); | |
215 | if ($locationTypeID) { | |
216 | foreach ($valuesothers as $vals) { | |
217 | if ( CRM_Utils_Array::value('location_type_id', $vals) == | |
218 | $locationTypeID) { | |
219 | foreach ($vals as $k => $v) { | |
220 | if (in_array($k, array( | |
221 | 'email', 'phone', 'im', 'openid'))) { | |
222 | if ($k == 'im') { | |
223 | $rows[$value][$k] = $v['1']['name']; | |
224 | } | |
225 | else { | |
226 | $rows[$value][$k] = $v['1'][$k]; | |
227 | } | |
228 | $rows[$value][$k . '_id'] = $v['1']['id']; | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
235 | else { | |
236 | if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { | |
237 | continue; | |
238 | } | |
239 | ||
a7488080 | 240 | if (!empty($contact['addressee_display'])) { |
2d3e3c7b | 241 | $contact['addressee_display'] = trim($contact['addressee_display']); |
242 | } | |
a7488080 | 243 | if (!empty($contact['addressee'])) { |
2d3e3c7b | 244 | $contact['addressee'] = $contact['addressee_display']; |
245 | } | |
246 | ||
247 | // now create the rows for generating mailing labels | |
248 | foreach ($contact as $field => $fieldValue) { | |
249 | $rows[$value][$field] = $fieldValue; | |
250 | } | |
251 | } | |
252 | } | |
253 | // sigh couldn't extract out tokenfields yet | |
254 | return array($rows, $tokenFields); | |
255 | } | |
256 | ||
257 | /** | |
258 | * function to extract the return properties from the mailing format | |
259 | * @todo I'm placing bets this is a duplicate of code elsewhere - find & merge | |
260 | * @param unknown_type $format | |
261 | * @return multitype:number | |
262 | */ | |
263 | function regexReturnProperties(&$format) { | |
264 | $returnProperties = array(); | |
265 | $matches = array(); | |
266 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
267 | $format, | |
268 | $matches, | |
269 | PREG_PATTERN_ORDER | |
270 | ); | |
271 | if ($matches[1]) { | |
272 | foreach ($matches[1] as $token) { | |
273 | list($type, $name) = preg_split('/\./', $token, 2); | |
274 | if ($name) { | |
275 | $returnProperties["{$name}"] = 1; | |
276 | } | |
277 | } | |
278 | } | |
279 | ||
280 | return $returnProperties; | |
281 | } | |
282 | ||
283 | /** | |
284 | * Get array of return properties for address fields required for mailing label | |
285 | * @return array return properites for address e.g | |
286 | * array ( | |
287 | * - [street_address] => 1, | |
288 | * - [supplemental_address_1] => 1, | |
289 | * - [supplemental_address_2] => 1 | |
290 | * ) | |
291 | */ | |
292 | function getAddressReturnProperties() { | |
293 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
294 | 'mailing_format' | |
295 | ); | |
296 | ||
297 | $addressFields = CRM_Utils_Address::sequence($mailingFormat); | |
298 | $addressReturnProperties = array_fill_keys($addressFields, 1); | |
299 | ||
300 | if (array_key_exists('postal_code', $addressReturnProperties)) { | |
301 | $addressReturnProperties['postal_code_suffix'] = 1; | |
302 | } | |
303 | return $addressReturnProperties; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Get token list from mailing format & contacts | |
308 | * @param unknown_type $contacts | |
309 | * @return unknown | |
310 | */ | |
311 | function getTokenData(&$contacts) { | |
312 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
313 | 'mailing_format' | |
314 | ); | |
315 | $tokens = $tokenFields = array(); | |
316 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); | |
317 | ||
318 | // also get all token values | |
319 | CRM_Utils_Hook::tokenValues($contacts, | |
320 | array_keys($contacts), | |
321 | NULL, | |
322 | $messageToken, | |
323 | 'CRM_Contact_Form_Task_LabelCommon' | |
324 | ); | |
325 | ||
326 | CRM_Utils_Hook::tokens($tokens); | |
327 | ||
328 | foreach ($tokens as $category => $catTokens) { | |
329 | foreach ($catTokens as $token => $tokenName) { | |
330 | $tokenFields[] = $token; | |
331 | } | |
332 | } | |
333 | return $tokenFields; | |
334 | ||
335 | } | |
336 | /** | |
337 | * Merge contacts with the Same address to get one shared label | |
338 | * @param unknown_type $rows | |
339 | */ | |
340 | function mergeSameAddress(&$rows) { | |
341 | $uniqueAddress = array(); | |
342 | foreach (array_keys($rows) as $rowID) { | |
343 | // load complete address as array key | |
344 | $address = | |
345 | trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); | |
346 | if (isset($rows[$rowID]['last_name'])) { | |
347 | $name = $rows[$rowID]['last_name']; | |
348 | } | |
349 | else { | |
350 | $name = $rows[$rowID]['display_name']; | |
351 | } | |
352 | // fill uniqueAddress array with last/first name tree | |
353 | if (isset($uniqueAddress[$address])) { | |
354 | $uniqueAddress[$address]['names'][$name][] = $rows[$rowID]['first_name']; | |
355 | // drop unnecessary rows | |
356 | unset($rows[$rowID]); | |
357 | // this is the first listing at this address | |
358 | } | |
359 | else { | |
360 | $uniqueAddress[$address]['ID'] = $rowID; | |
361 | $uniqueAddress[$address]['names'][$name][] = $rows[$rowID]['first_name']; | |
362 | } | |
363 | } | |
364 | foreach ($uniqueAddress as $address => $data) { | |
365 | // copy data back to $rows | |
366 | $count = 0; | |
367 | // one last name list per row | |
368 | foreach ($data['names'] as $last_name => $first_names) { | |
369 | // too many to list | |
370 | if ($count > 2) { | |
371 | break; | |
372 | } | |
373 | // collapse the tree to summarize | |
374 | $family = trim(implode(" & ", $first_names) . " " . $last_name); | |
375 | if ($count) { | |
376 | $processedNames .= "\n" . $family; | |
377 | } | |
378 | else { | |
379 | // build display_name string | |
380 | $processedNames = $family; | |
381 | } | |
382 | $count++; | |
383 | } | |
384 | $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; | |
385 | } | |
386 | } | |
387 | ||
388 | function mergeSameHousehold(&$rows) { | |
389 | # group selected contacts by type | |
390 | $individuals = array(); | |
391 | $households = array(); | |
392 | foreach ($rows as $contact_id => $row) { | |
393 | if ($row['contact_type'] == 'Household') { | |
394 | $households[$contact_id] = $row; | |
395 | } | |
396 | elseif ($row['contact_type'] == 'Individual') { | |
397 | $individuals[$contact_id] = $row; | |
398 | } | |
399 | } | |
400 | ||
401 | # exclude individuals belonging to selected households | |
402 | foreach ($households as $household_id => $row) { | |
403 | $dao = new CRM_Contact_DAO_Relationship(); | |
404 | $dao->contact_id_b = $household_id; | |
405 | $dao->find(); | |
406 | while ($dao->fetch()) { | |
407 | $individual_id = $dao->contact_id_a; | |
408 | if (array_key_exists($individual_id, $individuals)) { | |
409 | unset($individuals[$individual_id]); | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
414 | # merge back individuals and households | |
415 | $rows = array_merge($individuals, $households); | |
416 | return $rows; | |
417 | } | |
418 | } |