[NFC] Code cleanup around comments, strict comparison, formatting
[civicrm-core.git] / CRM / Member / Form / Task / Label.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class helps to print the labels for contacts
22 *
23 */
24 class CRM_Member_Form_Task_Label extends CRM_Member_Form_Task {
25
26 /**
27 * Build all the data structures needed to build the form.
28 *
29 * @return void
30 */
31 public function preProcess() {
32 parent::preProcess();
33 $this->setContactIDs();
34 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js');
35 }
36
37 /**
38 * Build the form object.
39 *
40 *
41 * @return void
42 */
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)'));
46 }
47
48 /**
49 * Set default values for the form.
50 *
51 * @return array
52 * array of default values
53 */
54 public function setDefaultValues() {
55 $defaults = [];
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;
61 return $defaults;
62 }
63
64 /**
65 * Process the form after the input has been submitted and validated.
66 *
67 *
68 * @return void
69 */
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;
82 }
83 // so no-one is tempted to refer to this again after relevant values are extracted
84 unset($formValues);
85
86 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
87
88 $individualFormat = FALSE;
89 if ($mergeSameAddress) {
90 CRM_Core_BAO_Address::mergeSameAddress($rows);
91 $individualFormat = TRUE;
92 }
93 if ($mergeSameHousehold) {
94 $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows);
95 $individualFormat = TRUE;
96 }
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');
102 $temp = [];
103 foreach ($val as $vals) {
104 $temp[] = $comm[$vals];
105 }
106 $row['preferred_communication_method'] = implode(', ', $temp);
107 }
108 $row['id'] = $id;
109 $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $tokenFields);
110 $rows[$id] = [$formatted];
111 }
112 if ($isPerMembership) {
113 $labelRows = [];
114 $memberships = civicrm_api3('membership', 'get', [
115 'id' => ['IN' => $this->_memberIds],
116 'return' => 'contact_id',
117 ]);
118 foreach ($memberships['values'] as $id => $membership) {
119 if (isset($rows[$membership['contact_id']])) {
120 $labelRows[$id] = $rows[$membership['contact_id']];
121 }
122 }
123 }
124 else {
125 $labelRows = $rows;
126 }
127 //call function to create labels
128 CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName);
129 CRM_Utils_System::civiExit();
130 }
131
132 }