8c5b05605537d34fb4a90a96b9143bc7aa5a6227
[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 (reference) an assoc array of name/value pairs
45 *
46 * @return CRM_Core_DAO_UFJoin object
47 * @static
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();
57 $dao->copyValues($params);
58 if ($params['uf_group_id']) {
59 $dao->save();
60 }
61 else {
62 $dao->delete();
63 }
64
65 return $dao;
66 }
67
68 /**
69 * @param array $params
70 */
71 public static function deleteAll(&$params) {
72 $module = CRM_Utils_Array::value('module', $params);
73 $entityTable = CRM_Utils_Array::value('entity_table', $params);
74 $entityID = CRM_Utils_Array::value('entity_id', $params);
75
76 if (empty($entityTable) ||
77 empty($entityID) ||
78 empty($module)
79 ) {
80 return;
81 }
82
83 $dao = new CRM_Core_DAO_UFJoin();
84 $dao->module = $module;
85 $dao->entity_table = $entityTable;
86 $dao->entity_id = $entityID;
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 *
94 * @param array $params (reference) an assoc array of name/value pairs
95 *
96 * @return int or null
97 * @static
98 */
99 public static function findJoinEntryId(&$params) {
100 if (!empty($params['id'])) {
101 return $params['id'];
102 }
103
104 $dao = new CRM_Core_DAO_UFJoin();
105
106 // CRM-4377 (ab)uses the module column
107 if (isset($params['module'])) {
108 $dao->module = CRM_Utils_Array::value('module', $params);
109 }
110 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
111 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
112 // user reg / my account can have multiple entries, so we return if thats
113 // the case. (since entity_table/id is empty in those cases
114 if (!$dao->entity_table ||
115 !$dao->entity_id
116 ) {
117 return NULL;
118 }
119 $dao->weight = CRM_Utils_Array::value('weight', $params);
120 if ($dao->find(TRUE)) {
121 return $dao->id;
122 }
123 return NULL;
124 }
125
126 /**
127 * Given an assoc list of params, find if there is a record
128 * for this set of params and return the group id
129 *
130 * @param array $params (reference) an assoc array of name/value pairs
131 *
132 * @return int or null
133 * @static
134 */
135 public static function findUFGroupId(&$params) {
136
137 $dao = new CRM_Core_DAO_UFJoin();
138
139 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
140 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
141 $dao->weight = CRM_Utils_Array::value('weight', $params);
142 if ($dao->find(TRUE)) {
143 return $dao->uf_group_id;
144 }
145 return NULL;
146 }
147
148 /**
149 * @param array $params
150 *
151 * @return array
152 */
153 public static function getUFGroupIds(&$params) {
154
155 $dao = new CRM_Core_DAO_UFJoin();
156
157 // CRM-4377 (ab)uses the module column
158 if (isset($params['module'])) {
159 $dao->module = CRM_Utils_Array::value('module', $params);
160 }
161 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
162 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
163 $dao->orderBy('weight asc');
164 $dao->find();
165 $first = $firstActive = NULL;
166 $second = $secondActive = array();
167
168 while ($dao->fetch()) {
169 if ($dao->weight == 1) {
170 $first = $dao->uf_group_id;
171 $firstActive = $dao->is_active;
172 }
173 else {
174 $second[] = $dao->uf_group_id;
175 $secondActive[] = $dao->is_active;
176 }
177 }
178 return array($first, $second, $firstActive, $secondActive);
179 }
180 }