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