Cleanup deprecated CRM_Core_BAO_Settings calls CRM-17507
[civicrm-core.git] / CRM / Contact / Form / Task / LabelCommon.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 */
33
34 /**
35 * This class provides the common functionality for sending email to one or a group of contact ids.
36 */
37 class CRM_Contact_Form_Task_LabelCommon {
38
39 /**
40 * Create labels (pdf).
41 *
42 * @param array $contactRows
43 * Associated array of contact data.
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.
48 */
49 public static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
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 /**
69 * Get the rows for the labels.
70 *
71 * @param $contactIDs
72 * @param int $locationTypeID
73 * @param bool $respectDoNotMail
74 * @param $mergeSameAddress
75 * @param bool $mergeSameHousehold
76 * UNUSED.
77 *
78 * @return array
79 * Array of rows for labels
80 */
81 public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) {
82 $locName = NULL;
83 //get the address format sequence from the config file
84 $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties();
85
86 //build the return properties
87 $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
88 $mailingFormat = Civi::settings()->get('mailing_format');
89
90 $mailingFormatProperties = array();
91 if ($mailingFormat) {
92 $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
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_') {
100 if (empty($customFormatProperties[$token])) {
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,
119 '=',
120 1,
121 0,
122 0,
123 );
124 }
125
126 // fix for CRM-2651
127 if (!empty($respectDoNotMail['do_not_mail'])) {
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) {
134 $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
135 $locName = $locType[$locationTypeID];
136 $location = array('location' => array("{$locName}" => $addressReturnProperties));
137 $returnProperties = array_merge($returnProperties, $location);
138 $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0);
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
155 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
156 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
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}"])) {
165 $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]);
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
177 if ($locName && !empty($contact[$locName])) {
178 // If location type is not primary, $contact contains
179 // one more array as "$contact[$locName] = array( values... )"
180
181 if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
182 continue;
183 }
184
185 unset($contact[$locName]);
186
187 if (!empty($contact['county_id'])) {
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) {
200 if (CRM_Utils_Array::value('location_type_id', $vals) ==
201 $locationTypeID
202 ) {
203 foreach ($vals as $k => $v) {
204 if (in_array($k, array(
205 'email',
206 'phone',
207 'im',
208 'openid',
209 ))) {
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 {
224 if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
225 continue;
226 }
227
228 if (!empty($contact['addressee_display'])) {
229 $contact['addressee_display'] = trim($contact['addressee_display']);
230 }
231 if (!empty($contact['addressee'])) {
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
245 /**
246 * Get array of return properties for address fields required for mailing label.
247 *
248 * @return array
249 * return properties for address e.g
250 * [street_address => 1, supplemental_address_1 => 1, supplemental_address_2 => 1]
251 */
252 public static function getAddressReturnProperties() {
253 $mailingFormat = Civi::settings()->get('mailing_format');
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
266 * @param array $contacts
267 * @return array
268 */
269 public static function getTokenData(&$contacts) {
270 $mailingFormat = Civi::settings()->get('mailing_format');
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'
280 );
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 }
292
293 /**
294 * @param array $rows
295 *
296 * @return array
297 */
298 public function mergeSameHousehold(&$rows) {
299 // group selected contacts by type
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
311 // exclude individuals belonging to selected households
312 foreach ($households as $household_id => $row) {
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 }
322 }
323
324 // merge back individuals and households
325 $rows = array_merge($individuals, $households);
326 return $rows;
327 }
328
329 }