| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * This class build form elements for select existing or create new soft block. |
| 20 | */ |
| 21 | class CRM_Contribute_Form_SoftCredit { |
| 22 | |
| 23 | /** |
| 24 | * Function used to build form element for soft credit block. |
| 25 | * |
| 26 | * @param CRM_Core_Form $form |
| 27 | * |
| 28 | * @return \CRM_Core_Form |
| 29 | */ |
| 30 | public static function buildQuickForm(&$form) { |
| 31 | if (!empty($form->_honor_block_is_active)) { |
| 32 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
| 33 | $ufJoinDAO->module = 'soft_credit'; |
| 34 | $ufJoinDAO->entity_id = $form->_id; |
| 35 | if ($ufJoinDAO->find(TRUE)) { |
| 36 | $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, 'soft_credit'); |
| 37 | if ($jsonData) { |
| 38 | foreach (['honor_block_title', 'honor_block_text'] as $name) { |
| 39 | $form->assign($name, $jsonData[$name]); |
| 40 | } |
| 41 | |
| 42 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); |
| 43 | |
| 44 | // radio button for Honor Type |
| 45 | foreach ($jsonData['soft_credit_types'] as $value) { |
| 46 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
| 47 | } |
| 48 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); |
| 49 | } |
| 50 | } |
| 51 | return $form; |
| 52 | } |
| 53 | |
| 54 | // by default generate 10 blocks |
| 55 | $item_count = $form->_softCreditItemCount; |
| 56 | |
| 57 | $showSoftCreditRow = 2; |
| 58 | if ($form->getAction() & CRM_Core_Action::UPDATE) { |
| 59 | $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); |
| 60 | } |
| 61 | elseif (!empty($form->_pledgeID)) { |
| 62 | //Check and select most recent completed contrubtion and use it to retrieve |
| 63 | //soft-credit information to use as default for current pledge payment, CRM-13981 |
| 64 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID); |
| 65 | foreach ($pledgePayments as $id => $record) { |
| 66 | if ($record['contribution_id']) { |
| 67 | $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE); |
| 68 | if ($record['status'] == 'Completed' && count($softCredits) > 0) { |
| 69 | $form->_softCreditInfo = $softCredits; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (property_exists($form, "_softCreditInfo")) { |
| 76 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
| 77 | $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); |
| 78 | $showSoftCreditRow++; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { |
| 83 | $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), ['create' => TRUE]); |
| 84 | |
| 85 | $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
| 86 | |
| 87 | $form->addSelect("soft_credit_type[{$rowNumber}]", [ |
| 88 | 'entity' => 'contribution_soft', |
| 89 | 'field' => 'soft_credit_type_id', |
| 90 | 'label' => ts('Type'), |
| 91 | ]); |
| 92 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
| 93 | $form->add('hidden', "soft_credit_id[{$rowNumber}]", |
| 94 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | self::addPCPFields($form); |
| 99 | |
| 100 | $form->assign('showSoftCreditRow', $showSoftCreditRow); |
| 101 | $form->assign('rowCount', $item_count); |
| 102 | $form->addElement('hidden', 'sct_default_id', |
| 103 | CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), |
| 104 | ['id' => 'sct_default_id'] |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add PCP fields for the new contribution form and others. |
| 110 | * |
| 111 | * @param CRM_Core_Form &$form |
| 112 | * The form being built. |
| 113 | * @param string $suffix |
| 114 | * A suffix to add to field names. |
| 115 | */ |
| 116 | public static function addPCPFields(&$form, $suffix = '') { |
| 117 | // CRM-7368 allow user to set or edit PCP link for contributions |
| 118 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); |
| 119 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { |
| 120 | $form->assign('siteHasPCPs', 1); |
| 121 | // Fixme: Not a true entityRef field. Relies on PCP.js.tpl |
| 122 | $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), ['class' => 'twenty', 'placeholder' => ts('- select -')]); |
| 123 | // stores the label |
| 124 | $form->add('hidden', "pcp_made_through$suffix"); |
| 125 | $form->addElement('checkbox', "pcp_display_in_roll$suffix", ts('Display in Honor Roll?'), NULL); |
| 126 | $form->addElement('text', "pcp_roll_nickname$suffix", ts('Name (for Honor Roll)')); |
| 127 | $form->addElement('textarea', "pcp_personal_note$suffix", ts('Personal Note (for Honor Roll)')); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Function used to set defaults for soft credit block. |
| 133 | * |
| 134 | * @param $defaults |
| 135 | * @param $form |
| 136 | */ |
| 137 | public static function setDefaultValues(&$defaults, &$form) { |
| 138 | //Used to hide/unhide PCP and/or Soft-credit Panes |
| 139 | $noPCP = $noSoftCredit = TRUE; |
| 140 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
| 141 | $noSoftCredit = FALSE; |
| 142 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
| 143 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); |
| 144 | $defaults["soft_credit_contact_id[$key]"] = $value['contact_id']; |
| 145 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
| 146 | } |
| 147 | } |
| 148 | if (!empty($form->_softCreditInfo['pcp_id'])) { |
| 149 | $noPCP = FALSE; |
| 150 | $pcpInfo = $form->_softCreditInfo; |
| 151 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
| 152 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
| 153 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); |
| 154 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
| 155 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
| 156 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); |
| 157 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); |
| 158 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); |
| 159 | } |
| 160 | |
| 161 | $form->assign('noSoftCredit', $noSoftCredit); |
| 162 | $form->assign('noPCP', $noPCP); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Global form rule. |
| 167 | * |
| 168 | * @param array $fields |
| 169 | * The input form values. |
| 170 | * |
| 171 | * @param $errors |
| 172 | * @param $self |
| 173 | * |
| 174 | * @return array |
| 175 | * Array of errors |
| 176 | */ |
| 177 | public static function formRule($fields, $errors, $self) { |
| 178 | $errors = []; |
| 179 | |
| 180 | // if honor roll fields are populated but no PCP is selected |
| 181 | if (empty($fields['pcp_made_through_id'])) { |
| 182 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
| 183 | CRM_Utils_Array::value('pcp_personal_note', $fields) |
| 184 | ) { |
| 185 | $errors['pcp_made_through_id'] = ts('Please select a Personal Campaign Page, OR uncheck Display in Honor Roll and clear both the Honor Roll Name and the Personal Note field.'); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (!empty($fields['soft_credit_amount'])) { |
| 190 | $repeat = array_count_values($fields['soft_credit_contact_id']); |
| 191 | foreach ($fields['soft_credit_amount'] as $key => $val) { |
| 192 | if (!empty($fields['soft_credit_contact_id'][$key])) { |
| 193 | if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { |
| 194 | $errors["soft_credit_contact_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); |
| 195 | } |
| 196 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
| 197 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount'])) |
| 198 | ) { |
| 199 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
| 200 | } |
| 201 | if (empty($fields['soft_credit_amount'][$key])) { |
| 202 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $errors; |
| 209 | } |
| 210 | |
| 211 | } |