Esempio n. 1
0
 /**
  * Process the form after the input has been submitted and validated.
  */
 public function postProcess()
 {
     $fv = $this->controller->exportValues($this->_name);
     $config = CRM_Core_Config::singleton();
     $locName = NULL;
     //get the address format sequence from the config file
     $mailingFormat = Civi::settings()->get('mailing_format');
     $sequence = CRM_Utils_Address::sequence($mailingFormat);
     foreach ($sequence as $v) {
         $address[$v] = 1;
     }
     if (array_key_exists('postal_code', $address)) {
         $address['postal_code_suffix'] = 1;
     }
     //build the returnproperties
     $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
     $mailingFormat = Civi::settings()->get('mailing_format');
     $mailingFormatProperties = array();
     if ($mailingFormat) {
         $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
         $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
     }
     //we should not consider addressee for data exists, CRM-6025
     if (array_key_exists('addressee', $mailingFormatProperties)) {
         unset($mailingFormatProperties['addressee']);
     }
     $customFormatProperties = array();
     if (stristr($mailingFormat, 'custom_')) {
         foreach ($mailingFormatProperties as $token => $true) {
             if (substr($token, 0, 7) == 'custom_') {
                 if (empty($customFormatProperties[$token])) {
                     $customFormatProperties[$token] = $mailingFormatProperties[$token];
                 }
             }
         }
     }
     if (!empty($customFormatProperties)) {
         $returnProperties = array_merge($returnProperties, $customFormatProperties);
     }
     if (isset($fv['merge_same_address'])) {
         // we need first name/last name for summarising to avoid spillage
         $returnProperties['first_name'] = 1;
         $returnProperties['last_name'] = 1;
     }
     $individualFormat = FALSE;
     /*
      * CRM-8338: replace ids of household members with the id of their household
      * so we can merge labels by household.
      */
     if (isset($fv['merge_same_household'])) {
         $this->mergeContactIdsByHousehold();
         $individualFormat = TRUE;
     }
     //get the contacts information
     $params = array();
     if (!empty($fv['location_type_id'])) {
         $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $locName = $locType[$fv['location_type_id']];
         $location = array('location' => array("{$locName}" => $address));
         $returnProperties = array_merge($returnProperties, $location);
         $params[] = array('location_type', '=', array(1 => $fv['location_type_id']), 0, 0);
     } else {
         $returnProperties = array_merge($returnProperties, $address);
     }
     $rows = array();
     foreach ($this->_contactIds as $key => $contactID) {
         $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0);
     }
     // fix for CRM-2651
     if (!empty($fv['do_not_mail'])) {
         $params[] = array('do_not_mail', '=', 0, 0, 0);
     }
     // fix for CRM-2613
     $params[] = array('is_deceased', '=', 0, 0, 0);
     $custom = array();
     foreach ($returnProperties as $name => $dontCare) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         if ($cfID) {
             $custom[] = $cfID;
         }
     }
     //get the total number of contacts to fetch from database.
     $numberofContacts = count($this->_contactIds);
     $query = new CRM_Contact_BAO_Query($params, $returnProperties);
     $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
     $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
     // also get all token values
     CRM_Utils_Hook::tokenValues($details[0], $this->_contactIds, NULL, $messageToken, 'CRM_Contact_Form_Task_Label');
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $tokenFields = array();
     foreach ($tokens as $category => $catTokens) {
         foreach ($catTokens as $token => $tokenName) {
             $tokenFields[] = $token;
         }
     }
     foreach ($this->_contactIds as $value) {
         foreach ($custom as $cfID) {
             if (isset($details[0][$value]["custom_{$cfID}"])) {
                 $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[0][$value]["custom_{$cfID}"], $cfID);
             }
         }
         $contact = CRM_Utils_Array::value($value, $details['0']);
         if (is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // we need to remove all the "_id"
         unset($contact['contact_id']);
         if ($locName && !empty($contact[$locName])) {
             // If location type is not primary, $contact contains
             // one more array as "$contact[$locName] = array( values... )"
             if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             $contact = array_merge($contact, $contact[$locName]);
             unset($contact[$locName]);
             if (!empty($contact['county_id'])) {
                 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 (!empty($fv['location_type_id'])) {
                 foreach ($valuesothers as $vals) {
                     if (CRM_Utils_Array::value('location_type_id', $vals) == CRM_Utils_Array::value('location_type_id', $fv)) {
                         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 (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             if (!empty($contact['addressee_display'])) {
                 $contact['addressee_display'] = trim($contact['addressee_display']);
             }
             if (!empty($contact['addressee'])) {
                 $contact['addressee'] = $contact['addressee_display'];
             }
             // now create the rows for generating mailing labels
             foreach ($contact as $field => $fieldValue) {
                 $rows[$value][$field] = $fieldValue;
             }
         }
     }
     if (isset($fv['merge_same_address'])) {
         CRM_Core_BAO_Address::mergeSameAddress($rows);
         $individualFormat = TRUE;
     }
     // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
     foreach ($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);
         // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it.
         // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366
         // Due to FriBidi peculiarities, this can't be called on
         // a multi-line string, hence the explode+implode approach.
         if (function_exists('fribidi_log2vis')) {
             $lines = explode("\n", $formatted);
             foreach ($lines as $i => $line) {
                 $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO, FRIBIDI_CHARSET_UTF8);
             }
             $formatted = implode("\n", $lines);
         }
         $rows[$id] = array($formatted);
     }
     //call function to create labels
     self::createLabel($rows, $fv['label_name']);
     CRM_Utils_System::civiExit(1);
 }
Esempio n. 2
0
 /**
  * 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 postProcess()
 {
     $values = $this->_contactIds;
     $config = CRM_Core_Config::singleton();
     $msg_id = $this->_submitValues['message_template'];
     if (!empty($msg_id)) {
         $mysql = " SELECT * FROM veda_civicrm_wordmailmerge WHERE msg_template_id = %1";
         $params = array(1 => array($msg_id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($mysql, $params);
         //$dao = CRM_Core_DAO::executeQuery($mysql);
         while ($dao->fetch()) {
             $fileId = $dao->file_id;
         }
         $sql = "SELECT * FROM civicrm_file WHERE id = %1";
         $params = array(1 => array($fileId, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($sql, $params);
         //$dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
             $default['fileID'] = $dao->id;
             $default['mime_type'] = $dao->mime_type;
             $default['fileName'] = $dao->uri;
             $default['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
             $default['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
             $default['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs('civicrm_file', $msg_id, $dao->id);
         }
         $defaults[$dao->id] = $default;
         $this->assign('defaults', $defaults);
         $noofContact = count($this->_contactIds);
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong/tbs_class.php';
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong-opentbs/tbs_plugin_opentbs.php';
         $TBS = new clsTinyButStrong();
         // new instance of TBS
         $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
         // load the OpenTBS plugin
         $template = $default['fullPath'];
         // contactrows to check for duplicate address
         $contactrows = array();
         foreach ($values as $key => $value) {
             $SelectedcontactID = $values[$key];
             // get the details for all selected contacts
             list($contactDetails) = CRM_Utils_Token::getTokenDetails(array($SelectedcontactID), $this->_returnProperties, NULL, NULL, FALSE, $this->_allTokens);
             // populate contactrows array to check dupliacte address
             $contactrows[$SelectedcontactID] = $contactDetails[$SelectedcontactID];
         }
         // if merge_letter_for_same_address selected check for duplicate address
         if (isset($this->_submitValues['merge_letter_for_same_address']) && $this->_submitValues['merge_letter_for_same_address']) {
             CRM_Core_BAO_Address::mergeSameAddress($contactrows);
         }
         foreach ($values as $key => $value) {
             if ($key < $noofContact) {
                 $selectedCID = $values[$key];
                 $contactFormatted = array();
                 // if contact_id found in filtered contactrows array get contact details from contactrows
                 if (array_key_exists($selectedCID, $contactrows)) {
                     $contactFormatted[$selectedCID] = $contactrows[$selectedCID];
                     $membershipFormatted = array();
                     if ($this->_searchFrom == 'member' && isset($contactFormatted[$selectedCID]['membership_id'])) {
                         $membershipFormatted = CRM_Utils_Token::getMembershipTokenDetails($contactFormatted[$selectedCID]['membership_id']);
                     }
                     foreach ($this->_tokenMerge as $atKey => $atValue) {
                         // Replace hook tokens
                         $explodedTokenName = explode('.', $atValue['token_name']);
                         // this is fixed by assigning 'address_block' token into 'contact' token array // gopi@vedaconsulting.co.uk
                         //need to do proper fix seems token named as contact.address_block
                         // $atValue['token_name'] = ($atValue['token_name'] == 'address_block') ? 'contact.'.$atValue['token_name'] : $atValue['token_name'];
                         if (array_key_exists($atValue['token_name'], $contactFormatted[$selectedCID])) {
                             if (!empty($explodedTokenName[1]) && $explodedTokenName[0] != 'contact') {
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             } else {
                                 $vars[$key][$atValue['token_name']] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             }
                         } else {
                             if ($explodedTokenName[0] == 'membership') {
                                 $explodedTokenName[1] = $explodedTokenName[1] == 'membership_id' ? 'id' : $explodedTokenName[1];
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = CRM_Utils_Token::getMembershipTokenReplacement($explodedTokenName[1], $membershipFormatted[$contactFormatted[$selectedCID]['membership_id']]);
                             } else {
                                 $vars[$key][$atValue['token_name']] = CRM_Utils_Token::getContactTokenReplacement($atValue['token_name'], $contactFormatted[$selectedCID], FALSE, FALSE);
                             }
                         }
                         //need to do proper fix, token_name.date seems not returning null value if not found
                         if ($explodedTokenName[0] == 'token_name' && !is_array($vars[$key]['token_name'])) {
                             $vars[$key][$atValue['token_name']] = '';
                         }
                     }
                     //to skip error, if by chance using membership token in 'find contact' search
                     if ($this->_searchFrom != 'member') {
                         foreach (CRM_Core_SelectValues::membershipTokens() as $token => $label) {
                             $token = str_replace(array('{', '}'), "", $token);
                             $tokenNames = explode('.', $token);
                             $vars[$key]['membership'][$tokenNames[1]] = $label;
                         }
                     }
                     foreach ($vars[$key] as $varKey => $varValue) {
                         $explodeValues = explode('.', $varKey);
                         if (isset($explodeValues[1]) && !empty($explodeValues[1])) {
                             $vars[$key][$explodeValues[0]][$explodeValues[1]] = $vars[$key][$varKey];
                             unset($vars[$key][$varKey]);
                         }
                     }
                     // blank lines removed while creating the address_block - gopi@vedaconsulting.co.uk
                     /*if (!empty($vars[$key]['contact']['address_block'])) {
                         $vars[$key]['contact']['address_block'] = str_replace('<br />', "", $vars[$key]['contact']['address_block']);
                       }*/
                     $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);
                     $TBS->MergeBlock(self::TOKEN_VAR_NAME, $vars);
                 }
             }
         }
         $output_file_name = 'CiviCRMWordExport.docx';
         $TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
         CRM_Utils_System::civiExit();
     }
     parent::postProcess();
 }