INFRA-132 - Drupal.Array.Array.CommaLastItem
[civicrm-core.git] / CRM / Member / Form / Task / Label.php
CommitLineData
2d3e3c7b 1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
2d3e3c7b 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
100fef9d 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 /**
c490a46a 54 * Build the form object
2d3e3c7b 55 *
2d3e3c7b 56 *
57 * @return void
58 */
00be9182 59 public function buildQuickForm() {
85d25c8f 60 CRM_Contact_Form_Task_Label::buildQuickForm($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 *
67 * @param null
68 *
a6c01b45
CW
69 * @return array
70 * array of default values
2d3e3c7b 71 */
00be9182 72 public function setDefaultValues() {
2d3e3c7b 73 $defaults = array();
74 $format = CRM_Core_BAO_LabelFormat::getDefaultValues();
75 $defaults['label_name'] = CRM_Utils_Array::value('name', $format);
76 $defaults['merge_same_address'] = 0;
77 $defaults['merge_same_household'] = 0;
78 $defaults['do_not_mail'] = 1;
79 return $defaults;
80 }
81
82 /**
100fef9d 83 * Process the form after the input has been submitted and validated
2d3e3c7b 84 *
2d3e3c7b 85 *
86 * @return void
87 */
88 public function postProcess() {
353ffa53 89 $formValues = $this->controller->exportValues($this->_name);
2d3e3c7b 90 $locationTypeID = $formValues['location_type_id'];
91 $respectDoNotMail = CRM_Utils_Array::value('do_not_mail', $formValues);
92 $labelName = $formValues['label_name'];
93 $mergeSameAddress = CRM_Utils_Array::value('merge_same_address', $formValues);
94 $mergeSameHousehold = CRM_Utils_Array::value('merge_same_household', $formValues);
95 $isPerMembership = CRM_Utils_Array::value('per_membership', $formValues);
22e263ad 96 if ($isPerMembership && ($mergeSameAddress || $mergeSameHousehold)) {
2d3e3c7b 97 // this shouldn't happen - perhaps is could if JS is disabled
98 CRM_Core_Session::setStatus(ts('As you are printing one label per membership your merge settings are being ignored'));
99 $mergeSameAddress = $mergeSameHousehold = FALSE;
100 }
101 // so no-one is tempted to refer to this again after relevant values are extracted
102 unset($formValues);
103
104 list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold);
105
106 $individualFormat = FALSE;
107 if ($mergeSameAddress) {
8bdfc216 108 CRM_Core_BAO_Address::mergeSameAddress($rows);
2d3e3c7b 109 $individualFormat = TRUE;
110 }
111 if ($mergeSameHousehold) {
112 $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows);
113 $individualFormat = TRUE;
114 }
115 // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
116 foreach ($rows as $id => $row) {
117 if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) {
353ffa53 118 $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods));
2d3e3c7b 119 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
120 $temp = array();
121 foreach ($val as $vals) {
122 $temp[] = $comm[$vals];
123 }
124 $row['preferred_communication_method'] = implode(', ', $temp);
125 }
126 $row['id'] = $id;
127 $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields);
128 $rows[$id] = array($formatted);
129 }
130
22e263ad 131 if ($isPerMembership) {
2d3e3c7b 132 $labelRows = array();
353ffa53
TO
133 $memberships = civicrm_api3('membership', 'get', array(
134 'id' => array('IN' => $this->_memberIds),
af9b09df 135 'return' => 'contact_id',
353ffa53 136 ));
2d3e3c7b 137 foreach ($memberships['values'] as $id => $membership) {
138 $labelRows[$id] = $rows[$membership['contact_id']];
139 }
140 }
92e4c2a5 141 else {
2d3e3c7b 142 $labelRows = $rows;
143 }
144 //call function to create labels
145 CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName);
146 CRM_Utils_System::civiExit(1);
147 }
96025800 148
2d3e3c7b 149}