Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | ||
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 | /** | |
f12c6f7d | 75 | * Build the form object. |
76 | * | |
77 | * It consists of | |
6a488035 TO |
78 | * - displaying the QILL (query in local language) |
79 | * - displaying elements for saving the search | |
6a488035 | 80 | */ |
00be9182 | 81 | public function buildQuickForm() { |
35e8e592 | 82 | // @todo sync this more with CRM_Group_Form_Edit. |
6a488035 | 83 | $query = new CRM_Contact_BAO_Query($this->get('queryParams')); |
35e8e592 | 84 | $this->assign('qill', $query->qill()); |
6a488035 | 85 | |
8f83bac7 CW |
86 | // Values from the search form |
87 | $formValues = $this->controller->exportValues(); | |
88 | ||
6a488035 TO |
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 | |
92 | ); | |
93 | ||
6a488035 TO |
94 | $this->addElement('textarea', 'description', ts('Description'), |
95 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
96 | ); | |
97 | ||
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') | |
106 | ) { | |
107 | unset($groupTypes['Mailing List']); | |
108 | } | |
109 | } | |
110 | ||
111 | if (!empty($groupTypes)) { | |
112 | $this->addCheckBox('group_type', | |
113 | ts('Group Type'), | |
114 | $groupTypes, | |
115 | NULL, NULL, NULL, NULL, ' ' | |
116 | ); | |
117 | } | |
118 | ||
31c31c92 BS |
119 | //CRM-14190 |
120 | CRM_Group_Form_Edit::buildParentGroups($this); | |
121 | ||
6a488035 TO |
122 | // get the group id for the saved search |
123 | $groupID = NULL; | |
124 | if (isset($this->_id)) { | |
125 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
126 | $this->_id, | |
127 | 'id', | |
128 | 'saved_search_id' | |
129 | ); | |
130 | $this->addDefaultButtons(ts('Update Smart Group')); | |
131 | } | |
132 | else { | |
133 | $this->addDefaultButtons(ts('Save Smart Group')); | |
8f83bac7 | 134 | $this->assign('partiallySelected', $formValues['radio_ts'] != 'ts_all'); |
6a488035 TO |
135 | } |
136 | $this->addRule('title', ts('Name already exists in Database.'), | |
137 | 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title') | |
138 | ); | |
139 | } | |
140 | ||
141 | /** | |
fe482240 | 142 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
143 | */ |
144 | public function postProcess() { | |
145 | // saved search form values | |
146 | // get form values of all the forms in this controller | |
147 | $formValues = $this->controller->exportValues(); | |
148 | ||
149 | $isAdvanced = $this->get('isAdvanced'); | |
150 | $isSearchBuilder = $this->get('isSearchBuilder'); | |
151 | ||
152 | // add mapping record only for search builder saved search | |
153 | $mappingId = NULL; | |
154 | if ($isAdvanced == '2' && $isSearchBuilder == '1') { | |
155 | //save the mapping for search builder | |
156 | ||
157 | if (!$this->_id) { | |
158 | //save record in mapping table | |
159 | $mappingParams = array('mapping_type' => 'Search Builder'); | |
353ffa53 TO |
160 | $temp = array(); |
161 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); | |
162 | $mappingId = $mapping->id; | |
6a488035 TO |
163 | } |
164 | else { | |
165 | //get the mapping id from saved search | |
166 | ||
167 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
168 | $savedSearch->id = $this->_id; | |
169 | $savedSearch->find(TRUE); | |
170 | $mappingId = $savedSearch->mapping_id; | |
171 | } | |
172 | ||
173 | //save mapping fields | |
174 | CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId); | |
175 | } | |
176 | ||
177 | //save the search | |
178 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
179 | $savedSearch->id = $this->_id; | |
d0f1a3ab | 180 | $queryParams = $this->get('queryParams'); |
181 | // CRM-18585 include selected operator in $savedSearch->form_values | |
182 | if (!empty($formValues['operator'])) { | |
183 | $queryParams[] = array('operator', '=', $formValues['operator'], 0, 0); | |
184 | } | |
5bc1ea1d | 185 | // Use the query parameters rather than the form values - these have already been assessed / converted |
186 | // with the extra knowledge that the form has. | |
187 | // Note that we want to move towards a standardised way of saving the query that is not | |
188 | // an exact match for the form requirements & task the form layer with converting backwards and forwards. | |
189 | // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the | |
190 | // sql operator syntax at the query layer. | |
b926c88d | 191 | if (!$isSearchBuilder) { |
9451fffa | 192 | CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues); |
d0f1a3ab | 193 | $savedSearch->form_values = serialize($queryParams); |
b926c88d | 194 | } |
195 | else { | |
196 | // We want search builder to be able to convert back & forth at the form layer | |
197 | // to a standardised style - but it can't yet! | |
198 | $savedSearch->form_values = serialize($formValues); | |
199 | } | |
200 | ||
6a488035 TO |
201 | $savedSearch->mapping_id = $mappingId; |
202 | $savedSearch->search_custom_id = $this->get('customSearchID'); | |
203 | $savedSearch->save(); | |
204 | $this->set('ssID', $savedSearch->id); | |
205 | CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success'); | |
206 | ||
207 | // also create a group that is associated with this saved search only if new saved search | |
208 | $params = array(); | |
209 | $params['title'] = $formValues['title']; | |
210 | $params['description'] = $formValues['description']; | |
211 | if (isset($formValues['group_type']) && | |
212 | is_array($formValues['group_type']) | |
213 | ) { | |
214 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
215 | array_keys($formValues['group_type']) |
216 | ) . 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 | ||
35e8e592 | 232 | CRM_Contact_BAO_Group::create($params); |
6a488035 TO |
233 | |
234 | // CRM-9464 | |
235 | $this->_id = $savedSearch->id; | |
31c31c92 BS |
236 | |
237 | //CRM-14190 | |
481a74f4 | 238 | if (!empty($formValues['parents'])) { |
31c31c92 BS |
239 | CRM_Contact_BAO_GroupNestingCache::update(); |
240 | } | |
6a488035 | 241 | } |
96025800 | 242 | |
6a488035 | 243 | } |