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