/** * Process the form after the input has been submitted and validated. * * * @return void */ public function postProcess() { $formValues = $this->controller->exportValues($this->_name); $locationTypeID = $formValues['location_type_id']; $respectDoNotMail = CRM_Utils_Array::value('do_not_mail', $formValues); $labelName = $formValues['label_name']; $mergeSameAddress = CRM_Utils_Array::value('merge_same_address', $formValues); $mergeSameHousehold = CRM_Utils_Array::value('merge_same_household', $formValues); $isPerMembership = CRM_Utils_Array::value('per_membership', $formValues); if ($isPerMembership && ($mergeSameAddress || $mergeSameHousehold)) { // this shouldn't happen - perhaps is could if JS is disabled CRM_Core_Session::setStatus(ts('As you are printing one label per membership your merge settings are being ignored')); $mergeSameAddress = $mergeSameHousehold = FALSE; } // so no-one is tempted to refer to this again after relevant values are extracted unset($formValues); list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold); $individualFormat = FALSE; if ($mergeSameAddress) { CRM_Core_BAO_Address::mergeSameAddress($rows); $individualFormat = TRUE; } if ($mergeSameHousehold) { $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows); $individualFormat = TRUE; } // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327) foreach ((array) $rows as $id => $row) { if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) { $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods)); $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); $temp = array(); foreach ($val as $vals) { $temp[] = $comm[$vals]; } $row['preferred_communication_method'] = implode(', ', $temp); } $row['id'] = $id; $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields); $rows[$id] = array($formatted); } if ($isPerMembership) { $labelRows = array(); $memberships = civicrm_api3('membership', 'get', array('id' => array('IN' => $this->_memberIds), 'return' => 'contact_id')); foreach ($memberships['values'] as $id => $membership) { if (isset($rows[$membership['contact_id']])) { $labelRows[$id] = $rows[$membership['contact_id']]; } } } else { $labelRows = $rows; } //call function to create labels CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName); CRM_Utils_System::civiExit(1); }
/** * function to get the rows for the labels * * @param array $contactIds Contact IDS to do labels for * @param integer $locationTypeID * @param boolean $respectDoNotMail * @return array of rows for labels * @access public */ static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold) { $locName = NULL; //get the address format sequence from the config file $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties(); //build the returnproperties $returnProperties = array('display_name' => 1, 'contact_type' => 1); $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format'); $mailingFormatProperties = array(); if ($mailingFormat) { $mailingFormatProperties = CRM_Contact_Form_Task_LabelCommon::regexReturnProperties($mailingFormat); $returnProperties = array_merge($returnProperties, $mailingFormatProperties); } $customFormatProperties = array(); if (stristr($mailingFormat, 'custom_')) { foreach ($mailingFormatProperties as $token => $true) { if (substr($token, 0, 7) == 'custom_') { if (!CRM_Utils_Array::value($token, $customFormatProperties)) { $customFormatProperties[$token] = $mailingFormatProperties[$token]; } } } } $returnProperties = array_merge($returnProperties, $customFormatProperties); if ($mergeSameAddress) { // we need first name/last name for summarising to avoid spillage $returnProperties['first_name'] = 1; $returnProperties['last_name'] = 1; } //get the contacts information $params = $custom = array(); foreach ($contactIDs as $key => $contactID) { $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0); } // fix for CRM-2651 if (CRM_Utils_Array::value('do_not_mail', $respectDoNotMail)) { $params[] = array('do_not_mail', '=', 0, 0, 0); } // fix for CRM-2613 $params[] = array('is_deceased', '=', 0, 0, 0); if ($locationTypeID) { $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $locName = $locType[$locationTypeID]; $location = array('location' => array("{$locName}" => $addressReturnProperties)); $returnProperties = array_merge($returnProperties, $location); $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0); } else { $returnProperties = array_merge($returnProperties, $addressReturnProperties); } foreach ($returnProperties as $name) { $cfID = CRM_Core_BAO_CustomField::getKeyID($name); if ($cfID) { $custom[] = $cfID; } } //get the total number of contacts to fetch from database. $numberofContacts = count($contactIDs); //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds) // except it also handles multiple locations $query = new CRM_Contact_BAO_Query($params, $returnProperties); $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); $messageToken = CRM_Utils_Token::getTokens($mailingFormat); $details = $details[0]; $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details); foreach ($contactIDs as $value) { foreach ($custom as $cfID) { if (isset($details[$value]["custom_{$cfID}"])) { $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($details[$value]["custom_{$cfID}"], $cfID, $details[1]); } } $contact = CRM_Utils_Array::value($value, $details); if (is_a($contact, 'CRM_Core_Error')) { return NULL; } // we need to remove all the "_id" unset($contact['contact_id']); if ($locName && CRM_Utils_Array::value($locName, $contact)) { // If location type is not primary, $contact contains // one more array as "$contact[$locName] = array( values... )" if (!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } unset($contact[$locName]); if (CRM_Utils_Array::value('county_id', $contact)) { unset($contact['county_id']); } foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } $valuesothers = array(); $paramsothers = array('contact_id' => $value); $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers); if ($locationTypeID) { foreach ($valuesothers as $vals) { if (CRM_Utils_Array::value('location_type_id', $vals) == $locationTypeID) { foreach ($vals as $k => $v) { if (in_array($k, array('email', 'phone', 'im', 'openid'))) { if ($k == 'im') { $rows[$value][$k] = $v['1']['name']; } else { $rows[$value][$k] = $v['1'][$k]; } $rows[$value][$k . '_id'] = $v['1']['id']; } } } } } } else { if (!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } if (CRM_Utils_Array::value('addressee_display', $contact)) { $contact['addressee_display'] = trim($contact['addressee_display']); } if (CRM_Utils_Array::value('addressee', $contact)) { $contact['addressee'] = $contact['addressee_display']; } // now create the rows for generating mailing labels foreach ($contact as $field => $fieldValue) { $rows[$value][$field] = $fieldValue; } } } // sigh couldn't extract out tokenfields yet return array($rows, $tokenFields); }