dev/core#35 Avoid variable leakage on recurring contribution receipts.
[civicrm-core.git] / CRM / Core / BAO / UFJoin.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin {
40
41 /**
fe482240 42 * Takes an associative array and creates a uf join object.
6a488035 43 *
6a0b768e
TO
44 * @param array $params
45 * (reference) an assoc array of name/value pairs.
6a488035 46 *
16b10e64 47 * @return CRM_Core_DAO_UFJoin
6a488035
TO
48 */
49 public static function &create($params) {
50 // see if a record exists with the same weight
51 $id = self::findJoinEntryId($params);
52 if ($id) {
53 $params['id'] = $id;
54 }
55
56 $dao = new CRM_Core_DAO_UFJoin();
5d6cf648 57 $dao->copyValues($params, TRUE);
6a488035
TO
58 if ($params['uf_group_id']) {
59 $dao->save();
60 }
61 else {
62 $dao->delete();
63 }
64
65 return $dao;
66 }
67
b5c2afd0 68 /**
c490a46a 69 * @param array $params
b5c2afd0 70 */
6a488035 71 public static function deleteAll(&$params) {
353ffa53 72 $module = CRM_Utils_Array::value('module', $params);
6a488035 73 $entityTable = CRM_Utils_Array::value('entity_table', $params);
353ffa53 74 $entityID = CRM_Utils_Array::value('entity_id', $params);
6a488035
TO
75
76 if (empty($entityTable) ||
77 empty($entityID) ||
78 empty($module)
79 ) {
80 return;
81 }
82
353ffa53
TO
83 $dao = new CRM_Core_DAO_UFJoin();
84 $dao->module = $module;
6a488035 85 $dao->entity_table = $entityTable;
353ffa53 86 $dao->entity_id = $entityID;
6a488035
TO
87 $dao->delete();
88 }
89
90 /**
91 * Given an assoc list of params, find if there is a record
92 * for this set of params
93 *
6a0b768e
TO
94 * @param array $params
95 * (reference) an assoc array of name/value pairs.
6a488035 96 *
a6c01b45
CW
97 * @return int
98 * or null
6a488035
TO
99 */
100 public static function findJoinEntryId(&$params) {
a7488080 101 if (!empty($params['id'])) {
6a488035
TO
102 return $params['id'];
103 }
104
105 $dao = new CRM_Core_DAO_UFJoin();
106
107 // CRM-4377 (ab)uses the module column
108 if (isset($params['module'])) {
109 $dao->module = CRM_Utils_Array::value('module', $params);
110 }
111 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
112 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
113 // user reg / my account can have multiple entries, so we return if thats
114 // the case. (since entity_table/id is empty in those cases
115 if (!$dao->entity_table ||
116 !$dao->entity_id
117 ) {
118 return NULL;
119 }
120 $dao->weight = CRM_Utils_Array::value('weight', $params);
121 if ($dao->find(TRUE)) {
122 return $dao->id;
123 }
124 return NULL;
125 }
126
127 /**
128 * Given an assoc list of params, find if there is a record
129 * for this set of params and return the group id
130 *
6a0b768e
TO
131 * @param array $params
132 * (reference) an assoc array of name/value pairs.
6a488035 133 *
a6c01b45
CW
134 * @return int
135 * or null
6a488035
TO
136 */
137 public static function findUFGroupId(&$params) {
138
139 $dao = new CRM_Core_DAO_UFJoin();
140
141 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
353ffa53
TO
142 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
143 $dao->weight = CRM_Utils_Array::value('weight', $params);
ffc28441 144 $dao->module = CRM_Utils_Array::value('module', $params);
6a488035
TO
145 if ($dao->find(TRUE)) {
146 return $dao->uf_group_id;
147 }
148 return NULL;
149 }
150
b5c2afd0 151 /**
c490a46a 152 * @param array $params
b5c2afd0
EM
153 *
154 * @return array
155 */
6a488035
TO
156 public static function getUFGroupIds(&$params) {
157
158 $dao = new CRM_Core_DAO_UFJoin();
159
160 // CRM-4377 (ab)uses the module column
161 if (isset($params['module'])) {
162 $dao->module = CRM_Utils_Array::value('module', $params);
163 }
164 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
165 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
166 $dao->orderBy('weight asc');
167 $dao->find();
168 $first = $firstActive = NULL;
169 $second = $secondActive = array();
170
171 while ($dao->fetch()) {
172 if ($dao->weight == 1) {
173 $first = $dao->uf_group_id;
174 $firstActive = $dao->is_active;
175 }
176 else {
177 $second[] = $dao->uf_group_id;
178 $secondActive[] = $dao->is_active;
179 }
180 }
181 return array($first, $second, $firstActive, $secondActive);
182 }
96025800 183
583bd2a5
SL
184 /**
185 * Whitelist of possible values for the entity_table field
186 * @return array
187 */
188 public static function entityTables() {
466fce54
CW
189 return array(
190 'civicrm_event' => 'Event',
191 'civicrm_contribution_page' => 'ContributionPage',
192 'civicrm_survey' => 'Survey',
583bd2a5 193 );
583bd2a5
SL
194 }
195
6a488035 196}