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