Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
00252851 | 19 | * This class helps to print the labels for contacts. |
6a488035 TO |
20 | */ |
21 | class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Build all the data structures needed to build the form. |
6a488035 | 25 | */ |
00be9182 | 26 | public function preProcess() { |
6a488035 TO |
27 | $this->set('contactIds', $this->_contactIds); |
28 | parent::preProcess(); | |
29 | } | |
30 | ||
31 | /** | |
fe482240 | 32 | * Build the form object. |
6a488035 | 33 | */ |
00be9182 | 34 | public function buildQuickForm() { |
14396107 | 35 | self::buildLabelForm($this); |
36 | } | |
37 | ||
38 | /** | |
39 | * Common Function to build Mailing Label Form. | |
bf48aa29 | 40 | * |
41 | * @param CRM_Core_Form $form | |
14396107 | 42 | */ |
43 | public static function buildLabelForm($form) { | |
6a488035 TO |
44 | CRM_Utils_System::setTitle(ts('Make Mailing Labels')); |
45 | ||
46 | //add select for label | |
47 | $label = CRM_Core_BAO_LabelFormat::getList(TRUE); | |
48 | ||
be2fb01f | 49 | $form->add('select', 'label_name', ts('Select Label'), ['' => ts('- select label -')] + $label, TRUE); |
6a488035 | 50 | |
6a488035 | 51 | // add select for Location Type |
14396107 | 52 | $form->addElement('select', 'location_type_id', ts('Select Location'), |
be2fb01f | 53 | [ |
af9b09df | 54 | '' => ts('Primary'), |
be2fb01f | 55 | ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE |
6a488035 TO |
56 | ); |
57 | ||
58 | // checkbox for SKIP contacts with Do Not Mail privacy option | |
14396107 | 59 | $form->addElement('checkbox', 'do_not_mail', ts('Do not print labels for contacts with "Do Not Mail" privacy option checked')); |
6a488035 | 60 | |
14396107 | 61 | $form->add('checkbox', 'merge_same_address', ts('Merge labels for contacts with the same address'), NULL); |
62 | $form->add('checkbox', 'merge_same_household', ts('Merge labels for contacts belonging to the same household'), NULL); | |
6a488035 | 63 | |
be2fb01f CW |
64 | $form->addButtons([ |
65 | [ | |
85d25c8f DG |
66 | 'type' => 'submit', |
67 | 'name' => ts('Make Mailing Labels'), | |
68 | 'isDefault' => TRUE, | |
be2fb01f CW |
69 | ], |
70 | [ | |
85d25c8f DG |
71 | 'type' => 'cancel', |
72 | 'name' => ts('Done'), | |
be2fb01f CW |
73 | ], |
74 | ]); | |
6a488035 TO |
75 | } |
76 | ||
77 | /** | |
c490a46a | 78 | * Set default values for the form. |
6a488035 | 79 | * |
a6c01b45 CW |
80 | * @return array |
81 | * array of default values | |
6a488035 | 82 | */ |
00be9182 | 83 | public function setDefaultValues() { |
be2fb01f | 84 | $defaults = []; |
6a488035 TO |
85 | $format = CRM_Core_BAO_LabelFormat::getDefaultValues(); |
86 | $defaults['label_name'] = CRM_Utils_Array::value('name', $format); | |
87 | $defaults['do_not_mail'] = 1; | |
88 | ||
89 | return $defaults; | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Process the form after the input has been submitted and validated. |
2582af2a MD |
94 | * |
95 | * @param array|NULL $params | |
6a488035 | 96 | */ |
2582af2a MD |
97 | public function postProcess($params = NULL) { |
98 | $fv = $params ?: $this->controller->exportValues($this->_name); | |
353ffa53 | 99 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
100 | $locName = NULL; |
101 | //get the address format sequence from the config file | |
aaffa79f | 102 | $mailingFormat = Civi::settings()->get('mailing_format'); |
6a488035 TO |
103 | |
104 | $sequence = CRM_Utils_Address::sequence($mailingFormat); | |
105 | ||
106 | foreach ($sequence as $v) { | |
107 | $address[$v] = 1; | |
108 | } | |
109 | ||
110 | if (array_key_exists('postal_code', $address)) { | |
111 | $address['postal_code_suffix'] = 1; | |
112 | } | |
113 | ||
114 | //build the returnproperties | |
be2fb01f | 115 | $returnProperties = ['display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1]; |
aaffa79f | 116 | $mailingFormat = Civi::settings()->get('mailing_format'); |
6a488035 | 117 | |
be2fb01f | 118 | $mailingFormatProperties = []; |
6a488035 | 119 | if ($mailingFormat) { |
bdd49e38 | 120 | $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat); |
6a488035 TO |
121 | $returnProperties = array_merge($returnProperties, $mailingFormatProperties); |
122 | } | |
123 | //we should not consider addressee for data exists, CRM-6025 | |
124 | if (array_key_exists('addressee', $mailingFormatProperties)) { | |
125 | unset($mailingFormatProperties['addressee']); | |
126 | } | |
127 | ||
be2fb01f | 128 | $customFormatProperties = []; |
6a488035 TO |
129 | if (stristr($mailingFormat, 'custom_')) { |
130 | foreach ($mailingFormatProperties as $token => $true) { | |
131 | if (substr($token, 0, 7) == 'custom_') { | |
a7488080 | 132 | if (empty($customFormatProperties[$token])) { |
6a488035 TO |
133 | $customFormatProperties[$token] = $mailingFormatProperties[$token]; |
134 | } | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | if (!empty($customFormatProperties)) { | |
140 | $returnProperties = array_merge($returnProperties, $customFormatProperties); | |
141 | } | |
142 | ||
143 | if (isset($fv['merge_same_address'])) { | |
144 | // we need first name/last name for summarising to avoid spillage | |
145 | $returnProperties['first_name'] = 1; | |
146 | $returnProperties['last_name'] = 1; | |
147 | } | |
148 | ||
57884c26 DJ |
149 | $individualFormat = FALSE; |
150 | ||
151 | /* | |
152 | * CRM-8338: replace ids of household members with the id of their household | |
153 | * so we can merge labels by household. | |
154 | */ | |
155 | if (isset($fv['merge_same_household'])) { | |
156 | $this->mergeContactIdsByHousehold(); | |
157 | $individualFormat = TRUE; | |
158 | } | |
159 | ||
6a488035 | 160 | //get the contacts information |
be2fb01f | 161 | $params = []; |
a7488080 | 162 | if (!empty($fv['location_type_id'])) { |
353ffa53 TO |
163 | $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
164 | $locName = $locType[$fv['location_type_id']]; | |
be2fb01f | 165 | $location = ['location' => ["{$locName}" => $address]]; |
6a488035 | 166 | $returnProperties = array_merge($returnProperties, $location); |
be2fb01f | 167 | $params[] = ['location_type', '=', [1 => $fv['location_type_id']], 0, 0]; |
d068e84b | 168 | $primaryLocationOnly = FALSE; |
6a488035 TO |
169 | } |
170 | else { | |
171 | $returnProperties = array_merge($returnProperties, $address); | |
d068e84b | 172 | $primaryLocationOnly = TRUE; |
6a488035 TO |
173 | } |
174 | ||
be2fb01f | 175 | $rows = []; |
6a488035 | 176 | foreach ($this->_contactIds as $key => $contactID) { |
be2fb01f | 177 | $params[] = [ |
6a488035 | 178 | CRM_Core_Form::CB_PREFIX . $contactID, |
353ffa53 TO |
179 | '=', |
180 | 1, | |
181 | 0, | |
182 | 0, | |
be2fb01f | 183 | ]; |
6a488035 TO |
184 | } |
185 | ||
186 | // fix for CRM-2651 | |
a7488080 | 187 | if (!empty($fv['do_not_mail'])) { |
be2fb01f | 188 | $params[] = ['do_not_mail', '=', 0, 0, 0]; |
6a488035 TO |
189 | } |
190 | // fix for CRM-2613 | |
be2fb01f | 191 | $params[] = ['is_deceased', '=', 0, 0, 0]; |
6a488035 | 192 | |
be2fb01f | 193 | $custom = []; |
6a488035 TO |
194 | foreach ($returnProperties as $name => $dontCare) { |
195 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
196 | if ($cfID) { | |
197 | $custom[] = $cfID; | |
198 | } | |
199 | } | |
200 | ||
201 | //get the total number of contacts to fetch from database. | |
202 | $numberofContacts = count($this->_contactIds); | |
353ffa53 | 203 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); |
d068e84b | 204 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts, TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, $primaryLocationOnly); |
6a488035 TO |
205 | $messageToken = CRM_Utils_Token::getTokens($mailingFormat); |
206 | ||
207 | // also get all token values | |
208 | CRM_Utils_Hook::tokenValues($details[0], | |
209 | $this->_contactIds, | |
210 | NULL, | |
211 | $messageToken, | |
212 | 'CRM_Contact_Form_Task_Label' | |
213 | ); | |
214 | ||
be2fb01f | 215 | $tokens = []; |
6a488035 | 216 | CRM_Utils_Hook::tokens($tokens); |
be2fb01f | 217 | $tokenFields = []; |
6a488035 TO |
218 | foreach ($tokens as $category => $catTokens) { |
219 | foreach ($catTokens as $token => $tokenName) { | |
220 | $tokenFields[] = $token; | |
221 | } | |
222 | } | |
223 | ||
224 | foreach ($this->_contactIds as $value) { | |
225 | foreach ($custom as $cfID) { | |
226 | if (isset($details[0][$value]["custom_{$cfID}"])) { | |
8cee0c70 | 227 | $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[0][$value]["custom_{$cfID}"], $cfID); |
6a488035 TO |
228 | } |
229 | } | |
230 | $contact = CRM_Utils_Array::value($value, $details['0']); | |
231 | ||
232 | if (is_a($contact, 'CRM_Core_Error')) { | |
233 | return NULL; | |
234 | } | |
235 | ||
236 | // we need to remove all the "_id" | |
237 | unset($contact['contact_id']); | |
238 | ||
8cc574cf | 239 | if ($locName && !empty($contact[$locName])) { |
6a488035 TO |
240 | // If location type is not primary, $contact contains |
241 | // one more array as "$contact[$locName] = array( values... )" | |
242 | ||
8bdfc216 | 243 | if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
6a488035 TO |
244 | continue; |
245 | } | |
246 | ||
ceebbde9 | 247 | $contact = array_merge($contact, $contact[$locName]); |
6a488035 TO |
248 | unset($contact[$locName]); |
249 | ||
a7488080 | 250 | if (!empty($contact['county_id'])) { |
6a488035 TO |
251 | unset($contact['county_id']); |
252 | } | |
253 | ||
254 | foreach ($contact as $field => $fieldValue) { | |
255 | $rows[$value][$field] = $fieldValue; | |
256 | } | |
257 | ||
be2fb01f CW |
258 | $valuesothers = []; |
259 | $paramsothers = ['contact_id' => $value]; | |
6a488035 | 260 | $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); |
a7488080 | 261 | if (!empty($fv['location_type_id'])) { |
6a488035 | 262 | foreach ($valuesothers as $vals) { |
353ffa53 TO |
263 | if (CRM_Utils_Array::value('location_type_id', $vals) == |
264 | CRM_Utils_Array::value('location_type_id', $fv) | |
265 | ) { | |
6a488035 | 266 | foreach ($vals as $k => $v) { |
be2fb01f | 267 | if (in_array($k, [ |
353ffa53 TO |
268 | 'email', |
269 | 'phone', | |
270 | 'im', | |
af9b09df | 271 | 'openid', |
be2fb01f | 272 | ])) { |
6a488035 TO |
273 | if ($k == 'im') { |
274 | $rows[$value][$k] = $v['1']['name']; | |
275 | } | |
276 | else { | |
277 | $rows[$value][$k] = $v['1'][$k]; | |
278 | } | |
279 | $rows[$value][$k . '_id'] = $v['1']['id']; | |
280 | } | |
281 | } | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | else { | |
8bdfc216 | 287 | if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
6a488035 TO |
288 | continue; |
289 | } | |
290 | ||
a7488080 | 291 | if (!empty($contact['addressee_display'])) { |
6a488035 TO |
292 | $contact['addressee_display'] = trim($contact['addressee_display']); |
293 | } | |
a7488080 | 294 | if (!empty($contact['addressee'])) { |
6a488035 TO |
295 | $contact['addressee'] = $contact['addressee_display']; |
296 | } | |
297 | ||
298 | // now create the rows for generating mailing labels | |
299 | foreach ($contact as $field => $fieldValue) { | |
300 | $rows[$value][$field] = $fieldValue; | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
6a488035 | 305 | if (isset($fv['merge_same_address'])) { |
8bdfc216 | 306 | CRM_Core_BAO_Address::mergeSameAddress($rows); |
6a488035 TO |
307 | $individualFormat = TRUE; |
308 | } | |
6a488035 TO |
309 | |
310 | // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327) | |
311 | foreach ($rows as $id => $row) { | |
312 | if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) { | |
353ffa53 | 313 | $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods)); |
e7e657f0 | 314 | $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); |
be2fb01f | 315 | $temp = []; |
6a488035 TO |
316 | foreach ($val as $vals) { |
317 | $temp[] = $comm[$vals]; | |
318 | } | |
319 | $row['preferred_communication_method'] = implode(', ', $temp); | |
320 | } | |
321 | $row['id'] = $id; | |
4c49535e | 322 | $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $tokenFields); |
6a488035 TO |
323 | |
324 | // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it. | |
325 | // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366 | |
326 | // Due to FriBidi peculiarities, this can't be called on | |
327 | // a multi-line string, hence the explode+implode approach. | |
328 | if (function_exists('fribidi_log2vis')) { | |
329 | $lines = explode("\n", $formatted); | |
330 | foreach ($lines as $i => $line) { | |
331 | $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO, FRIBIDI_CHARSET_UTF8); | |
332 | } | |
333 | $formatted = implode("\n", $lines); | |
334 | } | |
be2fb01f | 335 | $rows[$id] = [$formatted]; |
6a488035 TO |
336 | } |
337 | ||
2582af2a MD |
338 | if (!empty($fv['is_unit_testing'])) { |
339 | return $rows; | |
340 | } | |
341 | ||
6a488035 TO |
342 | //call function to create labels |
343 | self::createLabel($rows, $fv['label_name']); | |
292c8687 | 344 | CRM_Utils_System::civiExit(); |
6a488035 TO |
345 | } |
346 | ||
c91c5542 | 347 | /** |
fe482240 | 348 | * Check for presence of tokens to be swapped out. |
77b97be7 | 349 | * |
c91c5542 | 350 | * @param array $contact |
351 | * @param array $mailingFormatProperties | |
352 | * @param array $tokenFields | |
77b97be7 EM |
353 | * |
354 | * @return bool | |
c91c5542 | 355 | */ |
8bdfc216 | 356 | public static function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { |
c91c5542 | 357 | foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { |
8bdfc216 | 358 | //we should not consider addressee for data exists, CRM-6025 |
353ffa53 | 359 | if ($key != 'addressee' && !empty($contact[$key])) { |
c91c5542 | 360 | return TRUE; |
361 | } | |
362 | } | |
363 | return FALSE; | |
364 | } | |
353ffa53 | 365 | |
6a488035 | 366 | /** |
00252851 | 367 | * Create labels (pdf). |
6a488035 | 368 | * |
77c5b619 | 369 | * @param array $contactRows |
00252851 | 370 | * Associated array of contact data. |
77c5b619 TO |
371 | * @param string $format |
372 | * Format in which labels needs to be printed. | |
373 | * @param string $fileName | |
374 | * The name of the file to save the label in. | |
6a488035 | 375 | */ |
00be9182 | 376 | public function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { |
6a488035 TO |
377 | $pdf = new CRM_Utils_PDF_Label($format, 'mm'); |
378 | $pdf->Open(); | |
379 | $pdf->AddPage(); | |
380 | ||
381 | //build contact string that needs to be printed | |
382 | $val = NULL; | |
383 | foreach ($contactRows as $row => $value) { | |
384 | foreach ($value as $k => $v) { | |
385 | $val .= "$v\n"; | |
386 | } | |
387 | ||
388 | $pdf->AddPdfLabel($val); | |
389 | $val = ''; | |
390 | } | |
391 | $pdf->Output($fileName, 'D'); | |
392 | } | |
393 | ||
6a488035 | 394 | } |