Merge pull request #23924 from colemanw/afformPopup
[civicrm-core.git] / CRM / Core / BAO / UFJoin.php
CommitLineData
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 */
17
18/**
19 *
20 */
21class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin {
22
23 /**
fe482240 24 * Takes an associative array and creates a uf join object.
6a488035 25 *
6a0b768e
TO
26 * @param array $params
27 * (reference) an assoc array of name/value pairs.
6a488035 28 *
16b10e64 29 * @return CRM_Core_DAO_UFJoin
6a488035
TO
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();
fc944198 39 $dao->copyValues($params);
6a488035
TO
40 if ($params['uf_group_id']) {
41 $dao->save();
42 }
43 else {
44 $dao->delete();
45 }
46
47 return $dao;
48 }
49
b5c2afd0 50 /**
c490a46a 51 * @param array $params
b5c2afd0 52 */
6a488035 53 public static function deleteAll(&$params) {
9c1bc317
CW
54 $module = $params['module'] ?? NULL;
55 $entityTable = $params['entity_table'] ?? NULL;
56 $entityID = $params['entity_id'] ?? NULL;
6a488035
TO
57
58 if (empty($entityTable) ||
59 empty($entityID) ||
60 empty($module)
61 ) {
62 return;
63 }
64
353ffa53
TO
65 $dao = new CRM_Core_DAO_UFJoin();
66 $dao->module = $module;
6a488035 67 $dao->entity_table = $entityTable;
353ffa53 68 $dao->entity_id = $entityID;
6a488035
TO
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 *
6a0b768e
TO
76 * @param array $params
77 * (reference) an assoc array of name/value pairs.
6a488035 78 *
a6c01b45
CW
79 * @return int
80 * or null
6a488035
TO
81 */
82 public static function findJoinEntryId(&$params) {
a7488080 83 if (!empty($params['id'])) {
6a488035
TO
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'])) {
2cfe2cf4 91 $dao->module = $params['module'];
6a488035 92 }
9c1bc317
CW
93 $dao->entity_table = $params['entity_table'] ?? NULL;
94 $dao->entity_id = $params['entity_id'] ?? NULL;
6a488035
TO
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 }
9c1bc317 102 $dao->weight = $params['weight'] ?? NULL;
6a488035
TO
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 *
6a0b768e
TO
113 * @param array $params
114 * (reference) an assoc array of name/value pairs.
6a488035 115 *
a6c01b45
CW
116 * @return int
117 * or null
6a488035
TO
118 */
119 public static function findUFGroupId(&$params) {
120
121 $dao = new CRM_Core_DAO_UFJoin();
122
9c1bc317
CW
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;
6a488035
TO
127 if ($dao->find(TRUE)) {
128 return $dao->uf_group_id;
129 }
130 return NULL;
131 }
132
b5c2afd0 133 /**
c490a46a 134 * @param array $params
b5c2afd0
EM
135 *
136 * @return array
137 */
6a488035
TO
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'])) {
2cfe2cf4 144 $dao->module = $params['module'];
6a488035 145 }
9c1bc317
CW
146 $dao->entity_table = $params['entity_table'] ?? NULL;
147 $dao->entity_id = $params['entity_id'] ?? NULL;
6a488035
TO
148 $dao->orderBy('weight asc');
149 $dao->find();
150 $first = $firstActive = NULL;
be2fb01f 151 $second = $secondActive = [];
6a488035
TO
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 }
be2fb01f 163 return [$first, $second, $firstActive, $secondActive];
6a488035 164 }
96025800 165
583bd2a5
SL
166 /**
167 * Whitelist of possible values for the entity_table field
168 * @return array
169 */
170 public static function entityTables() {
be2fb01f 171 return [
466fce54
CW
172 'civicrm_event' => 'Event',
173 'civicrm_contribution_page' => 'ContributionPage',
174 'civicrm_survey' => 'Survey',
be2fb01f 175 ];
583bd2a5
SL
176 }
177
d291b369
CW
178 /**
179 * Override base method which assumes permissions should be based on entity_table.
180 *
181 * @return array
182 */
183 public function addSelectWhereClause() {
184 $clauses = [];
185 CRM_Utils_Hook::selectWhereClause($this, $clauses);
186 return $clauses;
187 }
188
6a488035 189}