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