3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Financial Type
21 class CRM_Financial_Form_FinancialType
extends CRM_Core_Form
{
23 use CRM_Core_Form_EntityFormTrait
;
25 protected $_BAOName = 'CRM_Financial_BAO_FinancialType';
28 * Fields for the entity to be assigned to the template.
32 protected $entityFields = [];
35 * Deletion message to be assigned to the form.
39 protected $deleteMessage;
42 * Set variables up before form is built.
44 * @throws \CRM_Core_Exception
46 public function preProcess() {
47 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
49 $this->setPageTitle(ts('Financial Type'));
51 $this->_title
= CRM_Core_PseudoConstant
::getLabel(
52 'CRM_Financial_BAO_FinancialType',
56 $this->assign('aid', $this->_id
);
61 * Set entity fields to be assigned to the form.
63 protected function setEntityFields() {
64 $this->entityFields
= [
69 'description' => ['name' => 'description'],
71 'name' => 'is_deductible',
72 'description' => ts('Are contributions of this type tax-deductible?'),
74 'is_reserved' => ['name' => 'is_reserved'],
75 'is_active' => ['name' => 'is_active'],
80 * Explicitly declare the entity api name.
82 public function getDefaultEntity() {
83 return 'FinancialType';
87 * Set the delete message.
89 * We do this from the constructor in order to do a translation.
91 public function setDeleteMessage() {
92 $this->deleteMessage
= ts('WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.') . ts('Deleting a financial type cannot be undone.') . ts('Do you want to continue?');
96 * Build the form object.
98 * @throws \CRM_Core_Exception
100 public function buildQuickForm() {
101 $this->buildQuickEntityForm();
102 if ($this->_action
& CRM_Core_Action
::DELETE
) {
105 if ($this->_action
== CRM_Core_Action
::UPDATE
&& CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id
, 'is_reserved')) {
106 $this->freeze(['is_active']);
108 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists',
109 ['CRM_Financial_DAO_FinancialType', $this->_id
]
114 * Process the form submission.
116 * @throws \CiviCRM_API3_Exception
118 public function postProcess() {
119 if ($this->_action
& CRM_Core_Action
::DELETE
) {
120 $errors = CRM_Financial_BAO_FinancialType
::del($this->_id
);
121 if (is_array($errors) && !empty($errors)) {
122 CRM_Core_Error
::statusBounce($errors['error_message'], CRM_Utils_System
::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete'));
124 CRM_Core_Session
::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success');
127 // store the submitted values in an array
128 $params = $this->exportValues();
130 $params['id'] = $this->_id
;
137 $params[$field] = CRM_Utils_Array
::value($field, $params, FALSE);
139 $financialType = civicrm_api3('FinancialType', 'create', $params);
140 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
141 $url = CRM_Utils_System
::url('civicrm/admin/financial/financialType', 'reset=1&action=browse');
142 CRM_Core_Session
::setStatus(ts('The financial type "%1" has been updated.', [1 => $params['name']]), ts('Saved'), 'success');
145 $url = CRM_Utils_System
::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType['id']);
148 1 => $params['name'],
150 $financialAccounts = civicrm_api3('EntityFinancialAccount', 'get', [
151 'return' => ['financial_account_id.name'],
152 'entity_table' => 'civicrm_financial_type',
153 'entity_id' => $financialType['id'],
154 'options' => ['sort' => "id"],
155 'account_relationship' => ['!=' => 'Income Account is'],
157 if (!empty($financialAccounts['values'])) {
158 foreach ($financialAccounts['values'] as $financialAccount) {
159 $statusArray[] = $financialAccount['financial_account_id.name'];
161 $text = ts('Your Financial "%1" Type has been created, along with a corresponding income account "%1". That income account, along with standard financial accounts "%2", "%3" and "%4" have been linked to the financial type. You may edit or replace those relationships here.', $statusArray);
164 $text = ts('Your Financial "%1" Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.', $statusArray);
166 CRM_Core_Session
::setStatus($text, ts('Saved'), 'success', ['expires' => 0]);
169 $session = CRM_Core_Session
::singleton();
170 $session->replaceUserContext($url);
175 * Set default values for the form. MobileProvider that in edit/view mode
176 * the default values are retrieved from the database
180 public function setDefaultValues() {
181 $defaults = $this->getEntityDefaults();
183 if ($this->_action
& CRM_Core_Action
::ADD
) {
184 $defaults['is_active'] = 1;