| 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 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class provides the functionality for batch profile update for contributions. |
| 36 | */ |
| 37 | class CRM_Contribute_Form_Task_Batch extends CRM_Contribute_Form_Task { |
| 38 | |
| 39 | /** |
| 40 | * The title of the group |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $_title; |
| 45 | |
| 46 | /** |
| 47 | * Maximum profile fields that will be displayed |
| 48 | */ |
| 49 | protected $_maxFields = 9; |
| 50 | |
| 51 | /** |
| 52 | * Variable to store redirect path |
| 53 | */ |
| 54 | protected $_userContext; |
| 55 | |
| 56 | /** |
| 57 | * Build all the data structures needed to build the form. |
| 58 | */ |
| 59 | public function preProcess() { |
| 60 | // initialize the task and row fields |
| 61 | parent::preProcess(); |
| 62 | |
| 63 | //get the contact read only fields to display. |
| 64 | $readOnlyFields = array_merge(array('sort_name' => ts('Name')), |
| 65 | CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 66 | 'contact_autocomplete_options', |
| 67 | TRUE, NULL, FALSE, 'name', TRUE |
| 68 | ) |
| 69 | ); |
| 70 | //get the read only field data. |
| 71 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); |
| 72 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_contributionIds, |
| 73 | 'CiviContribute', $returnProperties |
| 74 | ); |
| 75 | $this->assign('contactDetails', $contactDetails); |
| 76 | $this->assign('readOnlyFields', $readOnlyFields); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Build the form object. |
| 81 | */ |
| 82 | public function buildQuickForm() { |
| 83 | $ufGroupId = $this->get('ufGroupId'); |
| 84 | |
| 85 | if (!$ufGroupId) { |
| 86 | CRM_Core_Error::fatal('ufGroupId is missing'); |
| 87 | } |
| 88 | $this->_title = ts('Batch Update for Contributions') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); |
| 89 | CRM_Utils_System::setTitle($this->_title); |
| 90 | |
| 91 | $this->addDefaultButtons(ts('Save')); |
| 92 | $this->_fields = array(); |
| 93 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); |
| 94 | |
| 95 | // remove file type field and then limit fields |
| 96 | $suppressFields = FALSE; |
| 97 | $removehtmlTypes = array('File', 'Autocomplete-Select'); |
| 98 | foreach ($this->_fields as $name => $field) { |
| 99 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && |
| 100 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) |
| 101 | ) { |
| 102 | $suppressFields = TRUE; |
| 103 | unset($this->_fields[$name]); |
| 104 | } |
| 105 | |
| 106 | //fix to reduce size as we are using this field in grid |
| 107 | if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) { |
| 108 | //shrink class to "form-text-medium" |
| 109 | $this->_fields[$name]['attributes']['size'] = 19; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); |
| 114 | |
| 115 | $this->addButtons(array( |
| 116 | array( |
| 117 | 'type' => 'submit', |
| 118 | 'name' => ts('Update Contribution(s)'), |
| 119 | 'isDefault' => TRUE, |
| 120 | ), |
| 121 | array( |
| 122 | 'type' => 'cancel', |
| 123 | 'name' => ts('Cancel'), |
| 124 | ), |
| 125 | ) |
| 126 | ); |
| 127 | |
| 128 | $this->assign('profileTitle', $this->_title); |
| 129 | $this->assign('componentIds', $this->_contributionIds); |
| 130 | |
| 131 | //load all campaigns. |
| 132 | if (array_key_exists('contribution_campaign_id', $this->_fields)) { |
| 133 | $this->_componentCampaigns = array(); |
| 134 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, |
| 135 | 'CRM_Contribute_DAO_Contribution', |
| 136 | TRUE, 'campaign_id', 'id', |
| 137 | ' id IN (' . implode(' , ', array_values($this->_contributionIds)) . ' ) ' |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | //fix for CRM-2752 |
| 142 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); |
| 143 | foreach ($this->_contributionIds as $contributionId) { |
| 144 | $typeId = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $contributionId, 'financial_type_id'); |
| 145 | foreach ($this->_fields as $name => $field) { |
| 146 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { |
| 147 | $customValue = CRM_Utils_Array::value($customFieldID, $customFields); |
| 148 | if (!empty($customValue['extends_entity_column_value'])) { |
| 149 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 150 | $customValue['extends_entity_column_value'] |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | if (!empty($entityColumnValue[$typeId]) || |
| 155 | CRM_Utils_System::isNull($entityColumnValue[$typeId]) |
| 156 | ) { |
| 157 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId); |
| 158 | } |
| 159 | } |
| 160 | else { |
| 161 | // handle non custom fields |
| 162 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $this->assign('fields', $this->_fields); |
| 168 | |
| 169 | // don't set the status message when form is submitted. |
| 170 | $buttonName = $this->controller->getButtonName('submit'); |
| 171 | |
| 172 | if ($suppressFields && $buttonName != '_qf_Batch_next') { |
| 173 | CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Unsupported Field Type'), 'error'); |
| 174 | } |
| 175 | |
| 176 | $this->addDefaultButtons(ts('Update Contributions')); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set default values for the form. |
| 181 | */ |
| 182 | public function setDefaultValues() { |
| 183 | if (empty($this->_fields)) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | $defaults = array(); |
| 188 | foreach ($this->_contributionIds as $contributionId) { |
| 189 | $details[$contributionId] = array(); |
| 190 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $contributionId, 'Contribute'); |
| 191 | } |
| 192 | |
| 193 | return $defaults; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Process the form after the input has been submitted and validated. |
| 198 | */ |
| 199 | public function postProcess() { |
| 200 | $params = $this->exportValues(); |
| 201 | $dates = array( |
| 202 | 'receive_date', |
| 203 | 'receipt_date', |
| 204 | 'thankyou_date', |
| 205 | 'cancel_date', |
| 206 | ); |
| 207 | if (isset($params['field'])) { |
| 208 | foreach ($params['field'] as $key => $value) { |
| 209 | |
| 210 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, |
| 211 | $key, |
| 212 | 'Contribution' |
| 213 | ); |
| 214 | |
| 215 | $ids['contribution'] = $key; |
| 216 | foreach ($dates as $val) { |
| 217 | if (isset($value[$val])) { |
| 218 | $value[$val] = CRM_Utils_Date::processDate($value[$val]); |
| 219 | } |
| 220 | } |
| 221 | if (!empty($value['financial_type'])) { |
| 222 | $value['financial_type_id'] = $value['financial_type']; |
| 223 | } |
| 224 | |
| 225 | if (!empty($value['payment_instrument'])) { |
| 226 | $value['payment_instrument_id'] = $value['payment_instrument']; |
| 227 | } |
| 228 | |
| 229 | if (!empty($value['contribution_source'])) { |
| 230 | $value['source'] = $value['contribution_source']; |
| 231 | } |
| 232 | |
| 233 | unset($value['financial_type']); |
| 234 | unset($value['contribution_source']); |
| 235 | $contribution = CRM_Contribute_BAO_Contribution::add($value, $ids); |
| 236 | |
| 237 | // add custom field values |
| 238 | if (!empty($value['custom']) && |
| 239 | is_array($value['custom']) |
| 240 | ) { |
| 241 | CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_contribution', $contribution->id); |
| 242 | } |
| 243 | } |
| 244 | CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success'); |
| 245 | } |
| 246 | else { |
| 247 | CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert'); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } |