Merge pull request #13993 from eileenmcnaughton/recur_fixes
[civicrm-core.git] / CRM / Core / BAO / Persistent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_Persistent extends CRM_Core_DAO_Persistent {
36
37 /**
38 * Fetch object based on array of properties.
39 *
40 * @param array $params
41 * (reference ) an assoc array of name/value pairs.
42 * @param array $defaults
43 * (reference ) an assoc array to hold the flattened values.
44 *
45 * @return CRM_Core_BAO_Persistent
46 */
47 public static function retrieve(&$params, &$defaults) {
48 $dao = new CRM_Core_DAO_Persistent();
49 $dao->copyValues($params);
50
51 if ($dao->find(TRUE)) {
52 CRM_Core_DAO::storeValues($dao, $defaults);
53 if (CRM_Utils_Array::value('is_config', $defaults) == 1) {
54 $defaults['data'] = unserialize($defaults['data']);
55 }
56 return $dao;
57 }
58 return NULL;
59 }
60
61 /**
62 * Add the Persistent Record.
63 *
64 * @param array $params
65 * Reference array contains the values submitted by the form.
66 * @param array $ids
67 * Reference array contains the id.
68 *
69 *
70 * @return object
71 */
72 public static function add(&$params, &$ids) {
73 if (CRM_Utils_Array::value('is_config', $params) == 1) {
74 $params['data'] = serialize(explode(',', $params['data']));
75 }
76 $persistentDAO = new CRM_Core_DAO_Persistent();
77 $persistentDAO->copyValues($params);
78
79 $persistentDAO->id = CRM_Utils_Array::value('persistent', $ids);
80 $persistentDAO->save();
81 return $persistentDAO;
82 }
83
84 /**
85 * @param $context
86 * @param null $name
87 *
88 * @return mixed
89 */
90 public static function getContext($context, $name = NULL) {
91 static $contextNameData = [];
92
93 if (!array_key_exists($context, $contextNameData)) {
94 $contextNameData[$context] = [];
95 $persisntentDAO = new CRM_Core_DAO_Persistent();
96 $persisntentDAO->context = $context;
97 $persisntentDAO->find();
98
99 while ($persisntentDAO->fetch()) {
100 $contextNameData[$context][$persisntentDAO->name] = $persisntentDAO->is_config == 1 ? unserialize($persisntentDAO->data) : $persisntentDAO->data;
101 }
102 }
103 if (empty($name)) {
104 return $contextNameData[$context];
105 }
106 else {
107 return CRM_Utils_Array::value($name, $contextNameData[$context]);
108 }
109 }
110
111 }