Commit | Line | Data |
---|---|---|
2d3e3c7b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
2d3e3c7b | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | */ | |
41 | class 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 | * |
2d3e3c7b | 53 | */ |
00be9182 | 54 | public static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { |
2d3e3c7b | 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 | /** | |
fe482240 | 74 | * Get the rows for the labels. |
2d3e3c7b | 75 | * |
6c8f6e67 | 76 | * @param $contactIDs |
77c5b619 TO |
77 | * @param int $locationTypeID |
78 | * @param bool $respectDoNotMail | |
6c8f6e67 | 79 | * @param $mergeSameAddress |
77c5b619 TO |
80 | * @param bool $mergeSameHousehold |
81 | * UNUSED. | |
6c8f6e67 | 82 | * |
a6c01b45 | 83 | * @return array |
16b10e64 | 84 | * Array of rows for labels |
2d3e3c7b | 85 | */ |
00be9182 | 86 | public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) { |
2d3e3c7b | 87 | $locName = NULL; |
88 | //get the address format sequence from the config file | |
89 | $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties(); | |
90 | ||
bdd49e38 | 91 | //build the return properties |
ec1edc63 | 92 | $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1); |
2d3e3c7b | 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) { | |
bdd49e38 | 99 | $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat); |
2d3e3c7b | 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_') { | |
a7488080 | 107 | if (empty($customFormatProperties[$token])) { |
2d3e3c7b | 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, | |
353ffa53 TO |
126 | '=', |
127 | 1, | |
128 | 0, | |
129 | 0, | |
2d3e3c7b | 130 | ); |
131 | } | |
132 | ||
133 | // fix for CRM-2651 | |
a7488080 | 134 | if (!empty($respectDoNotMail['do_not_mail'])) { |
2d3e3c7b | 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) { | |
353ffa53 TO |
141 | $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
142 | $locName = $locType[$locationTypeID]; | |
143 | $location = array('location' => array("{$locName}" => $addressReturnProperties)); | |
2d3e3c7b | 144 | $returnProperties = array_merge($returnProperties, $location); |
353ffa53 | 145 | $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0); |
2d3e3c7b | 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 | |
353ffa53 TO |
162 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); |
163 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
2d3e3c7b | 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 | ||
8cc574cf | 184 | if ($locName && !empty($contact[$locName])) { |
2d3e3c7b | 185 | // If location type is not primary, $contact contains |
186 | // one more array as "$contact[$locName] = array( values... )" | |
187 | ||
8bdfc216 | 188 | if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
2d3e3c7b | 189 | continue; |
190 | } | |
191 | ||
192 | unset($contact[$locName]); | |
193 | ||
a7488080 | 194 | if (!empty($contact['county_id'])) { |
2d3e3c7b | 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) { | |
481a74f4 | 207 | if (CRM_Utils_Array::value('location_type_id', $vals) == |
353ffa53 TO |
208 | $locationTypeID |
209 | ) { | |
2d3e3c7b | 210 | foreach ($vals as $k => $v) { |
211 | if (in_array($k, array( | |
353ffa53 TO |
212 | 'email', |
213 | 'phone', | |
214 | 'im', | |
317fceb4 | 215 | 'openid', |
353ffa53 | 216 | ))) { |
2d3e3c7b | 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 { | |
8bdfc216 | 231 | if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { |
2d3e3c7b | 232 | continue; |
233 | } | |
234 | ||
a7488080 | 235 | if (!empty($contact['addressee_display'])) { |
2d3e3c7b | 236 | $contact['addressee_display'] = trim($contact['addressee_display']); |
237 | } | |
a7488080 | 238 | if (!empty($contact['addressee'])) { |
2d3e3c7b | 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 | ||
2d3e3c7b | 252 | /** |
fe482240 | 253 | * Get array of return properties for address fields required for mailing label. |
16b10e64 | 254 | * |
a6c01b45 | 255 | * @return array |
16b10e64 CW |
256 | * return properties for address e.g |
257 | * [street_address => 1, supplemental_address_1 => 1, supplemental_address_2 => 1] | |
2d3e3c7b | 258 | */ |
8bdfc216 | 259 | public static function getAddressReturnProperties() { |
2d3e3c7b | 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 | |
16b10e64 CW |
275 | * @param array $contacts |
276 | * @return array | |
2d3e3c7b | 277 | */ |
8bdfc216 | 278 | public static function getTokenData(&$contacts) { |
2d3e3c7b | 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' | |
353ffa53 | 291 | ); |
2d3e3c7b | 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 | } | |
2d3e3c7b | 303 | |
86538308 | 304 | /** |
16b10e64 | 305 | * @param array $rows |
86538308 EM |
306 | * |
307 | * @return array | |
308 | */ | |
00be9182 | 309 | public function mergeSameHousehold(&$rows) { |
16b10e64 | 310 | // group selected contacts by type |
2d3e3c7b | 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 | ||
16b10e64 | 322 | // exclude individuals belonging to selected households |
2d3e3c7b | 323 | foreach ($households as $household_id => $row) { |
353ffa53 TO |
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 | } | |
2d3e3c7b | 333 | } |
334 | ||
16b10e64 | 335 | // merge back individuals and households |
2d3e3c7b | 336 | $rows = array_merge($individuals, $households); |
337 | return $rows; | |
353ffa53 | 338 | } |
96025800 | 339 | |
2d3e3c7b | 340 | } |