Used Api to get financial account for an entity defined in civicrm_entity_financial_a...
[civicrm-core.git] / CRM / Contribute / PseudoConstant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
32 */
33
34 /**
35 * This class holds all the Pseudo constants that are specific to Contributions.
36 *
37 * This avoids polluting the core class and isolates the mass mailer class.
38 */
39 class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant {
40
41 /**
42 * Financial types
43 * @var array
44 */
45 private static $financialType;
46
47 /**
48 * Financial types
49 * @var array
50 */
51 private static $financialAccount;
52
53 /**
54 * Contribution pages
55 * @var array
56 */
57 private static $contributionPageActive = NULL;
58
59 /**
60 * Contribution pages
61 * @var array
62 */
63 private static $contributionPageAll = NULL;
64
65 /**
66 * Payment instruments
67 *
68 * @var array
69 */
70 private static $paymentInstrument;
71
72 /**
73 * Contribution status
74 *
75 * @var array
76 */
77 private static $contributionStatus;
78
79 /**
80 * Personal campaign pages
81 * @var array
82 */
83 private static $pcPage;
84
85 /**
86 * Status of personal campaign page
87 * @var array
88 */
89 private static $pcpStatus;
90
91 /**
92 * Contribution / financial batches
93 * @var array
94 */
95 private static $batch;
96
97 /**
98 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
99 *
100 * Get all the financial types
101 *
102 *
103 * @param int $id
104 *
105 * @return array
106 * array reference of all financial types if any
107 */
108 public static function &financialType($id = NULL) {
109 if (!self::$financialType) {
110 $condition = " is_active = 1 ";
111 CRM_Core_PseudoConstant::populate(
112 self::$financialType,
113 'CRM_Financial_DAO_FinancialType',
114 TRUE,
115 'name',
116 NULL,
117 $condition
118 );
119 }
120
121 if ($id) {
122 $result = CRM_Utils_Array::value($id, self::$financialType);
123 return $result;
124 }
125 return self::$financialType;
126 }
127
128 /**
129 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
130 *
131 * Get all the financial Accounts
132 *
133 *
134 * @param int $id
135 * @param int $financialAccountTypeId
136 * @param string $retrieveColumn
137 * @param string $key
138 *
139 * @return array
140 * array reference of all financial accounts if any
141 */
142 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
143 $condition = NULL;
144 if ($financialAccountTypeId) {
145 $condition = " financial_account_type_id = " . $financialAccountTypeId;
146 }
147 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
148 if (!isset(self::$financialAccount[$cacheKey])) {
149 CRM_Core_PseudoConstant::populate(
150 self::$financialAccount[$cacheKey],
151 'CRM_Financial_DAO_FinancialAccount',
152 TRUE,
153 $retrieveColumn,
154 'is_active',
155 $condition,
156 NULL,
157 $key
158 );
159
160 }
161 if ($id) {
162 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
163 return $result;
164 }
165 return self::$financialAccount[$cacheKey];
166 }
167
168 /**
169 * Flush given pseudoconstant so it can be reread from db
170 * nex time it's requested.
171 *
172 *
173 * @param bool|string $name pseudoconstant to be flushed
174 */
175 public static function flush($name = 'cache') {
176 if (isset(self::$$name)) {
177 self::$$name = NULL;
178 }
179 }
180
181 /**
182 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
183 *
184 * Get all the contribution pages
185 *
186 * @param int $id
187 * Id of the contribution page.
188 * @param bool $all
189 * Do we want all pages or only active pages.
190 *
191 *
192 * @return array
193 * array reference of all contribution pages if any
194 */
195 public static function &contributionPage($id = NULL, $all = FALSE) {
196 if ($all) {
197 $cacheVarToUse = &self::$contributionPageAll;
198 }
199 else {
200 $cacheVarToUse = &self::$contributionPageActive;
201 }
202
203 if (!$cacheVarToUse) {
204 CRM_Core_PseudoConstant::populate($cacheVarToUse,
205 'CRM_Contribute_DAO_ContributionPage',
206 $all, 'title'
207 );
208 }
209 if ($id) {
210 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
211 return $pageTitle;
212 }
213 return $cacheVarToUse;
214 }
215
216 /**
217 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
218 *
219 * Get all the payment instruments
220 *
221 *
222 * @param string $columnName
223 *
224 * @return array
225 * array reference of all payment instruments if any
226 */
227 public static function &paymentInstrument($columnName = 'label') {
228 if (!isset(self::$paymentInstrument[$columnName])) {
229 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
230 FALSE, FALSE, FALSE, NULL, $columnName
231 );
232 }
233
234 return self::$paymentInstrument[$columnName];
235 }
236
237 /**
238 * Get all the valid accepted credit cards.
239 *
240 *
241 * @return array
242 * array reference of all payment instruments if any
243 */
244 public static function &creditCard() {
245 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
246 }
247
248 /**
249 * Get all premiums.
250 *
251 *
252 * @param int $pageID
253 * @return array
254 * array of all Premiums if any
255 */
256 public static function products($pageID = NULL) {
257 $products = array();
258 $dao = new CRM_Contribute_DAO_Product();
259 $dao->is_active = 1;
260 $dao->orderBy('id');
261 $dao->find();
262
263 while ($dao->fetch()) {
264 $products[$dao->id] = $dao->name;
265 }
266 if ($pageID) {
267 $dao = new CRM_Contribute_DAO_Premium();
268 $dao->entity_table = 'civicrm_contribution_page';
269 $dao->entity_id = $pageID;
270 $dao->find(TRUE);
271 $premiumID = $dao->id;
272
273 $productID = array();
274
275 $dao = new CRM_Contribute_DAO_PremiumsProduct();
276 $dao->premiums_id = $premiumID;
277 $dao->find();
278 while ($dao->fetch()) {
279 $productID[$dao->product_id] = $dao->product_id;
280 }
281
282 $tempProduct = array();
283 foreach ($products as $key => $value) {
284 if (!array_key_exists($key, $productID)) {
285 $tempProduct[$key] = $value;
286 }
287 }
288
289 return $tempProduct;
290 }
291
292 return $products;
293 }
294
295 /**
296 * Get all the contribution statuses.
297 *
298 *
299 * @param int $id
300 * @param string $columnName
301 * @return array
302 * array reference of all contribution statuses
303 */
304 public static function &contributionStatus($id = NULL, $columnName = 'label') {
305 $cacheKey = $columnName;
306 if (!isset(self::$contributionStatus[$cacheKey])) {
307 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
308 FALSE, FALSE, FALSE, NULL, $columnName
309 );
310 }
311 $result = self::$contributionStatus[$cacheKey];
312 if ($id) {
313 $result = CRM_Utils_Array::value($id, $result);
314 }
315
316 return $result;
317 }
318
319 /**
320 * Get all the Personal campaign pages.
321 *
322 *
323 * @param null $pageType
324 * @param int $id
325 *
326 * @return array
327 * array reference of all pcp if any
328 */
329 public static function &pcPage($pageType = NULL, $id = NULL) {
330 if (!isset(self::$pcPage[$pageType])) {
331 if ($pageType) {
332 $params = "page_type='{$pageType}'";
333 }
334 else {
335 $params = '';
336 }
337 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
338 'CRM_PCP_DAO_PCP',
339 FALSE, 'title', 'is_active', $params
340 );
341 }
342 $result = self::$pcPage[$pageType];
343 if ($id) {
344 return $result = CRM_Utils_Array::value($id, $result);
345 }
346
347 return $result;
348 }
349
350 /**
351 * Get all PCP Statuses.
352 *
353 * The static array pcpStatus is returned
354 *
355 *
356 * @param string $column
357 * @return array
358 * array reference of all PCP activity statuses
359 */
360 public static function &pcpStatus($column = 'label') {
361 if (NULL === self::$pcpStatus) {
362 self::$pcpStatus = array();
363 }
364 if (!array_key_exists($column, self::$pcpStatus)) {
365 self::$pcpStatus[$column] = array();
366
367 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
368 FALSE, FALSE, NULL, $column
369 );
370 }
371 return self::$pcpStatus[$column];
372 }
373
374 /**
375 * Get financial account for a Financial type.
376 *
377 *
378 * @param int $entityId
379 * @param string $accountRelationType
380 * @param string $entityTable
381 * @return int
382 */
383 public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type') {
384 $result = civicrm_api3('EntityFinancialAccount', 'get', array(
385 'return' => array("financial_account_id"),
386 'account_relationship.name' => $accountRelationType,
387 'entity_table' => $entityTable,
388 'entity_id' => $entityId,
389 ));
390 if (!$result['count']) {
391 return NULL;
392 }
393 return $result['values'][$result['id']]['financial_account_id'];
394 }
395
396 /**
397 * Get all batches.
398 *
399 *
400 * @param int $id
401 * @return array
402 * array reference of all batches if any
403 */
404 public static function &batch($id = NULL) {
405 if (!self::$batch) {
406 $orderBy = " id DESC ";
407 CRM_Core_PseudoConstant::populate(
408 self::$batch,
409 'CRM_Batch_DAO_Batch',
410 TRUE,
411 'title',
412 NULL,
413 NULL,
414 $orderBy
415 );
416 }
417
418 if ($id) {
419 $result = CRM_Utils_Array::value($id, self::$batch);
420 return $result;
421 }
422 return self::$batch;
423 }
424
425 }