Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | /** | |
43 | * class constructor | |
44 | * | |
6c8f6e67 | 45 | * @return \CRM_Contact_BAO_SavedSearch CRM_Contact_BAO_SavedSearch |
6a488035 TO |
46 | */ |
47 | 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 | * @access public | |
57 | */ | |
58 | function getAll() { | |
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 | * | |
73 | * @param array $params (reference ) an assoc array of name/value pairs | |
74 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
75 | * | |
76 | * @return object CRM_Contact_BAO_SavedSearch | |
77 | * @access public | |
78 | * @static | |
79 | */ | |
80 | static function retrieve(&$params, &$defaults) { | |
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 | /** | |
91 | * given an id, extract the formValues of the saved search | |
92 | * | |
93 | * @param int $id the id of the saved search | |
94 | * | |
95 | * @return array the values of the posted saved search | |
96 | * @access public | |
97 | * @static | |
98 | */ | |
99 | static function &getFormValues($id) { | |
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 EM |
133 | /** |
134 | * @param $id | |
135 | * | |
136 | * @return array | |
137 | */ | |
6a488035 TO |
138 | static function getSearchParams($id) { |
139 | $fv = self::getFormValues($id); | |
140 | //check if the saved seach has mapping id | |
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 | /** | |
153 | * get the where clause for a saved search | |
154 | * | |
155 | * @param int $id saved search id | |
156 | * @param array $tables (reference ) add the tables that are needed for the select clause | |
157 | * @param array $whereTables (reference ) add the tables that are needed for the where clause | |
158 | * | |
159 | * @return string the where clause for this saved search | |
160 | * @access public | |
161 | * @static | |
162 | */ | |
163 | static function whereClause($id, &$tables, &$whereTables) { | |
164 | $params = self::getSearchParams($id); | |
165 | if ($params) { | |
a7488080 | 166 | if (!empty($params['customSearchID'])) { |
6a488035 TO |
167 | // this has not yet been implemented |
168 | } else { | |
169 | return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
170 | } | |
171 | } | |
172 | return NULL; | |
173 | } | |
174 | ||
86538308 EM |
175 | /** |
176 | * @param $id | |
177 | * | |
178 | * @return string | |
179 | */ | |
6a488035 TO |
180 | static function contactIDsSQL($id) { |
181 | $params = self::getSearchParams($id); | |
8cc574cf | 182 | if ($params && !empty($params['customSearchID'])) { |
6a488035 TO |
183 | return CRM_Contact_BAO_SearchCustom::contactIDSQL(NULL, $id); |
184 | } | |
185 | else { | |
186 | $tables = $whereTables = array('civicrm_contact' => 1); | |
187 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); | |
188 | if (!$where) { | |
189 | $where = '( 1 )'; | |
190 | } | |
191 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
192 | return " | |
193 | SELECT contact_a.id | |
194 | $from | |
195 | WHERE $where"; | |
196 | } | |
197 | } | |
198 | ||
86538308 EM |
199 | /** |
200 | * @param $id | |
201 | * | |
202 | * @return array | |
203 | */ | |
6a488035 TO |
204 | static function fromWhereEmail($id) { |
205 | $params = self::getSearchParams($id); | |
206 | ||
207 | if ($params) { | |
a7488080 | 208 | if (!empty($params['customSearchID'])) { |
6a488035 TO |
209 | return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id); |
210 | } | |
211 | else { | |
212 | $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1); | |
213 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); | |
214 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
215 | return array($from, $where); | |
216 | } | |
217 | } | |
218 | else { | |
219 | // fix for CRM-7240 | |
220 | $from = " | |
221 | FROM civicrm_contact contact_a | |
222 | LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) | |
223 | "; | |
224 | $where = " ( 1 ) "; | |
225 | $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1; | |
226 | $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1; | |
227 | return array($from, $where); | |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
232 | * given a saved search compute the clause and the tables | |
233 | * and store it for future use | |
234 | */ | |
235 | function buildClause() { | |
236 | $fv = unserialize($this->form_values); | |
237 | ||
238 | if ($this->mapping_id) { | |
239 | $params = CRM_Core_BAO_Mapping::formattedFields($fv); | |
240 | } | |
241 | else { | |
242 | $params = CRM_Contact_BAO_Query::convertFormValues($fv); | |
243 | } | |
244 | ||
245 | if (!empty($params)) { | |
246 | $tables = $whereTables = array(); | |
247 | $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
248 | if (!empty($tables)) { | |
249 | $this->select_tables = serialize($tables); | |
250 | } | |
251 | if (!empty($whereTables)) { | |
252 | $this->where_tables = serialize($whereTables); | |
253 | } | |
254 | } | |
255 | ||
256 | return; | |
257 | } | |
258 | ||
259 | function save() { | |
260 | // first build the computed fields | |
261 | $this->buildClause(); | |
262 | ||
263 | parent::save(); | |
264 | } | |
265 | ||
266 | /** | |
267 | * given an id, get the name of the saved search | |
268 | * | |
269 | * @param int $id the id of the saved search | |
270 | * | |
77b97be7 EM |
271 | * @param string $value |
272 | * | |
6a488035 TO |
273 | * @return string the name of the saved search |
274 | * @access public | |
275 | * @static | |
276 | */ | |
277 | static function getName($id, $value = 'name') { | |
278 | $group = new CRM_Contact_DAO_Group(); | |
279 | $group->saved_search_id = $id; | |
280 | if ($group->find(TRUE)) { | |
281 | return $group->$value; | |
282 | } | |
283 | return NULL; | |
284 | } | |
285 | ||
286 | /** | |
287 | * Given a label and a set of normalized POST | |
288 | * formValues, create a smart group with that | |
289 | */ | |
290 | static function create(&$params) { | |
291 | $savedSearch = new CRM_Contact_DAO_SavedSearch(); | |
292 | if (isset($params['formValues']) && | |
293 | !empty($params['formValues']) | |
294 | ) { | |
295 | $savedSearch->form_values = serialize($params['formValues']); | |
296 | } | |
297 | else { | |
298 | $savedSearch->form_values = 'null'; | |
299 | } | |
300 | ||
301 | $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1); | |
302 | $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null'); | |
303 | $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null'); | |
304 | $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL); | |
305 | ||
306 | $savedSearch->save(); | |
307 | ||
308 | return $savedSearch; | |
309 | } | |
310 | } | |
311 |