Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
f12c6f7d | 35 | * This class provides the functionality to save a search. |
36 | * | |
6a488035 TO |
37 | * Saved Searches are used for saving frequently used queries |
38 | */ | |
39 | class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task { | |
40 | ||
41 | /** | |
fe482240 | 42 | * Saved search id if any. |
6a488035 TO |
43 | * |
44 | * @var int | |
45 | */ | |
46 | protected $_id; | |
47 | ||
48 | /** | |
fe482240 | 49 | * Build all the data structures needed to build the form. |
95ea96be | 50 | */ |
d5cc0fc2 | 51 | public function preProcess() { |
6a488035 TO |
52 | $this->_id = NULL; |
53 | ||
54 | // get the submitted values of the search form | |
55 | // we'll need to get fv from either search or adv search in the future | |
56 | if ($this->_action == CRM_Core_Action::ADVANCED) { | |
57 | $values = $this->controller->exportValues('Advanced'); | |
58 | } | |
59 | elseif ($this->_action == CRM_Core_Action::PROFILE) { | |
60 | $values = $this->controller->exportValues('Builder'); | |
61 | } | |
62 | elseif ($this->_action == CRM_Core_Action::COPY) { | |
63 | $values = $this->controller->exportValues('Custom'); | |
64 | } | |
65 | else { | |
66 | $values = $this->controller->exportValues('Basic'); | |
67 | } | |
68 | ||
eda34f9b MW |
69 | // Get Task name |
70 | $modeValue = CRM_Contact_Form_Search::getModeValue($values['component_mode']); | |
71 | $className = $modeValue['taskClassName']; | |
72 | $taskList = $className::taskTitles(); | |
6a488035 | 73 | $this->_task = CRM_Utils_Array::value('task', $values); |
eda34f9b | 74 | $this->assign('taskName', CRM_Utils_Array::value($this->_task, $taskList)); |
6a488035 TO |
75 | } |
76 | ||
77 | /** | |
f12c6f7d | 78 | * Build the form object. |
79 | * | |
80 | * It consists of | |
6a488035 TO |
81 | * - displaying the QILL (query in local language) |
82 | * - displaying elements for saving the search | |
6a488035 | 83 | */ |
00be9182 | 84 | public function buildQuickForm() { |
35e8e592 | 85 | // @todo sync this more with CRM_Group_Form_Edit. |
6a488035 | 86 | $query = new CRM_Contact_BAO_Query($this->get('queryParams')); |
35e8e592 | 87 | $this->assign('qill', $query->qill()); |
6a488035 | 88 | |
8f83bac7 CW |
89 | // Values from the search form |
90 | $formValues = $this->controller->exportValues(); | |
91 | ||
6a488035 TO |
92 | // the name and description are actually stored with the group and not the saved search |
93 | $this->add('text', 'title', ts('Name'), | |
94 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE | |
95 | ); | |
96 | ||
6a488035 TO |
97 | $this->addElement('textarea', 'description', ts('Description'), |
98 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
99 | ); | |
100 | ||
101 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
102 | unset($groupTypes['Access Control']); | |
103 | if (!CRM_Core_Permission::access('CiviMail')) { | |
104 | $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled(); | |
105 | if ($isWorkFlowEnabled && | |
106 | !CRM_Core_Permission::check('create mailings') && | |
107 | !CRM_Core_Permission::check('schedule mailings') && | |
108 | !CRM_Core_Permission::check('approve mailings') | |
109 | ) { | |
110 | unset($groupTypes['Mailing List']); | |
111 | } | |
112 | } | |
113 | ||
114 | if (!empty($groupTypes)) { | |
115 | $this->addCheckBox('group_type', | |
116 | ts('Group Type'), | |
117 | $groupTypes, | |
118 | NULL, NULL, NULL, NULL, ' ' | |
119 | ); | |
120 | } | |
121 | ||
31c31c92 BS |
122 | //CRM-14190 |
123 | CRM_Group_Form_Edit::buildParentGroups($this); | |
d44e3aea | 124 | CRM_Group_Form_Edit::buildGroupOrganizations($this); |
31c31c92 | 125 | |
6a488035 TO |
126 | // get the group id for the saved search |
127 | $groupID = NULL; | |
128 | if (isset($this->_id)) { | |
129 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
130 | $this->_id, | |
131 | 'id', | |
132 | 'saved_search_id' | |
133 | ); | |
134 | $this->addDefaultButtons(ts('Update Smart Group')); | |
135 | } | |
136 | else { | |
137 | $this->addDefaultButtons(ts('Save Smart Group')); | |
8f83bac7 | 138 | $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all'); |
6a488035 TO |
139 | } |
140 | $this->addRule('title', ts('Name already exists in Database.'), | |
141 | 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title') | |
142 | ); | |
143 | } | |
144 | ||
145 | /** | |
fe482240 | 146 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
147 | */ |
148 | public function postProcess() { | |
149 | // saved search form values | |
150 | // get form values of all the forms in this controller | |
151 | $formValues = $this->controller->exportValues(); | |
152 | ||
153 | $isAdvanced = $this->get('isAdvanced'); | |
154 | $isSearchBuilder = $this->get('isSearchBuilder'); | |
155 | ||
156 | // add mapping record only for search builder saved search | |
157 | $mappingId = NULL; | |
158 | if ($isAdvanced == '2' && $isSearchBuilder == '1') { | |
159 | //save the mapping for search builder | |
160 | ||
161 | if (!$this->_id) { | |
162 | //save record in mapping table | |
c997dfa8 | 163 | $mappingParams = array( |
95f52e3b | 164 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'), |
c997dfa8 TS |
165 | ); |
166 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
353ffa53 | 167 | $mappingId = $mapping->id; |
6a488035 TO |
168 | } |
169 | else { | |
170 | //get the mapping id from saved search | |
171 | ||
172 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
173 | $savedSearch->id = $this->_id; | |
174 | $savedSearch->find(TRUE); | |
175 | $mappingId = $savedSearch->mapping_id; | |
176 | } | |
177 | ||
178 | //save mapping fields | |
179 | CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId); | |
180 | } | |
181 | ||
182 | //save the search | |
183 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
184 | $savedSearch->id = $this->_id; | |
d0f1a3ab | 185 | $queryParams = $this->get('queryParams'); |
3f471f2d | 186 | |
5bc1ea1d | 187 | // Use the query parameters rather than the form values - these have already been assessed / converted |
188 | // with the extra knowledge that the form has. | |
189 | // Note that we want to move towards a standardised way of saving the query that is not | |
190 | // an exact match for the form requirements & task the form layer with converting backwards and forwards. | |
191 | // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the | |
192 | // sql operator syntax at the query layer. | |
b926c88d | 193 | if (!$isSearchBuilder) { |
9451fffa | 194 | CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues); |
3f471f2d | 195 | CRM_Contact_BAO_SavedSearch::saveSkippedElement($queryParams, $formValues); |
d0f1a3ab | 196 | $savedSearch->form_values = serialize($queryParams); |
b926c88d | 197 | } |
198 | else { | |
199 | // We want search builder to be able to convert back & forth at the form layer | |
200 | // to a standardised style - but it can't yet! | |
201 | $savedSearch->form_values = serialize($formValues); | |
202 | } | |
203 | ||
6a488035 TO |
204 | $savedSearch->mapping_id = $mappingId; |
205 | $savedSearch->search_custom_id = $this->get('customSearchID'); | |
206 | $savedSearch->save(); | |
207 | $this->set('ssID', $savedSearch->id); | |
208 | CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success'); | |
209 | ||
210 | // also create a group that is associated with this saved search only if new saved search | |
211 | $params = array(); | |
212 | $params['title'] = $formValues['title']; | |
213 | $params['description'] = $formValues['description']; | |
eda34f9b | 214 | if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($formValues['group_type'])) { |
6a488035 | 215 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
eda34f9b | 216 | array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
217 | } |
218 | else { | |
219 | $params['group_type'] = ''; | |
220 | } | |
221 | $params['visibility'] = 'User and User Admin Only'; | |
222 | $params['saved_search_id'] = $savedSearch->id; | |
223 | $params['is_active'] = 1; | |
224 | ||
31c31c92 BS |
225 | //CRM-14190 |
226 | $params['parents'] = $formValues['parents']; | |
227 | ||
6a488035 TO |
228 | if ($this->_id) { |
229 | $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id'); | |
230 | } | |
231 | ||
f8d96738 | 232 | $group = CRM_Contact_BAO_Group::create($params); |
6a488035 | 233 | |
c997dfa8 | 234 | // Update mapping with the name and description of the group. |
f8d96738 | 235 | if ($mappingId && $group) { |
c997dfa8 TS |
236 | $mappingParams = array( |
237 | 'id' => $mappingId, | |
238 | 'name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $group->id, 'name', 'id'), | |
239 | 'description' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $group->id, 'description', 'id'), | |
95f52e3b | 240 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'), |
c997dfa8 TS |
241 | ); |
242 | CRM_Core_BAO_Mapping::add($mappingParams); | |
243 | } | |
244 | ||
6a488035 TO |
245 | // CRM-9464 |
246 | $this->_id = $savedSearch->id; | |
31c31c92 BS |
247 | |
248 | //CRM-14190 | |
481a74f4 | 249 | if (!empty($formValues['parents'])) { |
31c31c92 BS |
250 | CRM_Contact_BAO_GroupNestingCache::update(); |
251 | } | |
6a488035 | 252 | } |
96025800 | 253 | |
d44e3aea | 254 | /** |
255 | * Set form defaults. | |
256 | * | |
257 | * return array | |
258 | */ | |
259 | public function setDefaultValues() { | |
260 | $defaults = array(); | |
261 | if (empty($defaults['parents'])) { | |
262 | $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId(); | |
263 | } | |
264 | return $defaults; | |
265 | } | |
266 | ||
6a488035 | 267 | } |