Merge pull request #4933 from civicrm/typo-corrections
[civicrm-core.git] / CRM / Contact / Form / Task / SaveSearch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class provides the functionality to save a search
38 * Saved Searches are used for saving frequently used queries
39 */
40class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task {
41
42 /**
100fef9d 43 * Saved search id if any
6a488035
TO
44 *
45 * @var int
46 */
47 protected $_id;
48
49 /**
100fef9d 50 * Build all the data structures needed to build the form
6a488035
TO
51 *
52 * @return void
95ea96be
EM
53 */
54 function preProcess() {
6a488035
TO
55 $this->_id = NULL;
56
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');
61 }
62 elseif ($this->_action == CRM_Core_Action::PROFILE) {
63 $values = $this->controller->exportValues('Builder');
64 }
65 elseif ($this->_action == CRM_Core_Action::COPY) {
66 $values = $this->controller->exportValues('Custom');
67 }
68 else {
69 $values = $this->controller->exportValues('Basic');
70 }
71
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));
75 }
76
77 /**
c490a46a 78 * Build the form object - it consists of
6a488035
TO
79 * - displaying the QILL (query in local language)
80 * - displaying elements for saving the search
81 *
6a488035
TO
82 *
83 * @return void
84 */
00be9182 85 public function buildQuickForm() {
6a488035
TO
86 // get the qill
87 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
88 $qill = $query->qill();
89
90 // need to save qill for the smarty template
91 $this->assign('qill', $qill);
92
93 // the name and description are actually stored with the group and not the saved search
94 $this->add('text', 'title', ts('Name'),
95 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
96 );
97
98
99 $this->addElement('textarea', 'description', ts('Description'),
100 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
101 );
102
103 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
104 unset($groupTypes['Access Control']);
105 if (!CRM_Core_Permission::access('CiviMail')) {
106 $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled();
107 if ($isWorkFlowEnabled &&
108 !CRM_Core_Permission::check('create mailings') &&
109 !CRM_Core_Permission::check('schedule mailings') &&
110 !CRM_Core_Permission::check('approve mailings')
111 ) {
112 unset($groupTypes['Mailing List']);
113 }
114 }
115
116 if (!empty($groupTypes)) {
117 $this->addCheckBox('group_type',
118 ts('Group Type'),
119 $groupTypes,
120 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
121 );
122 }
123
31c31c92
BS
124 //CRM-14190
125 CRM_Group_Form_Edit::buildParentGroups($this);
126
6a488035
TO
127 // get the group id for the saved search
128 $groupID = NULL;
129 if (isset($this->_id)) {
130 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
131 $this->_id,
132 'id',
133 'saved_search_id'
134 );
135 $this->addDefaultButtons(ts('Update Smart Group'));
136 }
137 else {
138 $this->addDefaultButtons(ts('Save Smart Group'));
139 }
140 $this->addRule('title', ts('Name already exists in Database.'),
141 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title')
142 );
143 }
144
145 /**
100fef9d 146 * Process the form after the input has been submitted and validated
6a488035 147 *
6a488035
TO
148 *
149 * @return void
150 */
151 public function postProcess() {
152 // saved search form values
153 // get form values of all the forms in this controller
154 $formValues = $this->controller->exportValues();
155
156 $isAdvanced = $this->get('isAdvanced');
157 $isSearchBuilder = $this->get('isSearchBuilder');
158
159 // add mapping record only for search builder saved search
160 $mappingId = NULL;
161 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
162 //save the mapping for search builder
163
164 if (!$this->_id) {
165 //save record in mapping table
166 $mappingParams = array('mapping_type' => 'Search Builder');
353ffa53
TO
167 $temp = array();
168 $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp);
169 $mappingId = $mapping->id;
6a488035
TO
170 }
171 else {
172 //get the mapping id from saved search
173
174 $savedSearch = new CRM_Contact_BAO_SavedSearch();
175 $savedSearch->id = $this->_id;
176 $savedSearch->find(TRUE);
177 $mappingId = $savedSearch->mapping_id;
178 }
179
180 //save mapping fields
181 CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId);
182 }
183
184 //save the search
185 $savedSearch = new CRM_Contact_BAO_SavedSearch();
186 $savedSearch->id = $this->_id;
187 $savedSearch->form_values = serialize($this->get('formValues'));
188 $savedSearch->mapping_id = $mappingId;
189 $savedSearch->search_custom_id = $this->get('customSearchID');
190 $savedSearch->save();
191 $this->set('ssID', $savedSearch->id);
192 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success');
193
194 // also create a group that is associated with this saved search only if new saved search
195 $params = array();
196 $params['title'] = $formValues['title'];
197 $params['description'] = $formValues['description'];
198 if (isset($formValues['group_type']) &&
199 is_array($formValues['group_type'])
200 ) {
201 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
202 array_keys($formValues['group_type'])
203 ) . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
204 }
205 else {
206 $params['group_type'] = '';
207 }
208 $params['visibility'] = 'User and User Admin Only';
209 $params['saved_search_id'] = $savedSearch->id;
210 $params['is_active'] = 1;
211
31c31c92
BS
212 //CRM-14190
213 $params['parents'] = $formValues['parents'];
214
6a488035
TO
215 if ($this->_id) {
216 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
217 }
218
219 $group = CRM_Contact_BAO_Group::create($params);
220
221 // CRM-9464
222 $this->_id = $savedSearch->id;
31c31c92
BS
223
224 //CRM-14190
481a74f4 225 if (!empty($formValues['parents'])) {
31c31c92
BS
226 CRM_Contact_BAO_GroupNestingCache::update();
227 }
6a488035
TO
228 }
229}