3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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)
41 class CRM_Contact_Form_Task_LabelCommon
{
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.
54 public static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') {
55 $pdf = new CRM_Utils_PDF_Label($format, 'mm');
59 //build contact string that needs to be printed
61 foreach ($contactRows as $row => $value) {
62 foreach ($value as $k => $v) {
66 $pdf->AddPdfLabel($val);
69 $pdf->Output($fileName, 'D');
74 * Get the rows for the labels.
77 * @param int $locationTypeID
78 * @param bool $respectDoNotMail
79 * @param $mergeSameAddress
80 * @param bool $mergeSameHousehold
84 * Array of rows for labels
86 public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) {
88 //get the address format sequence from the config file
89 $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon
::getAddressReturnProperties();
91 //build the return properties
92 $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
93 $mailingFormat = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
97 $mailingFormatProperties = array();
99 $mailingFormatProperties = CRM_Utils_Token
::getReturnProperties($mailingFormat);
100 $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
103 $customFormatProperties = array();
104 if (stristr($mailingFormat, 'custom_')) {
105 foreach ($mailingFormatProperties as $token => $true) {
106 if (substr($token, 0, 7) == 'custom_') {
107 if (empty($customFormatProperties[$token])) {
108 $customFormatProperties[$token] = $mailingFormatProperties[$token];
113 $returnProperties = array_merge($returnProperties, $customFormatProperties);
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;
121 //get the contacts information
122 $params = $custom = array();
123 foreach ($contactIDs as $key => $contactID) {
125 CRM_Core_Form
::CB_PREFIX
. $contactID,
134 if (!empty($respectDoNotMail['do_not_mail'])) {
135 $params[] = array('do_not_mail', '=', 0, 0, 0);
138 $params[] = array('is_deceased', '=', 0, 0, 0);
140 if ($locationTypeID) {
141 $locType = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
142 $locName = $locType[$locationTypeID];
143 $location = array('location' => array("{$locName}" => $addressReturnProperties));
144 $returnProperties = array_merge($returnProperties, $location);
145 $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0);
148 $returnProperties = array_merge($returnProperties, $addressReturnProperties);
151 foreach ($returnProperties as $name) {
152 $cfID = CRM_Core_BAO_CustomField
::getKeyID($name);
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
162 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
163 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
165 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
166 $details = $details[0];
167 $tokenFields = CRM_Contact_Form_Task_LabelCommon
::getTokenData($details);
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]);
175 $contact = CRM_Utils_Array
::value($value, $details);
177 if (is_a($contact, 'CRM_Core_Error')) {
181 // we need to remove all the "_id"
182 unset($contact['contact_id']);
184 if ($locName && !empty($contact[$locName])) {
185 // If location type is not primary, $contact contains
186 // one more array as "$contact[$locName] = array( values... )"
188 if (!CRM_Contact_Form_Task_Label
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
192 unset($contact[$locName]);
194 if (!empty($contact['county_id'])) {
195 unset($contact['county_id']);
198 foreach ($contact as $field => $fieldValue) {
199 $rows[$value][$field] = $fieldValue;
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) {
207 if (CRM_Utils_Array
::value('location_type_id', $vals) ==
210 foreach ($vals as $k => $v) {
211 if (in_array($k, array(
218 $rows[$value][$k] = $v['1']['name'];
221 $rows[$value][$k] = $v['1'][$k];
223 $rows[$value][$k . '_id'] = $v['1']['id'];
231 if (!CRM_Contact_Form_Task_Label
::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
235 if (!empty($contact['addressee_display'])) {
236 $contact['addressee_display'] = trim($contact['addressee_display']);
238 if (!empty($contact['addressee'])) {
239 $contact['addressee'] = $contact['addressee_display'];
242 // now create the rows for generating mailing labels
243 foreach ($contact as $field => $fieldValue) {
244 $rows[$value][$field] = $fieldValue;
248 // sigh couldn't extract out tokenfields yet
249 return array($rows, $tokenFields);
253 * Get array of return properties for address fields required for mailing label.
256 * return properties for address e.g
257 * [street_address => 1, supplemental_address_1 => 1, supplemental_address_2 => 1]
259 public static function getAddressReturnProperties() {
260 $mailingFormat = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
264 $addressFields = CRM_Utils_Address
::sequence($mailingFormat);
265 $addressReturnProperties = array_fill_keys($addressFields, 1);
267 if (array_key_exists('postal_code', $addressReturnProperties)) {
268 $addressReturnProperties['postal_code_suffix'] = 1;
270 return $addressReturnProperties;
274 * Get token list from mailing format & contacts
275 * @param array $contacts
278 public static function getTokenData(&$contacts) {
279 $mailingFormat = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
282 $tokens = $tokenFields = array();
283 $messageToken = CRM_Utils_Token
::getTokens($mailingFormat);
285 // also get all token values
286 CRM_Utils_Hook
::tokenValues($contacts,
287 array_keys($contacts),
290 'CRM_Contact_Form_Task_LabelCommon'
293 CRM_Utils_Hook
::tokens($tokens);
295 foreach ($tokens as $category => $catTokens) {
296 foreach ($catTokens as $token => $tokenName) {
297 $tokenFields[] = $token;
309 public function mergeSameHousehold(&$rows) {
310 // group selected contacts by type
311 $individuals = array();
312 $households = array();
313 foreach ($rows as $contact_id => $row) {
314 if ($row['contact_type'] == 'Household') {
315 $households[$contact_id] = $row;
317 elseif ($row['contact_type'] == 'Individual') {
318 $individuals[$contact_id] = $row;
322 // exclude individuals belonging to selected households
323 foreach ($households as $household_id => $row) {
324 $dao = new CRM_Contact_DAO_Relationship();
325 $dao->contact_id_b
= $household_id;
327 while ($dao->fetch()) {
328 $individual_id = $dao->contact_id_a
;
329 if (array_key_exists($individual_id, $individuals)) {
330 unset($individuals[$individual_id]);
335 // merge back individuals and households
336 $rows = array_merge($individuals, $households);