3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Business object for Saved searches
40 class CRM_Contact_BAO_SavedSearch
extends CRM_Contact_DAO_SavedSearch
{
45 * @return object CRM_Contact_BAO_SavedSearch
47 function __construct() {
48 parent
::__construct();
52 * query the db for all saved searches.
54 * @return array $aSavedSearch - contains the search name as value and and id as key
59 $savedSearch = new CRM_Contact_DAO_SavedSearch();
60 $savedSearch->selectAdd();
61 $savedSearch->selectAdd('id, name');
63 while ($savedSearch->fetch()) {
64 $aSavedSearch[$savedSearch->id
] = $savedSearch->name
;
70 * Takes a bunch of params that are needed to match certain criteria and
71 * retrieves the relevant objects.
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
76 * @return object CRM_Contact_BAO_SavedSearch
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);
91 * given an id, extract the formValues of the saved search
93 * @param int $id the id of the saved search
95 * @return array the values of the posted saved search
99 static function &getFormValues($id) {
100 $fv = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values');
103 // make sure u unserialize - since it's stored in serialized form
104 $result = unserialize($fv);
107 // check to see if we need to convert the old privacy array
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;
117 unset($result['privacy']['do_not_toggle']);
120 $result['privacy_options'] = array();
121 foreach ($result['privacy'] as $name => $value) {
123 $result['privacy_options'][] = $name;
127 unset($result['privacy']);
133 static function getSearchParams($id) {
134 $fv = self
::getFormValues($id);
135 //check if the saved seach has mapping id
136 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) {
137 return CRM_Core_BAO_Mapping
::formattedFields($fv);
139 elseif (CRM_Utils_Array
::value('customSearchID', $fv)) {
143 return CRM_Contact_BAO_Query
::convertFormValues($fv);
148 * get the where clause for a saved search
150 * @param int $id saved search id
151 * @param array $tables (reference ) add the tables that are needed for the select clause
152 * @param array $whereTables (reference ) add the tables that are needed for the where clause
154 * @return string the where clause for this saved search
158 static function whereClause($id, &$tables, &$whereTables) {
159 $params = self
::getSearchParams($id);
161 if (CRM_Utils_Array
::value('customSearchID', $params)) {
162 // this has not yet been implemented
164 return CRM_Contact_BAO_Query
::getWhereClause($params, NULL, $tables, $whereTables);
170 static function contactIDsSQL($id) {
171 $params = self
::getSearchParams($id);
173 CRM_Utils_Array
::value('customSearchID', $params)
175 return CRM_Contact_BAO_SearchCustom
::contactIDSQL(NULL, $id);
178 $tables = $whereTables = array('civicrm_contact' => 1);
179 $where = CRM_Contact_BAO_SavedSearch
::whereClause($id, $tables, $whereTables);
183 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
191 static function fromWhereEmail($id) {
192 $params = self
::getSearchParams($id);
195 if (CRM_Utils_Array
::value('customSearchID', $params)) {
196 return CRM_Contact_BAO_SearchCustom
::fromWhereEmail(NULL, $id);
199 $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1);
200 $where = CRM_Contact_BAO_SavedSearch
::whereClause($id, $tables, $whereTables);
201 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
202 return array($from, $where);
208 FROM civicrm_contact contact_a
209 LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)
212 $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
213 $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
214 return array($from, $where);
219 * given a saved search compute the clause and the tables
220 * and store it for future use
222 function buildClause() {
223 $fv = unserialize($this->form_values
);
225 if ($this->mapping_id
) {
226 $params = CRM_Core_BAO_Mapping
::formattedFields($fv);
229 $params = CRM_Contact_BAO_Query
::convertFormValues($fv);
232 if (!empty($params)) {
233 $tables = $whereTables = array();
234 $this->where_clause
= CRM_Contact_BAO_Query
::getWhereClause($params, NULL, $tables, $whereTables);
235 if (!empty($tables)) {
236 $this->select_tables
= serialize($tables);
238 if (!empty($whereTables)) {
239 $this->where_tables
= serialize($whereTables);
247 // first build the computed fields
248 $this->buildClause();
254 * given an id, get the name of the saved search
256 * @param int $id the id of the saved search
258 * @return string the name of the saved search
262 static function getName($id, $value = 'name') {
263 $group = new CRM_Contact_DAO_Group();
264 $group->saved_search_id
= $id;
265 if ($group->find(TRUE)) {
266 return $group->$value;
272 * Given a label and a set of normalized POST
273 * formValues, create a smart group with that
275 static function create(&$params) {
276 $savedSearch = new CRM_Contact_DAO_SavedSearch();
277 if (isset($params['formValues']) &&
278 !empty($params['formValues'])
280 $savedSearch->form_values
= serialize($params['formValues']);
283 $savedSearch->form_values
= 'null';
286 $savedSearch->is_active
= CRM_Utils_Array
::value('is_active', $params, 1);
287 $savedSearch->mapping_id
= CRM_Utils_Array
::value('mapping_id', $params, 'null');
288 $savedSearch->custom_search_id
= CRM_Utils_Array
::value('custom_search_id', $params, 'null');
289 $savedSearch->id
= CRM_Utils_Array
::value('id', $params, NULL);
291 $savedSearch->save();