From 2d3e3c7bc96bd9d995cf562ed269e45987f9d595 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 28 Aug 2013 16:54:32 +1200 Subject: [PATCH] Add Options to Membership search to print labels & send letters --- CRM/Contact/Form/Task/LabelCommon.php | 415 +++++++++++++++++++ CRM/Mailing/BAO/Mailing.php | 6 +- CRM/Member/Form/Task/Label.php | 169 ++++++++ CRM/Member/Form/Task/PDFLetter.php | 134 ++++++ CRM/Member/Form/Task/PDFLetterCommon.php | 71 ++++ CRM/Member/Task.php | 10 + CRM/Utils/Token.php | 102 +++++ templates/CRM/Member/Form/Task/Label.js | 17 + templates/CRM/Member/Form/Task/Label.tpl | 52 +++ templates/CRM/Member/Form/Task/PDFLetter.tpl | 33 ++ 10 files changed, 1008 insertions(+), 1 deletion(-) create mode 100644 CRM/Contact/Form/Task/LabelCommon.php create mode 100644 CRM/Member/Form/Task/Label.php create mode 100644 CRM/Member/Form/Task/PDFLetter.php create mode 100644 CRM/Member/Form/Task/PDFLetterCommon.php create mode 100644 templates/CRM/Member/Form/Task/Label.js create mode 100644 templates/CRM/Member/Form/Task/Label.tpl create mode 100644 templates/CRM/Member/Form/Task/PDFLetter.tpl diff --git a/CRM/Contact/Form/Task/LabelCommon.php b/CRM/Contact/Form/Task/LabelCommon.php new file mode 100644 index 0000000000..ffbcbeb28b --- /dev/null +++ b/CRM/Contact/Form/Task/LabelCommon.php @@ -0,0 +1,415 @@ + $dontCare) { + //we should not consider addressee for data exists, CRM-6025 + if ($key != 'addressee' && CRM_Utils_Array::value($key, $contact)) { + return TRUE; + } + } + return FALSE; + } + /** + * function to create labels (pdf) + * + * @param array $contactRows assciated array of contact data + * @param string $format format in which labels needs to be printed + * @param string $fileName The name of the file to save the label in + * + * @return null + * @access public + */ + static function createLabel(&$contactRows, &$format, $fileName = 'MailingLabels_CiviCRM.pdf') { + $pdf = new CRM_Utils_PDF_Label($format, 'mm'); + $pdf->Open(); + $pdf->AddPage(); + + //build contact string that needs to be printed + $val = NULL; + foreach ($contactRows as $row => $value) { + foreach ($value as $k => $v) { + $val .= "$v\n"; + } + + $pdf->AddPdfLabel($val); + $val = ''; + } + $pdf->Output($fileName, 'D'); + } + + + /** + * 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); + } + + /** + * function to extract the return properties from the mailing format + * @todo I'm placing bets this is a duplicate of code elsewhere - find & merge + * @param unknown_type $format + * @return multitype:number + */ + function regexReturnProperties(&$format) { + $returnProperties = array(); + $matches = array(); + preg_match_all('/(? 1, + * - [supplemental_address_1] => 1, + * - [supplemental_address_2] => 1 + * ) + */ + function getAddressReturnProperties() { + $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'mailing_format' + ); + + $addressFields = CRM_Utils_Address::sequence($mailingFormat); + $addressReturnProperties = array_fill_keys($addressFields, 1); + + if (array_key_exists('postal_code', $addressReturnProperties)) { + $addressReturnProperties['postal_code_suffix'] = 1; + } + return $addressReturnProperties; + } + + /** + * Get token list from mailing format & contacts + * @param unknown_type $contacts + * @return unknown + */ + function getTokenData(&$contacts) { + $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'mailing_format' + ); + $tokens = $tokenFields = array(); + $messageToken = CRM_Utils_Token::getTokens($mailingFormat); + + // also get all token values + CRM_Utils_Hook::tokenValues($contacts, + array_keys($contacts), + NULL, + $messageToken, + 'CRM_Contact_Form_Task_LabelCommon' + ); + + CRM_Utils_Hook::tokens($tokens); + + foreach ($tokens as $category => $catTokens) { + foreach ($catTokens as $token => $tokenName) { + $tokenFields[] = $token; + } + } + return $tokenFields; + + } + /** + * Merge contacts with the Same address to get one shared label + * @param unknown_type $rows + */ + function mergeSameAddress(&$rows) { + $uniqueAddress = array(); + foreach (array_keys($rows) as $rowID) { + // load complete address as array key + $address = + trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); + if (isset($rows[$rowID]['last_name'])) { + $name = $rows[$rowID]['last_name']; + } + else { + $name = $rows[$rowID]['display_name']; + } + // fill uniqueAddress array with last/first name tree + if (isset($uniqueAddress[$address])) { + $uniqueAddress[$address]['names'][$name][] = $rows[$rowID]['first_name']; + // drop unnecessary rows + unset($rows[$rowID]); + // this is the first listing at this address + } + else { + $uniqueAddress[$address]['ID'] = $rowID; + $uniqueAddress[$address]['names'][$name][] = $rows[$rowID]['first_name']; + } + } + foreach ($uniqueAddress as $address => $data) { + // copy data back to $rows + $count = 0; + // one last name list per row + foreach ($data['names'] as $last_name => $first_names) { + // too many to list + if ($count > 2) { + break; + } + // collapse the tree to summarize + $family = trim(implode(" & ", $first_names) . " " . $last_name); + if ($count) { + $processedNames .= "\n" . $family; + } + else { + // build display_name string + $processedNames = $family; + } + $count++; + } + $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; + } + } + + function mergeSameHousehold(&$rows) { + # group selected contacts by type + $individuals = array(); + $households = array(); + foreach ($rows as $contact_id => $row) { + if ($row['contact_type'] == 'Household') { + $households[$contact_id] = $row; + } + elseif ($row['contact_type'] == 'Individual') { + $individuals[$contact_id] = $row; + } + } + + # exclude individuals belonging to selected households + foreach ($households as $household_id => $row) { + $dao = new CRM_Contact_DAO_Relationship(); + $dao->contact_id_b = $household_id; + $dao->find(); + while ($dao->fetch()) { + $individual_id = $dao->contact_id_a; + if (array_key_exists($individual_id, $individuals)) { + unset($individuals[$individual_id]); + } + } + } + + # merge back individuals and households + $rows = array_merge($individuals, $households); + return $rows; + } +} diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index ea64f4f1ef..28cb7e20dc 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -2469,11 +2469,15 @@ LEFT JOIN civicrm_mailing_group g ON g.mailing_id = m.id if (CRM_Utils_System::getClassName($form) == 'CRM_Mailing_Form_Upload') { $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens); } - + //@todo move this fn onto the form if (CRM_Utils_System::getClassName($form) == 'CRM_Contribute_Form_Task_PDFLetter') { $tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens); } + if(method_exists($form, 'listTokens')) { + $tokens = array_merge($form->listTokens(), $tokens); + } + //sorted in ascending order tokens by ignoring word case natcasesort($tokens); diff --git a/CRM/Member/Form/Task/Label.php b/CRM/Member/Form/Task/Label.php new file mode 100644 index 0000000000..6097a29f87 --- /dev/null +++ b/CRM/Member/Form/Task/Label.php @@ -0,0 +1,169 @@ +setContactIDs(); + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js'); + } + + /** + * Build the form + * + * @access public + * + * @return void + */ + function buildQuickForm() { + CRM_Utils_System::setTitle(ts('Make Mailing Labels')); + + //add select for label + $label = CRM_Core_BAO_LabelFormat::getList(TRUE); + + $this->add('select', 'label_name', ts('Select Label'), array('' => ts('- select label -')) + $label, TRUE); + + + // add select for Location Type + $this->addElement('select', 'location_type_id', ts('Select Location'), + array( + '' => ts('Primary')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), TRUE + ); + $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)')); + + // checkbox for SKIP contacts with Do Not Mail privacy option + $this->addElement('checkbox', 'do_not_mail', ts('Do not print labels for contacts with "Do Not Mail" privacy option checked')); + + $this->add('checkbox', 'merge_same_address', ts('Merge labels for contacts with the same address'), NULL); + $this->add('checkbox', 'merge_same_household', ts('Merge labels for contacts belonging to the same household'), NULL); + + $this->addDefaultButtons(ts('Make Mailing Labels')); + } + + /** + * This function sets the default values for the form. + * + * @param null + * + * @return array array of default values + * @access public + */ + function setDefaultValues() { + $defaults = array(); + $format = CRM_Core_BAO_LabelFormat::getDefaultValues(); + $defaults['label_name'] = CRM_Utils_Array::value('name', $format); + $defaults['merge_same_address'] = 0; + $defaults['merge_same_household'] = 0; + $defaults['do_not_mail'] = 1; + return $defaults; + } + + /** + * process the form after the input has been submitted and validated + * + * @access public + * + * @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_Contact_Form_Task_LabelCommon::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 ($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) { + $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); + } +} + diff --git a/CRM/Member/Form/Task/PDFLetter.php b/CRM/Member/Form/Task/PDFLetter.php new file mode 100644 index 0000000000..f939a58bdf --- /dev/null +++ b/CRM/Member/Form/Task/PDFLetter.php @@ -0,0 +1,134 @@ +skipOnHold = $this->skipDeceased = FALSE; + parent::preProcess(); + $this->setContactIDs(); + CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this); + } + + /** + * Set defaults + * (non-PHPdoc) + * @see CRM_Core_Form::setDefaultValues() + */ + function setDefaultValues() { + return CRM_Contact_Form_Task_PDFLetterCommon::setDefaultValues(); + } + + /** + * Build the form + * + * @access public + * + * @return void + */ + public function buildQuickForm() { + //enable form element + $this->assign('suppressForm', FALSE); + + // use contact form as a base + CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($this); + + $this->addButtons(array( + array( + 'type' => 'submit', + 'name' => ts('Make PDF Letters'), + 'isDefault' => TRUE, + ), + array( + 'type' => 'cancel', + 'name' => ts('Done'), + ), + ) + ); + + } + + /** + * process the form after the input has been submitted and validated + * + * @access public + * + * @return None + */ + public function postProcess() { + // TODO: rewrite using contribution token and one letter by contribution + $this->setContactIDs(); + $skipOnHold = isset($this->skipOnHold) ? $this->skipOnHold : FALSE; + $skipDeceased = isset($this->skipDeceased) ? $this->skipDeceased : TRUE; + CRM_Member_Form_Task_PDFLetterCommon::postProcess($this, $this->_memberIds, $skipOnHold, $skipDeceased, $this->_contactIds); + } + + /** + * list available tokens, at time of writing these were + * {membership.id} => Membership ID + * {membership.status} => Membership Status + * {membership.type} => Membership Type + * {membership.start_date} => Membership Start Date + * {membership.join_date} => Membership Join Date + * {membership.end_date} => Membership End Date + * {membership.fee} => Membership Fee + * @return Ambigous > + */ + public function listTokens() { + return CRM_Core_SelectValues::membershipTokens(); + } +} + diff --git a/CRM/Member/Form/Task/PDFLetterCommon.php b/CRM/Member/Form/Task/PDFLetterCommon.php new file mode 100644 index 0000000000..5888a79232 --- /dev/null +++ b/CRM/Member/Form/Task/PDFLetterCommon.php @@ -0,0 +1,71 @@ +postProcessHook(); + + CRM_Utils_System::civiExit(1); + } + //end of function + + /** + * generate htmlfor pdf letters + * @param unknown_type $membershipIDs + * @param unknown_type $returnProperties + * @param unknown_type $skipOnHold + * @param unknown_type $skipDeceased + * @param unknown_type $messageToken + * @return unknown + */ + static function generateHTML($membershipIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $html_message) { + $memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs); + + foreach ($memberships as $membershipID => $membership) { + // get contact information + $contactId = $membership['contact_id']; + $params = array('contact_id' => $contactId); + //getTokenDetails is much like calling the api contact.get function - but - with some minor + // special handlings. It preceeds the existance of the api + list($contacts) = CRM_Utils_Token::getTokenDetails( + $params, + $returnProperties, + $skipOnHold, + $skipDeceased, + NULL, + $messageToken, + 'CRM_Contribution_Form_Task_PDFLetterCommon' + ); + + $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contacts[$contactId], TRUE, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $tokenHtml, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE); + $tokenHtml = CRM_Utils_Token::parseThroughSmarty($tokenHtml, $contact); + + $html[] = $tokenHtml; + + } + return $html; + } +} + diff --git a/CRM/Member/Task.php b/CRM/Member/Task.php index 6ca5dffbdc..1924b8782b 100644 --- a/CRM/Member/Task.php +++ b/CRM/Member/Task.php @@ -95,6 +95,16 @@ class CRM_Member_Task { ), 'result' => TRUE, ), + 6 => array('title' => ts('Print address labels'), + 'class' => array( + 'CRM_Member_Form_Task_Label', + ), + 'result' => TRUE, + ), + 7 => array('title' => ts('Write PDF Letters (per membership)'), + 'class' => 'CRM_Member_Form_Task_PDFLetter', + 'result' => FALSE, + ), ); //CRM-4418, check for delete diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 28ca9417d3..73ce56a018 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -717,6 +717,22 @@ class CRM_Utils_Token { return $str; } + /** + * Parse html through Smarty resolving any smarty functions + * @param string $tokenHtml + * @param array $entity + * @param string $entityType + * @return string html parsed through smarty + */ + public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') { + if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { + $smarty = CRM_Core_Smarty::singleton(); + // also add the tokens to the template + $smarty->assign_by_ref($entityType, $entity); + $tokenHtml = $smarty->fetch("string:$tokenHtml"); + } + return $tokenHtml; + } public static function getHookTokenReplacement( $token, &$contact, @@ -1173,6 +1189,14 @@ class CRM_Utils_Token { return $details; } + /** + * Get Membership Token Details + * @param array $membershipIDs array of membership IDS + */ + static function getMembershipTokenDetails($membershipIDs) { + $memberships = civicrm_api3('membership', 'get', array('membership_id' => array('IN' => (array) $membershipIDs))); + return $memberships['values']; + } /** * replace greeting tokens exists in message/subject * @@ -1304,6 +1328,45 @@ class CRM_Utils_Token { } } + /** + * store membership tokens on the static _tokens array + */ + protected static function _buildMembershipTokens() { + $key = 'membership'; + if (self::$_tokens[$key] == NULL) { + $membershipTokens = array(); + $tokens = CRM_Core_SelectValues::membershipTokens(); + foreach ($tokens as $token => $dontCare) { + $membershipTokens[] = substr($token, (strpos($token, '.') + 1), -1); + } + self::$_tokens[$key] = $membershipTokens; + } + } + + /** + * Replace tokens for an entity + * @param string $entity + * @param array $entityArray (e.g. in format from api) + * @param string $str string to replace in + * @param array $knownTokens array of tokens present + * @param boolean $escapeSmarty + * @return string string with replacements made + */ + public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) { + if (!$knownTokens || !CRM_Utils_Array::value($entity, $knownTokens)) { + return $str; + } + + $fn = 'get' . ucFirst($entity) . 'tokenReplacement'; + //since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace + foreach ($knownTokens[$entity] as $token) { + $replaceMent = CRM_Utils_Token::$fn($token, $entityArray, $escapeSmarty); + $str = str_replace('{' . $entity . '.' . $token . '}', $replaceMent, $str); + } + $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); + return $str; + } + public static function &replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) { self::_buildContributionTokens(); @@ -1328,6 +1391,45 @@ class CRM_Utils_Token { return $str; } + /** + * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance + * - this is used by the pdfLetter task from membership search + * @param string $token + * @param array $membership an api result array for a single membership + * @param boolean $escapeSmarty + * @return string token replacement + */ + public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) { + $entity = 'membership'; + self::_buildMembershipTokens(); + switch ($token) { + case 'type': + $value = $membership['membership_name']; + break; + case 'status': + $statuses = CRM_Member_BAO_Membership::buildOptions('status_id'); + $value = $statuses[$membership['status_id']]; + break; + case 'fee': + $value = civicrm_api3('membership_type', 'getvalue', array('id' => $membership['membership_type_id'], 'return' => 'minimum_fee')); + break; + default: + if (in_array($token, self::$_tokens[$entity])) { + $value = $membership[$token]; + } + else { + //ie unchanged + $value = "{$entity}.{$token}"; + } + break; + } + + if ($escapeSmarty) { + $value = self::tokenEscapeSmarty($value); + } + return $value; + } + public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) { self::_buildContributionTokens(); diff --git a/templates/CRM/Member/Form/Task/Label.js b/templates/CRM/Member/Form/Task/Label.js new file mode 100644 index 0000000000..ee774b4956 --- /dev/null +++ b/templates/CRM/Member/Form/Task/Label.js @@ -0,0 +1,17 @@ +cj(function ($) { + 'use strict'; + $('#per_membership').click(function() { + if($(this).is(":checked")) { + $('#merge_same_address').attr('disabled', true); + $('#merge_same_household').attr('disabled', true); + $('#merge_same_address').attr('checked', false); + $('#merge_same_household').attr('checked', false); + } + else { + $('#merge_same_address').attr('disabled', false); + $('#merge_same_household').attr('disabled', false); + } + }); + + +}); \ No newline at end of file diff --git a/templates/CRM/Member/Form/Task/Label.tpl b/templates/CRM/Member/Form/Task/Label.tpl new file mode 100644 index 0000000000..435606082b --- /dev/null +++ b/templates/CRM/Member/Form/Task/Label.tpl @@ -0,0 +1,52 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.4 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2013 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +
+
{include file="CRM/Member/Form/Task.tpl"}
+
{include file="CRM/common/formButtons.tpl" location="top"}
+ + + + + + + + + + + + + + + + + + + + + +
{$form.label_name.label}{$form.label_name.html} {help id="id-select-label" file="CRM/Contact/Form/Task/Label.hlp"}
{$form.location_type_id.label}{$form.location_type_id.html}
{$form.per_membership.html} {$form.per_membership.label}
{$form.do_not_mail.html} {$form.do_not_mail.label}
{$form.merge_same_address.html} {$form.merge_same_address.label}
{$form.merge_same_household.html} {$form.merge_same_household.label}
+
{include file="CRM/common/formButtons.tpl" location="bottom"}
+
diff --git a/templates/CRM/Member/Form/Task/PDFLetter.tpl b/templates/CRM/Member/Form/Task/PDFLetter.tpl new file mode 100644 index 0000000000..f0269a3258 --- /dev/null +++ b/templates/CRM/Member/Form/Task/PDFLetter.tpl @@ -0,0 +1,33 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 4.4 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2013 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} +
+

{ts}Membership Letter (PDF){/ts}

+
{include file="CRM/Member/Form/Task.tpl"}
+ +{include file="CRM/Contact/Form/Task/PDFLetterCommon.tpl"} + +
{include file="CRM/common/formButtons.tpl" location="bottom"}
+
-- 2.25.1