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