INFRA-132 - Comment grammar cleanup
[civicrm-core.git] / CRM / Core / BAO / UFJoin.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin {
40
41 /**
100fef9d 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 *
c490a46a 47 * @return CRM_Core_DAO_UFJoin object
6a488035
TO
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
b5c2afd0 69 /**
c490a46a 70 * @param array $params
b5c2afd0 71 */
6a488035
TO
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 *
6a0b768e
TO
95 * @param array $params
96 * (reference) an assoc array of name/value pairs.
6a488035
TO
97 *
98 * @return int or null
6a488035
TO
99 * @static
100 */
101 public static function findJoinEntryId(&$params) {
a7488080 102 if (!empty($params['id'])) {
6a488035
TO
103 return $params['id'];
104 }
105
106 $dao = new CRM_Core_DAO_UFJoin();
107
108 // CRM-4377 (ab)uses the module column
109 if (isset($params['module'])) {
110 $dao->module = CRM_Utils_Array::value('module', $params);
111 }
112 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
113 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
114 // user reg / my account can have multiple entries, so we return if thats
115 // the case. (since entity_table/id is empty in those cases
116 if (!$dao->entity_table ||
117 !$dao->entity_id
118 ) {
119 return NULL;
120 }
121 $dao->weight = CRM_Utils_Array::value('weight', $params);
122 if ($dao->find(TRUE)) {
123 return $dao->id;
124 }
125 return NULL;
126 }
127
128 /**
129 * Given an assoc list of params, find if there is a record
130 * for this set of params and return the group id
131 *
6a0b768e
TO
132 * @param array $params
133 * (reference) an assoc array of name/value pairs.
6a488035
TO
134 *
135 * @return int or null
6a488035
TO
136 * @static
137 */
138 public static function findUFGroupId(&$params) {
139
140 $dao = new CRM_Core_DAO_UFJoin();
141
142 $dao->entity_table = CRM_Utils_Array::value('entity_table', $params);
143 $dao->entity_id = CRM_Utils_Array::value('entity_id', $params);
144 $dao->weight = CRM_Utils_Array::value('weight', $params);
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 }
183}