| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class helps to print the labels for contacts |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Member_Form_Task_Label extends CRM_Member_Form_Task { |
| 41 | |
| 42 | /** |
| 43 | * Build all the data structures needed to build the form. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function preProcess() { |
| 48 | parent::preProcess(); |
| 49 | $this->setContactIDs(); |
| 50 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Member/Form/Task/Label.js'); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Build the form object. |
| 55 | * |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function buildQuickForm() { |
| 60 | CRM_Contact_Form_Task_Label::buildQuickForm($this); |
| 61 | $this->addElement('checkbox', 'per_membership', ts('Print one label per Membership (rather than per contact)')); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set default values for the form. |
| 66 | * |
| 67 | * @return array |
| 68 | * array of default values |
| 69 | */ |
| 70 | public function setDefaultValues() { |
| 71 | $defaults = array(); |
| 72 | $format = CRM_Core_BAO_LabelFormat::getDefaultValues(); |
| 73 | $defaults['label_name'] = CRM_Utils_Array::value('name', $format); |
| 74 | $defaults['merge_same_address'] = 0; |
| 75 | $defaults['merge_same_household'] = 0; |
| 76 | $defaults['do_not_mail'] = 1; |
| 77 | return $defaults; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Process the form after the input has been submitted and validated. |
| 82 | * |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function postProcess() { |
| 87 | $formValues = $this->controller->exportValues($this->_name); |
| 88 | $locationTypeID = $formValues['location_type_id']; |
| 89 | $respectDoNotMail = CRM_Utils_Array::value('do_not_mail', $formValues); |
| 90 | $labelName = $formValues['label_name']; |
| 91 | $mergeSameAddress = CRM_Utils_Array::value('merge_same_address', $formValues); |
| 92 | $mergeSameHousehold = CRM_Utils_Array::value('merge_same_household', $formValues); |
| 93 | $isPerMembership = CRM_Utils_Array::value('per_membership', $formValues); |
| 94 | if ($isPerMembership && ($mergeSameAddress || $mergeSameHousehold)) { |
| 95 | // this shouldn't happen - perhaps is could if JS is disabled |
| 96 | CRM_Core_Session::setStatus(ts('As you are printing one label per membership your merge settings are being ignored')); |
| 97 | $mergeSameAddress = $mergeSameHousehold = FALSE; |
| 98 | } |
| 99 | // so no-one is tempted to refer to this again after relevant values are extracted |
| 100 | unset($formValues); |
| 101 | |
| 102 | list($rows, $tokenFields) = CRM_Contact_Form_Task_LabelCommon::getRows($this->_contactIds, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold); |
| 103 | |
| 104 | $individualFormat = FALSE; |
| 105 | if ($mergeSameAddress) { |
| 106 | CRM_Core_BAO_Address::mergeSameAddress($rows); |
| 107 | $individualFormat = TRUE; |
| 108 | } |
| 109 | if ($mergeSameHousehold) { |
| 110 | $rows = CRM_Contact_Form_Task_LabelCommon::mergeSameHousehold($rows); |
| 111 | $individualFormat = TRUE; |
| 112 | } |
| 113 | // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327) |
| 114 | foreach ($rows as $id => $row) { |
| 115 | if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) { |
| 116 | $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods)); |
| 117 | $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); |
| 118 | $temp = array(); |
| 119 | foreach ($val as $vals) { |
| 120 | $temp[] = $comm[$vals]; |
| 121 | } |
| 122 | $row['preferred_communication_method'] = implode(', ', $temp); |
| 123 | } |
| 124 | $row['id'] = $id; |
| 125 | $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields); |
| 126 | $rows[$id] = array($formatted); |
| 127 | } |
| 128 | |
| 129 | if ($isPerMembership) { |
| 130 | $labelRows = array(); |
| 131 | $memberships = civicrm_api3('membership', 'get', array( |
| 132 | 'id' => array('IN' => $this->_memberIds), |
| 133 | 'return' => 'contact_id', |
| 134 | )); |
| 135 | foreach ($memberships['values'] as $id => $membership) { |
| 136 | $labelRows[$id] = $rows[$membership['contact_id']]; |
| 137 | } |
| 138 | } |
| 139 | else { |
| 140 | $labelRows = $rows; |
| 141 | } |
| 142 | //call function to create labels |
| 143 | CRM_Contact_Form_Task_LabelCommon::createLabel($labelRows, $labelName); |
| 144 | CRM_Utils_System::civiExit(1); |
| 145 | } |
| 146 | |
| 147 | } |