3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class helps to print the labels for contacts
22 class CRM_Member_Form_Task_Label
extends CRM_Member_Form_Task
{
25 * Build all the data structures needed to build the form.
29 public function preProcess() {
31 $this->setContactIDs();
32 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js');
36 * Build the form object.
41 public function buildQuickForm() {
42 CRM_Contact_Form_Task_Label
::buildLabelForm($this);
43 $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)'));
47 * Set default values for the form.
50 * array of default values
52 public function setDefaultValues() {
54 $format = CRM_Core_BAO_LabelFormat
::getDefaultValues();
55 $defaults['label_name'] = $format['name'] ??
NULL;
56 $defaults['merge_same_address'] = 0;
57 $defaults['merge_same_household'] = 0;
58 $defaults['do_not_mail'] = 1;
63 * Process the form after the input has been submitted and validated.
68 public function postProcess() {
69 $formValues = $this->controller
->exportValues($this->_name
);
70 $locationTypeID = $formValues['location_type_id'];
71 $respectDoNotMail = $formValues['do_not_mail'] ??
NULL;
72 $labelName = $formValues['label_name'];
73 $mergeSameAddress = $formValues['merge_same_address'] ??
NULL;
74 $mergeSameHousehold = $formValues['merge_same_household'] ??
NULL;
75 $isPerMembership = $formValues['per_membership'] ??
NULL;
76 if ($isPerMembership && ($mergeSameAddress ||
$mergeSameHousehold)) {
77 // this shouldn't happen - perhaps is could if JS is disabled
78 CRM_Core_Session
::setStatus(ts('As you are printing one label per membership your merge settings are being ignored'));
79 $mergeSameAddress = $mergeSameHousehold = FALSE;
81 // so no-one is tempted to refer to this again after relevant values are extracted
84 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon
::getRows($this->_contactIds
, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
86 $individualFormat = FALSE;
87 if ($mergeSameAddress) {
88 CRM_Core_BAO_Address
::mergeSameAddress($rows);
89 $individualFormat = TRUE;
91 if ($mergeSameHousehold) {
92 $rows = CRM_Contact_Form_Task_LabelCommon
::mergeSameHousehold($rows);
93 $individualFormat = TRUE;
95 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
96 foreach ((array) $rows as $id => $row) {
97 if ($commMethods = CRM_Utils_Array
::value('preferred_communication_method', $row)) {
98 $val = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $commMethods));
99 $comm = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
101 foreach ($val as $vals) {
102 $temp[] = $comm[$vals];
104 $row['preferred_communication_method'] = implode(', ', $temp);
107 $formatted = CRM_Utils_Address
::format($row, 'mailing_format', FALSE, TRUE, $tokenFields);
108 $rows[$id] = [$formatted];
110 if ($isPerMembership) {
112 $memberships = civicrm_api3('membership', 'get', [
113 'id' => ['IN' => $this->_memberIds
],
114 'return' => 'contact_id',
116 foreach ($memberships['values'] as $id => $membership) {
117 if (isset($rows[$membership['contact_id']])) {
118 $labelRows[$id] = $rows[$membership['contact_id']];
125 //call function to create labels
126 CRM_Contact_Form_Task_LabelCommon
::createLabel($labelRows, $labelName);
127 CRM_Utils_System
::civiExit();