Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
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 | */ |
33 | ||
34 | /** | |
c037736a | 35 | * Business object for Saved searches. |
6a488035 TO |
36 | */ |
37 | class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Class constructor. |
6a488035 | 41 | */ |
00be9182 | 42 | public function __construct() { |
6a488035 TO |
43 | parent::__construct(); |
44 | } | |
45 | ||
46 | /** | |
100fef9d | 47 | * Query the db for all saved searches. |
6a488035 | 48 | * |
a6c01b45 CW |
49 | * @return array |
50 | * contains the search name as value and and id as key | |
6a488035 | 51 | */ |
00be9182 | 52 | public function getAll() { |
6a488035 TO |
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 | /** | |
fe482240 EM |
64 | * Retrieve DB object based on input parameters. |
65 | * | |
66 | * It also stores all the retrieved values in the default array. | |
6a488035 | 67 | * |
77c5b619 TO |
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. | |
6a488035 | 72 | * |
c490a46a | 73 | * @return CRM_Contact_BAO_SavedSearch |
6a488035 | 74 | */ |
00be9182 | 75 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
100fef9d | 86 | * Given an id, extract the formValues of the saved search |
6a488035 | 87 | * |
77c5b619 TO |
88 | * @param int $id |
89 | * The id of the saved search. | |
6a488035 | 90 | * |
a6c01b45 CW |
91 | * @return array |
92 | * the values of the posted saved search | |
6a488035 | 93 | */ |
00be9182 | 94 | public static function &getFormValues($id) { |
6a488035 TO |
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 | // check to see if we need to convert the old privacy array | |
103 | // CRM-9180 | |
104 | if (isset($result['privacy'])) { | |
105 | if (is_array($result['privacy'])) { | |
106 | $result['privacy_operator'] = 'AND'; | |
107 | $result['privacy_toggle'] = 1; | |
108 | if (isset($result['privacy']['do_not_toggle'])) { | |
109 | if ($result['privacy']['do_not_toggle']) { | |
110 | $result['privacy_toggle'] = 2; | |
111 | } | |
112 | unset($result['privacy']['do_not_toggle']); | |
113 | } | |
114 | ||
115 | $result['privacy_options'] = array(); | |
116 | foreach ($result['privacy'] as $name => $value) { | |
117 | if ($value) { | |
118 | $result['privacy_options'][] = $name; | |
119 | } | |
120 | } | |
121 | } | |
122 | unset($result['privacy']); | |
123 | } | |
124 | ||
125 | return $result; | |
126 | } | |
127 | ||
86538308 | 128 | /** |
100fef9d | 129 | * @param int $id |
86538308 EM |
130 | * |
131 | * @return array | |
132 | */ | |
00be9182 | 133 | public static function getSearchParams($id) { |
6a488035 | 134 | $fv = self::getFormValues($id); |
959528d2 | 135 | //check if the saved search has mapping id |
6a488035 TO |
136 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) { |
137 | return CRM_Core_BAO_Mapping::formattedFields($fv); | |
138 | } | |
a7488080 | 139 | elseif (!empty($fv['customSearchID'])) { |
6a488035 TO |
140 | return $fv; |
141 | } | |
142 | else { | |
143 | return CRM_Contact_BAO_Query::convertFormValues($fv); | |
144 | } | |
145 | } | |
146 | ||
147 | /** | |
fe482240 | 148 | * Get the where clause for a saved search. |
6a488035 | 149 | * |
77c5b619 TO |
150 | * @param int $id |
151 | * Saved search id. | |
152 | * @param array $tables | |
153 | * (reference ) add the tables that are needed for the select clause. | |
154 | * @param array $whereTables | |
155 | * (reference ) add the tables that are needed for the where clause. | |
6a488035 | 156 | * |
a6c01b45 CW |
157 | * @return string |
158 | * the where clause for this saved search | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function whereClause($id, &$tables, &$whereTables) { |
6a488035 TO |
161 | $params = self::getSearchParams($id); |
162 | if ($params) { | |
a7488080 | 163 | if (!empty($params['customSearchID'])) { |
6a488035 | 164 | // this has not yet been implemented |
0db6c3e1 TO |
165 | } |
166 | else { | |
353ffa53 TO |
167 | return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); |
168 | } | |
6a488035 TO |
169 | } |
170 | return NULL; | |
171 | } | |
172 | ||
86538308 | 173 | /** |
100fef9d | 174 | * @param int $id |
86538308 EM |
175 | * |
176 | * @return string | |
177 | */ | |
00be9182 | 178 | public static function contactIDsSQL($id) { |
6a488035 | 179 | $params = self::getSearchParams($id); |
8cc574cf | 180 | if ($params && !empty($params['customSearchID'])) { |
6a488035 TO |
181 | return CRM_Contact_BAO_SearchCustom::contactIDSQL(NULL, $id); |
182 | } | |
183 | else { | |
184 | $tables = $whereTables = array('civicrm_contact' => 1); | |
185 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); | |
186 | if (!$where) { | |
187 | $where = '( 1 )'; | |
188 | } | |
189 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
190 | return " | |
191 | SELECT contact_a.id | |
192 | $from | |
193 | WHERE $where"; | |
194 | } | |
195 | } | |
196 | ||
86538308 | 197 | /** |
100fef9d | 198 | * @param int $id |
86538308 EM |
199 | * |
200 | * @return array | |
201 | */ | |
00be9182 | 202 | public static function fromWhereEmail($id) { |
6a488035 TO |
203 | $params = self::getSearchParams($id); |
204 | ||
205 | if ($params) { | |
a7488080 | 206 | if (!empty($params['customSearchID'])) { |
6a488035 TO |
207 | return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id); |
208 | } | |
209 | else { | |
210 | $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1); | |
353ffa53 TO |
211 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); |
212 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
6a488035 TO |
213 | return array($from, $where); |
214 | } | |
215 | } | |
216 | else { | |
217 | // fix for CRM-7240 | |
218 | $from = " | |
219 | FROM civicrm_contact contact_a | |
220 | LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) | |
221 | "; | |
222 | $where = " ( 1 ) "; | |
223 | $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1; | |
224 | $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1; | |
225 | return array($from, $where); | |
226 | } | |
227 | } | |
228 | ||
229 | /** | |
100fef9d | 230 | * Given a saved search compute the clause and the tables |
6a488035 TO |
231 | * and store it for future use |
232 | */ | |
00be9182 | 233 | public function buildClause() { |
6a488035 TO |
234 | $fv = unserialize($this->form_values); |
235 | ||
236 | if ($this->mapping_id) { | |
237 | $params = CRM_Core_BAO_Mapping::formattedFields($fv); | |
238 | } | |
239 | else { | |
240 | $params = CRM_Contact_BAO_Query::convertFormValues($fv); | |
241 | } | |
242 | ||
243 | if (!empty($params)) { | |
244 | $tables = $whereTables = array(); | |
245 | $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
246 | if (!empty($tables)) { | |
247 | $this->select_tables = serialize($tables); | |
248 | } | |
249 | if (!empty($whereTables)) { | |
250 | $this->where_tables = serialize($whereTables); | |
251 | } | |
252 | } | |
6a488035 TO |
253 | } |
254 | ||
00be9182 | 255 | public function save() { |
6a488035 TO |
256 | // first build the computed fields |
257 | $this->buildClause(); | |
258 | ||
259 | parent::save(); | |
260 | } | |
261 | ||
262 | /** | |
100fef9d | 263 | * Given an id, get the name of the saved search |
6a488035 | 264 | * |
77c5b619 TO |
265 | * @param int $id |
266 | * The id of the saved search. | |
6a488035 | 267 | * |
77b97be7 EM |
268 | * @param string $value |
269 | * | |
a6c01b45 CW |
270 | * @return string |
271 | * the name of the saved search | |
6a488035 | 272 | */ |
00be9182 | 273 | public static function getName($id, $value = 'name') { |
6a488035 TO |
274 | $group = new CRM_Contact_DAO_Group(); |
275 | $group->saved_search_id = $id; | |
276 | if ($group->find(TRUE)) { | |
277 | return $group->$value; | |
278 | } | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | /** | |
283 | * Given a label and a set of normalized POST | |
284 | * formValues, create a smart group with that | |
285 | */ | |
00be9182 | 286 | public static function create(&$params) { |
6a488035 TO |
287 | $savedSearch = new CRM_Contact_DAO_SavedSearch(); |
288 | if (isset($params['formValues']) && | |
289 | !empty($params['formValues']) | |
290 | ) { | |
291 | $savedSearch->form_values = serialize($params['formValues']); | |
292 | } | |
293 | else { | |
1c18b4d5 | 294 | $savedSearch->form_values = NULL; |
6a488035 TO |
295 | } |
296 | ||
297 | $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1); | |
298 | $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null'); | |
299 | $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null'); | |
300 | $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL); | |
301 | ||
302 | $savedSearch->save(); | |
303 | ||
304 | return $savedSearch; | |
305 | } | |
96025800 | 306 | |
4f0a0e58 JV |
307 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { |
308 | if ($fieldName == 'form_values') { | |
309 | // A dummy value for form_values. | |
310 | $this->{$fieldName} = serialize( | |
311 | array('sort_name' => "SortName{$counter}")); | |
312 | } | |
313 | else { | |
314 | parent::assignTestValues($fieldName, $fieldDef, $counter); | |
315 | } | |
316 | } | |
317 | ||
6a488035 | 318 | } |