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