Scheduled jobs belonging to extensions cannot be disabled (#16430)
[civicrm-core.git] / CRM / Financial / Form / FinancialType.php
CommitLineData
6a488035 1<?php
6a488035 2/*
bc77d7c0
TO
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 +--------------------------------------------------------------------+
006389de 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class generates form components for Financial Type
6a488035
TO
20 */
21class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form {
22
251808cb
PN
23 use CRM_Core_Form_EntityFormTrait;
24
25 /**
26 * Fields for the entity to be assigned to the template.
27 *
28 * @var array
29 */
30 protected $entityFields = [];
31
32 /**
33 * Deletion message to be assigned to the form.
34 *
35 * @var string
36 */
37 protected $deleteMessage;
38
1ac87087
PN
39 /**
40 * Set variables up before form is built.
beb414cc 41 *
42 * @throws \CRM_Core_Exception
1ac87087
PN
43 */
44 public function preProcess() {
45 // Check permission for Financial Type when ACL-FT is enabled
46 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
47 && !CRM_Core_Permission::check('administer CiviCRM Financial Types')
48 ) {
beb414cc 49 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
1ac87087 50 }
251808cb 51 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
1ac87087 52 parent::preProcess();
251808cb
PN
53 $this->setPageTitle(ts('Financial Type'));
54 if ($this->_id) {
55 $this->_title = CRM_Core_PseudoConstant::getLabel(
56 'CRM_Financial_BAO_FinancialType',
57 'financial_type',
58 $this->_id
59 );
60 $this->assign('aid', $this->_id);
61 }
62 }
63
64 /**
65 * Set entity fields to be assigned to the form.
66 */
67 protected function setEntityFields() {
68 $this->entityFields = [
69 'name' => [
70 'name' => 'name',
71 'required' => TRUE,
72 ],
73 'description' => ['name' => 'description'],
74 'is_deductible' => [
75 'name' => 'is_deductible',
76 'description' => ts('Are contributions of this type tax-deductible?'),
77 ],
78 'is_reserved' => ['name' => 'is_reserved'],
79 'is_active' => ['name' => 'is_active'],
80 ];
81 }
82
83 /**
84 * Explicitly declare the entity api name.
85 */
86 public function getDefaultEntity() {
87 return 'FinancialType';
88 }
89
90 /**
91 * Set the delete message.
92 *
93 * We do this from the constructor in order to do a translation.
94 */
95 public function setDeleteMessage() {
96 $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?');
1ac87087
PN
97 }
98
6a488035 99 /**
fe482240 100 * Build the form object.
6a488035 101 */
ddaa8ef1 102 public function buildQuickForm() {
251808cb 103 self::buildQuickEntityForm();
ddaa8ef1 104 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
105 return;
106 }
251808cb
PN
107 if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved')) {
108 $this->freeze(['is_active']);
6a488035 109 }
045f52a3 110 $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists',
251808cb 111 ['CRM_Financial_DAO_FinancialType', $this->_id]
77292b55 112 );
6a488035
TO
113 }
114
115 /**
fe482240 116 * Process the form submission.
6a488035
TO
117 */
118 public function postProcess() {
119 if ($this->_action & CRM_Core_Action::DELETE) {
120 $errors = CRM_Financial_BAO_FinancialType::del($this->_id);
06a2050e 121 if (is_array($errors) && !empty($errors)) {
43e00027 122 CRM_Core_Error::statusBounce($errors['error_message'], CRM_Utils_System::url('civicrm/admin/financial/financialType', "reset=1&action=browse"), ts('Cannot Delete'));
6a488035 123 }
46526baa 124 CRM_Core_Session::setStatus(ts('Selected financial type has been deleted.'), ts('Record Deleted'), 'success');
eea16664 125 }
ddaa8ef1 126 else {
6a488035
TO
127 // store the submitted values in an array
128 $params = $this->exportValues();
251808cb
PN
129 if ($this->_id) {
130 $params['id'] = $this->_id;
6a488035 131 }
251808cb
PN
132 foreach ([
133 'is_active',
134 'is_reserved',
135 'is_deductible',
136 ] as $field) {
137 $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
138 }
139 $financialType = civicrm_api3('FinancialType', 'create', $params);
ddaa8ef1
PN
140 if ($this->_action & CRM_Core_Action::UPDATE) {
141 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1&action=browse');
251808cb 142 CRM_Core_Session::setStatus(ts('The financial type "%1" has been updated.', [1 => $params['name']]), ts('Saved'), 'success');
eea16664 143 }
ddaa8ef1 144 else {
251808cb
PN
145 $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', 'reset=1&action=browse&aid=' . $financialType['id']);
146
147 $statusArray = [
148 1 => $params['name'],
149 ];
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"],
156 ]);
157 if (!empty($financialAccounts['values'])) {
158 foreach ($financialAccounts['values'] as $financialAccount) {
159 $statusArray[] = $financialAccount['financial_account_id.name'];
160 }
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);
77292b55
PN
162 }
163 else {
251808cb 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);
77292b55 165 }
251808cb 166 CRM_Core_Session::setStatus($text, ts('Saved'), 'success', ['expires' => 0]);
6a488035
TO
167 }
168
ddaa8ef1 169 $session = CRM_Core_Session::singleton();
6a488035
TO
170 $session->replaceUserContext($url);
171 }
172 }
e2046b33 173
232624b1 174}