3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * This class helps to print the labels for contacts.
37 class CRM_Contact_Form_Task_Label
extends CRM_Contact_Form_Task
{
40 * Build all the data structures needed to build the form.
42 public function preProcess() {
43 $this->set('contactIds', $this->_contactIds
);
48 * Build the form object.
50 public function buildQuickForm() {
51 CRM_Utils_System
::setTitle(ts('Make Mailing Labels'));
53 //add select for label
54 $label = CRM_Core_BAO_LabelFormat
::getList(TRUE);
56 $this->add('select', 'label_name', ts('Select Label'), array('' => ts('- select label -')) +
$label, TRUE);
58 // add select for Location Type
59 $this->addElement('select', 'location_type_id', ts('Select Location'),
62 ) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE
65 // checkbox for SKIP contacts with Do Not Mail privacy option
66 $this->addElement('checkbox', 'do_not_mail', ts('Do not print labels for contacts with "Do Not Mail" privacy option checked'));
68 $this->add('checkbox', 'merge_same_address', ts('Merge labels for contacts with the same address'), NULL);
69 $this->add('checkbox', 'merge_same_household', ts('Merge labels for contacts belonging to the same household'), NULL);
71 $this->addButtons(array(
74 'name' => ts('Make Mailing Labels'),
85 * Set default values for the form.
88 * array of default values
90 public function setDefaultValues() {
92 $format = CRM_Core_BAO_LabelFormat
::getDefaultValues();
93 $defaults['label_name'] = CRM_Utils_Array
::value('name', $format);
94 $defaults['do_not_mail'] = 1;
100 * Process the form after the input has been submitted and validated.
102 public function postProcess() {
103 $fv = $this->controller
->exportValues($this->_name
);
104 $config = CRM_Core_Config
::singleton();
106 //get the address format sequence from the config file
107 $mailingFormat = Civi
::settings()->get('mailing_format');
109 $sequence = CRM_Utils_Address
::sequence($mailingFormat);
111 foreach ($sequence as $v) {
115 if (array_key_exists('postal_code', $address)) {
116 $address['postal_code_suffix'] = 1;
119 //build the returnproperties
120 $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
121 $mailingFormat = Civi
::settings()->get('mailing_format');
123 $mailingFormatProperties = array();
124 if ($mailingFormat) {
125 $mailingFormatProperties = CRM_Utils_Token
::getReturnProperties($mailingFormat);
126 $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
128 //we should not consider addressee for data exists, CRM-6025
129 if (array_key_exists('addressee', $mailingFormatProperties)) {
130 unset($mailingFormatProperties['addressee']);
133 $customFormatProperties = array();
134 if (stristr($mailingFormat, 'custom_')) {
135 foreach ($mailingFormatProperties as $token => $true) {
136 if (substr($token, 0, 7) == 'custom_') {
137 if (empty($customFormatProperties[$token])) {
138 $customFormatProperties[$token] = $mailingFormatProperties[$token];
144 if (!empty($customFormatProperties)) {
145 $returnProperties = array_merge($returnProperties, $customFormatProperties);
148 if (isset($fv['merge_same_address'])) {
149 // we need first name/last name for summarising to avoid spillage
150 $returnProperties['first_name'] = 1;
151 $returnProperties['last_name'] = 1;
154 $individualFormat = FALSE;
157 * CRM-8338: replace ids of household members with the id of their household
158 * so we can merge labels by household.
160 if (isset($fv['merge_same_household'])) {
161 $this->mergeContactIdsByHousehold();
162 $individualFormat = TRUE;
165 //get the contacts information
167 if (!empty($fv['location_type_id'])) {
168 $locType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
169 $locName = $locType[$fv['location_type_id']];
170 $location = array('location' => array("{$locName}" => $address));
171 $returnProperties = array_merge($returnProperties, $location);
172 $params[] = array('location_type', '=', array(1 => $fv['location_type_id']), 0, 0);
175 $returnProperties = array_merge($returnProperties, $address);
180 foreach ($this->_contactIds
as $key => $contactID) {
182 CRM_Core_Form
::CB_PREFIX
. $contactID,
191 if (!empty($fv['do_not_mail'])) {
192 $params[] = array('do_not_mail', '=', 0, 0, 0);
195 $params[] = array('is_deceased', '=', 0, 0, 0);
198 foreach ($returnProperties as $name => $dontCare) {
199 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
205 //get the total number of contacts to fetch from database.
206 $numberofContacts = count($this->_contactIds
);
207 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
208 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
210 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
212 // also get all token values
213 CRM_Utils_Hook
::tokenValues($details[0],
217 'CRM_Contact_Form_Task_Label'
221 CRM_Utils_Hook
::tokens($tokens);
222 $tokenFields = array();
223 foreach ($tokens as $category => $catTokens) {
224 foreach ($catTokens as $token => $tokenName) {
225 $tokenFields[] = $token;
229 foreach ($this->_contactIds
as $value) {
230 foreach ($custom as $cfID) {
231 if (isset($details[0][$value]["custom_{$cfID}"])) {
232 $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField
::displayValue($details[0][$value]["custom_{$cfID}"], $cfID);
235 $contact = CRM_Utils_Array
::value($value, $details['0']);
237 if (is_a($contact, 'CRM_Core_Error')) {
241 // we need to remove all the "_id"
242 unset($contact['contact_id']);
244 if ($locName && !empty($contact[$locName])) {
245 // If location type is not primary, $contact contains
246 // one more array as "$contact[$locName] = array( values... )"
248 if (!self
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
252 $contact = array_merge($contact, $contact[$locName]);
253 unset($contact[$locName]);
255 if (!empty($contact['county_id'])) {
256 unset($contact['county_id']);
259 foreach ($contact as $field => $fieldValue) {
260 $rows[$value][$field] = $fieldValue;
263 $valuesothers = array();
264 $paramsothers = array('contact_id' => $value);
265 $valuesothers = CRM_Core_BAO_Location
::getValues($paramsothers, $valuesothers);
266 if (!empty($fv['location_type_id'])) {
267 foreach ($valuesothers as $vals) {
268 if (CRM_Utils_Array
::value('location_type_id', $vals) ==
269 CRM_Utils_Array
::value('location_type_id', $fv)
271 foreach ($vals as $k => $v) {
272 if (in_array($k, array(
279 $rows[$value][$k] = $v['1']['name'];
282 $rows[$value][$k] = $v['1'][$k];
284 $rows[$value][$k . '_id'] = $v['1']['id'];
292 if (!self
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
296 if (!empty($contact['addressee_display'])) {
297 $contact['addressee_display'] = trim($contact['addressee_display']);
299 if (!empty($contact['addressee'])) {
300 $contact['addressee'] = $contact['addressee_display'];
303 // now create the rows for generating mailing labels
304 foreach ($contact as $field => $fieldValue) {
305 $rows[$value][$field] = $fieldValue;
310 if (isset($fv['merge_same_address'])) {
311 CRM_Core_BAO_Address
::mergeSameAddress($rows);
312 $individualFormat = TRUE;
315 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
316 foreach ($rows as $id => $row) {
317 if ($commMethods = CRM_Utils_Array
::value('preferred_communication_method', $row)) {
318 $val = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $commMethods));
319 $comm = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
321 foreach ($val as $vals) {
322 $temp[] = $comm[$vals];
324 $row['preferred_communication_method'] = implode(', ', $temp);
327 $formatted = CRM_Utils_Address
::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields);
329 // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it.
330 // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366
331 // Due to FriBidi peculiarities, this can't be called on
332 // a multi-line string, hence the explode+implode approach.
333 if (function_exists('fribidi_log2vis')) {
334 $lines = explode("\n", $formatted);
335 foreach ($lines as $i => $line) {
336 $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO
, FRIBIDI_CHARSET_UTF8
);
338 $formatted = implode("\n", $lines);
340 $rows[$id] = array($formatted);
343 //call function to create labels
344 self
::createLabel($rows, $fv['label_name']);
345 CRM_Utils_System
::civiExit(1);
349 * Check for presence of tokens to be swapped out.
351 * @param array $contact
352 * @param array $mailingFormatProperties
353 * @param array $tokenFields
357 public static function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) {
358 foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) {
359 //we should not consider addressee for data exists, CRM-6025
360 if ($key != 'addressee' && !empty($contact[$key])) {
368 * Create labels (pdf).
370 * @param array $contactRows
371 * Associated array of contact data.
372 * @param string $format
373 * Format in which labels needs to be printed.
374 * @param string $fileName
375 * The name of the file to save the label in.
377 public function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
378 $pdf = new CRM_Utils_PDF_Label($format, 'mm');
382 //build contact string that needs to be printed
384 foreach ($contactRows as $row => $value) {
385 foreach ($value as $k => $v) {
389 $pdf->AddPdfLabel($val);
392 $pdf->Output($fileName, 'D');