Merge pull request #16837 from tunbola/case-api-case-clients
[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 * $Id$
17 *
18 */
19
20 /**
21 *
22 */
23 class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin {
24
25 /**
26 * Takes an associative array and creates a uf join object.
27 *
28 * @param array $params
29 * (reference) an assoc array of name/value pairs.
30 *
31 * @return CRM_Core_DAO_UFJoin
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();
41 $dao->copyValues($params);
42 if ($params['uf_group_id']) {
43 $dao->save();
44 }
45 else {
46 $dao->delete();
47 }
48
49 return $dao;
50 }
51
52 /**
53 * @param array $params
54 */
55 public static function deleteAll(&$params) {
56 $module = $params['module'] ?? NULL;
57 $entityTable = $params['entity_table'] ?? NULL;
58 $entityID = $params['entity_id'] ?? NULL;
59
60 if (empty($entityTable) ||
61 empty($entityID) ||
62 empty($module)
63 ) {
64 return;
65 }
66
67 $dao = new CRM_Core_DAO_UFJoin();
68 $dao->module = $module;
69 $dao->entity_table = $entityTable;
70 $dao->entity_id = $entityID;
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 *
78 * @param array $params
79 * (reference) an assoc array of name/value pairs.
80 *
81 * @return int
82 * or null
83 */
84 public static function findJoinEntryId(&$params) {
85 if (!empty($params['id'])) {
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 = $params['module'] ?? NULL;
94 }
95 $dao->entity_table = $params['entity_table'] ?? NULL;
96 $dao->entity_id = $params['entity_id'] ?? NULL;
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 = $params['weight'] ?? NULL;
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 *
115 * @param array $params
116 * (reference) an assoc array of name/value pairs.
117 *
118 * @return int
119 * or null
120 */
121 public static function findUFGroupId(&$params) {
122
123 $dao = new CRM_Core_DAO_UFJoin();
124
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;
129 if ($dao->find(TRUE)) {
130 return $dao->uf_group_id;
131 }
132 return NULL;
133 }
134
135 /**
136 * @param array $params
137 *
138 * @return array
139 */
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 = $params['module'] ?? NULL;
147 }
148 $dao->entity_table = $params['entity_table'] ?? NULL;
149 $dao->entity_id = $params['entity_id'] ?? NULL;
150 $dao->orderBy('weight asc');
151 $dao->find();
152 $first = $firstActive = NULL;
153 $second = $secondActive = [];
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 }
165 return [$first, $second, $firstActive, $secondActive];
166 }
167
168 /**
169 * Whitelist of possible values for the entity_table field
170 * @return array
171 */
172 public static function entityTables() {
173 return [
174 'civicrm_event' => 'Event',
175 'civicrm_contribution_page' => 'ContributionPage',
176 'civicrm_survey' => 'Survey',
177 ];
178 }
179
180 }