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