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