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