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
21 * This class helps to print the labels for contacts
24 class CRM_Member_Form_Task_Label
extends CRM_Member_Form_Task
{
27 * Build all the data structures needed to build the form.
31 public function preProcess() {
33 $this->setContactIDs();
34 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js');
38 * Build the form object.
43 public function buildQuickForm() {
44 CRM_Contact_Form_Task_Label
::buildLabelForm($this);
45 $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)'));
49 * Set default values for the form.
52 * array of default values
54 public function setDefaultValues() {
56 $format = CRM_Core_BAO_LabelFormat
::getDefaultValues();
57 $defaults['label_name'] = $format['name'] ??
NULL;
58 $defaults['merge_same_address'] = 0;
59 $defaults['merge_same_household'] = 0;
60 $defaults['do_not_mail'] = 1;
65 * Process the form after the input has been submitted and validated.
70 public function postProcess() {
71 $formValues = $this->controller
->exportValues($this->_name
);
72 $locationTypeID = $formValues['location_type_id'];
73 $respectDoNotMail = $formValues['do_not_mail'] ??
NULL;
74 $labelName = $formValues['label_name'];
75 $mergeSameAddress = $formValues['merge_same_address'] ??
NULL;
76 $mergeSameHousehold = $formValues['merge_same_household'] ??
NULL;
77 $isPerMembership = $formValues['per_membership'] ??
NULL;
78 if ($isPerMembership && ($mergeSameAddress ||
$mergeSameHousehold)) {
79 // this shouldn't happen - perhaps is could if JS is disabled
80 CRM_Core_Session
::setStatus(ts('As you are printing one label per membership your merge settings are being ignored'));
81 $mergeSameAddress = $mergeSameHousehold = FALSE;
83 // so no-one is tempted to refer to this again after relevant values are extracted
86 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon
::getRows($this->_contactIds
, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
88 $individualFormat = FALSE;
89 if ($mergeSameAddress) {
90 CRM_Core_BAO_Address
::mergeSameAddress($rows);
91 $individualFormat = TRUE;
93 if ($mergeSameHousehold) {
94 $rows = CRM_Contact_Form_Task_LabelCommon
::mergeSameHousehold($rows);
95 $individualFormat = TRUE;
97 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
98 foreach ((array) $rows as $id => $row) {
99 if ($commMethods = CRM_Utils_Array
::value('preferred_communication_method', $row)) {
100 $val = array_filter(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $commMethods));
101 $comm = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
103 foreach ($val as $vals) {
104 $temp[] = $comm[$vals];
106 $row['preferred_communication_method'] = implode(', ', $temp);
109 $formatted = CRM_Utils_Address
::format($row, 'mailing_format', FALSE, TRUE, $tokenFields);
110 $rows[$id] = [$formatted];
112 if ($isPerMembership) {
114 $memberships = civicrm_api3('membership', 'get', [
115 'id' => ['IN' => $this->_memberIds
],
116 'return' => 'contact_id',
118 foreach ($memberships['values'] as $id => $membership) {
119 if (isset($rows[$membership['contact_id']])) {
120 $labelRows[$id] = $rows[$membership['contact_id']];
127 //call function to create labels
128 CRM_Contact_Form_Task_LabelCommon
::createLabel($labelRows, $labelName);
129 CRM_Utils_System
::civiExit();