3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class provides the common functionality for sending email to one or a group of contact ids.
37 class CRM_Contact_Form_Task_LabelCommon
{
40 * Create labels (pdf).
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.
49 public static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
50 $pdf = new CRM_Utils_PDF_Label($format, 'mm');
54 //build contact string that needs to be printed
56 foreach ((array) $contactRows as $row => $value) {
57 foreach ($value as $k => $v) {
61 $pdf->AddPdfLabel($val);
64 $pdf->Output($fileName, 'D');
68 * Get the rows for the labels.
71 * @param int $locationTypeID
72 * @param bool $respectDoNotMail
73 * @param $mergeSameAddress
74 * @param bool $mergeSameHousehold
78 * Array of rows for labels
80 public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) {
83 //get the address format sequence from the config file
84 $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon
::getAddressReturnProperties();
86 //build the return properties
87 $returnProperties = ['display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1];
88 $mailingFormat = Civi
::settings()->get('mailing_format');
90 $mailingFormatProperties = [];
92 $mailingFormatProperties = CRM_Utils_Token
::getReturnProperties($mailingFormat);
93 $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
96 $customFormatProperties = [];
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];
106 $returnProperties = array_merge($returnProperties, $customFormatProperties);
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;
114 //get the contacts information
115 $params = $custom = [];
116 foreach ($contactIDs as $key => $contactID) {
118 CRM_Core_Form
::CB_PREFIX
. $contactID,
127 if (!empty($respectDoNotMail['do_not_mail'])) {
128 $params[] = ['do_not_mail', '=', 0, 0, 0];
131 $params[] = ['is_deceased', '=', 0, 0, 0];
133 if ($locationTypeID) {
134 $locType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
135 $locName = $locType[$locationTypeID];
136 $location = ['location' => ["{$locName}" => $addressReturnProperties]];
137 $returnProperties = array_merge($returnProperties, $location);
138 $params[] = ['location_type', '=', [$locationTypeID => 1], 0, 0];
141 $returnProperties = array_merge($returnProperties, $addressReturnProperties);
144 foreach ($returnProperties as $name) {
145 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
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);
158 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
159 $details = $details[0];
160 $tokenFields = CRM_Contact_Form_Task_LabelCommon
::getTokenData($details);
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
::displayValue($details[$value]["custom_{$cfID}"], $cfID);
168 $contact = CRM_Utils_Array
::value($value, $details);
170 if (is_a($contact, 'CRM_Core_Error')) {
174 // we need to remove all the "_id"
175 unset($contact['contact_id']);
177 if ($locName && !empty($contact[$locName])) {
178 // If location type is not primary, $contact contains
179 // one more array as "$contact[$locName] = array( values... )"
181 if (!CRM_Contact_Form_Task_Label
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
185 unset($contact[$locName]);
187 if (!empty($contact['county_id'])) {
188 unset($contact['county_id']);
191 foreach ($contact as $field => $fieldValue) {
192 $rows[$value][$field] = $fieldValue;
196 $paramsothers = ['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) ==
203 foreach ($vals as $k => $v) {
211 $rows[$value][$k] = $v['1']['name'];
214 $rows[$value][$k] = $v['1'][$k];
216 $rows[$value][$k . '_id'] = $v['1']['id'];
224 if (!CRM_Contact_Form_Task_Label
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
228 if (!empty($contact['addressee_display'])) {
229 $contact['addressee_display'] = trim($contact['addressee_display']);
231 if (!empty($contact['addressee'])) {
232 $contact['addressee'] = $contact['addressee_display'];
235 // now create the rows for generating mailing labels
236 foreach ($contact as $field => $fieldValue) {
237 $rows[$value][$field] = $fieldValue;
241 // sigh couldn't extract out tokenfields yet
242 return [$rows, $tokenFields];
246 * Get array of return properties for address fields required for mailing label.
249 * return properties for address e.g
250 * [street_address => 1, supplemental_address_1 => 1, supplemental_address_2 => 1]
252 public static function getAddressReturnProperties() {
253 $mailingFormat = Civi
::settings()->get('mailing_format');
255 $addressFields = CRM_Utils_Address
::sequence($mailingFormat);
256 $addressReturnProperties = array_fill_keys($addressFields, 1);
258 if (array_key_exists('postal_code', $addressReturnProperties)) {
259 $addressReturnProperties['postal_code_suffix'] = 1;
261 return $addressReturnProperties;
265 * Get token list from mailing format & contacts
266 * @param array $contacts
269 public static function getTokenData(&$contacts) {
270 $mailingFormat = Civi
::settings()->get('mailing_format');
271 $tokens = $tokenFields = [];
272 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
274 // also get all token values
275 CRM_Utils_Hook
::tokenValues($contacts,
276 array_keys($contacts),
279 'CRM_Contact_Form_Task_LabelCommon'
282 CRM_Utils_Hook
::tokens($tokens);
284 foreach ($tokens as $category => $catTokens) {
285 foreach ($catTokens as $token => $tokenName) {
286 $tokenFields[] = $token;
298 public function mergeSameHousehold(&$rows) {
299 // group selected contacts by type
302 foreach ($rows as $contact_id => $row) {
303 if ($row['contact_type'] == 'Household') {
304 $households[$contact_id] = $row;
306 elseif ($row['contact_type'] == 'Individual') {
307 $individuals[$contact_id] = $row;
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;
316 while ($dao->fetch()) {
317 $individual_id = $dao->contact_id_a
;
318 if (array_key_exists($individual_id, $individuals)) {
319 unset($individuals[$individual_id]);
324 // merge back individuals and households
325 $rows = array_merge($individuals, $households);