Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 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 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class helps to print the labels for contacts | |
38 | * | |
39 | */ | |
40 | class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { | |
41 | ||
42 | /** | |
43 | * build all the data structures needed to build the form | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | function preProcess() { | |
49 | $this->set('contactIds', $this->_contactIds); | |
50 | parent::preProcess(); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Build the form | |
55 | * | |
56 | * @access public | |
57 | * | |
58 | * @return void | |
59 | */ | |
60 | function buildQuickForm() { | |
61 | CRM_Utils_System::setTitle(ts('Make Mailing Labels')); | |
62 | ||
63 | //add select for label | |
64 | $label = CRM_Core_BAO_LabelFormat::getList(TRUE); | |
65 | ||
66 | $this->add('select', 'label_name', ts('Select Label'), array('' => ts('- select label -')) + $label, TRUE); | |
67 | ||
68 | ||
69 | // add select for Location Type | |
70 | $this->addElement('select', 'location_type_id', ts('Select Location'), | |
71 | array( | |
b2b0530a | 72 | '' => ts('Primary')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE |
6a488035 TO |
73 | ); |
74 | ||
75 | // checkbox for SKIP contacts with Do Not Mail privacy option | |
76 | $this->addElement('checkbox', 'do_not_mail', ts('Do not print labels for contacts with "Do Not Mail" privacy option checked')); | |
77 | ||
78 | $this->add('checkbox', 'merge_same_address', ts('Merge labels for contacts with the same address'), NULL); | |
79 | $this->add('checkbox', 'merge_same_household', ts('Merge labels for contacts belonging to the same household'), NULL); | |
80 | ||
85d25c8f DG |
81 | $this->addButtons(array( |
82 | array( | |
83 | 'type' => 'submit', | |
84 | 'name' => ts('Make Mailing Labels'), | |
85 | 'isDefault' => TRUE, | |
86 | ), | |
87 | array( | |
88 | 'type' => 'cancel', | |
89 | 'name' => ts('Done'), | |
90 | ), | |
91 | )); | |
6a488035 TO |
92 | } |
93 | ||
94 | /** | |
95 | * This function sets the default values for the form. | |
96 | * | |
97 | * @param null | |
98 | * | |
99 | * @return array array of default values | |
100 | * @access public | |
101 | */ | |
102 | function setDefaultValues() { | |
103 | $defaults = array(); | |
104 | $format = CRM_Core_BAO_LabelFormat::getDefaultValues(); | |
105 | $defaults['label_name'] = CRM_Utils_Array::value('name', $format); | |
106 | $defaults['do_not_mail'] = 1; | |
107 | ||
108 | return $defaults; | |
109 | } | |
110 | ||
111 | /** | |
112 | * process the form after the input has been submitted and validated | |
113 | * | |
114 | * @access public | |
115 | * | |
116 | * @return void | |
117 | */ | |
118 | public function postProcess() { | |
119 | $fv = $this->controller->exportValues($this->_name); | |
120 | $config = CRM_Core_Config::singleton(); | |
121 | $locName = NULL; | |
122 | //get the address format sequence from the config file | |
123 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
124 | 'mailing_format' | |
125 | ); | |
126 | ||
127 | $sequence = CRM_Utils_Address::sequence($mailingFormat); | |
128 | ||
129 | foreach ($sequence as $v) { | |
130 | $address[$v] = 1; | |
131 | } | |
132 | ||
133 | if (array_key_exists('postal_code', $address)) { | |
134 | $address['postal_code_suffix'] = 1; | |
135 | } | |
136 | ||
137 | //build the returnproperties | |
3131433e | 138 | $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1); |
6a488035 TO |
139 | $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
140 | 'mailing_format' | |
141 | ); | |
142 | ||
143 | $mailingFormatProperties = array(); | |
144 | if ($mailingFormat) { | |
145 | $mailingFormatProperties = self::getReturnProperties($mailingFormat); | |
146 | $returnProperties = array_merge($returnProperties, $mailingFormatProperties); | |
147 | } | |
148 | //we should not consider addressee for data exists, CRM-6025 | |
149 | if (array_key_exists('addressee', $mailingFormatProperties)) { | |
150 | unset($mailingFormatProperties['addressee']); | |
151 | } | |
152 | ||
153 | $customFormatProperties = array(); | |
154 | if (stristr($mailingFormat, 'custom_')) { | |
155 | foreach ($mailingFormatProperties as $token => $true) { | |
156 | if (substr($token, 0, 7) == 'custom_') { | |
a7488080 | 157 | if (empty($customFormatProperties[$token])) { |
6a488035 TO |
158 | $customFormatProperties[$token] = $mailingFormatProperties[$token]; |
159 | } | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
164 | if (!empty($customFormatProperties)) { | |
165 | $returnProperties = array_merge($returnProperties, $customFormatProperties); | |
166 | } | |
167 | ||
168 | if (isset($fv['merge_same_address'])) { | |
169 | // we need first name/last name for summarising to avoid spillage | |
170 | $returnProperties['first_name'] = 1; | |
171 | $returnProperties['last_name'] = 1; | |
172 | } | |
173 | ||
57884c26 DJ |
174 | $individualFormat = FALSE; |
175 | ||
176 | /* | |
177 | * CRM-8338: replace ids of household members with the id of their household | |
178 | * so we can merge labels by household. | |
179 | */ | |
180 | if (isset($fv['merge_same_household'])) { | |
181 | $this->mergeContactIdsByHousehold(); | |
182 | $individualFormat = TRUE; | |
183 | } | |
184 | ||
6a488035 TO |
185 | //get the contacts information |
186 | $params = array(); | |
a7488080 | 187 | if (!empty($fv['location_type_id'])) { |
b2b0530a | 188 | $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
189 | $locName = $locType[$fv['location_type_id']]; |
190 | $location = array('location' => array("{$locName}" => $address)); | |
191 | $returnProperties = array_merge($returnProperties, $location); | |
192 | $params[] = array('location_type', '=', array($fv['location_type_id'] => 1), 0, 0); | |
193 | } | |
194 | else { | |
195 | $returnProperties = array_merge($returnProperties, $address); | |
196 | } | |
197 | ||
198 | $rows = array(); | |
199 | ||
200 | foreach ($this->_contactIds as $key => $contactID) { | |
201 | $params[] = array( | |
202 | CRM_Core_Form::CB_PREFIX . $contactID, | |
203 | '=', 1, 0, 0, | |
204 | ); | |
205 | } | |
206 | ||
207 | // fix for CRM-2651 | |
a7488080 | 208 | if (!empty($fv['do_not_mail'])) { |
6a488035 TO |
209 | $params[] = array('do_not_mail', '=', 0, 0, 0); |
210 | } | |
211 | // fix for CRM-2613 | |
212 | $params[] = array('is_deceased', '=', 0, 0, 0); | |
213 | ||
214 | $custom = array(); | |
215 | foreach ($returnProperties as $name => $dontCare) { | |
216 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
217 | if ($cfID) { | |
218 | $custom[] = $cfID; | |
219 | } | |
220 | } | |
221 | ||
222 | //get the total number of contacts to fetch from database. | |
223 | $numberofContacts = count($this->_contactIds); | |
224 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); | |
225 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
226 | ||
227 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); | |
228 | ||
229 | // also get all token values | |
230 | CRM_Utils_Hook::tokenValues($details[0], | |
231 | $this->_contactIds, | |
232 | NULL, | |
233 | $messageToken, | |
234 | 'CRM_Contact_Form_Task_Label' | |
235 | ); | |
236 | ||
237 | $tokens = array(); | |
238 | CRM_Utils_Hook::tokens($tokens); | |
239 | $tokenFields = array(); | |
240 | foreach ($tokens as $category => $catTokens) { | |
241 | foreach ($catTokens as $token => $tokenName) { | |
242 | $tokenFields[] = $token; | |
243 | } | |
244 | } | |
245 | ||
246 | foreach ($this->_contactIds as $value) { | |
247 | foreach ($custom as $cfID) { | |
248 | if (isset($details[0][$value]["custom_{$cfID}"])) { | |
249 | $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[0][$value]["custom_{$cfID}"], $cfID, $details[1]); | |
250 | } | |
251 | } | |
252 | $contact = CRM_Utils_Array::value($value, $details['0']); | |
253 | ||
254 | if (is_a($contact, 'CRM_Core_Error')) { | |
255 | return NULL; | |
256 | } | |
257 | ||
258 | // we need to remove all the "_id" | |
259 | unset($contact['contact_id']); | |
260 | ||
8cc574cf | 261 | if ($locName && !empty($contact[$locName])) { |
6a488035 TO |
262 | // If location type is not primary, $contact contains |
263 | // one more array as "$contact[$locName] = array( values... )" | |
264 | ||
c91c5542 | 265 | if(!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
6a488035 TO |
266 | continue; |
267 | } | |
268 | ||
269 | unset($contact[$locName]); | |
270 | ||
a7488080 | 271 | if (!empty($contact['county_id'])) { |
6a488035 TO |
272 | unset($contact['county_id']); |
273 | } | |
274 | ||
275 | foreach ($contact as $field => $fieldValue) { | |
276 | $rows[$value][$field] = $fieldValue; | |
277 | } | |
278 | ||
279 | $valuesothers = array(); | |
280 | $paramsothers = array('contact_id' => $value); | |
281 | $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); | |
a7488080 | 282 | if (!empty($fv['location_type_id'])) { |
6a488035 TO |
283 | foreach ($valuesothers as $vals) { |
284 | if ( CRM_Utils_Array::value('location_type_id', $vals) == | |
285 | CRM_Utils_Array::value('location_type_id', $fv ) ) { | |
286 | foreach ($vals as $k => $v) { | |
287 | if (in_array($k, array( | |
288 | 'email', 'phone', 'im', 'openid'))) { | |
289 | if ($k == 'im') { | |
290 | $rows[$value][$k] = $v['1']['name']; | |
291 | } | |
292 | else { | |
293 | $rows[$value][$k] = $v['1'][$k]; | |
294 | } | |
295 | $rows[$value][$k . '_id'] = $v['1']['id']; | |
296 | } | |
297 | } | |
298 | } | |
299 | } | |
300 | } | |
301 | } | |
302 | else { | |
c91c5542 | 303 | if(!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
6a488035 TO |
304 | continue; |
305 | } | |
306 | ||
a7488080 | 307 | if (!empty($contact['addressee_display'])) { |
6a488035 TO |
308 | $contact['addressee_display'] = trim($contact['addressee_display']); |
309 | } | |
a7488080 | 310 | if (!empty($contact['addressee'])) { |
6a488035 TO |
311 | $contact['addressee'] = $contact['addressee_display']; |
312 | } | |
313 | ||
314 | // now create the rows for generating mailing labels | |
315 | foreach ($contact as $field => $fieldValue) { | |
316 | $rows[$value][$field] = $fieldValue; | |
317 | } | |
318 | } | |
319 | } | |
320 | ||
6a488035 TO |
321 | if (isset($fv['merge_same_address'])) { |
322 | $this->mergeSameAddress($rows); | |
323 | $individualFormat = TRUE; | |
324 | } | |
6a488035 TO |
325 | |
326 | // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327) | |
327 | foreach ($rows as $id => $row) { | |
328 | if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) { | |
329 | $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods)); | |
e7e657f0 | 330 | $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); |
6a488035 TO |
331 | $temp = array(); |
332 | foreach ($val as $vals) { | |
333 | $temp[] = $comm[$vals]; | |
334 | } | |
335 | $row['preferred_communication_method'] = implode(', ', $temp); | |
336 | } | |
337 | $row['id'] = $id; | |
338 | $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields); | |
339 | ||
340 | // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it. | |
341 | // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366 | |
342 | // Due to FriBidi peculiarities, this can't be called on | |
343 | // a multi-line string, hence the explode+implode approach. | |
344 | if (function_exists('fribidi_log2vis')) { | |
345 | $lines = explode("\n", $formatted); | |
346 | foreach ($lines as $i => $line) { | |
347 | $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO, FRIBIDI_CHARSET_UTF8); | |
348 | } | |
349 | $formatted = implode("\n", $lines); | |
350 | } | |
351 | $rows[$id] = array($formatted); | |
352 | } | |
353 | ||
354 | //call function to create labels | |
355 | self::createLabel($rows, $fv['label_name']); | |
356 | CRM_Utils_System::civiExit(1); | |
357 | } | |
358 | ||
c91c5542 | 359 | /** |
360 | * Check for presence of tokens to be swapped out | |
77b97be7 | 361 | * |
c91c5542 | 362 | * @param array $contact |
363 | * @param array $mailingFormatProperties | |
364 | * @param array $tokenFields | |
77b97be7 EM |
365 | * |
366 | * @return bool | |
c91c5542 | 367 | */ |
368 | function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { | |
369 | foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { | |
a7488080 | 370 | if (!empty($contact[$key])) { |
c91c5542 | 371 | return TRUE; |
372 | } | |
373 | } | |
374 | return FALSE; | |
375 | } | |
6a488035 TO |
376 | /** |
377 | * function to create labels (pdf) | |
378 | * | |
379 | * @param array $contactRows assciated array of contact data | |
380 | * @param string $format format in which labels needs to be printed | |
381 | * @param string $fileName The name of the file to save the label in | |
382 | * | |
383 | * @return null | |
384 | * @access public | |
385 | */ | |
386 | function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { | |
387 | $pdf = new CRM_Utils_PDF_Label($format, 'mm'); | |
388 | $pdf->Open(); | |
389 | $pdf->AddPage(); | |
390 | ||
391 | //build contact string that needs to be printed | |
392 | $val = NULL; | |
393 | foreach ($contactRows as $row => $value) { | |
394 | foreach ($value as $k => $v) { | |
395 | $val .= "$v\n"; | |
396 | } | |
397 | ||
398 | $pdf->AddPdfLabel($val); | |
399 | $val = ''; | |
400 | } | |
401 | $pdf->Output($fileName, 'D'); | |
402 | } | |
403 | ||
404 | /** | |
405 | * function to create the array of returnProperties | |
406 | * | |
407 | * @param string $format format for which return properties build | |
408 | * | |
409 | * @return array of returnProperties | |
410 | * @access public | |
411 | */ | |
412 | function getReturnProperties(&$format) { | |
413 | $returnProperties = array(); | |
414 | $matches = array(); | |
415 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
416 | $format, | |
417 | $matches, | |
418 | PREG_PATTERN_ORDER | |
419 | ); | |
420 | if ($matches[1]) { | |
421 | foreach ($matches[1] as $token) { | |
422 | list($type, $name) = preg_split('/\./', $token, 2); | |
423 | if ($name) { | |
424 | $returnProperties["{$name}"] = 1; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | return $returnProperties; | |
430 | } | |
431 | ||
86538308 EM |
432 | /** |
433 | * @param $rows | |
434 | */ | |
6a488035 TO |
435 | function mergeSameAddress(&$rows) { |
436 | $uniqueAddress = array(); | |
437 | foreach (array_keys($rows) as $rowID) { | |
438 | // load complete address as array key | |
439 | $address = | |
440 | trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); | |
441 | if (isset($rows[$rowID]['last_name'])) { | |
442 | $name = $rows[$rowID]['last_name']; | |
443 | } | |
444 | else { | |
445 | $name = $rows[$rowID]['display_name']; | |
446 | } | |
3131433e | 447 | |
448 | // CRM-15120 | |
449 | $formatted = array( | |
450 | 'first_name' => $rows[$rowID]['first_name'], | |
451 | 'individual_prefix' => $rows[$rowID]['individual_prefix'] | |
452 | ); | |
a1d109d3 | 453 | $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'display_name_format'); |
454 | $firstNameWithPrefix = CRM_Utils_Address::format($formatted, $format, FALSE, FALSE, TRUE); | |
3131433e | 455 | $firstNameWithPrefix = trim($firstNameWithPrefix); |
456 | ||
6a488035 TO |
457 | // fill uniqueAddress array with last/first name tree |
458 | if (isset($uniqueAddress[$address])) { | |
3131433e | 459 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; |
460 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
6a488035 TO |
461 | // drop unnecessary rows |
462 | unset($rows[$rowID]); | |
463 | // this is the first listing at this address | |
464 | } | |
465 | else { | |
466 | $uniqueAddress[$address]['ID'] = $rowID; | |
3131433e | 467 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; |
468 | $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
6a488035 TO |
469 | } |
470 | } | |
471 | foreach ($uniqueAddress as $address => $data) { | |
472 | // copy data back to $rows | |
473 | $count = 0; | |
474 | // one last name list per row | |
475 | foreach ($data['names'] as $last_name => $first_names) { | |
476 | // too many to list | |
477 | if ($count > 2) { | |
478 | break; | |
479 | } | |
1149b927 JL |
480 | if(count($first_names) == 1){ |
481 | $family = $first_names[current(array_keys($first_names))]['addressee_display']; | |
482 | } | |
483 | else { | |
484 | // collapse the tree to summarize | |
485 | $family = trim(implode(" & ", array_keys($first_names)) . " " . $last_name); | |
486 | } | |
487 | if ($count) { | |
488 | $processedNames .= "\n" . $family; | |
489 | } | |
490 | else { | |
491 | // build display_name string | |
492 | $processedNames = $family; | |
493 | } | |
6a488035 TO |
494 | $count++; |
495 | } | |
496 | $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; | |
497 | } | |
498 | } | |
6a488035 TO |
499 | } |
500 |