Merge pull request #18857 from eileenmcnaughton/sort2
[civicrm-core.git] / CRM / Contact / Form / Task / SaveSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
f12c6f7d 19 * This class provides the functionality to save a search.
20 *
6a488035
TO
21 * Saved Searches are used for saving frequently used queries
22 */
23class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task {
24
25 /**
fe482240 26 * Saved search id if any.
6a488035
TO
27 *
28 * @var int
29 */
30 protected $_id;
31
32 /**
fe482240 33 * Build all the data structures needed to build the form.
95ea96be 34 */
d5cc0fc2 35 public function preProcess() {
6a488035
TO
36 $this->_id = NULL;
37
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');
42 }
43 elseif ($this->_action == CRM_Core_Action::PROFILE) {
44 $values = $this->controller->exportValues('Builder');
45 }
46 elseif ($this->_action == CRM_Core_Action::COPY) {
47 $values = $this->controller->exportValues('Custom');
48 }
49 else {
50 $values = $this->controller->exportValues('Basic');
51 }
52
eda34f9b 53 // Get Task name
e3764701 54 $modeValue = CRM_Contact_Form_Search::getModeValue(CRM_Utils_Array::value('component_mode', $values, CRM_Contact_BAO_Query::MODE_CONTACTS));
eda34f9b 55 $className = $modeValue['taskClassName'];
9c1bc317 56 $this->_task = $values['task'] ?? NULL;
6a488035
TO
57 }
58
59 /**
f12c6f7d 60 * Build the form object.
61 *
62 * It consists of
6a488035
TO
63 * - displaying the QILL (query in local language)
64 * - displaying elements for saving the search
6a488035 65 */
00be9182 66 public function buildQuickForm() {
35e8e592 67 // @todo sync this more with CRM_Group_Form_Edit.
6a488035 68 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
35e8e592 69 $this->assign('qill', $query->qill());
6a488035 70
8f83bac7
CW
71 // Values from the search form
72 $formValues = $this->controller->exportValues();
73
6a488035 74 // the name and description are actually stored with the group and not the saved search
34634ee0 75 $this->add('text', 'title', ts('Group Title'),
6a488035
TO
76 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
77 );
78
34634ee0 79 $this->addElement('textarea', 'description', ts('Group Description'),
6a488035
TO
80 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
81 );
82
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')
91 ) {
92 unset($groupTypes['Mailing List']);
93 }
94 }
95
96 if (!empty($groupTypes)) {
97 $this->addCheckBox('group_type',
98 ts('Group Type'),
99 $groupTypes,
100 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
101 );
102 }
103
31c31c92
BS
104 //CRM-14190
105 CRM_Group_Form_Edit::buildParentGroups($this);
d44e3aea 106 CRM_Group_Form_Edit::buildGroupOrganizations($this);
31c31c92 107
6a488035
TO
108 // get the group id for the saved search
109 $groupID = NULL;
110 if (isset($this->_id)) {
111 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
112 $this->_id,
113 'id',
114 'saved_search_id'
115 );
116 $this->addDefaultButtons(ts('Update Smart Group'));
117 }
118 else {
119 $this->addDefaultButtons(ts('Save Smart Group'));
8f83bac7 120 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
6a488035 121 }
34634ee0 122 $this->addRule('title', ts('Group Title already exists in Database.'),
be2fb01f 123 'objectExists', ['CRM_Contact_DAO_Group', $groupID, 'title']
6a488035
TO
124 );
125 }
126
127 /**
fe482240 128 * Process the form after the input has been submitted and validated.
6a488035
TO
129 */
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();
134
135 $isAdvanced = $this->get('isAdvanced');
136 $isSearchBuilder = $this->get('isSearchBuilder');
137
138 // add mapping record only for search builder saved search
139 $mappingId = NULL;
140 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
141 //save the mapping for search builder
142
143 if (!$this->_id) {
144 //save record in mapping table
be2fb01f 145 $mappingParams = [
95f52e3b 146 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
be2fb01f 147 ];
c997dfa8 148 $mapping = CRM_Core_BAO_Mapping::add($mappingParams);
353ffa53 149 $mappingId = $mapping->id;
6a488035
TO
150 }
151 else {
152 //get the mapping id from saved search
153
154 $savedSearch = new CRM_Contact_BAO_SavedSearch();
155 $savedSearch->id = $this->_id;
156 $savedSearch->find(TRUE);
157 $mappingId = $savedSearch->mapping_id;
158 }
159
160 //save mapping fields
161 CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId);
162 }
163
164 //save the search
165 $savedSearch = new CRM_Contact_BAO_SavedSearch();
166 $savedSearch->id = $this->_id;
d0f1a3ab 167 $queryParams = $this->get('queryParams');
3f471f2d 168
5bc1ea1d 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.
b926c88d 175 if (!$isSearchBuilder) {
3f471f2d 176 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
d0f1a3ab 177 $savedSearch->form_values = serialize($queryParams);
b926c88d 178 }
179 else {
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);
183 }
184
6a488035
TO
185 $savedSearch->mapping_id = $mappingId;
186 $savedSearch->search_custom_id = $this->get('customSearchID');
187 $savedSearch->save();
188 $this->set('ssID', $savedSearch->id);
be2fb01f 189 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Group Saved'), 'success');
6a488035
TO
190
191 // also create a group that is associated with this saved search only if new saved search
be2fb01f 192 $params = [];
6a488035
TO
193 $params['title'] = $formValues['title'];
194 $params['description'] = $formValues['description'];
eda34f9b 195 if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($formValues['group_type'])) {
6a488035 196 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
eda34f9b 197 array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
198 }
199 else {
200 $params['group_type'] = '';
201 }
202 $params['visibility'] = 'User and User Admin Only';
203 $params['saved_search_id'] = $savedSearch->id;
204 $params['is_active'] = 1;
205
31c31c92
BS
206 //CRM-14190
207 $params['parents'] = $formValues['parents'];
208
6a488035
TO
209 if ($this->_id) {
210 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
211 }
212
f8d96738 213 $group = CRM_Contact_BAO_Group::create($params);
6a488035 214
c997dfa8 215 // Update mapping with the name and description of the group.
f8d96738 216 if ($mappingId && $group) {
be2fb01f 217 $mappingParams = [
c997dfa8
TS
218 'id' => $mappingId,
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'),
95f52e3b 221 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
be2fb01f 222 ];
c997dfa8
TS
223 CRM_Core_BAO_Mapping::add($mappingParams);
224 }
225
6a488035
TO
226 // CRM-9464
227 $this->_id = $savedSearch->id;
31c31c92
BS
228
229 //CRM-14190
481a74f4 230 if (!empty($formValues['parents'])) {
31c31c92
BS
231 CRM_Contact_BAO_GroupNestingCache::update();
232 }
6a488035 233 }
96025800 234
d44e3aea 235 /**
236 * Set form defaults.
237 *
238 * return array
239 */
240 public function setDefaultValues() {
be2fb01f 241 $defaults = [];
d44e3aea 242 if (empty($defaults['parents'])) {
243 $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
244 }
245 return $defaults;
246 }
247
6a488035 248}