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