INFRA-132 - Docblock formatting fixes
[civicrm-core.git] / CRM / Contact / Form / Task / LabelCommon.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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
2d3e3c7b 32 * $Id$
33 *
34 */
35
36/**
37 * This class provides the common functionality for sending email to
38 * one or a group of contact ids. This class is reused by all the search
39 * components in CiviCRM (since they all have send email as a task)
40 */
41class CRM_Contact_Form_Task_LabelCommon {
8bdfc216 42
2d3e3c7b 43 /**
100fef9d 44 * Create labels (pdf)
2d3e3c7b 45 *
77c5b619
TO
46 * @param array $contactRows
47 * Assciated array of contact data.
48 * @param string $format
49 * Format in which labels needs to be printed.
50 * @param string $fileName
51 * The name of the file to save the label in.
2d3e3c7b 52 *
2b37475d 53 * @return null
2d3e3c7b 54 */
00be9182 55 public static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
2d3e3c7b 56 $pdf = new CRM_Utils_PDF_Label($format, 'mm');
57 $pdf->Open();
58 $pdf->AddPage();
59
60 //build contact string that needs to be printed
61 $val = NULL;
62 foreach ($contactRows as $row => $value) {
63 foreach ($value as $k => $v) {
64 $val .= "$v\n";
65 }
66
67 $pdf->AddPdfLabel($val);
68 $val = '';
69 }
70 $pdf->Output($fileName, 'D');
71 }
72
73
74 /**
100fef9d 75 * Get the rows for the labels
2d3e3c7b 76 *
6c8f6e67 77 * @param $contactIDs
77c5b619
TO
78 * @param int $locationTypeID
79 * @param bool $respectDoNotMail
6c8f6e67 80 * @param $mergeSameAddress
77c5b619
TO
81 * @param bool $mergeSameHousehold
82 * UNUSED.
6c8f6e67 83 *
a6c01b45 84 * @return array
16b10e64 85 * Array of rows for labels
2d3e3c7b 86 */
87
00be9182 88 public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) {
2d3e3c7b 89 $locName = NULL;
90 //get the address format sequence from the config file
91 $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties();
92
bdd49e38 93 //build the return properties
ec1edc63 94 $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
2d3e3c7b 95 $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
96 'mailing_format'
97 );
98
99 $mailingFormatProperties = array();
100 if ($mailingFormat) {
bdd49e38 101 $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
2d3e3c7b 102 $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
103 }
104
105 $customFormatProperties = array();
106 if (stristr($mailingFormat, 'custom_')) {
107 foreach ($mailingFormatProperties as $token => $true) {
108 if (substr($token, 0, 7) == 'custom_') {
a7488080 109 if (empty($customFormatProperties[$token])) {
2d3e3c7b 110 $customFormatProperties[$token] = $mailingFormatProperties[$token];
111 }
112 }
113 }
114 }
115 $returnProperties = array_merge($returnProperties, $customFormatProperties);
116
117 if ($mergeSameAddress) {
118 // we need first name/last name for summarising to avoid spillage
119 $returnProperties['first_name'] = 1;
120 $returnProperties['last_name'] = 1;
121 }
122
123 //get the contacts information
124 $params = $custom = array();
125 foreach ($contactIDs as $key => $contactID) {
126 $params[] = array(
127 CRM_Core_Form::CB_PREFIX . $contactID,
353ffa53
TO
128 '=',
129 1,
130 0,
131 0,
2d3e3c7b 132 );
133 }
134
135 // fix for CRM-2651
a7488080 136 if (!empty($respectDoNotMail['do_not_mail'])) {
2d3e3c7b 137 $params[] = array('do_not_mail', '=', 0, 0, 0);
138 }
139 // fix for CRM-2613
140 $params[] = array('is_deceased', '=', 0, 0, 0);
141
142 if ($locationTypeID) {
353ffa53
TO
143 $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
144 $locName = $locType[$locationTypeID];
145 $location = array('location' => array("{$locName}" => $addressReturnProperties));
2d3e3c7b 146 $returnProperties = array_merge($returnProperties, $location);
353ffa53 147 $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0);
2d3e3c7b 148 }
149 else {
150 $returnProperties = array_merge($returnProperties, $addressReturnProperties);
151 }
152
153 foreach ($returnProperties as $name) {
154 $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
155 if ($cfID) {
156 $custom[] = $cfID;
157 }
158 }
159
160 //get the total number of contacts to fetch from database.
161 $numberofContacts = count($contactIDs);
162 //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds)
163 // except it also handles multiple locations
353ffa53
TO
164 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
165 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
2d3e3c7b 166
167 $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
168 $details = $details[0];
169 $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details);
170
171 foreach ($contactIDs as $value) {
172 foreach ($custom as $cfID) {
173 if (isset($details[$value]["custom_{$cfID}"])) {
174 $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]);
175 }
176 }
177 $contact = CRM_Utils_Array::value($value, $details);
178
179 if (is_a($contact, 'CRM_Core_Error')) {
180 return NULL;
181 }
182
183 // we need to remove all the "_id"
184 unset($contact['contact_id']);
185
8cc574cf 186 if ($locName && !empty($contact[$locName])) {
2d3e3c7b 187 // If location type is not primary, $contact contains
188 // one more array as "$contact[$locName] = array( values... )"
189
8bdfc216 190 if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
2d3e3c7b 191 continue;
192 }
193
194 unset($contact[$locName]);
195
a7488080 196 if (!empty($contact['county_id'])) {
2d3e3c7b 197 unset($contact['county_id']);
198 }
199
200 foreach ($contact as $field => $fieldValue) {
201 $rows[$value][$field] = $fieldValue;
202 }
203
204 $valuesothers = array();
205 $paramsothers = array('contact_id' => $value);
206 $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers);
207 if ($locationTypeID) {
208 foreach ($valuesothers as $vals) {
481a74f4 209 if (CRM_Utils_Array::value('location_type_id', $vals) ==
353ffa53
TO
210 $locationTypeID
211 ) {
2d3e3c7b 212 foreach ($vals as $k => $v) {
213 if (in_array($k, array(
353ffa53
TO
214 'email',
215 'phone',
216 'im',
217 'openid'
218 ))) {
2d3e3c7b 219 if ($k == 'im') {
220 $rows[$value][$k] = $v['1']['name'];
221 }
222 else {
223 $rows[$value][$k] = $v['1'][$k];
224 }
225 $rows[$value][$k . '_id'] = $v['1']['id'];
226 }
227 }
228 }
229 }
230 }
231 }
232 else {
8bdfc216 233 if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
2d3e3c7b 234 continue;
235 }
236
a7488080 237 if (!empty($contact['addressee_display'])) {
2d3e3c7b 238 $contact['addressee_display'] = trim($contact['addressee_display']);
239 }
a7488080 240 if (!empty($contact['addressee'])) {
2d3e3c7b 241 $contact['addressee'] = $contact['addressee_display'];
242 }
243
244 // now create the rows for generating mailing labels
245 foreach ($contact as $field => $fieldValue) {
246 $rows[$value][$field] = $fieldValue;
247 }
248 }
249 }
250 // sigh couldn't extract out tokenfields yet
251 return array($rows, $tokenFields);
252 }
253
2d3e3c7b 254 /**
255 * Get array of return properties for address fields required for mailing label
16b10e64 256 *
a6c01b45 257 * @return array
16b10e64
CW
258 * return properties for address e.g
259 * [street_address => 1, supplemental_address_1 => 1, supplemental_address_2 => 1]
2d3e3c7b 260 */
8bdfc216 261 public static function getAddressReturnProperties() {
2d3e3c7b 262 $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
263 'mailing_format'
264 );
265
266 $addressFields = CRM_Utils_Address::sequence($mailingFormat);
267 $addressReturnProperties = array_fill_keys($addressFields, 1);
268
269 if (array_key_exists('postal_code', $addressReturnProperties)) {
270 $addressReturnProperties['postal_code_suffix'] = 1;
271 }
272 return $addressReturnProperties;
273 }
274
275 /**
276 * Get token list from mailing format & contacts
16b10e64
CW
277 * @param array $contacts
278 * @return array
2d3e3c7b 279 */
8bdfc216 280 public static function getTokenData(&$contacts) {
2d3e3c7b 281 $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
282 'mailing_format'
283 );
284 $tokens = $tokenFields = array();
285 $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
286
287 // also get all token values
288 CRM_Utils_Hook::tokenValues($contacts,
289 array_keys($contacts),
290 NULL,
291 $messageToken,
292 'CRM_Contact_Form_Task_LabelCommon'
353ffa53 293 );
2d3e3c7b 294
295 CRM_Utils_Hook::tokens($tokens);
296
297 foreach ($tokens as $category => $catTokens) {
298 foreach ($catTokens as $token => $tokenName) {
299 $tokenFields[] = $token;
300 }
301 }
302 return $tokenFields;
303
304 }
2d3e3c7b 305
86538308 306 /**
16b10e64 307 * @param array $rows
86538308
EM
308 *
309 * @return array
310 */
00be9182 311 public function mergeSameHousehold(&$rows) {
16b10e64 312 // group selected contacts by type
2d3e3c7b 313 $individuals = array();
314 $households = array();
315 foreach ($rows as $contact_id => $row) {
316 if ($row['contact_type'] == 'Household') {
317 $households[$contact_id] = $row;
318 }
319 elseif ($row['contact_type'] == 'Individual') {
320 $individuals[$contact_id] = $row;
321 }
322 }
323
16b10e64 324 // exclude individuals belonging to selected households
2d3e3c7b 325 foreach ($households as $household_id => $row) {
353ffa53
TO
326 $dao = new CRM_Contact_DAO_Relationship();
327 $dao->contact_id_b = $household_id;
328 $dao->find();
329 while ($dao->fetch()) {
330 $individual_id = $dao->contact_id_a;
331 if (array_key_exists($individual_id, $individuals)) {
332 unset($individuals[$individual_id]);
333 }
334 }
2d3e3c7b 335 }
336
16b10e64 337 // merge back individuals and households
2d3e3c7b 338 $rows = array_merge($individuals, $households);
339 return $rows;
353ffa53 340 }
2d3e3c7b 341}