3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This file is for activity search.
37 class CRM_Activity_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
64 protected $_prefix = "activity_";
67 * The saved search ID retrieved from the GET vars.
76 public function getDefaultEntity() {
81 * Processing needed for buildForm and later.
83 public function preProcess() {
84 $this->set('searchFormName', 'Search');
86 // set the button names
87 $this->_searchButtonName
= $this->getButtonName('refresh');
88 $this->_actionButtonName
= $this->getButtonName('next', 'action');
92 $this->loadStandardSearchOptionsFromUrl();
94 // get user submitted values
95 // get it from controller only if form has been submitted, else preProcess has set this
96 if (!empty($_POST) && !$this->controller
->isModal()) {
97 $this->_formValues
= $this->controller
->exportValues($this->_name
);
100 $this->_formValues
= $this->get('formValues');
103 // If we force the search then merge form values with url values
104 // and set submit values to form values.
105 // @todo this is not good security practice. Instead define the fields in metadata & use
106 // getEntityDefaults.
107 $this->_formValues
= array_merge((array) $this->_formValues
, CRM_Utils_Request
::exportValues());
108 $this->_submitValues
= $this->_formValues
;
112 if (empty($this->_formValues
)) {
113 if (isset($this->_ssID
)) {
114 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
119 $this->postProcess();
120 $this->set('force', 0);
124 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
125 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
126 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
130 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
131 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
139 if ($this->_context
== 'user') {
140 $prefix = $this->_prefix
;
143 $this->assign("{$prefix}limit", $this->_limit
);
144 $this->assign("{$prefix}single", $this->_single
);
146 $controller = new CRM_Core_Selector_Controller($selector,
147 $this->get(CRM_Utils_Pager
::PAGE_ID
),
149 CRM_Core_Action
::VIEW
,
151 CRM_Core_Selector_Controller
::TRANSFER
,
154 $controller->setEmbedded(TRUE);
155 $controller->moveFromSessionToTemplate();
157 $this->assign('summary', $this->get('summary'));
161 * Build the form object.
163 public function buildQuickForm() {
164 parent
::buildQuickForm();
165 $this->addSortNameField();
167 CRM_Activity_BAO_Query
::buildSearchForm($this);
169 $rows = $this->get('rows');
170 if (is_array($rows)) {
171 if (!$this->_single
) {
172 $this->addRowSelectors($rows);
175 $this->addTaskMenu(CRM_Activity_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
181 * The post processing of the form gets done here.
183 * Key things done during post processing are
184 * - check for reset or next request. if present, skip post procesing.
185 * - now check if user requested running a saved search, if so, then
186 * the form values associated with the saved search are used for searching.
187 * - if user has done a submit with new values the regular post submissing is
190 * The processing consists of using a Selector / Controller framework for getting the
193 public function postProcess() {
200 if (!empty($_POST)) {
201 $this->_formValues
= $this->controller
->exportValues($this->_name
);
209 'status_id' => 'activity_status_id',
210 'priority_id' => 'activity_priority_id',
213 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams, $changeNames);
215 $this->fixFormValues();
217 if (isset($this->_ssID
) && empty($_POST)) {
218 // if we are editing / running a saved search and the form has not been posted
219 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
222 // We don't show test records in summaries or dashboards
223 if (empty($this->_formValues
['activity_test']) && $this->_force
) {
224 $this->_formValues
["activity_test"] = 0;
227 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
229 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
231 $this->set('formValues', $this->_formValues
);
232 $this->set('queryParams', $this->_queryParams
);
234 $buttonName = $this->controller
->getButtonName();
235 if ($buttonName == $this->_actionButtonName
) {
236 // check actionName and if next, then do not repeat a search, since we are going to the next page
237 // hack, make sure we reset the task values
238 $stateMachine = $this->controller
->getStateMachine();
239 $formName = $stateMachine->getTaskFormName();
240 $this->controller
->resetPage($formName);
245 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
246 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
247 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
251 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
253 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
260 $selector->setKey($this->controller
->_key
);
263 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
264 $prefix = $this->_prefix
;
267 $controller = new CRM_Core_Selector_Controller($selector,
268 $this->get(CRM_Utils_Pager
::PAGE_ID
),
270 CRM_Core_Action
::VIEW
,
272 CRM_Core_Selector_Controller
::SESSION
,
275 $controller->setEmbedded(TRUE);
276 $query = &$selector->getQuery();
278 if ($this->_context
== 'user') {
279 $query->setSkipPermission(TRUE);
284 public function fixFormValues() {
285 if (!$this->_force
) {
289 $status = CRM_Utils_Request
::retrieve('status', 'String', $this);
291 $this->_formValues
['activity_status_id'] = $status;
292 $this->_defaults
['activity_status_id'] = $status;
295 $survey = CRM_Utils_Request
::retrieve('survey', 'Positive');
298 $this->_formValues
['activity_survey_id'] = $this->_defaults
['activity_survey_id'] = $survey;
299 $sid = CRM_Utils_Array
::value('activity_survey_id', $this->_formValues
);
300 $activity_type_id = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
302 // since checkbox are replaced by multiple select option
303 $this->_formValues
['activity_type_id'] = $activity_type_id;
304 $this->_defaults
['activity_type_id'] = $activity_type_id;
306 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
309 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
311 $this->_formValues
['contact_id'] = $cid;
313 $activity_role = CRM_Utils_Request
::retrieve('activity_role', 'Positive', $this);
315 if ($activity_role) {
316 $this->_formValues
['activity_role'] = $activity_role;
319 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
321 // also assign individual mode to the template
322 $this->_single
= TRUE;
326 // Enable search activity by custom value
327 // @todo this is not good security practice. Instead define entity fields in metadata &
328 // use getEntity Defaults
329 $requestParams = CRM_Utils_Request
::exportValues();
330 foreach (array_keys($requestParams) as $key) {
331 if (substr($key, 0, 7) != 'custom_') {
334 elseif (empty($requestParams[$key])) {
337 $customValue = CRM_Utils_Request
::retrieve($key, 'String', $this);
339 $this->_formValues
[$key] = $customValue;
340 $this->_defaults
[$key] = $customValue;
344 if (!empty($this->_defaults
)) {
345 $this->setDefaults($this->_defaults
);
352 public function getFormValues() {
357 * Return a descriptive name for the page, used in wizard header
361 public function getTitle() {
362 return ts('Find Activities');
365 protected function getEntityMetadata() {
366 return CRM_Activity_BAO_Query
::getSearchFieldMetadata();