commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Form / Task / Label.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
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 */
47 public function preProcess() {
48 $this->set('contactIds', $this->_contactIds);
49 parent::preProcess();
50 }
51
52 /**
53 * Build the form object.
54 *
55 *
56 * @return void
57 */
58 public function buildQuickForm() {
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
66 // add select for Location Type
67 $this->addElement('select', 'location_type_id', ts('Select Location'),
68 array(
69 '' => ts('Primary'),
70 ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE
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
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 ));
90 }
91
92 /**
93 * Set default values for the form.
94 *
95 * @return array
96 * array of default values
97 */
98 public function setDefaultValues() {
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 /**
108 * Process the form after the input has been submitted and validated.
109 *
110 *
111 * @return void
112 */
113 public function postProcess() {
114 $fv = $this->controller->exportValues($this->_name);
115 $config = CRM_Core_Config::singleton();
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
133 $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
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) {
140 $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
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_') {
152 if (empty($customFormatProperties[$token])) {
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
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
180 //get the contacts information
181 $params = array();
182 if (!empty($fv['location_type_id'])) {
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));
186 $returnProperties = array_merge($returnProperties, $location);
187 $params[] = array('location_type', '=', array($fv['location_type_id'] => 1), 0, 0);
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,
198 '=',
199 1,
200 0,
201 0,
202 );
203 }
204
205 // fix for CRM-2651
206 if (!empty($fv['do_not_mail'])) {
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);
222 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
223 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
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
259 if ($locName && !empty($contact[$locName])) {
260 // If location type is not primary, $contact contains
261 // one more array as "$contact[$locName] = array( values... )"
262
263 if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
264 continue;
265 }
266
267 $contact = array_merge($contact, $contact[$locName]);
268 unset($contact[$locName]);
269
270 if (!empty($contact['county_id'])) {
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);
281 if (!empty($fv['location_type_id'])) {
282 foreach ($valuesothers as $vals) {
283 if (CRM_Utils_Array::value('location_type_id', $vals) ==
284 CRM_Utils_Array::value('location_type_id', $fv)
285 ) {
286 foreach ($vals as $k => $v) {
287 if (in_array($k, array(
288 'email',
289 'phone',
290 'im',
291 'openid',
292 ))) {
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 {
307 if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
308 continue;
309 }
310
311 if (!empty($contact['addressee_display'])) {
312 $contact['addressee_display'] = trim($contact['addressee_display']);
313 }
314 if (!empty($contact['addressee'])) {
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
325 if (isset($fv['merge_same_address'])) {
326 CRM_Core_BAO_Address::mergeSameAddress($rows);
327 $individualFormat = TRUE;
328 }
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)) {
333 $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods));
334 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
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
363 /**
364 * Check for presence of tokens to be swapped out.
365 *
366 * @param array $contact
367 * @param array $mailingFormatProperties
368 * @param array $tokenFields
369 *
370 * @return bool
371 */
372 public static function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) {
373 foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) {
374 //we should not consider addressee for data exists, CRM-6025
375 if ($key != 'addressee' && !empty($contact[$key])) {
376 return TRUE;
377 }
378 }
379 return FALSE;
380 }
381
382 /**
383 * Create labels (pdf)
384 *
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.
391 */
392 public function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
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
410 }