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 $taskList = $className::taskTitles();
57 $this->_task
= $values['task'] ??
NULL;
58 $this->assign('taskName', CRM_Utils_Array
::value($this->_task
, $taskList));
62 * Build the form object.
65 * - displaying the QILL (query in local language)
66 * - displaying elements for saving the search
68 public function buildQuickForm() {
69 // @todo sync this more with CRM_Group_Form_Edit.
70 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
71 $this->assign('qill', $query->qill());
73 // Values from the search form
74 $formValues = $this->controller
->exportValues();
76 // the name and description are actually stored with the group and not the saved search
77 $this->add('text', 'title', ts('Group Title'),
78 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
81 $this->addElement('textarea', 'description', ts('Group Description'),
82 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
85 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
86 unset($groupTypes['Access Control']);
87 if (!CRM_Core_Permission
::access('CiviMail')) {
88 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
89 if ($isWorkFlowEnabled &&
90 !CRM_Core_Permission
::check('create mailings') &&
91 !CRM_Core_Permission
::check('schedule mailings') &&
92 !CRM_Core_Permission
::check('approve mailings')
94 unset($groupTypes['Mailing List']);
98 if (!empty($groupTypes)) {
99 $this->addCheckBox('group_type',
102 NULL, NULL, NULL, NULL, ' '
107 CRM_Group_Form_Edit
::buildParentGroups($this);
108 CRM_Group_Form_Edit
::buildGroupOrganizations($this);
110 // get the group id for the saved search
112 if (isset($this->_id
)) {
113 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
118 $this->addDefaultButtons(ts('Update Smart Group'));
121 $this->addDefaultButtons(ts('Save Smart Group'));
122 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
124 $this->addRule('title', ts('Group Title already exists in Database.'),
125 'objectExists', ['CRM_Contact_DAO_Group', $groupID, 'title']
130 * Process the form after the input has been submitted and validated.
132 public function postProcess() {
133 // saved search form values
134 // get form values of all the forms in this controller
135 $formValues = $this->controller
->exportValues();
137 $isAdvanced = $this->get('isAdvanced');
138 $isSearchBuilder = $this->get('isSearchBuilder');
140 // add mapping record only for search builder saved search
142 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
143 //save the mapping for search builder
146 //save record in mapping table
148 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
150 $mapping = CRM_Core_BAO_Mapping
::add($mappingParams);
151 $mappingId = $mapping->id
;
154 //get the mapping id from saved search
156 $savedSearch = new CRM_Contact_BAO_SavedSearch();
157 $savedSearch->id
= $this->_id
;
158 $savedSearch->find(TRUE);
159 $mappingId = $savedSearch->mapping_id
;
162 //save mapping fields
163 CRM_Core_BAO_Mapping
::saveMappingFields($formValues, $mappingId);
167 $savedSearch = new CRM_Contact_BAO_SavedSearch();
168 $savedSearch->id
= $this->_id
;
169 $queryParams = $this->get('queryParams');
171 // Use the query parameters rather than the form values - these have already been assessed / converted
172 // with the extra knowledge that the form has.
173 // Note that we want to move towards a standardised way of saving the query that is not
174 // an exact match for the form requirements & task the form layer with converting backwards and forwards.
175 // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the
176 // sql operator syntax at the query layer.
177 if (!$isSearchBuilder) {
178 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
179 $savedSearch->form_values
= serialize($queryParams);
182 // We want search builder to be able to convert back & forth at the form layer
183 // to a standardised style - but it can't yet!
184 $savedSearch->form_values
= serialize($formValues);
187 $savedSearch->mapping_id
= $mappingId;
188 $savedSearch->search_custom_id
= $this->get('customSearchID');
189 $savedSearch->save();
190 $this->set('ssID', $savedSearch->id
);
191 CRM_Core_Session
::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Group Saved'), 'success');
193 // also create a group that is associated with this saved search only if new saved search
195 $params['title'] = $formValues['title'];
196 $params['description'] = $formValues['description'];
197 if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($formValues['group_type'])) {
198 $params['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
199 array_keys($formValues['group_type'])) . CRM_Core_DAO
::VALUE_SEPARATOR
;
202 $params['group_type'] = '';
204 $params['visibility'] = 'User and User Admin Only';
205 $params['saved_search_id'] = $savedSearch->id
;
206 $params['is_active'] = 1;
209 $params['parents'] = $formValues['parents'];
212 $params['id'] = CRM_Contact_BAO_SavedSearch
::getName($this->_id
, 'id');
215 $group = CRM_Contact_BAO_Group
::create($params);
217 // Update mapping with the name and description of the group.
218 if ($mappingId && $group) {
221 'name' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'name', 'id'),
222 'description' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'description', 'id'),
223 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
225 CRM_Core_BAO_Mapping
::add($mappingParams);
229 $this->_id
= $savedSearch->id
;
232 if (!empty($formValues['parents'])) {
233 CRM_Contact_BAO_GroupNestingCache
::update();
242 public function setDefaultValues() {
244 if (empty($defaults['parents'])) {
245 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();