Merge pull request #16541 from bhahumanists/subtype-issue-991
[civicrm-core.git] / CRM / Financial / Page / FinancialType.php
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 * Page for displaying list of financial types
20 */
21 class CRM_Financial_Page_FinancialType extends CRM_Core_Page_Basic {
22
23 public $useLivePageJS = TRUE;
24 /**
25 * The action links that we need to display for the browse screen.
26 *
27 * @var array
28 */
29 public static $_links = NULL;
30
31 /**
32 * Get BAO Name.
33 *
34 * @return string
35 * Classname of BAO.
36 */
37 public function getBAOName() {
38 return 'CRM_Financial_BAO_FinancialType';
39 }
40
41 /**
42 * Get action Links.
43 *
44 * @return array
45 * (reference) of action links
46 */
47 public function &links() {
48 if (!(self::$_links)) {
49 self::$_links = [
50 CRM_Core_Action::BROWSE => [
51 'name' => ts('Accounts'),
52 'url' => 'civicrm/admin/financial/financialType/accounts',
53 'qs' => 'reset=1&action=browse&aid=%%id%%',
54 'title' => ts('Accounts'),
55 ],
56 CRM_Core_Action::UPDATE => [
57 'name' => ts('Edit'),
58 'url' => 'civicrm/admin/financial/financialType',
59 'qs' => 'action=update&id=%%id%%&reset=1',
60 'title' => ts('Edit Financial Type'),
61 ],
62 CRM_Core_Action::DISABLE => [
63 'name' => ts('Disable'),
64 'ref' => 'crm-enable-disable',
65 'title' => ts('Disable Financial Type'),
66 ],
67 CRM_Core_Action::ENABLE => [
68 'name' => ts('Enable'),
69 'ref' => 'crm-enable-disable',
70 'title' => ts('Enable Financial Type'),
71 ],
72 CRM_Core_Action::DELETE => [
73 'name' => ts('Delete'),
74 'url' => 'civicrm/admin/financial/financialType',
75 'qs' => 'action=delete&id=%%id%%',
76 'title' => ts('Delete Financial Type'),
77 ],
78 ];
79 }
80 return self::$_links;
81 }
82
83 /**
84 * Browse all financial types.
85 */
86 public function browse() {
87 // Check permission for Financial Type when ACL-FT is enabled
88 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
89 && !CRM_Core_Permission::check('administer CiviCRM Financial Types')
90 ) {
91 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
92 }
93 // get all financial types sorted by weight
94 $financialType = [];
95 $dao = new CRM_Financial_DAO_FinancialType();
96 $dao->orderBy('name');
97 $dao->find();
98
99 while ($dao->fetch()) {
100 $financialType[$dao->id] = [];
101 CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]);
102 $defaults = $financialAccountId = [];
103 $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount();
104 $financialAccountIds = [];
105
106 $params['entity_id'] = $dao->id;
107 $params['entity_table'] = 'civicrm_financial_type';
108 $null = [];
109 CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $null, $financialAccountIds);
110
111 foreach ($financialAccountIds as $key => $values) {
112 if (!empty($financialAccounts[$values['financial_account_id']])) {
113 $financialAccountId[$values['financial_account_id']] = $financialAccounts[$values['financial_account_id']] ?? NULL;
114 }
115 }
116
117 if (!empty($financialAccountId)) {
118 $financialType[$dao->id]['financial_account'] = implode(',', $financialAccountId);
119 }
120
121 // form all action links
122 $action = array_sum(array_keys($this->links()));
123
124 // update enable/disable links depending on if it is is_reserved or is_active
125 if ($dao->is_reserved) {
126 $action -= CRM_Core_Action::ENABLE;
127 $action -= CRM_Core_Action::DISABLE;
128 $action -= CRM_Core_Action::DELETE;
129 }
130 else {
131 if ($dao->is_active) {
132 $action -= CRM_Core_Action::ENABLE;
133 }
134 else {
135 $action -= CRM_Core_Action::DISABLE;
136 }
137 }
138
139 $financialType[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
140 ['id' => $dao->id],
141 ts('more'),
142 FALSE,
143 'financialType.manage.action',
144 'FinancialType',
145 $dao->id
146 );
147 }
148 $this->assign('rows', $financialType);
149 }
150
151 /**
152 * Get name of edit form.
153 *
154 * @return string
155 * Classname of edit form.
156 */
157 public function editForm() {
158 return 'CRM_Financial_Form_FinancialType';
159 }
160
161 /**
162 * Get edit form name.
163 *
164 * @return string
165 * name of this page.
166 */
167 public function editName() {
168 return 'Financial Types';
169 }
170
171 /**
172 * Get user context.
173 *
174 * @param null $mode
175 *
176 * @return string
177 * user context.
178 */
179 public function userContext($mode = NULL) {
180 return 'civicrm/admin/financial/financialType';
181 }
182
183 }