Merge pull request #7764 from pradpnayak/CRM-16617-master
[civicrm-core.git] / CRM / Contact / Form / Task / SaveSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * This class provides the functionality to save a search.
36 *
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 /**
42 * Saved search id if any.
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * Build all the data structures needed to build the form.
50 */
51 public function preProcess() {
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
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));
72 }
73
74 /**
75 * Build the form object.
76 *
77 * It consists of
78 * - displaying the QILL (query in local language)
79 * - displaying elements for saving the search
80 */
81 public function buildQuickForm() {
82 // get the qill
83 $query = new CRM_Contact_BAO_Query($this->get('queryParams'));
84 $qill = $query->qill();
85
86 // Values from the search form
87 $formValues = $this->controller->exportValues();
88
89 // need to save qill for the smarty template
90 $this->assign('qill', $qill);
91
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
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, '&nbsp;&nbsp;&nbsp;'
119 );
120 }
121
122 //CRM-14190
123 CRM_Group_Form_Edit::buildParentGroups($this);
124
125 // get the group id for the saved search
126 $groupID = NULL;
127 if (isset($this->_id)) {
128 $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
129 $this->_id,
130 'id',
131 'saved_search_id'
132 );
133 $this->addDefaultButtons(ts('Update Smart Group'));
134 }
135 else {
136 $this->addDefaultButtons(ts('Save Smart Group'));
137 $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all');
138 }
139 $this->addRule('title', ts('Name already exists in Database.'),
140 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title')
141 );
142 }
143
144 /**
145 * Process the form after the input has been submitted and validated.
146 */
147 public function postProcess() {
148 // saved search form values
149 // get form values of all the forms in this controller
150 $formValues = $this->controller->exportValues();
151
152 $isAdvanced = $this->get('isAdvanced');
153 $isSearchBuilder = $this->get('isSearchBuilder');
154
155 // add mapping record only for search builder saved search
156 $mappingId = NULL;
157 if ($isAdvanced == '2' && $isSearchBuilder == '1') {
158 //save the mapping for search builder
159
160 if (!$this->_id) {
161 //save record in mapping table
162 $mappingParams = array('mapping_type' => 'Search Builder');
163 $temp = array();
164 $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp);
165 $mappingId = $mapping->id;
166 }
167 else {
168 //get the mapping id from saved search
169
170 $savedSearch = new CRM_Contact_BAO_SavedSearch();
171 $savedSearch->id = $this->_id;
172 $savedSearch->find(TRUE);
173 $mappingId = $savedSearch->mapping_id;
174 }
175
176 //save mapping fields
177 CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId);
178 }
179
180 //save the search
181 $savedSearch = new CRM_Contact_BAO_SavedSearch();
182 $savedSearch->id = $this->_id;
183 // Use the query parameters rather than the form values - these have already been assessed / converted
184 // with the extra knowledge that the form has.
185 // Note that we want to move towards a standardised way of saving the query that is not
186 // an exact match for the form requirements & task the form layer with converting backwards and forwards.
187 // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the
188 // sql operator syntax at the query layer.
189 if (!$isSearchBuilder) {
190 $savedSearch->form_values = serialize($this->get('queryParams'));
191 }
192 else {
193 // We want search builder to be able to convert back & forth at the form layer
194 // to a standardised style - but it can't yet!
195 $savedSearch->form_values = serialize($formValues);
196 }
197
198 $savedSearch->mapping_id = $mappingId;
199 $savedSearch->search_custom_id = $this->get('customSearchID');
200 $savedSearch->save();
201 $this->set('ssID', $savedSearch->id);
202 CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success');
203
204 // also create a group that is associated with this saved search only if new saved search
205 $params = array();
206 $params['title'] = $formValues['title'];
207 $params['description'] = $formValues['description'];
208 if (isset($formValues['group_type']) &&
209 is_array($formValues['group_type'])
210 ) {
211 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
212 array_keys($formValues['group_type'])
213 ) . CRM_Core_DAO::VALUE_SEPARATOR;
214 }
215 else {
216 $params['group_type'] = '';
217 }
218 $params['visibility'] = 'User and User Admin Only';
219 $params['saved_search_id'] = $savedSearch->id;
220 $params['is_active'] = 1;
221
222 //CRM-14190
223 $params['parents'] = $formValues['parents'];
224
225 if ($this->_id) {
226 $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id');
227 }
228
229 $group = CRM_Contact_BAO_Group::create($params);
230
231 // CRM-9464
232 $this->_id = $savedSearch->id;
233
234 //CRM-14190
235 if (!empty($formValues['parents'])) {
236 CRM_Contact_BAO_GroupNestingCache::update();
237 }
238 }
239
240 }