Merge pull request #13620 from eileenmcnaughton/signuptype
[civicrm-core.git] / CRM / Member / Form / Task / Label.php
CommitLineData
2d3e3c7b 1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
2d3e3c7b 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
2d3e3c7b 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
2d3e3c7b 27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
2d3e3c7b 32 * $Id$
33 *
34 */
35
36/**
37 * This class helps to print the labels for contacts
38 *
39 */
40class CRM_Member_Form_Task_Label extends CRM_Member_Form_Task {
41
42 /**
fe482240 43 * Build all the data structures needed to build the form.
2d3e3c7b 44 *
45 * @return void
2d3e3c7b 46 */
00be9182 47 public function preProcess() {
2d3e3c7b 48 parent::preProcess();
49 $this->setContactIDs();
50 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js');
51 }
52
53 /**
fe482240 54 * Build the form object.
2d3e3c7b 55 *
2d3e3c7b 56 *
57 * @return void
58 */
00be9182 59 public function buildQuickForm() {
14396107 60 CRM_Contact_Form_Task_Label::buildLabelForm($this);
2d3e3c7b 61 $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)'));
2d3e3c7b 62 }
63
64 /**
c490a46a 65 * Set default values for the form.
2d3e3c7b 66 *
a6c01b45
CW
67 * @return array
68 * array of default values
2d3e3c7b 69 */
00be9182 70 public function setDefaultValues() {
2d3e3c7b 71 $defaults = array();
72 $format = CRM_Core_BAO_LabelFormat::getDefaultValues();
73 $defaults['label_name'] = CRM_Utils_Array::value('name', $format);
74 $defaults['merge_same_address'] = 0;
75 $defaults['merge_same_household'] = 0;
76 $defaults['do_not_mail'] = 1;
77 return $defaults;
78 }
79
80 /**
fe482240 81 * Process the form after the input has been submitted and validated.
2d3e3c7b 82 *
2d3e3c7b 83 *
84 * @return void
85 */
86 public function postProcess() {
353ffa53 87 $formValues = $this->controller->exportValues($this->_name);
2d3e3c7b 88 $locationTypeID = $formValues['location_type_id'];
89 $respectDoNotMail = CRM_Utils_Array::value('do_not_mail', $formValues);
90 $labelName = $formValues['label_name'];
91 $mergeSameAddress = CRM_Utils_Array::value('merge_same_address', $formValues);
92 $mergeSameHousehold = CRM_Utils_Array::value('merge_same_household', $formValues);
93 $isPerMembership = CRM_Utils_Array::value('per_membership', $formValues);
22e263ad 94 if ($isPerMembership && ($mergeSameAddress || $mergeSameHousehold)) {
2d3e3c7b 95 // this shouldn't happen - perhaps is could if JS is disabled
96 CRM_Core_Session::setStatus(ts('As you are printing one label per membership your merge settings are being ignored'));
97 $mergeSameAddress = $mergeSameHousehold = FALSE;
98 }
99 // so no-one is tempted to refer to this again after relevant values are extracted
100 unset($formValues);
101
102 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
103
104 $individualFormat = FALSE;
105 if ($mergeSameAddress) {
8bdfc216 106 CRM_Core_BAO_Address::mergeSameAddress($rows);
2d3e3c7b 107 $individualFormat = TRUE;
108 }
109 if ($mergeSameHousehold) {
110 $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows);
111 $individualFormat = TRUE;
112 }
113 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
8b9bc216 114 foreach ((array) $rows as $id => $row) {
115 if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) {
116 $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods));
117 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
118 $temp = array();
119 foreach ($val as $vals) {
120 $temp[] = $comm[$vals];
2d3e3c7b 121 }
8b9bc216 122 $row['preferred_communication_method'] = implode(', ', $temp);
123 }
124 $row['id'] = $id;
4c49535e 125 $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $tokenFields);
8b9bc216 126 $rows[$id] = array($formatted);
2d3e3c7b 127 }
22e263ad 128 if ($isPerMembership) {
2d3e3c7b 129 $labelRows = array();
353ffa53 130 $memberships = civicrm_api3('membership', 'get', array(
c5c263ca
AH
131 'id' => array('IN' => $this->_memberIds),
132 'return' => 'contact_id',
133 ));
2d3e3c7b 134 foreach ($memberships['values'] as $id => $membership) {
bbe47da8 135 if (isset($rows[$membership['contact_id']])) {
0c3f8395 136 $labelRows[$id] = $rows[$membership['contact_id']];
bbe47da8 137 }
2d3e3c7b 138 }
139 }
92e4c2a5 140 else {
2d3e3c7b 141 $labelRows = $rows;
142 }
143 //call function to create labels
144 CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName);
292c8687 145 CRM_Utils_System::civiExit();
2d3e3c7b 146 }
96025800 147
2d3e3c7b 148}