3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * This class helps to print the labels for contacts
40 class CRM_Contact_Form_Task_Label
extends CRM_Contact_Form_Task
{
43 * Build all the data structures needed to build the form.
47 public function preProcess() {
48 $this->set('contactIds', $this->_contactIds
);
53 * Build the form object.
58 public function buildQuickForm() {
59 CRM_Utils_System
::setTitle(ts('Make Mailing Labels'));
61 //add select for label
62 $label = CRM_Core_BAO_LabelFormat
::getList(TRUE);
64 $this->add('select', 'label_name', ts('Select Label'), array('' => ts('- select label -')) +
$label, TRUE);
66 // add select for Location Type
67 $this->addElement('select', 'location_type_id', ts('Select Location'),
70 ) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE
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'));
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);
79 $this->addButtons(array(
82 'name' => ts('Make Mailing Labels'),
93 * Set default values for the form.
96 * array of default values
98 public function setDefaultValues() {
100 $format = CRM_Core_BAO_LabelFormat
::getDefaultValues();
101 $defaults['label_name'] = CRM_Utils_Array
::value('name', $format);
102 $defaults['do_not_mail'] = 1;
108 * Process the form after the input has been submitted and validated.
113 public function postProcess() {
114 $fv = $this->controller
->exportValues($this->_name
);
115 $config = CRM_Core_Config
::singleton();
117 //get the address format sequence from the config file
118 $mailingFormat = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
122 $sequence = CRM_Utils_Address
::sequence($mailingFormat);
124 foreach ($sequence as $v) {
128 if (array_key_exists('postal_code', $address)) {
129 $address['postal_code_suffix'] = 1;
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
,
138 $mailingFormatProperties = array();
139 if ($mailingFormat) {
140 $mailingFormatProperties = CRM_Utils_Token
::getReturnProperties($mailingFormat);
141 $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
143 //we should not consider addressee for data exists, CRM-6025
144 if (array_key_exists('addressee', $mailingFormatProperties)) {
145 unset($mailingFormatProperties['addressee']);
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];
159 if (!empty($customFormatProperties)) {
160 $returnProperties = array_merge($returnProperties, $customFormatProperties);
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;
169 $individualFormat = FALSE;
172 * CRM-8338: replace ids of household members with the id of their household
173 * so we can merge labels by household.
175 if (isset($fv['merge_same_household'])) {
176 $this->mergeContactIdsByHousehold();
177 $individualFormat = TRUE;
180 //get the contacts information
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);
190 $returnProperties = array_merge($returnProperties, $address);
195 foreach ($this->_contactIds
as $key => $contactID) {
197 CRM_Core_Form
::CB_PREFIX
. $contactID,
206 if (!empty($fv['do_not_mail'])) {
207 $params[] = array('do_not_mail', '=', 0, 0, 0);
210 $params[] = array('is_deceased', '=', 0, 0, 0);
213 foreach ($returnProperties as $name => $dontCare) {
214 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
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);
225 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
227 // also get all token values
228 CRM_Utils_Hook
::tokenValues($details[0],
232 'CRM_Contact_Form_Task_Label'
236 CRM_Utils_Hook
::tokens($tokens);
237 $tokenFields = array();
238 foreach ($tokens as $category => $catTokens) {
239 foreach ($catTokens as $token => $tokenName) {
240 $tokenFields[] = $token;
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]);
250 $contact = CRM_Utils_Array
::value($value, $details['0']);
252 if (is_a($contact, 'CRM_Core_Error')) {
256 // we need to remove all the "_id"
257 unset($contact['contact_id']);
259 if ($locName && !empty($contact[$locName])) {
260 // If location type is not primary, $contact contains
261 // one more array as "$contact[$locName] = array( values... )"
263 if (!self
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
267 $contact = array_merge($contact, $contact[$locName]);
268 unset($contact[$locName]);
270 if (!empty($contact['county_id'])) {
271 unset($contact['county_id']);
274 foreach ($contact as $field => $fieldValue) {
275 $rows[$value][$field] = $fieldValue;
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)
286 foreach ($vals as $k => $v) {
287 if (in_array($k, array(
294 $rows[$value][$k] = $v['1']['name'];
297 $rows[$value][$k] = $v['1'][$k];
299 $rows[$value][$k . '_id'] = $v['1']['id'];
307 if (!self
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
311 if (!empty($contact['addressee_display'])) {
312 $contact['addressee_display'] = trim($contact['addressee_display']);
314 if (!empty($contact['addressee'])) {
315 $contact['addressee'] = $contact['addressee_display'];
318 // now create the rows for generating mailing labels
319 foreach ($contact as $field => $fieldValue) {
320 $rows[$value][$field] = $fieldValue;
325 if (isset($fv['merge_same_address'])) {
326 CRM_Core_BAO_Address
::mergeSameAddress($rows);
327 $individualFormat = TRUE;
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');
336 foreach ($val as $vals) {
337 $temp[] = $comm[$vals];
339 $row['preferred_communication_method'] = implode(', ', $temp);
342 $formatted = CRM_Utils_Address
::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields);
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
);
353 $formatted = implode("\n", $lines);
355 $rows[$id] = array($formatted);
358 //call function to create labels
359 self
::createLabel($rows, $fv['label_name']);
360 CRM_Utils_System
::civiExit(1);
364 * Check for presence of tokens to be swapped out.
366 * @param array $contact
367 * @param array $mailingFormatProperties
368 * @param array $tokenFields
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])) {
383 * Create labels (pdf)
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.
392 public function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
393 $pdf = new CRM_Utils_PDF_Label($format, 'mm');
397 //build contact string that needs to be printed
399 foreach ($contactRows as $row => $value) {
400 foreach ($value as $k => $v) {
404 $pdf->AddPdfLabel($val);
407 $pdf->Output($fileName, 'D');