3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * Business object for Saved searches.
37 class CRM_Contact_BAO_SavedSearch
extends CRM_Contact_DAO_SavedSearch
{
42 public function __construct() {
43 parent
::__construct();
47 * Query the db for all saved searches.
50 * contains the search name as value and and id as key
52 public function getAll() {
53 $savedSearch = new CRM_Contact_DAO_SavedSearch();
54 $savedSearch->selectAdd();
55 $savedSearch->selectAdd('id, name');
57 while ($savedSearch->fetch()) {
58 $aSavedSearch[$savedSearch->id
] = $savedSearch->name
;
64 * Retrieve DB object based on input parameters.
66 * It also stores all the retrieved values in the default array.
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.
73 * @return CRM_Contact_BAO_SavedSearch
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);
86 * Given an id, extract the formValues of the saved search
89 * The id of the saved search.
92 * the values of the posted saved search
94 public static function &getFormValues($id) {
95 $fv = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values');
98 // make sure u unserialize - since it's stored in serialized form
99 $result = unserialize($fv);
102 // check to see if we need to convert the old privacy array
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;
112 unset($result['privacy']['do_not_toggle']);
115 $result['privacy_options'] = array();
116 foreach ($result['privacy'] as $name => $value) {
118 $result['privacy_options'][] = $name;
122 unset($result['privacy']);
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);
139 elseif (!empty($fv['customSearchID'])) {
143 return CRM_Contact_BAO_Query
::convertFormValues($fv);
148 * Get the where clause for a saved search.
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.
158 * the where clause for this saved search
160 public static function whereClause($id, &$tables, &$whereTables) {
161 $params = self
::getSearchParams($id);
163 if (!empty($params['customSearchID'])) {
164 // this has not yet been implemented
167 return CRM_Contact_BAO_Query
::getWhereClause($params, NULL, $tables, $whereTables);
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);
184 $tables = $whereTables = array('civicrm_contact' => 1);
185 $where = CRM_Contact_BAO_SavedSearch
::whereClause($id, $tables, $whereTables);
189 $from = CRM_Contact_BAO_Query
::fromClause($whereTables);
202 public static function fromWhereEmail($id) {
203 $params = self
::getSearchParams($id);
206 if (!empty($params['customSearchID'])) {
207 return CRM_Contact_BAO_SearchCustom
::fromWhereEmail(NULL, $id);
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);
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)
223 $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1;
224 $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1;
225 return array($from, $where);
230 * Given a saved search compute the clause and the tables
231 * and store it for future use
233 public function buildClause() {
234 $fv = unserialize($this->form_values
);
236 if ($this->mapping_id
) {
237 $params = CRM_Core_BAO_Mapping
::formattedFields($fv);
240 $params = CRM_Contact_BAO_Query
::convertFormValues($fv);
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);
249 if (!empty($whereTables)) {
250 $this->where_tables
= serialize($whereTables);
255 public function save() {
256 // first build the computed fields
257 $this->buildClause();
263 * Given an id, get the name of the saved search
266 * The id of the saved search.
268 * @param string $value
271 * the name of the saved search
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;
283 * Given a label and a set of normalized POST
284 * formValues, create a smart group with that
286 public static function create(&$params) {
287 $savedSearch = new CRM_Contact_DAO_SavedSearch();
288 if (isset($params['formValues']) &&
289 !empty($params['formValues'])
291 $savedSearch->form_values
= serialize($params['formValues']);
294 $savedSearch->form_values
= NULL;
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);
302 $savedSearch->save();
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}"));
314 parent
::assignTestValues($fieldName, $fieldDef, $counter);