b89446ee24e973eede7f6c8976f87cd18a619aeb
[civicrm-core.git] / CRM / Contact / BAO / SavedSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * Business object for Saved searches.
36 */
37 class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct();
44 }
45
46 /**
47 * Query the db for all saved searches.
48 *
49 * @return array
50 * contains the search name as value and and id as key
51 */
52 public function getAll() {
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 /**
64 * Retrieve DB object based on input parameters.
65 *
66 * It also stores all the retrieved values in the default array.
67 *
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.
72 *
73 * @return CRM_Contact_BAO_SavedSearch
74 */
75 public static function retrieve(&$params, &$defaults) {
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 /**
86 * Given an id, extract the formValues of the saved search
87 *
88 * @param int $id
89 * The id of the saved search.
90 *
91 * @return array
92 * the values of the posted saved search
93 */
94 public static function &getFormValues($id) {
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
128 /**
129 * @param int $id
130 *
131 * @return array
132 */
133 public static function getSearchParams($id) {
134 $fv = self::getFormValues($id);
135 //check if the saved search has mapping id
136 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) {
137 return CRM_Core_BAO_Mapping::formattedFields($fv);
138 }
139 elseif (!empty($fv['customSearchID'])) {
140 return $fv;
141 }
142 else {
143 return CRM_Contact_BAO_Query::convertFormValues($fv);
144 }
145 }
146
147 /**
148 * Get the where clause for a saved search.
149 *
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.
156 *
157 * @return string
158 * the where clause for this saved search
159 */
160 public static function whereClause($id, &$tables, &$whereTables) {
161 $params = self::getSearchParams($id);
162 if ($params) {
163 if (!empty($params['customSearchID'])) {
164 // this has not yet been implemented
165 }
166 else {
167 return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
168 }
169 }
170 return NULL;
171 }
172
173 /**
174 * @param int $id
175 *
176 * @return string
177 */
178 public static function contactIDsSQL($id) {
179 $params = self::getSearchParams($id);
180 if ($params && !empty($params['customSearchID'])) {
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
197 /**
198 * @param int $id
199 *
200 * @return array
201 */
202 public static function fromWhereEmail($id) {
203 $params = self::getSearchParams($id);
204
205 if ($params) {
206 if (!empty($params['customSearchID'])) {
207 return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id);
208 }
209 else {
210 $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
211 $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables);
212 $from = CRM_Contact_BAO_Query::fromClause($whereTables);
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 /**
230 * Given a saved search compute the clause and the tables
231 * and store it for future use
232 */
233 public function buildClause() {
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 }
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
266 * The id of the saved search.
267 *
268 * @param string $value
269 *
270 * @return string
271 * the name of the saved search
272 */
273 public static function getName($id, $value = 'name') {
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 */
286 public static function create(&$params) {
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 {
294 $savedSearch->form_values = NULL;
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 }
306
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
318 }