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