Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | */ | |
40 | class CRM_Contact_Form_Task_SaveSearch extends CRM_Contact_Form_Task { | |
41 | ||
42 | /** | |
fe482240 | 43 | * Saved search id if any. |
6a488035 TO |
44 | * |
45 | * @var int | |
46 | */ | |
47 | protected $_id; | |
48 | ||
49 | /** | |
fe482240 | 50 | * Build all the data structures needed to build the form. |
6a488035 TO |
51 | * |
52 | * @return void | |
95ea96be | 53 | */ |
d5cc0fc2 | 54 | public 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 | ||
6a488035 TO |
98 | $this->addElement('textarea', 'description', ts('Description'), |
99 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description') | |
100 | ); | |
101 | ||
102 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); | |
103 | unset($groupTypes['Access Control']); | |
104 | if (!CRM_Core_Permission::access('CiviMail')) { | |
105 | $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled(); | |
106 | if ($isWorkFlowEnabled && | |
107 | !CRM_Core_Permission::check('create mailings') && | |
108 | !CRM_Core_Permission::check('schedule mailings') && | |
109 | !CRM_Core_Permission::check('approve mailings') | |
110 | ) { | |
111 | unset($groupTypes['Mailing List']); | |
112 | } | |
113 | } | |
114 | ||
115 | if (!empty($groupTypes)) { | |
116 | $this->addCheckBox('group_type', | |
117 | ts('Group Type'), | |
118 | $groupTypes, | |
119 | NULL, NULL, NULL, NULL, ' ' | |
120 | ); | |
121 | } | |
122 | ||
31c31c92 BS |
123 | //CRM-14190 |
124 | CRM_Group_Form_Edit::buildParentGroups($this); | |
125 | ||
6a488035 TO |
126 | // get the group id for the saved search |
127 | $groupID = NULL; | |
128 | if (isset($this->_id)) { | |
129 | $groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
130 | $this->_id, | |
131 | 'id', | |
132 | 'saved_search_id' | |
133 | ); | |
134 | $this->addDefaultButtons(ts('Update Smart Group')); | |
135 | } | |
136 | else { | |
137 | $this->addDefaultButtons(ts('Save Smart Group')); | |
138 | } | |
139 | $this->addRule('title', ts('Name already exists in Database.'), | |
140 | 'objectExists', array('CRM_Contact_DAO_Group', $groupID, 'title') | |
141 | ); | |
142 | } | |
143 | ||
144 | /** | |
fe482240 | 145 | * Process the form after the input has been submitted and validated. |
6a488035 | 146 | * |
6a488035 TO |
147 | * |
148 | * @return void | |
149 | */ | |
150 | public function postProcess() { | |
151 | // saved search form values | |
152 | // get form values of all the forms in this controller | |
153 | $formValues = $this->controller->exportValues(); | |
154 | ||
155 | $isAdvanced = $this->get('isAdvanced'); | |
156 | $isSearchBuilder = $this->get('isSearchBuilder'); | |
157 | ||
158 | // add mapping record only for search builder saved search | |
159 | $mappingId = NULL; | |
160 | if ($isAdvanced == '2' && $isSearchBuilder == '1') { | |
161 | //save the mapping for search builder | |
162 | ||
163 | if (!$this->_id) { | |
164 | //save record in mapping table | |
165 | $mappingParams = array('mapping_type' => 'Search Builder'); | |
353ffa53 TO |
166 | $temp = array(); |
167 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); | |
168 | $mappingId = $mapping->id; | |
6a488035 TO |
169 | } |
170 | else { | |
171 | //get the mapping id from saved search | |
172 | ||
173 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
174 | $savedSearch->id = $this->_id; | |
175 | $savedSearch->find(TRUE); | |
176 | $mappingId = $savedSearch->mapping_id; | |
177 | } | |
178 | ||
179 | //save mapping fields | |
180 | CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId); | |
181 | } | |
182 | ||
183 | //save the search | |
184 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
185 | $savedSearch->id = $this->_id; | |
186 | $savedSearch->form_values = serialize($this->get('formValues')); | |
187 | $savedSearch->mapping_id = $mappingId; | |
188 | $savedSearch->search_custom_id = $this->get('customSearchID'); | |
189 | $savedSearch->save(); | |
190 | $this->set('ssID', $savedSearch->id); | |
191 | CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success'); | |
192 | ||
193 | // also create a group that is associated with this saved search only if new saved search | |
194 | $params = array(); | |
195 | $params['title'] = $formValues['title']; | |
196 | $params['description'] = $formValues['description']; | |
197 | if (isset($formValues['group_type']) && | |
198 | is_array($formValues['group_type']) | |
199 | ) { | |
200 | $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
201 | array_keys($formValues['group_type']) |
202 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
203 | } |
204 | else { | |
205 | $params['group_type'] = ''; | |
206 | } | |
207 | $params['visibility'] = 'User and User Admin Only'; | |
208 | $params['saved_search_id'] = $savedSearch->id; | |
209 | $params['is_active'] = 1; | |
210 | ||
31c31c92 BS |
211 | //CRM-14190 |
212 | $params['parents'] = $formValues['parents']; | |
213 | ||
6a488035 TO |
214 | if ($this->_id) { |
215 | $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id'); | |
216 | } | |
217 | ||
218 | $group = CRM_Contact_BAO_Group::create($params); | |
219 | ||
220 | // CRM-9464 | |
221 | $this->_id = $savedSearch->id; | |
31c31c92 BS |
222 | |
223 | //CRM-14190 | |
481a74f4 | 224 | if (!empty($formValues['parents'])) { |
31c31c92 BS |
225 | CRM_Contact_BAO_GroupNestingCache::update(); |
226 | } | |
6a488035 | 227 | } |
96025800 | 228 | |
6a488035 | 229 | } |