8b82ca352458c03e6657f8689748249212224a2a
[civicrm-core.git] / CRM / Contact / BAO / SavedSearch.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 * Business object for Saved searches
38 *
39 */
40 class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
41
42 /**
43 * Class constructor
44 *
45 * @return \CRM_Contact_BAO_SavedSearch CRM_Contact_BAO_SavedSearch
46 */
47 public function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Query the db for all saved searches.
53 *
54 * @return array
55 * contains the search name as value and and id as key
56 *
57 */
58 public function getAll() {
59 $savedSearch = new CRM_Contact_DAO_SavedSearch();
60 $savedSearch->selectAdd();
61 $savedSearch->selectAdd('id, name');
62 $savedSearch->find();
63 while ($savedSearch->fetch()) {
64 $aSavedSearch[$savedSearch->id] = $savedSearch->name;
65 }
66 return $aSavedSearch;
67 }
68
69 /**
70 * Takes a bunch of params that are needed to match certain criteria and
71 * retrieves the relevant objects.
72 *
73 * @param array $params
74 * (reference ) an assoc array of name/value pairs.
75 * @param array $defaults
76 * (reference ) an assoc array to hold the flattened values.
77 *
78 * @return CRM_Contact_BAO_SavedSearch
79 * @static
80 */
81 public static function retrieve(&$params, &$defaults) {
82 $savedSearch = new CRM_Contact_DAO_SavedSearch();
83 $savedSearch->copyValues($params);
84 if ($savedSearch->find(TRUE)) {
85 CRM_Core_DAO::storeValues($savedSearch, $defaults);
86 return $savedSearch;
87 }
88 return NULL;
89 }
90
91 /**
92 * Given an id, extract the formValues of the saved search
93 *
94 * @param int $id
95 * The id of the saved search.
96 *
97 * @return array
98 * the values of the posted saved search
99 * @static
100 */
101 public static function &getFormValues($id) {
102 $fv = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values');
103 $result = NULL;
104 if ($fv) {
105 // make sure u unserialize - since it's stored in serialized form
106 $result = unserialize($fv);
107 }
108
109 // check to see if we need to convert the old privacy array
110 // CRM-9180
111 if (isset($result['privacy'])) {
112 if (is_array($result['privacy'])) {
113 $result['privacy_operator'] = 'AND';
114 $result['privacy_toggle'] = 1;
115 if (isset($result['privacy']['do_not_toggle'])) {
116 if ($result['privacy']['do_not_toggle']) {
117 $result['privacy_toggle'] = 2;
118 }
119 unset($result['privacy']['do_not_toggle']);
120 }
121
122 $result['privacy_options'] = array();
123 foreach ($result['privacy'] as $name => $value) {
124 if ($value) {
125 $result['privacy_options'][] = $name;
126 }
127 }
128 }
129 unset($result['privacy']);
130 }
131
132 return $result;
133 }
134
135 /**
136 * @param int $id
137 *
138 * @return array
139 */
140 public static function getSearchParams($id) {
141 $fv = self::getFormValues($id);
142 //check if the saved search has mapping id
143 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) {
144 return CRM_Core_BAO_Mapping::formattedFields($fv);
145 }
146 elseif (!empty($fv['customSearchID'])) {
147 return $fv;
148 }
149 else {
150 return CRM_Contact_BAO_Query::convertFormValues($fv);
151 }
152 }
153
154 /**
155 * Get the where clause for a saved search
156 *
157 * @param int $id
158 * Saved search id.
159 * @param array $tables
160 * (reference ) add the tables that are needed for the select clause.
161 * @param array $whereTables
162 * (reference ) add the tables that are needed for the where clause.
163 *
164 * @return string
165 * the where clause for this saved search
166 * @static
167 */
168 public static function whereClause($id, &$tables, &$whereTables) {
169 $params = self::getSearchParams($id);
170 if ($params) {
171 if (!empty($params['customSearchID'])) {
172 // this has not yet been implemented
173 }
174 else {
175 return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
176 }
177 }
178 return NULL;
179 }
180
181 /**
182 * @param int $id
183 *
184 * @return string
185 */
186 public static function contactIDsSQL($id) {
187 $params = self::getSearchParams($id);
188 if ($params && !empty($params['customSearchID'])) {
189 return CRM_Contact_BAO_SearchCustom::contactIDSQL(NULL, $id);
190 }
191 else {
192 $tables = $whereTables = array('civicrm_contact' => 1);
193 $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
194 if (!$where) {
195 $where = '( 1 )';
196 }
197 $from = CRM_Contact_BAO_Query::fromClause($whereTables);
198 return "
199 SELECT contact_a.id
200 $from
201 WHERE $where";
202 }
203 }
204
205 /**
206 * @param int $id
207 *
208 * @return array
209 */
210 public static function fromWhereEmail($id) {
211 $params = self::getSearchParams($id);
212
213 if ($params) {
214 if (!empty($params['customSearchID'])) {
215 return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id);
216 }
217 else {
218 $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
219 $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
220 $from = CRM_Contact_BAO_Query::fromClause($whereTables);
221 return array($from, $where);
222 }
223 }
224 else {
225 // fix for CRM-7240
226 $from = "
227 FROM civicrm_contact contact_a
228 LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)
229 ";
230 $where = " ( 1 ) ";
231 $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
232 $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
233 return array($from, $where);
234 }
235 }
236
237 /**
238 * Given a saved search compute the clause and the tables
239 * and store it for future use
240 */
241 public function buildClause() {
242 $fv = unserialize($this->form_values);
243
244 if ($this->mapping_id) {
245 $params = CRM_Core_BAO_Mapping::formattedFields($fv);
246 }
247 else {
248 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
249 }
250
251 if (!empty($params)) {
252 $tables = $whereTables = array();
253 $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
254 if (!empty($tables)) {
255 $this->select_tables = serialize($tables);
256 }
257 if (!empty($whereTables)) {
258 $this->where_tables = serialize($whereTables);
259 }
260 }
261
262 return;
263 }
264
265 public function save() {
266 // first build the computed fields
267 $this->buildClause();
268
269 parent::save();
270 }
271
272 /**
273 * Given an id, get the name of the saved search
274 *
275 * @param int $id
276 * The id of the saved search.
277 *
278 * @param string $value
279 *
280 * @return string
281 * the name of the saved search
282 * @static
283 */
284 public static function getName($id, $value = 'name') {
285 $group = new CRM_Contact_DAO_Group();
286 $group->saved_search_id = $id;
287 if ($group->find(TRUE)) {
288 return $group->$value;
289 }
290 return NULL;
291 }
292
293 /**
294 * Given a label and a set of normalized POST
295 * formValues, create a smart group with that
296 */
297 public static function create(&$params) {
298 $savedSearch = new CRM_Contact_DAO_SavedSearch();
299 if (isset($params['formValues']) &&
300 !empty($params['formValues'])
301 ) {
302 $savedSearch->form_values = serialize($params['formValues']);
303 }
304 else {
305 $savedSearch->form_values = 'null';
306 }
307
308 $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1);
309 $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null');
310 $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null');
311 $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL);
312
313 $savedSearch->save();
314
315 return $savedSearch;
316 }
317 }