Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * | |
22 | */ | |
23 | class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin { | |
24 | ||
25 | /** | |
fe482240 | 26 | * Takes an associative array and creates a uf join object. |
6a488035 | 27 | * |
6a0b768e TO |
28 | * @param array $params |
29 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 30 | * |
16b10e64 | 31 | * @return CRM_Core_DAO_UFJoin |
6a488035 TO |
32 | */ |
33 | public static function &create($params) { | |
34 | // see if a record exists with the same weight | |
35 | $id = self::findJoinEntryId($params); | |
36 | if ($id) { | |
37 | $params['id'] = $id; | |
38 | } | |
39 | ||
40 | $dao = new CRM_Core_DAO_UFJoin(); | |
5d6cf648 | 41 | $dao->copyValues($params, TRUE); |
6a488035 TO |
42 | if ($params['uf_group_id']) { |
43 | $dao->save(); | |
44 | } | |
45 | else { | |
46 | $dao->delete(); | |
47 | } | |
48 | ||
49 | return $dao; | |
50 | } | |
51 | ||
b5c2afd0 | 52 | /** |
c490a46a | 53 | * @param array $params |
b5c2afd0 | 54 | */ |
6a488035 | 55 | public static function deleteAll(&$params) { |
353ffa53 | 56 | $module = CRM_Utils_Array::value('module', $params); |
6a488035 | 57 | $entityTable = CRM_Utils_Array::value('entity_table', $params); |
353ffa53 | 58 | $entityID = CRM_Utils_Array::value('entity_id', $params); |
6a488035 TO |
59 | |
60 | if (empty($entityTable) || | |
61 | empty($entityID) || | |
62 | empty($module) | |
63 | ) { | |
64 | return; | |
65 | } | |
66 | ||
353ffa53 TO |
67 | $dao = new CRM_Core_DAO_UFJoin(); |
68 | $dao->module = $module; | |
6a488035 | 69 | $dao->entity_table = $entityTable; |
353ffa53 | 70 | $dao->entity_id = $entityID; |
6a488035 TO |
71 | $dao->delete(); |
72 | } | |
73 | ||
74 | /** | |
75 | * Given an assoc list of params, find if there is a record | |
76 | * for this set of params | |
77 | * | |
6a0b768e TO |
78 | * @param array $params |
79 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 80 | * |
a6c01b45 CW |
81 | * @return int |
82 | * or null | |
6a488035 TO |
83 | */ |
84 | public static function findJoinEntryId(&$params) { | |
a7488080 | 85 | if (!empty($params['id'])) { |
6a488035 TO |
86 | return $params['id']; |
87 | } | |
88 | ||
89 | $dao = new CRM_Core_DAO_UFJoin(); | |
90 | ||
91 | // CRM-4377 (ab)uses the module column | |
92 | if (isset($params['module'])) { | |
93 | $dao->module = CRM_Utils_Array::value('module', $params); | |
94 | } | |
95 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); | |
96 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); | |
97 | // user reg / my account can have multiple entries, so we return if thats | |
98 | // the case. (since entity_table/id is empty in those cases | |
99 | if (!$dao->entity_table || | |
100 | !$dao->entity_id | |
101 | ) { | |
102 | return NULL; | |
103 | } | |
104 | $dao->weight = CRM_Utils_Array::value('weight', $params); | |
105 | if ($dao->find(TRUE)) { | |
106 | return $dao->id; | |
107 | } | |
108 | return NULL; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Given an assoc list of params, find if there is a record | |
113 | * for this set of params and return the group id | |
114 | * | |
6a0b768e TO |
115 | * @param array $params |
116 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 117 | * |
a6c01b45 CW |
118 | * @return int |
119 | * or null | |
6a488035 TO |
120 | */ |
121 | public static function findUFGroupId(&$params) { | |
122 | ||
123 | $dao = new CRM_Core_DAO_UFJoin(); | |
124 | ||
125 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); | |
353ffa53 TO |
126 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); |
127 | $dao->weight = CRM_Utils_Array::value('weight', $params); | |
ffc28441 | 128 | $dao->module = CRM_Utils_Array::value('module', $params); |
6a488035 TO |
129 | if ($dao->find(TRUE)) { |
130 | return $dao->uf_group_id; | |
131 | } | |
132 | return NULL; | |
133 | } | |
134 | ||
b5c2afd0 | 135 | /** |
c490a46a | 136 | * @param array $params |
b5c2afd0 EM |
137 | * |
138 | * @return array | |
139 | */ | |
6a488035 TO |
140 | public static function getUFGroupIds(&$params) { |
141 | ||
142 | $dao = new CRM_Core_DAO_UFJoin(); | |
143 | ||
144 | // CRM-4377 (ab)uses the module column | |
145 | if (isset($params['module'])) { | |
146 | $dao->module = CRM_Utils_Array::value('module', $params); | |
147 | } | |
148 | $dao->entity_table = CRM_Utils_Array::value('entity_table', $params); | |
149 | $dao->entity_id = CRM_Utils_Array::value('entity_id', $params); | |
150 | $dao->orderBy('weight asc'); | |
151 | $dao->find(); | |
152 | $first = $firstActive = NULL; | |
be2fb01f | 153 | $second = $secondActive = []; |
6a488035 TO |
154 | |
155 | while ($dao->fetch()) { | |
156 | if ($dao->weight == 1) { | |
157 | $first = $dao->uf_group_id; | |
158 | $firstActive = $dao->is_active; | |
159 | } | |
160 | else { | |
161 | $second[] = $dao->uf_group_id; | |
162 | $secondActive[] = $dao->is_active; | |
163 | } | |
164 | } | |
be2fb01f | 165 | return [$first, $second, $firstActive, $secondActive]; |
6a488035 | 166 | } |
96025800 | 167 | |
583bd2a5 SL |
168 | /** |
169 | * Whitelist of possible values for the entity_table field | |
170 | * @return array | |
171 | */ | |
172 | public static function entityTables() { | |
be2fb01f | 173 | return [ |
466fce54 CW |
174 | 'civicrm_event' => 'Event', |
175 | 'civicrm_contribution_page' => 'ContributionPage', | |
176 | 'civicrm_survey' => 'Survey', | |
be2fb01f | 177 | ]; |
583bd2a5 SL |
178 | } |
179 | ||
6a488035 | 180 | } |