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