Merge pull request #23080 from eileenmcnaughton/isset
[civicrm-core.git] / CRM / Member / Form / Task / Label.php
CommitLineData
2d3e3c7b 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
2d3e3c7b 5 | |
bc77d7c0
TO
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 |
2d3e3c7b 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
2d3e3c7b 11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
2d3e3c7b 16 */
17
18/**
19 * This class helps to print the labels for contacts
20 *
21 */
22class CRM_Member_Form_Task_Label extends CRM_Member_Form_Task {
23
24 /**
fe482240 25 * Build all the data structures needed to build the form.
2d3e3c7b 26 *
27 * @return void
2d3e3c7b 28 */
00be9182 29 public function preProcess() {
2d3e3c7b 30 parent::preProcess();
31 $this->setContactIDs();
32 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js');
33 }
34
35 /**
fe482240 36 * Build the form object.
2d3e3c7b 37 *
2d3e3c7b 38 *
39 * @return void
40 */
00be9182 41 public function buildQuickForm() {
14396107 42 CRM_Contact_Form_Task_Label::buildLabelForm($this);
2d3e3c7b 43 $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)'));
2d3e3c7b 44 }
45
46 /**
c490a46a 47 * Set default values for the form.
2d3e3c7b 48 *
a6c01b45
CW
49 * @return array
50 * array of default values
2d3e3c7b 51 */
00be9182 52 public function setDefaultValues() {
be2fb01f 53 $defaults = [];
2d3e3c7b 54 $format = CRM_Core_BAO_LabelFormat::getDefaultValues();
9c1bc317 55 $defaults['label_name'] = $format['name'] ?? NULL;
2d3e3c7b 56 $defaults['merge_same_address'] = 0;
57 $defaults['merge_same_household'] = 0;
58 $defaults['do_not_mail'] = 1;
59 return $defaults;
60 }
61
62 /**
fe482240 63 * Process the form after the input has been submitted and validated.
2d3e3c7b 64 *
2d3e3c7b 65 *
66 * @return void
67 */
68 public function postProcess() {
353ffa53 69 $formValues = $this->controller->exportValues($this->_name);
2d3e3c7b 70 $locationTypeID = $formValues['location_type_id'];
9c1bc317 71 $respectDoNotMail = $formValues['do_not_mail'] ?? NULL;
2d3e3c7b 72 $labelName = $formValues['label_name'];
9c1bc317
CW
73 $mergeSameAddress = $formValues['merge_same_address'] ?? NULL;
74 $mergeSameHousehold = $formValues['merge_same_household'] ?? NULL;
75 $isPerMembership = $formValues['per_membership'] ?? NULL;
22e263ad 76 if ($isPerMembership && ($mergeSameAddress || $mergeSameHousehold)) {
2d3e3c7b 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;
80 }
81 // so no-one is tempted to refer to this again after relevant values are extracted
82 unset($formValues);
83
84 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
85
86 $individualFormat = FALSE;
87 if ($mergeSameAddress) {
8bdfc216 88 CRM_Core_BAO_Address::mergeSameAddress($rows);
2d3e3c7b 89 $individualFormat = TRUE;
90 }
91 if ($mergeSameHousehold) {
92 $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows);
93 $individualFormat = TRUE;
94 }
95 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
8b9bc216 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');
be2fb01f 100 $temp = [];
8b9bc216 101 foreach ($val as $vals) {
102 $temp[] = $comm[$vals];
2d3e3c7b 103 }
8b9bc216 104 $row['preferred_communication_method'] = implode(', ', $temp);
105 }
106 $row['id'] = $id;
68c1b1f5 107 $formatted = CRM_Utils_Address::formatMailingLabel($row, 'mailing_format', FALSE, TRUE, $tokenFields);
be2fb01f 108 $rows[$id] = [$formatted];
2d3e3c7b 109 }
22e263ad 110 if ($isPerMembership) {
be2fb01f
CW
111 $labelRows = [];
112 $memberships = civicrm_api3('membership', 'get', [
113 'id' => ['IN' => $this->_memberIds],
c5c263ca 114 'return' => 'contact_id',
be2fb01f 115 ]);
2d3e3c7b 116 foreach ($memberships['values'] as $id => $membership) {
bbe47da8 117 if (isset($rows[$membership['contact_id']])) {
0c3f8395 118 $labelRows[$id] = $rows[$membership['contact_id']];
bbe47da8 119 }
2d3e3c7b 120 }
121 }
92e4c2a5 122 else {
2d3e3c7b 123 $labelRows = $rows;
124 }
125 //call function to create labels
126 CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName);
292c8687 127 CRM_Utils_System::civiExit();
2d3e3c7b 128 }
96025800 129
2d3e3c7b 130}