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
{
26 public $submitOnce = TRUE;
29 * The params that are sent to the query.
33 protected $_queryParams;
36 * Are we restricting ourselves to a single contact.
40 protected $_single = FALSE;
43 * Are we restricting ourselves to a single contact.
50 * Prefix for the controller.
53 protected $_prefix = 'activity_';
56 * The saved search ID retrieved from the GET vars.
65 public function getDefaultEntity(): string {
70 * Processing needed for buildForm and later.
72 * @throws \CRM_Core_Exception
73 * @throws \CiviCRM_API3_Exception
75 public function preProcess(): void
{
76 $this->set('searchFormName', 'Search');
78 // set the button names
79 $this->_actionButtonName
= $this->getButtonName('next', 'action');
82 $this->sortNameOnly
= TRUE;
86 if (empty($this->_formValues
) && isset($this->_ssID
)) {
87 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
90 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
91 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
99 if ($this->_context
=== 'user') {
100 $prefix = $this->_prefix
;
103 $this->assign("{$prefix}limit", $this->_limit
);
104 $this->assign("{$prefix}single", $this->_single
);
106 $controller = new CRM_Core_Selector_Controller($selector,
107 $this->get(CRM_Utils_Pager
::PAGE_ID
),
109 CRM_Core_Action
::VIEW
,
111 CRM_Core_Selector_Controller
::TRANSFER
,
114 $controller->setEmbedded(TRUE);
115 $controller->moveFromSessionToTemplate();
117 $this->assign('summary', $this->get('summary'));
121 * Build the form object.
123 * @throws \CRM_Core_Exception
124 * @throws \CiviCRM_API3_Exception
126 public function buildQuickForm(): void
{
127 parent
::buildQuickForm();
128 $this->addSortNameField();
130 CRM_Activity_BAO_Query
::buildSearchForm($this);
132 $rows = $this->get('rows');
133 if (is_array($rows)) {
134 if (!$this->_single
) {
135 $this->addRowSelectors($rows);
138 $this->addTaskMenu(CRM_Activity_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
144 * The post processing of the form gets done here.
146 * Key things done during post processing are
147 * - check for reset or next request. if present, skip post procesing.
148 * - now check if user requested running a saved search, if so, then
149 * the form values associated with the saved search are used for searching.
150 * - if user has done a submit with new values the regular post submissing is
153 * The processing consists of using a Selector / Controller framework for getting the
156 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
158 public function postProcess(): void
{
164 $this->setFormValues();
165 if (!empty($_POST)) {
171 'priority_id' => 'activity_priority_id',
174 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams, $changeNames);
176 $this->fixFormValues();
178 if (isset($this->_ssID
) && empty($_POST)) {
179 // if we are editing / running a saved search and the form has not been posted
180 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
183 // We don't show test records in summaries or dashboards
184 if (empty($this->_formValues
['activity_test']) && $this->_force
) {
185 $this->_formValues
['activity_test'] = 0;
188 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
190 $this->set('queryParams', $this->_queryParams
);
192 $buttonName = $this->controller
->getButtonName();
193 if ($buttonName == $this->_actionButtonName
) {
194 // check actionName and if next, then do not repeat a search, since we are going to the next page
195 // hack, make sure we reset the task values
196 $stateMachine = $this->controller
->getStateMachine();
197 $formName = $stateMachine->getTaskFormName();
198 $this->controller
->resetPage($formName);
202 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
204 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
211 $selector->setKey($this->controller
->_key
);
214 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
215 $prefix = $this->_prefix
;
218 $controller = new CRM_Core_Selector_Controller($selector,
219 $this->get(CRM_Utils_Pager
::PAGE_ID
),
221 CRM_Core_Action
::VIEW
,
223 CRM_Core_Selector_Controller
::SESSION
,
226 $controller->setEmbedded(TRUE);
227 $query = &$selector->getQuery();
229 if ($this->_context
=== 'user') {
230 $query->setSkipPermission(TRUE);
236 * Probably more hackery than anything else.
238 * @throws \CRM_Core_Exception
240 public function fixFormValues() {
241 if (!$this->_force
) {
245 $status = CRM_Utils_Request
::retrieve('status', 'String', $this);
247 $this->_formValues
['activity_status_id'] = $status;
248 $this->_defaults
['activity_status_id'] = $status;
251 $survey = CRM_Utils_Request
::retrieve('survey', 'Positive');
254 $this->_formValues
['activity_survey_id'] = $this->_defaults
['activity_survey_id'] = $survey;
255 $sid = $this->_formValues
['activity_survey_id'] ??
NULL;
256 $activity_type_id = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
258 // since checkbox are replaced by multiple select option
259 $this->_formValues
['activity_type_id'] = $activity_type_id;
260 $this->_defaults
['activity_type_id'] = $activity_type_id;
262 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
265 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
267 $this->_formValues
['contact_id'] = $cid;
269 $activity_role = CRM_Utils_Request
::retrieve('activity_role', 'Positive', $this);
271 if ($activity_role) {
272 $this->_formValues
['activity_role'] = $activity_role;
275 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
277 // also assign individual mode to the template
278 $this->_single
= TRUE;
282 // Enable search activity by custom value
283 // @todo this is not good security practice. Instead define entity fields in metadata &
284 // use getEntity Defaults
285 $requestParams = CRM_Utils_Request
::exportValues();
286 foreach (array_keys($requestParams) as $key) {
287 if (substr($key, 0, 7) !== 'custom_') {
290 elseif (empty($requestParams[$key])) {
293 $customValue = CRM_Utils_Request
::retrieve($key, 'String', $this);
295 $this->_formValues
[$key] = $customValue;
296 $this->_defaults
[$key] = $customValue;
300 if (!empty($this->_defaults
)) {
301 $this->setDefaults($this->_defaults
);
306 * Return a descriptive name for the page, used in wizard header
310 public function getTitle() {
311 return ts('Find Activities');
315 * Get metadata for the entity fields.
319 * @throws \CiviCRM_API3_Exception
321 protected function getEntityMetadata() {
322 return CRM_Activity_BAO_Query
::getSearchFieldMetadata();
326 * Set the metadata for the form.
328 protected function setSearchMetadata() {
329 $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]);