3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is for activity search.
21 class CRM_Activity_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact.
35 protected $_single = FALSE;
38 * Are we restricting ourselves to a single contact.
42 protected $_limit = NULL;
45 * Prefix for the controller.
48 protected $_prefix = "activity_";
51 * The saved search ID retrieved from the GET vars.
60 public function getDefaultEntity() {
65 * Processing needed for buildForm and later.
67 public function preProcess() {
68 $this->set('searchFormName', 'Search');
70 // set the button names
71 $this->_searchButtonName
= $this->getButtonName('refresh');
72 $this->_actionButtonName
= $this->getButtonName('next', 'action');
76 $this->loadStandardSearchOptionsFromUrl();
78 // get user submitted values
79 // get it from controller only if form has been submitted, else preProcess has set this
80 if (!empty($_POST) && !$this->controller
->isModal()) {
81 $this->_formValues
= $this->controller
->exportValues($this->_name
);
84 $this->_formValues
= $this->get('formValues');
87 // If we force the search then merge form values with url values
88 // and set submit values to form values.
89 // @todo this is not good security practice. Instead define the fields in metadata & use
91 $this->_formValues
= array_merge((array) $this->_formValues
, CRM_Utils_Request
::exportValues());
92 $this->_submitValues
= $this->_formValues
;
96 if (empty($this->_formValues
)) {
97 if (isset($this->_ssID
)) {
98 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
103 $this->postProcess();
104 $this->set('force', 0);
108 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
109 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
110 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
114 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
115 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
123 if ($this->_context
== 'user') {
124 $prefix = $this->_prefix
;
127 $this->assign("{$prefix}limit", $this->_limit
);
128 $this->assign("{$prefix}single", $this->_single
);
130 $controller = new CRM_Core_Selector_Controller($selector,
131 $this->get(CRM_Utils_Pager
::PAGE_ID
),
133 CRM_Core_Action
::VIEW
,
135 CRM_Core_Selector_Controller
::TRANSFER
,
138 $controller->setEmbedded(TRUE);
139 $controller->moveFromSessionToTemplate();
141 $this->assign('summary', $this->get('summary'));
145 * Build the form object.
147 public function buildQuickForm() {
148 parent
::buildQuickForm();
149 $this->addSortNameField();
151 CRM_Activity_BAO_Query
::buildSearchForm($this);
153 $rows = $this->get('rows');
154 if (is_array($rows)) {
155 if (!$this->_single
) {
156 $this->addRowSelectors($rows);
159 $this->addTaskMenu(CRM_Activity_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
165 * The post processing of the form gets done here.
167 * Key things done during post processing are
168 * - check for reset or next request. if present, skip post procesing.
169 * - now check if user requested running a saved search, if so, then
170 * the form values associated with the saved search are used for searching.
171 * - if user has done a submit with new values the regular post submissing is
174 * The processing consists of using a Selector / Controller framework for getting the
177 public function postProcess() {
183 $this->setFormValues();
184 if (!empty($_POST)) {
191 'status_id' => 'activity_status_id',
192 'priority_id' => 'activity_priority_id',
195 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams, $changeNames);
197 $this->fixFormValues();
199 if (isset($this->_ssID
) && empty($_POST)) {
200 // if we are editing / running a saved search and the form has not been posted
201 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
204 // We don't show test records in summaries or dashboards
205 if (empty($this->_formValues
['activity_test']) && $this->_force
) {
206 $this->_formValues
["activity_test"] = 0;
209 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
211 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
213 $this->set('formValues', $this->_formValues
);
214 $this->set('queryParams', $this->_queryParams
);
216 $buttonName = $this->controller
->getButtonName();
217 if ($buttonName == $this->_actionButtonName
) {
218 // check actionName and if next, then do not repeat a search, since we are going to the next page
219 // hack, make sure we reset the task values
220 $stateMachine = $this->controller
->getStateMachine();
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller
->resetPage($formName);
227 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
228 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
229 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
233 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
235 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
242 $selector->setKey($this->controller
->_key
);
245 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
246 $prefix = $this->_prefix
;
249 $controller = new CRM_Core_Selector_Controller($selector,
250 $this->get(CRM_Utils_Pager
::PAGE_ID
),
252 CRM_Core_Action
::VIEW
,
254 CRM_Core_Selector_Controller
::SESSION
,
257 $controller->setEmbedded(TRUE);
258 $query = &$selector->getQuery();
260 if ($this->_context
== 'user') {
261 $query->setSkipPermission(TRUE);
266 public function fixFormValues() {
267 if (!$this->_force
) {
271 $status = CRM_Utils_Request
::retrieve('status', 'String', $this);
273 $this->_formValues
['activity_status_id'] = $status;
274 $this->_defaults
['activity_status_id'] = $status;
277 $survey = CRM_Utils_Request
::retrieve('survey', 'Positive');
280 $this->_formValues
['activity_survey_id'] = $this->_defaults
['activity_survey_id'] = $survey;
281 $sid = CRM_Utils_Array
::value('activity_survey_id', $this->_formValues
);
282 $activity_type_id = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
284 // since checkbox are replaced by multiple select option
285 $this->_formValues
['activity_type_id'] = $activity_type_id;
286 $this->_defaults
['activity_type_id'] = $activity_type_id;
288 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
291 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
293 $this->_formValues
['contact_id'] = $cid;
295 $activity_role = CRM_Utils_Request
::retrieve('activity_role', 'Positive', $this);
297 if ($activity_role) {
298 $this->_formValues
['activity_role'] = $activity_role;
301 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
303 // also assign individual mode to the template
304 $this->_single
= TRUE;
308 // Enable search activity by custom value
309 // @todo this is not good security practice. Instead define entity fields in metadata &
310 // use getEntity Defaults
311 $requestParams = CRM_Utils_Request
::exportValues();
312 foreach (array_keys($requestParams) as $key) {
313 if (substr($key, 0, 7) != 'custom_') {
316 elseif (empty($requestParams[$key])) {
319 $customValue = CRM_Utils_Request
::retrieve($key, 'String', $this);
321 $this->_formValues
[$key] = $customValue;
322 $this->_defaults
[$key] = $customValue;
326 if (!empty($this->_defaults
)) {
327 $this->setDefaults($this->_defaults
);
332 * Return a descriptive name for the page, used in wizard header
336 public function getTitle() {
337 return ts('Find Activities');
340 protected function getEntityMetadata() {
341 return CRM_Activity_BAO_Query
::getSearchFieldMetadata();
345 * Set the metadata for the form.
347 protected function setSearchMetadata() {
348 $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]);