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