3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This class provides the functionality to save a search
38 * Saved Searches are used for saving frequently used queries
40 class CRM_Contact_Form_Task_SaveSearch
extends CRM_Contact_Form_Task
{
43 * Saved search id if any.
50 * Build all the data structures needed to build the form.
54 public function preProcess() {
57 // get the submitted values of the search form
58 // we'll need to get fv from either search or adv search in the future
59 if ($this->_action
== CRM_Core_Action
::ADVANCED
) {
60 $values = $this->controller
->exportValues('Advanced');
62 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
63 $values = $this->controller
->exportValues('Builder');
65 elseif ($this->_action
== CRM_Core_Action
::COPY
) {
66 $values = $this->controller
->exportValues('Custom');
69 $values = $this->controller
->exportValues('Basic');
72 $this->_task
= CRM_Utils_Array
::value('task', $values);
73 $crmContactTaskTasks = CRM_Contact_Task
::taskTitles();
74 $this->assign('taskName', CRM_Utils_Array
::value($this->_task
, $crmContactTaskTasks));
78 * Build the form object - it consists of
79 * - displaying the QILL (query in local language)
80 * - displaying elements for saving the search
85 public function buildQuickForm() {
87 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
88 $qill = $query->qill();
90 // Values from the search form
91 $formValues = $this->controller
->exportValues();
93 // need to save qill for the smarty template
94 $this->assign('qill', $qill);
96 // the name and description are actually stored with the group and not the saved search
97 $this->add('text', 'title', ts('Name'),
98 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
101 $this->addElement('textarea', 'description', ts('Description'),
102 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'description')
105 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
106 unset($groupTypes['Access Control']);
107 if (!CRM_Core_Permission
::access('CiviMail')) {
108 $isWorkFlowEnabled = CRM_Mailing_Info
::workflowEnabled();
109 if ($isWorkFlowEnabled &&
110 !CRM_Core_Permission
::check('create mailings') &&
111 !CRM_Core_Permission
::check('schedule mailings') &&
112 !CRM_Core_Permission
::check('approve mailings')
114 unset($groupTypes['Mailing List']);
118 if (!empty($groupTypes)) {
119 $this->addCheckBox('group_type',
122 NULL, NULL, NULL, NULL, ' '
127 CRM_Group_Form_Edit
::buildParentGroups($this);
129 // get the group id for the saved search
131 if (isset($this->_id
)) {
132 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Group',
137 $this->addDefaultButtons(ts('Update Smart Group'));
140 $this->addDefaultButtons(ts('Save Smart Group'));
141 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
143 $this->addRule('title', ts('Name already exists in Database.'),
144 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title')
149 * Process the form after the input has been submitted and validated.
154 public function postProcess() {
155 // saved search form values
156 // get form values of all the forms in this controller
157 $formValues = $this->controller
->exportValues();
159 $isAdvanced = $this->get('isAdvanced');
160 $isSearchBuilder = $this->get('isSearchBuilder');
162 // add mapping record only for search builder saved search
164 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
165 //save the mapping for search builder
168 //save record in mapping table
169 $mappingParams = array('mapping_type' => 'Search Builder');
171 $mapping = CRM_Core_BAO_Mapping
::add($mappingParams, $temp);
172 $mappingId = $mapping->id
;
175 //get the mapping id from saved search
177 $savedSearch = new CRM_Contact_BAO_SavedSearch();
178 $savedSearch->id
= $this->_id
;
179 $savedSearch->find(TRUE);
180 $mappingId = $savedSearch->mapping_id
;
183 //save mapping fields
184 CRM_Core_BAO_Mapping
::saveMappingFields($formValues, $mappingId);
188 $savedSearch = new CRM_Contact_BAO_SavedSearch();
189 $savedSearch->id
= $this->_id
;
190 $savedSearch->form_values
= serialize($this->get('formValues'));
191 $savedSearch->mapping_id
= $mappingId;
192 $savedSearch->search_custom_id
= $this->get('customSearchID');
193 $savedSearch->save();
194 $this->set('ssID', $savedSearch->id
);
195 CRM_Core_Session
::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success');
197 // also create a group that is associated with this saved search only if new saved search
199 $params['title'] = $formValues['title'];
200 $params['description'] = $formValues['description'];
201 if (isset($formValues['group_type']) &&
202 is_array($formValues['group_type'])
204 $params['group_type'] = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
205 array_keys($formValues['group_type'])
206 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
209 $params['group_type'] = '';
211 $params['visibility'] = 'User and User Admin Only';
212 $params['saved_search_id'] = $savedSearch->id
;
213 $params['is_active'] = 1;
216 $params['parents'] = $formValues['parents'];
219 $params['id'] = CRM_Contact_BAO_SavedSearch
::getName($this->_id
, 'id');
222 $group = CRM_Contact_BAO_Group
::create($params);
225 $this->_id
= $savedSearch->id
;
228 if (!empty($formValues['parents'])) {
229 CRM_Contact_BAO_GroupNestingCache
::update();