Change comments to tags so phpstorm picks up the deprecated functions
[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 *
101 * Get all the financial types
102 *
103 *
104 * @param int $id
105 *
106 * @return array
107 * array reference of all financial types if any
108 */
109 public static function &financialType($id = NULL) {
110 if (!self::$financialType) {
111 $condition = " is_active = 1 ";
112 CRM_Core_PseudoConstant::populate(
113 self::$financialType,
114 'CRM_Financial_DAO_FinancialType',
115 TRUE,
116 'name',
117 NULL,
118 $condition
119 );
120 }
121
122 if ($id) {
123 $result = CRM_Utils_Array::value($id, self::$financialType);
124 return $result;
125 }
126 return self::$financialType;
127 }
128
129 /**
130 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
131 *
132 * Get all the financial Accounts
133 *
134 *
135 * @param int $id
136 * @param int $financialAccountTypeId
137 * @param string $retrieveColumn
138 * @param string $key
139 *
140 * @return array
141 * array reference of all financial accounts if any
142 */
143 public static function &financialAccount($id = NULL, $financialAccountTypeId = NULL, $retrieveColumn = 'name', $key = 'id') {
144 $condition = NULL;
145 if ($financialAccountTypeId) {
146 $condition = " financial_account_type_id = " . $financialAccountTypeId;
147 }
148 $cacheKey = "{$id}_{$financialAccountTypeId}_{$retrieveColumn}_{$key}";
149 if (!isset(self::$financialAccount[$cacheKey])) {
150 CRM_Core_PseudoConstant::populate(
151 self::$financialAccount[$cacheKey],
152 'CRM_Financial_DAO_FinancialAccount',
153 TRUE,
154 $retrieveColumn,
155 'is_active',
156 $condition,
157 NULL,
158 $key
159 );
160
161 }
162 if ($id) {
163 $result = CRM_Utils_Array::value($id, self::$financialAccount[$cacheKey]);
164 return $result;
165 }
166 return self::$financialAccount[$cacheKey];
167 }
168
169 /**
170 * Flush given pseudoconstant so it can be reread from db
171 * nex time it's requested.
172 *
173 *
174 * @param bool|string $name pseudoconstant to be flushed
175 */
176 public static function flush($name = 'cache') {
177 if (isset(self::$$name)) {
178 self::$$name = NULL;
179 }
180 }
181
182 /**
183 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
184 *
185 * Get all the contribution pages
186 *
187 * @param int $id
188 * Id of the contribution page.
189 * @param bool $all
190 * Do we want all pages or only active pages.
191 *
192 *
193 * @return array
194 * array reference of all contribution pages if any
195 */
196 public static function &contributionPage($id = NULL, $all = FALSE) {
197 if ($all) {
198 $cacheVarToUse = &self::$contributionPageAll;
199 }
200 else {
201 $cacheVarToUse = &self::$contributionPageActive;
202 }
203
204 if (!$cacheVarToUse) {
205 CRM_Core_PseudoConstant::populate($cacheVarToUse,
206 'CRM_Contribute_DAO_ContributionPage',
207 $all, 'title'
208 );
209 }
210 if ($id) {
211 $pageTitle = CRM_Utils_Array::value($id, $cacheVarToUse);
212 return $pageTitle;
213 }
214 return $cacheVarToUse;
215 }
216
217 /**
218 * @deprecated. Please use the buildOptions() method in the appropriate BAO object.
219 *
220 * Get all the payment instruments
221 *
222 *
223 * @param string $columnName
224 *
225 * @return array
226 * array reference of all payment instruments if any
227 */
228 public static function &paymentInstrument($columnName = 'label') {
229 if (!isset(self::$paymentInstrument[$columnName])) {
230 self::$paymentInstrument[$columnName] = CRM_Core_OptionGroup::values('payment_instrument',
231 FALSE, FALSE, FALSE, NULL, $columnName
232 );
233 }
234
235 return self::$paymentInstrument[$columnName];
236 }
237
238 /**
239 * Get all the valid accepted credit cards.
240 *
241 *
242 * @return array
243 * array reference of all payment instruments if any
244 */
245 public static function &creditCard() {
246 return CRM_Core_OptionGroup::values('accept_creditcard', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
247 }
248
249 /**
250 * Get all premiums.
251 *
252 *
253 * @param int $pageID
254 * @return array
255 * array of all Premiums if any
256 */
257 public static function products($pageID = NULL) {
258 $products = array();
259 $dao = new CRM_Contribute_DAO_Product();
260 $dao->is_active = 1;
261 $dao->orderBy('id');
262 $dao->find();
263
264 while ($dao->fetch()) {
265 $products[$dao->id] = $dao->name;
266 }
267 if ($pageID) {
268 $dao = new CRM_Contribute_DAO_Premium();
269 $dao->entity_table = 'civicrm_contribution_page';
270 $dao->entity_id = $pageID;
271 $dao->find(TRUE);
272 $premiumID = $dao->id;
273
274 $productID = array();
275
276 $dao = new CRM_Contribute_DAO_PremiumsProduct();
277 $dao->premiums_id = $premiumID;
278 $dao->find();
279 while ($dao->fetch()) {
280 $productID[$dao->product_id] = $dao->product_id;
281 }
282
283 $tempProduct = array();
284 foreach ($products as $key => $value) {
285 if (!array_key_exists($key, $productID)) {
286 $tempProduct[$key] = $value;
287 }
288 }
289
290 return $tempProduct;
291 }
292
293 return $products;
294 }
295
296 /**
297 * Get all the contribution statuses.
298 *
299 *
300 * @param int $id
301 * @param string $columnName
302 * @return array
303 * array reference of all contribution statuses
304 */
305 public static function &contributionStatus($id = NULL, $columnName = 'label') {
306 $cacheKey = $columnName;
307 if (!isset(self::$contributionStatus[$cacheKey])) {
308 self::$contributionStatus[$cacheKey] = CRM_Core_OptionGroup::values('contribution_status',
309 FALSE, FALSE, FALSE, NULL, $columnName
310 );
311 }
312 $result = self::$contributionStatus[$cacheKey];
313 if ($id) {
314 $result = CRM_Utils_Array::value($id, $result);
315 }
316
317 return $result;
318 }
319
320 /**
321 * Get all the Personal campaign pages.
322 *
323 *
324 * @param null $pageType
325 * @param int $id
326 *
327 * @return array
328 * array reference of all pcp if any
329 */
330 public static function &pcPage($pageType = NULL, $id = NULL) {
331 if (!isset(self::$pcPage[$pageType])) {
332 if ($pageType) {
333 $params = "page_type='{$pageType}'";
334 }
335 else {
336 $params = '';
337 }
338 CRM_Core_PseudoConstant::populate(self::$pcPage[$pageType],
339 'CRM_PCP_DAO_PCP',
340 FALSE, 'title', 'is_active', $params
341 );
342 }
343 $result = self::$pcPage[$pageType];
344 if ($id) {
345 return $result = CRM_Utils_Array::value($id, $result);
346 }
347
348 return $result;
349 }
350
351 /**
352 * Get all PCP Statuses.
353 *
354 * The static array pcpStatus is returned
355 *
356 *
357 * @param string $column
358 * @return array
359 * array reference of all PCP activity statuses
360 */
361 public static function &pcpStatus($column = 'label') {
362 if (NULL === self::$pcpStatus) {
363 self::$pcpStatus = array();
364 }
365 if (!array_key_exists($column, self::$pcpStatus)) {
366 self::$pcpStatus[$column] = array();
367
368 self::$pcpStatus[$column] = CRM_Core_OptionGroup::values('pcp_status', FALSE,
369 FALSE, FALSE, NULL, $column
370 );
371 }
372 return self::$pcpStatus[$column];
373 }
374
375 /**
376 * Get financial account for a Financial type.
377 *
378 *
379 * @param int $entityId
380 * @param string $accountRelationType
381 * @param string $entityTable
382 * @param string $returnField
383 * @return int
384 */
385 public static function getRelationalFinancialAccount($entityId, $accountRelationType, $entityTable = 'civicrm_financial_type', $returnField = 'financial_account_id') {
386 $params = array(
387 'return' => array($returnField),
388 'entity_table' => $entityTable,
389 'entity_id' => $entityId,
390 );
391 if ($accountRelationType) {
392 $params['account_relationship.name'] = $accountRelationType;
393 }
394 $result = civicrm_api3('EntityFinancialAccount', 'get', $params);
395 if (!$result['count']) {
396 return NULL;
397 }
398 return $result['values'][$result['id']][$returnField];
399 }
400
401 /**
402 * Get all batches.
403 *
404 *
405 * @param int $id
406 * @return array
407 * array reference of all batches if any
408 */
409 public static function &batch($id = NULL) {
410 if (!self::$batch) {
411 $orderBy = " id DESC ";
412 CRM_Core_PseudoConstant::populate(
413 self::$batch,
414 'CRM_Batch_DAO_Batch',
415 TRUE,
416 'title',
417 NULL,
418 NULL,
419 $orderBy
420 );
421 }
422
423 if ($id) {
424 $result = CRM_Utils_Array::value($id, self::$batch);
425 return $result;
426 }
427 return self::$batch;
428 }
429
430 }