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 | |
138 | $returnProperties = array('display_name' => 1, 'contact_type' => 1); | |
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 | |
361 | * @param array $contact | |
362 | * @param array $mailingFormatProperties | |
363 | * @param array $tokenFields | |
364 | */ | |
365 | function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { | |
366 | foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { | |
a7488080 | 367 | if (!empty($contact[$key])) { |
c91c5542 | 368 | return TRUE; |
369 | } | |
370 | } | |
371 | return FALSE; | |
372 | } | |
6a488035 TO |
373 | /** |
374 | * function to create labels (pdf) | |
375 | * | |
376 | * @param array $contactRows assciated array of contact data | |
377 | * @param string $format format in which labels needs to be printed | |
378 | * @param string $fileName The name of the file to save the label in | |
379 | * | |
380 | * @return null | |
381 | * @access public | |
382 | */ | |
383 | function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { | |
384 | $pdf = new CRM_Utils_PDF_Label($format, 'mm'); | |
385 | $pdf->Open(); | |
386 | $pdf->AddPage(); | |
387 | ||
388 | //build contact string that needs to be printed | |
389 | $val = NULL; | |
390 | foreach ($contactRows as $row => $value) { | |
391 | foreach ($value as $k => $v) { | |
392 | $val .= "$v\n"; | |
393 | } | |
394 | ||
395 | $pdf->AddPdfLabel($val); | |
396 | $val = ''; | |
397 | } | |
398 | $pdf->Output($fileName, 'D'); | |
399 | } | |
400 | ||
401 | /** | |
402 | * function to create the array of returnProperties | |
403 | * | |
404 | * @param string $format format for which return properties build | |
405 | * | |
406 | * @return array of returnProperties | |
407 | * @access public | |
408 | */ | |
409 | function getReturnProperties(&$format) { | |
410 | $returnProperties = array(); | |
411 | $matches = array(); | |
412 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
413 | $format, | |
414 | $matches, | |
415 | PREG_PATTERN_ORDER | |
416 | ); | |
417 | if ($matches[1]) { | |
418 | foreach ($matches[1] as $token) { | |
419 | list($type, $name) = preg_split('/\./', $token, 2); | |
420 | if ($name) { | |
421 | $returnProperties["{$name}"] = 1; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | return $returnProperties; | |
427 | } | |
428 | ||
429 | function mergeSameAddress(&$rows) { | |
430 | $uniqueAddress = array(); | |
431 | foreach (array_keys($rows) as $rowID) { | |
432 | // load complete address as array key | |
433 | $address = | |
434 | trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); | |
435 | if (isset($rows[$rowID]['last_name'])) { | |
436 | $name = $rows[$rowID]['last_name']; | |
437 | } | |
438 | else { | |
439 | $name = $rows[$rowID]['display_name']; | |
440 | } | |
441 | // fill uniqueAddress array with last/first name tree | |
442 | if (isset($uniqueAddress[$address])) { | |
1149b927 JL |
443 | $uniqueAddress[$address]['names'][$name][$rows[$rowID]['first_name']]['first_name'] = $rows[$rowID]['first_name']; |
444 | $uniqueAddress[$address]['names'][$name][$rows[$rowID]['first_name']]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
6a488035 TO |
445 | // drop unnecessary rows |
446 | unset($rows[$rowID]); | |
447 | // this is the first listing at this address | |
448 | } | |
449 | else { | |
450 | $uniqueAddress[$address]['ID'] = $rowID; | |
1149b927 JL |
451 | $uniqueAddress[$address]['names'][$name][$rows[$rowID]['first_name']]['first_name'] = $rows[$rowID]['first_name']; |
452 | $uniqueAddress[$address]['names'][$name][$rows[$rowID]['first_name']]['addressee_display'] = $rows[$rowID]['addressee_display']; | |
6a488035 TO |
453 | } |
454 | } | |
455 | foreach ($uniqueAddress as $address => $data) { | |
456 | // copy data back to $rows | |
457 | $count = 0; | |
458 | // one last name list per row | |
459 | foreach ($data['names'] as $last_name => $first_names) { | |
460 | // too many to list | |
461 | if ($count > 2) { | |
462 | break; | |
463 | } | |
1149b927 JL |
464 | if(count($first_names) == 1){ |
465 | $family = $first_names[current(array_keys($first_names))]['addressee_display']; | |
466 | } | |
467 | else { | |
468 | // collapse the tree to summarize | |
469 | $family = trim(implode(" & ", array_keys($first_names)) . " " . $last_name); | |
470 | } | |
471 | if ($count) { | |
472 | $processedNames .= "\n" . $family; | |
473 | } | |
474 | else { | |
475 | // build display_name string | |
476 | $processedNames = $family; | |
477 | } | |
6a488035 TO |
478 | $count++; |
479 | } | |
480 | $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; | |
481 | } | |
482 | } | |
6a488035 TO |
483 | } |
484 |