Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-02-18-36-16
[civicrm-core.git] / CRM / Financial / Page / FinancialType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Page for displaying list of financial types
38 */
39 class CRM_Financial_Page_FinancialType extends CRM_Core_Page_Basic {
40
41 public $useLivePageJS = TRUE;
42 /**
43 * The action links that we need to display for the browse screen
44 *
45 * @var array
46 */
47 static $_links = NULL;
48
49 /**
50 * Get BAO Name
51 *
52 * @return string
53 * Classname of BAO.
54 */
55 public function getBAOName() {
56 return 'CRM_Financial_BAO_FinancialType';
57 }
58
59 /**
60 * Get action Links
61 *
62 * @return array
63 * (reference) of action links
64 */
65 public function &links() {
66 if (!(self::$_links)) {
67 self::$_links = array(
68 CRM_Core_Action::BROWSE => array(
69 'name' => ts('Accounts'),
70 'url' => 'civicrm/admin/financial/financialType/accounts',
71 'qs' => 'reset=1&action=browse&aid=%%id%%',
72 'title' => ts('Accounts'),
73 ),
74 CRM_Core_Action::UPDATE => array(
75 'name' => ts('Edit'),
76 'url' => 'civicrm/admin/financial/financialType',
77 'qs' => 'action=update&id=%%id%%&reset=1',
78 'title' => ts('Edit Financial Type'),
79 ),
80 CRM_Core_Action::DISABLE => array(
81 'name' => ts('Disable'),
82 'ref' => 'crm-enable-disable',
83 'title' => ts('Disable Financial Type'),
84 ),
85 CRM_Core_Action::ENABLE => array(
86 'name' => ts('Enable'),
87 'ref' => 'crm-enable-disable',
88 'title' => ts('Enable Financial Type'),
89 ),
90 CRM_Core_Action::DELETE => array(
91 'name' => ts('Delete'),
92 'url' => 'civicrm/admin/financial/financialType',
93 'qs' => 'action=delete&id=%%id%%',
94 'title' => ts('Delete Financial Type'),
95 ),
96 );
97 }
98 return self::$_links;
99 }
100
101 /**
102 * Run the page.
103 *
104 * This method is called after the page is created. It checks for the
105 * type of action and executes that action.
106 * Finally it calls the parent's run method.
107 *
108 * @return void
109 */
110 public function run() {
111 // get the requested action
112 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); // default to 'browse'
113
114 // assign vars to templates
115 $this->assign('action', $action);
116 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
117
118 // what action to take ?
119 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
120 $this->edit($action, $id);
121 }
122
123 // parent run
124 return parent::run();
125 }
126
127 /**
128 * Browse all financial types
129 *
130 *
131 * @return void
132 */
133 public function browse() {
134 // get all financial types sorted by weight
135 $financialType = array();
136 $dao = new CRM_Financial_DAO_FinancialType();
137 $dao->orderBy('name');
138 $dao->find();
139
140 while ($dao->fetch()) {
141 $financialType[$dao->id] = array();
142 CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]);
143 $defaults = $financialAccountId = array();
144 $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount();
145 $financialAccountIds = array();
146
147 $params['entity_id'] = $dao->id;
148 $params['entity_table'] = 'civicrm_financial_type';
149 CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, CRM_Core_DAO::$_nullArray, $financialAccountIds);
150
151 foreach ($financialAccountIds as $key => $values) {
152 if (!empty($financialAccounts[$values['financial_account_id']])) {
153 $financialAccountId[$values['financial_account_id']] = CRM_Utils_Array::value(
154 $values['financial_account_id'], $financialAccounts);
155 }
156 }
157
158 if (!empty($financialAccountId)) {
159 $financialType[$dao->id]['financial_account'] = implode(',', $financialAccountId);
160 }
161
162 // form all action links
163 $action = array_sum(array_keys($this->links()));
164
165 // update enable/disable links depending on if it is is_reserved or is_active
166 if ($dao->is_reserved) {
167 $action -= CRM_Core_Action::ENABLE;
168 $action -= CRM_Core_Action::DISABLE;
169 $action -= CRM_Core_Action::DELETE;
170 //continue;
171 }
172 else {
173 if ($dao->is_active) {
174 $action -= CRM_Core_Action::ENABLE;
175 }
176 else {
177 $action -= CRM_Core_Action::DISABLE;
178 }
179 }
180
181 $financialType[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
182 array('id' => $dao->id),
183 ts('more'),
184 FALSE,
185 'financialType.manage.action',
186 'FinancialType',
187 $dao->id
188 );
189 }
190 $this->assign('rows', $financialType);
191 }
192
193 /**
194 * Get name of edit form
195 *
196 * @return string
197 * Classname of edit form.
198 */
199 public function editForm() {
200 return 'CRM_Financial_Form_FinancialType';
201 }
202
203 /**
204 * Get edit form name
205 *
206 * @return string
207 * name of this page.
208 */
209 public function editName() {
210 return 'Financial Types';
211 }
212
213 /**
214 * Get user context.
215 *
216 * @param null $mode
217 *
218 * @return string
219 * user context.
220 */
221 public function userContext($mode = NULL) {
222 return 'civicrm/admin/financial/financialType';
223 }
224
225 }