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