comment fixes
[civicrm-core.git] / CRM / Contact / BAO / SavedSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * Business object for Saved searches.
36 */
37 class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct();
44 }
45
46 /**
47 * Query the db for all saved searches.
48 *
49 * @return array
50 * contains the search name as value and and id as key
51 */
52 public function getAll() {
53 $savedSearch = new CRM_Contact_DAO_SavedSearch();
54 $savedSearch->selectAdd();
55 $savedSearch->selectAdd('id, name');
56 $savedSearch->find();
57 while ($savedSearch->fetch()) {
58 $aSavedSearch[$savedSearch->id] = $savedSearch->name;
59 }
60 return $aSavedSearch;
61 }
62
63 /**
64 * Retrieve DB object based on input parameters.
65 *
66 * It also stores all the retrieved values in the default array.
67 *
68 * @param array $params
69 * (reference ) an assoc array of name/value pairs.
70 * @param array $defaults
71 * (reference ) an assoc array to hold the flattened values.
72 *
73 * @return CRM_Contact_BAO_SavedSearch
74 */
75 public static function retrieve(&$params, &$defaults) {
76 $savedSearch = new CRM_Contact_DAO_SavedSearch();
77 $savedSearch->copyValues($params);
78 if ($savedSearch->find(TRUE)) {
79 CRM_Core_DAO::storeValues($savedSearch, $defaults);
80 return $savedSearch;
81 }
82 return NULL;
83 }
84
85 /**
86 * Given an id, extract the formValues of the saved search.
87 *
88 * @param int $id
89 * The id of the saved search.
90 *
91 * @return array
92 * the values of the posted saved search used as default values in various Search Form
93 */
94 public static function &getFormValues($id) {
95 $fv = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values');
96 $result = NULL;
97 if ($fv) {
98 // make sure u unserialize - since it's stored in serialized form
99 $result = unserialize($fv);
100 }
101
102 $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id');
103 foreach ($result as $element => $value) {
104 if (CRM_Contact_BAO_Query::isAlreadyProcessedForQueryFormat($value)) {
105 $id = CRM_Utils_Array::value(0, $value);
106 $value = CRM_Utils_Array::value(2, $value);
107 if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
108 $value = CRM_Utils_Array::value(key($value), $value);
109 }
110 $result[$id] = $value;
111 unset($result[$element]);
112 continue;
113 }
114 if (!empty($value) && is_array($value)) {
115 if (in_array($element, $specialFields)) {
116 $element = str_replace('member_membership_type_id', 'membership_type_id', $element);
117 $element = str_replace('member_status_id', 'membership_status_id', $element);
118 CRM_Contact_BAO_Query::legacyConvertFormValues($element, $value);
119 $result[$element] = $value;
120 }
121 // As per the OK (Operator as Key) value format, value array may contain key
122 // as an operator so to ensure the default is always set actual value
123 elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
124 $result[$element] = CRM_Utils_Array::value(key($value), $value);
125 if (is_string($result[$element])) {
126 $result[$element] = str_replace("%", '', $result[$element]);
127 }
128 }
129 }
130 if (substr($element, 0, 7) == 'custom_' &&
131 (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to')
132 ) {
133 // Ensure the _relative field is set if from or to are set to ensure custom date
134 // fields with 'from' or 'to' values are displayed when the are set in the smart group
135 // being loaded. (CRM-17116)
136 if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) {
137 $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0;
138 }
139 }
140 // check to see if we need to convert the old privacy array
141 // CRM-9180
142 if (!empty($result['privacy'])) {
143 if (is_array($result['privacy'])) {
144 $result['privacy_operator'] = 'AND';
145 $result['privacy_toggle'] = 1;
146 if (isset($result['privacy']['do_not_toggle'])) {
147 if ($result['privacy']['do_not_toggle']) {
148 $result['privacy_toggle'] = 2;
149 }
150 unset($result['privacy']['do_not_toggle']);
151 }
152
153 $result['privacy_options'] = array();
154 foreach ($result['privacy'] as $name => $value) {
155 if ($value) {
156 $result['privacy_options'][] = $name;
157 }
158 }
159 }
160 unset($result['privacy']);
161 }
162 }
163
164 return $result;
165 }
166
167 /**
168 * Get search parameters.
169 *
170 * @param int $id
171 *
172 * @return array
173 */
174 public static function getSearchParams($id) {
175 $fv = self::getFormValues($id);
176 //check if the saved search has mapping id
177 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) {
178 return CRM_Core_BAO_Mapping::formattedFields($fv);
179 }
180 elseif (!empty($fv['customSearchID'])) {
181 return $fv;
182 }
183 else {
184 return CRM_Contact_BAO_Query::convertFormValues($fv);
185 }
186 }
187
188 /**
189 * Get the where clause for a saved search.
190 *
191 * @param int $id
192 * Saved search id.
193 * @param array $tables
194 * (reference ) add the tables that are needed for the select clause.
195 * @param array $whereTables
196 * (reference ) add the tables that are needed for the where clause.
197 *
198 * @return string
199 * the where clause for this saved search
200 */
201 public static function whereClause($id, &$tables, &$whereTables) {
202 $params = self::getSearchParams($id);
203 if ($params) {
204 if (!empty($params['customSearchID'])) {
205 // this has not yet been implemented
206 }
207 else {
208 return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
209 }
210 }
211 return NULL;
212 }
213
214 /**
215 * Contact IDS Sql (whatever that means!).
216 *
217 * @param int $id
218 *
219 * @return string
220 */
221 public static function contactIDsSQL($id) {
222 $params = self::getSearchParams($id);
223 if ($params && !empty($params['customSearchID'])) {
224 return CRM_Contact_BAO_SearchCustom::contactIDSQL(NULL, $id);
225 }
226 else {
227 $tables = $whereTables = array('civicrm_contact' => 1);
228 $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
229 if (!$where) {
230 $where = '( 1 )';
231 }
232 $from = CRM_Contact_BAO_Query::fromClause($whereTables);
233 return "
234 SELECT contact_a.id
235 $from
236 WHERE $where";
237 }
238 }
239
240 /**
241 * Get from where email (whatever that means!).
242 *
243 * @param int $id
244 *
245 * @return array
246 */
247 public static function fromWhereEmail($id) {
248 $params = self::getSearchParams($id);
249
250 if ($params) {
251 if (!empty($params['customSearchID'])) {
252 return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id);
253 }
254 else {
255 $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
256 $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
257 $from = CRM_Contact_BAO_Query::fromClause($whereTables);
258 return array($from, $where);
259 }
260 }
261 else {
262 // fix for CRM-7240
263 $from = "
264 FROM civicrm_contact contact_a
265 LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)
266 ";
267 $where = " ( 1 ) ";
268 $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
269 $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
270 return array($from, $where);
271 }
272 }
273
274 /**
275 * Given a saved search compute the clause and the tables and store it for future use.
276 */
277 public function buildClause() {
278 $fv = unserialize($this->form_values);
279
280 if ($this->mapping_id) {
281 $params = CRM_Core_BAO_Mapping::formattedFields($fv);
282 }
283 else {
284 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
285 }
286
287 if (!empty($params)) {
288 $tables = $whereTables = array();
289 $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
290 if (!empty($tables)) {
291 $this->select_tables = serialize($tables);
292 }
293 if (!empty($whereTables)) {
294 $this->where_tables = serialize($whereTables);
295 }
296 }
297 }
298
299 /**
300 * Save the search.
301 *
302 * @param bool $hook
303 */
304 public function save($hook = TRUE) {
305 // first build the computed fields
306 $this->buildClause();
307
308 parent::save($hook);
309 }
310
311 /**
312 * Given an id, get the name of the saved search
313 *
314 * @param int $id
315 * The id of the saved search.
316 *
317 * @param string $value
318 *
319 * @return string
320 * the name of the saved search
321 */
322 public static function getName($id, $value = 'name') {
323 $group = new CRM_Contact_DAO_Group();
324 $group->saved_search_id = $id;
325 if ($group->find(TRUE)) {
326 return $group->$value;
327 }
328 return NULL;
329 }
330
331 /**
332 * Given a label and a set of normalized POST
333 * formValues, create a smart group with that
334 *
335 * @param array $params
336 *
337 * @return \CRM_Contact_DAO_SavedSearch
338 */
339 public static function create(&$params) {
340 $savedSearch = new CRM_Contact_DAO_SavedSearch();
341 if (isset($params['formValues']) &&
342 !empty($params['formValues'])
343 ) {
344 $savedSearch->form_values = serialize($params['formValues']);
345 }
346 else {
347 $savedSearch->form_values = NULL;
348 }
349
350 $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1);
351 $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null');
352 $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null');
353 $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL);
354
355 $savedSearch->save();
356
357 return $savedSearch;
358 }
359
360 /**
361 * Assign test value.
362 *
363 * @param string $fieldName
364 * @param array $fieldDef
365 * @param int $counter
366 */
367 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
368 if ($fieldName == 'form_values') {
369 // A dummy value for form_values.
370 $this->{$fieldName} = serialize(
371 array('sort_name' => "SortName{$counter}"));
372 }
373 else {
374 parent::assignTestValues($fieldName, $fieldDef, $counter);
375 }
376 }
377
378 }