Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[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 * $Id$
17 *
18 */
19
20/**
21 *
22 */
23class 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();
fc944198 41 $dao->copyValues($params);
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) {
9c1bc317
CW
56 $module = $params['module'] ?? NULL;
57 $entityTable = $params['entity_table'] ?? NULL;
58 $entityID = $params['entity_id'] ?? NULL;
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'])) {
2cfe2cf4 93 $dao->module = $params['module'];
6a488035 94 }
9c1bc317
CW
95 $dao->entity_table = $params['entity_table'] ?? NULL;
96 $dao->entity_id = $params['entity_id'] ?? NULL;
6a488035
TO
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 }
9c1bc317 104 $dao->weight = $params['weight'] ?? NULL;
6a488035
TO
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
9c1bc317
CW
125 $dao->entity_table = $params['entity_table'] ?? NULL;
126 $dao->entity_id = $params['entity_id'] ?? NULL;
127 $dao->weight = $params['weight'] ?? NULL;
128 $dao->module = $params['module'] ?? NULL;
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'])) {
2cfe2cf4 146 $dao->module = $params['module'];
6a488035 147 }
9c1bc317
CW
148 $dao->entity_table = $params['entity_table'] ?? NULL;
149 $dao->entity_id = $params['entity_id'] ?? NULL;
6a488035
TO
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}