3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This class provides the functionality to save a search.
37 * Saved Searches are used for saving frequently used queries
39 class CRM_Contact_Form_Task_SaveSearch
extends CRM_Contact_Form_Task
{
42 * Saved search id if any.
49 * Build all the data structures needed to build the form.
51 public function preProcess() {
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');
59 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
60 $values = $this->controller
->exportValues('Builder');
62 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
63 $values = $this->controller
->exportValues('Custom');
66 $values = $this->controller
->exportValues('Basic');
69 $this->_task
= CRM_Utils_Array
::value('task', $values);
70 $crmContactTaskTasks = CRM_Contact_Task
::taskTitles();
71 $this->assign('taskName', CRM_Utils_Array
::value($this->_task
, $crmContactTaskTasks));
75 * Build the form object.
78 * - displaying the QILL (query in local language)
79 * - displaying elements for saving the search
81 public function buildQuickForm() {
82 // @todo sync this more with CRM_Group_Form_Edit.
83 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
84 $this->assign('qill', $query->qill());
86 // Values from the search form
87 $formValues = $this->controller
->exportValues();
89 // the name and description are actually stored with the group and not the saved search
90 $this->add('text', 'title', ts('Name'),
91 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
94 $this->addElement('textarea', 'description', ts('Description'),
95 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
98 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
99 unset($groupTypes['Access Control']);
100 if (!CRM_Core_Permission
::access('CiviMail')) {
101 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
102 if ($isWorkFlowEnabled &&
103 !CRM_Core_Permission
::check('create mailings') &&
104 !CRM_Core_Permission
::check('schedule mailings') &&
105 !CRM_Core_Permission
::check('approve mailings')
107 unset($groupTypes['Mailing List']);
111 if (!empty($groupTypes)) {
112 $this->addCheckBox('group_type',
115 NULL, NULL, NULL, NULL, ' '
120 CRM_Group_Form_Edit
::buildParentGroups($this);
121 CRM_Group_Form_Edit
::buildGroupOrganizations($this);
123 // get the group id for the saved search
125 if (isset($this->_id
)) {
126 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
131 $this->addDefaultButtons(ts('Update Smart Group'));
134 $this->addDefaultButtons(ts('Save Smart Group'));
135 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
137 $this->addRule('title', ts('Name already exists in Database.'),
138 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title')
143 * Process the form after the input has been submitted and validated.
145 public function postProcess() {
146 // saved search form values
147 // get form values of all the forms in this controller
148 $formValues = $this->controller
->exportValues();
150 $isAdvanced = $this->get('isAdvanced');
151 $isSearchBuilder = $this->get('isSearchBuilder');
153 // add mapping record only for search builder saved search
155 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
156 //save the mapping for search builder
159 //save record in mapping table
160 $mappingParams = array(
161 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
166 $mapping = CRM_Core_BAO_Mapping
::add($mappingParams);
167 $mappingId = $mapping->id
;
170 //get the mapping id from saved search
172 $savedSearch = new CRM_Contact_BAO_SavedSearch();
173 $savedSearch->id
= $this->_id
;
174 $savedSearch->find(TRUE);
175 $mappingId = $savedSearch->mapping_id
;
178 //save mapping fields
179 CRM_Core_BAO_Mapping
::saveMappingFields($formValues, $mappingId);
183 $savedSearch = new CRM_Contact_BAO_SavedSearch();
184 $savedSearch->id
= $this->_id
;
185 $queryParams = $this->get('queryParams');
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.
193 if (!$isSearchBuilder) {
194 CRM_Contact_BAO_SavedSearch
::saveRelativeDates($queryParams, $formValues);
195 CRM_Contact_BAO_SavedSearch
::saveSkippedElement($queryParams, $formValues);
196 $savedSearch->form_values
= serialize($queryParams);
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);
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');
210 // also create a group that is associated with this saved search only if new saved search
212 $params['title'] = $formValues['title'];
213 $params['description'] = $formValues['description'];
214 if (isset($formValues['group_type']) &&
215 is_array($formValues['group_type'])
217 $params['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
218 array_keys($formValues['group_type'])
219 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
222 $params['group_type'] = '';
224 $params['visibility'] = 'User and User Admin Only';
225 $params['saved_search_id'] = $savedSearch->id
;
226 $params['is_active'] = 1;
229 $params['parents'] = $formValues['parents'];
232 $params['id'] = CRM_Contact_BAO_SavedSearch
::getName($this->_id
, 'id');
235 $group = CRM_Contact_BAO_Group
::create($params);
237 // Update mapping with the name and description of the group.
238 if ($mappingId && $group) {
239 $mappingParams = array(
241 'name' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'name', 'id'),
242 'description' => CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group', $group->id
, 'description', 'id'),
243 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
248 CRM_Core_BAO_Mapping
::add($mappingParams);
252 $this->_id
= $savedSearch->id
;
255 if (!empty($formValues['parents'])) {
256 CRM_Contact_BAO_GroupNestingCache
::update();
265 public function setDefaultValues() {
267 if (empty($defaults['parents'])) {
268 $defaults['parents'] = CRM_Core_BAO_Domain
::getGroupId();