fixed spelling of address on lines 122 and 247
[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
MW
55 $className = $modeValue['taskClassName'];
56 $taskList = $className::taskTitles();
9c1bc317 57 $this->_task = $values['task'] ?? NULL;
eda34f9b 58 $this->assign('taskName', CRM_Utils_Array::value($this->_task, $taskList));
6a488035
TO
59 }
60
61 /**
f12c6f7d 62 * Build the form object.
63 *
64 * It consists of
6a488035
TO
65 * - displaying the QILL (query in local language)
66 * - displaying elements for saving the search
6a488035 67 */
00be9182 68 public function buildQuickForm() {
35e8e592 69 // @todo sync this more with CRM_Group_Form_Edit.
6a488035 70 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
35e8e592 71 $this->assign('qill', $query->qill());
6a488035 72
8f83bac7
CW
73 // Values from the search form
74 $formValues = $this->controller->exportValues();
75
6a488035 76 // the name and description are actually stored with the group and not the saved search
34634ee0 77 $this->add('text', 'title', ts('Group Title'),
6a488035
TO
78 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
79 );
80
34634ee0 81 $this->addElement('textarea', 'description', ts('Group Description'),
6a488035
TO
82 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
83 );
84
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')
93 ) {
94 unset($groupTypes['Mailing List']);
95 }
96 }
97
98 if (!empty($groupTypes)) {
99 $this->addCheckBox('group_type',
100 ts('Group Type'),
101 $groupTypes,
102 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
103 );
104 }
105
31c31c92
BS
106 //CRM-14190
107 CRM_Group_Form_Edit::buildParentGroups($this);
d44e3aea 108 CRM_Group_Form_Edit::buildGroupOrganizations($this);
31c31c92 109
6a488035
TO
110 // get the group id for the saved search
111 $groupID = NULL;
112 if (isset($this->_id)) {
113 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
114 $this->_id,
115 'id',
116 'saved_search_id'
117 );
118 $this->addDefaultButtons(ts('Update Smart Group'));
119 }
120 else {
121 $this->addDefaultButtons(ts('Save Smart Group'));
8f83bac7 122 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
6a488035 123 }
34634ee0 124 $this->addRule('title', ts('Group Title already exists in Database.'),
be2fb01f 125 'objectExists', ['CRM_Contact_DAO_Group', $groupID, 'title']
6a488035
TO
126 );
127 }
128
129 /**
fe482240 130 * Process the form after the input has been submitted and validated.
6a488035
TO
131 */
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();
136
137 $isAdvanced = $this->get('isAdvanced');
138 $isSearchBuilder = $this->get('isSearchBuilder');
139
140 // add mapping record only for search builder saved search
141 $mappingId = NULL;
142 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
143 //save the mapping for search builder
144
145 if (!$this->_id) {
146 //save record in mapping table
be2fb01f 147 $mappingParams = [
95f52e3b 148 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
be2fb01f 149 ];
c997dfa8 150 $mapping = CRM_Core_BAO_Mapping::add($mappingParams);
353ffa53 151 $mappingId = $mapping->id;
6a488035
TO
152 }
153 else {
154 //get the mapping id from saved search
155
156 $savedSearch = new CRM_Contact_BAO_SavedSearch();
157 $savedSearch->id = $this->_id;
158 $savedSearch->find(TRUE);
159 $mappingId = $savedSearch->mapping_id;
160 }
161
162 //save mapping fields
163 CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId);
164 }
165
166 //save the search
167 $savedSearch = new CRM_Contact_BAO_SavedSearch();
168 $savedSearch->id = $this->_id;
d0f1a3ab 169 $queryParams = $this->get('queryParams');
3f471f2d 170
5bc1ea1d 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.
b926c88d 177 if (!$isSearchBuilder) {
3f471f2d 178 CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues);
d0f1a3ab 179 $savedSearch->form_values = serialize($queryParams);
b926c88d 180 }
181 else {
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);
185 }
186
6a488035
TO
187 $savedSearch->mapping_id = $mappingId;
188 $savedSearch->search_custom_id = $this->get('customSearchID');
189 $savedSearch->save();
190 $this->set('ssID', $savedSearch->id);
be2fb01f 191 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", [1 => $formValues['title']]), ts('Group Saved'), 'success');
6a488035
TO
192
193 // also create a group that is associated with this saved search only if new saved search
be2fb01f 194 $params = [];
6a488035
TO
195 $params['title'] = $formValues['title'];
196 $params['description'] = $formValues['description'];
eda34f9b 197 if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($formValues['group_type'])) {
6a488035 198 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
eda34f9b 199 array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
200 }
201 else {
202 $params['group_type'] = '';
203 }
204 $params['visibility'] = 'User and User Admin Only';
205 $params['saved_search_id'] = $savedSearch->id;
206 $params['is_active'] = 1;
207
31c31c92
BS
208 //CRM-14190
209 $params['parents'] = $formValues['parents'];
210
6a488035
TO
211 if ($this->_id) {
212 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
213 }
214
f8d96738 215 $group = CRM_Contact_BAO_Group::create($params);
6a488035 216
c997dfa8 217 // Update mapping with the name and description of the group.
f8d96738 218 if ($mappingId && $group) {
be2fb01f 219 $mappingParams = [
c997dfa8
TS
220 'id' => $mappingId,
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'),
95f52e3b 223 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
be2fb01f 224 ];
c997dfa8
TS
225 CRM_Core_BAO_Mapping::add($mappingParams);
226 }
227
6a488035
TO
228 // CRM-9464
229 $this->_id = $savedSearch->id;
31c31c92
BS
230
231 //CRM-14190
481a74f4 232 if (!empty($formValues['parents'])) {
31c31c92
BS
233 CRM_Contact_BAO_GroupNestingCache::update();
234 }
6a488035 235 }
96025800 236
d44e3aea 237 /**
238 * Set form defaults.
239 *
240 * return array
241 */
242 public function setDefaultValues() {
be2fb01f 243 $defaults = [];
d44e3aea 244 if (empty($defaults['parents'])) {
245 $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
246 }
247 return $defaults;
248 }
249
6a488035 250}