3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to save a search.
21 * Saved Searches are used for saving frequently used queries
23 class CRM_Contact_Form_Task_SaveSearch
extends CRM_Contact_Form_Task
{
26 * Saved search id if any.
33 * Build all the data structures needed to build the form.
35 public function preProcess() {
38 // get the submitted values of the search form
39 // we'll need to get fv from either search or adv search in the future
40 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
41 $values = $this->controller
->exportValues('Advanced');
43 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
44 $values = $this->controller
->exportValues('Builder');
46 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
47 $values = $this->controller
->exportValues('Custom');
50 $values = $this->controller
->exportValues('Basic');
54 $modeValue = CRM_Contact_Form_Search
::getModeValue(CRM_Utils_Array
::value('component_mode', $values, CRM_Contact_BAO_Query
::MODE_CONTACTS
));
55 $className = $modeValue['taskClassName'];
56 $this->_task
= $values['task'] ??
NULL;
60 * Build the form object.
63 * - displaying the QILL (query in local language)
64 * - displaying elements for saving the search
66 public function buildQuickForm() {
67 // @todo sync this more with CRM_Group_Form_Edit.
68 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
69 $this->assign('qill', $query->qill());
71 // Values from the search form
72 $formValues = $this->controller
->exportValues();
74 // the name and description are actually stored with the group and not the saved search
75 $this->add('text', 'title', ts('Group Title'),
76 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
79 $this->addElement('textarea', 'description', ts('Group Description'),
80 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
83 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
84 unset($groupTypes['Access Control']);
85 if (!CRM_Core_Permission
::access('CiviMail')) {
86 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
87 if ($isWorkFlowEnabled &&
88 !CRM_Core_Permission
::check('create mailings') &&
89 !CRM_Core_Permission
::check('schedule mailings') &&
90 !CRM_Core_Permission
::check('approve mailings')
92 unset($groupTypes['Mailing List']);
96 if (!empty($groupTypes)) {
97 $this->addCheckBox('group_type',
100 NULL, NULL, NULL, NULL, ' '
105 CRM_Group_Form_Edit
::buildParentGroups($this);
106 CRM_Group_Form_Edit
::buildGroupOrganizations($this);
108 // get the group id for the saved search
110 if (isset($this->_id
)) {
111 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
116 $this->addDefaultButtons(ts('Update Smart Group'));
119 $this->addDefaultButtons(ts('Save Smart Group'));
120 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
122 $this->addRule('title', ts('Group Title already exists in Database.'),
123 'objectExists', ['CRM_Contact_DAO_Group', $groupID, 'title']
128 * Process the form after the input has been submitted and validated.
130 public function postProcess() {
131 // saved search form values
132 // get form values of all the forms in this controller
133 $formValues = $this->controller
->exportValues();
135 $isAdvanced = $this->get('isAdvanced');
136 $isSearchBuilder = $this->get('isSearchBuilder');
138 // add mapping record only for search builder saved search
140 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
141 //save the mapping for search builder
144 //save record in mapping table
146 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
148 $mapping = CRM_Core_BAO_Mapping
::add($mappingParams);
149 $mappingId = $mapping->id
;
152 //get the mapping id from saved search
154 $savedSearch = new CRM_Contact_BAO_SavedSearch();
155 $savedSearch->id
= $this->_id
;
156 $savedSearch->find(TRUE);
157 $mappingId = $savedSearch->mapping_id
;
160 //save mapping fields
161 CRM_Core_BAO_Mapping
::saveMappingFields($formValues, $mappingId);
165 $savedSearch = new CRM_Contact_BAO_SavedSearch();
166 $savedSearch->id
= $this->_id
;
167 $queryParams = $this->get('queryParams');
169 // Use the query parameters rather than the form values - these have already been assessed / converted
170 // with the extra knowledge that the form has.
171 // Note that we want to move towards a standardised way of saving the query that is not
172 // an exact match for the form requirements & task the form layer with converting backwards and forwards.
173 // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the
174 // sql operator syntax at the query layer.
175 if (!$isSearchBuilder) {
176 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
177 $savedSearch->form_values
= serialize($queryParams);
180 // We want search builder to be able to convert back & forth at the form layer
181 // to a standardised style - but it can't yet!
182 $savedSearch->form_values
= serialize($formValues);
185 $savedSearch->mapping_id
= $mappingId;
186 $savedSearch->search_custom_id
= $this->get('customSearchID');
187 $savedSearch->save();
188 $this->set('ssID', $savedSearch->id
);
189 CRM_Core_Session
::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Group Saved'), 'success');
191 // also create a group that is associated with this saved search only if new saved search
193 $params['title'] = $formValues['title'];
194 $params['description'] = $formValues['description'];
195 if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($formValues['group_type'])) {
196 $params['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
197 array_keys($formValues['group_type'])) . CRM_Core_DAO
::VALUE_SEPARATOR
;
200 $params['group_type'] = '';
202 $params['visibility'] = 'User and User Admin Only';
203 $params['saved_search_id'] = $savedSearch->id
;
204 $params['is_active'] = 1;
207 $params['parents'] = $formValues['parents'];
210 $params['id'] = CRM_Contact_BAO_SavedSearch
::getName($this->_id
, 'id');
213 $group = CRM_Contact_BAO_Group
::create($params);
215 // Update mapping with the name and description of the group.
216 if ($mappingId && $group) {
219 'name' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'name', 'id'),
220 'description' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'description', 'id'),
221 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
223 CRM_Core_BAO_Mapping
::add($mappingParams);
227 $this->_id
= $savedSearch->id
;
230 if (!empty($formValues['parents'])) {
231 CRM_Contact_BAO_GroupNestingCache
::update();
240 public function setDefaultValues() {
242 if (empty($defaults['parents'])) {
243 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();