(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Contact / Form / Task / SaveSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to save a search.
20 *
21 * Saved Searches are used for saving frequently used queries
22 */
23 class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task {
24
25 /**
26 * Saved search id if any.
27 *
28 * @var int
29 */
30 protected $_id;
31
32 /**
33 * Build all the data structures needed to build the form.
34 */
35 public function preProcess() {
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
53 // Get Task name
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 = CRM_Utils_Array::value('task', $values);
58 $this->assign('taskName', CRM_Utils_Array::value($this->_task, $taskList));
59 }
60
61 /**
62 * Build the form object.
63 *
64 * It consists of
65 * - displaying the QILL (query in local language)
66 * - displaying elements for saving the search
67 */
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());
72
73 // Values from the search form
74 $formValues = $this->controller->exportValues();
75
76 // the name and description are actually stored with the group and not the saved search
77 $this->add('text', 'title', ts('Name'),
78 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
79 );
80
81 $this->addElement('textarea', 'description', ts('Description'),
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
106 //CRM-14190
107 CRM_Group_Form_Edit::buildParentGroups($this);
108 CRM_Group_Form_Edit::buildGroupOrganizations($this);
109
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'));
122 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
123 }
124 $this->addRule('title', ts('Name already exists in Database.'),
125 'objectExists', ['CRM_Contact_DAO_Group', $groupID, 'title']
126 );
127 }
128
129 /**
130 * Process the form after the input has been submitted and validated.
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
147 $mappingParams = [
148 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
149 ];
150 $mapping = CRM_Core_BAO_Mapping::add($mappingParams);
151 $mappingId = $mapping->id;
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;
169 $queryParams = $this->get('queryParams');
170
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);
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
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');
192
193 // also create a group that is associated with this saved search only if new saved search
194 $params = [];
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;
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
208 //CRM-14190
209 $params['parents'] = $formValues['parents'];
210
211 if ($this->_id) {
212 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
213 }
214
215 $group = CRM_Contact_BAO_Group::create($params);
216
217 // Update mapping with the name and description of the group.
218 if ($mappingId && $group) {
219 $mappingParams = [
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'),
223 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'),
224 ];
225 CRM_Core_BAO_Mapping::add($mappingParams);
226 }
227
228 // CRM-9464
229 $this->_id = $savedSearch->id;
230
231 //CRM-14190
232 if (!empty($formValues['parents'])) {
233 CRM_Contact_BAO_GroupNestingCache::update();
234 }
235 }
236
237 /**
238 * Set form defaults.
239 *
240 * return array
241 */
242 public function setDefaultValues() {
243 $defaults = [];
244 if (empty($defaults['parents'])) {
245 $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
246 }
247 return $defaults;
248 }
249
250 }