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.
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 * @throws \CRM_Core_Exception
68 * @throws \CiviCRM_API3_Exception
70 public function preProcess() {
71 $this->set('searchFormName', 'Search');
73 // set the button names
74 $this->_actionButtonName
= $this->getButtonName('next', 'action');
77 $this->sortNameOnly
= TRUE;
81 if (empty($this->_formValues
)) {
82 if (isset($this->_ssID
)) {
83 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
87 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
88 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
96 if ($this->_context
== 'user') {
97 $prefix = $this->_prefix
;
100 $this->assign("{$prefix}limit", $this->_limit
);
101 $this->assign("{$prefix}single", $this->_single
);
103 $controller = new CRM_Core_Selector_Controller($selector,
104 $this->get(CRM_Utils_Pager
::PAGE_ID
),
106 CRM_Core_Action
::VIEW
,
108 CRM_Core_Selector_Controller
::TRANSFER
,
111 $controller->setEmbedded(TRUE);
112 $controller->moveFromSessionToTemplate();
114 $this->assign('summary', $this->get('summary'));
118 * Build the form object.
120 * @throws \CRM_Core_Exception
121 * @throws \CiviCRM_API3_Exception
123 public function buildQuickForm() {
124 parent
::buildQuickForm();
125 $this->addSortNameField();
127 CRM_Activity_BAO_Query
::buildSearchForm($this);
129 $rows = $this->get('rows');
130 if (is_array($rows)) {
131 if (!$this->_single
) {
132 $this->addRowSelectors($rows);
135 $this->addTaskMenu(CRM_Activity_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
141 * The post processing of the form gets done here.
143 * Key things done during post processing are
144 * - check for reset or next request. if present, skip post procesing.
145 * - now check if user requested running a saved search, if so, then
146 * the form values associated with the saved search are used for searching.
147 * - if user has done a submit with new values the regular post submissing is
150 * The processing consists of using a Selector / Controller framework for getting the
153 * @throws \CRM_Core_Exception
155 public function postProcess() {
161 $this->setFormValues();
162 if (!empty($_POST)) {
168 'priority_id' => 'activity_priority_id',
171 CRM_Contact_BAO_Query
::processSpecialFormValue($this->_formValues
, $specialParams, $changeNames);
173 $this->fixFormValues();
175 if (isset($this->_ssID
) && empty($_POST)) {
176 // if we are editing / running a saved search and the form has not been posted
177 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
180 // We don't show test records in summaries or dashboards
181 if (empty($this->_formValues
['activity_test']) && $this->_force
) {
182 $this->_formValues
["activity_test"] = 0;
185 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
187 $this->set('queryParams', $this->_queryParams
);
189 $buttonName = $this->controller
->getButtonName();
190 if ($buttonName == $this->_actionButtonName
) {
191 // check actionName and if next, then do not repeat a search, since we are going to the next page
192 // hack, make sure we reset the task values
193 $stateMachine = $this->controller
->getStateMachine();
194 $formName = $stateMachine->getTaskFormName();
195 $this->controller
->resetPage($formName);
199 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
201 $selector = new CRM_Activity_Selector_Search($this->_queryParams
,
208 $selector->setKey($this->controller
->_key
);
211 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
212 $prefix = $this->_prefix
;
215 $controller = new CRM_Core_Selector_Controller($selector,
216 $this->get(CRM_Utils_Pager
::PAGE_ID
),
218 CRM_Core_Action
::VIEW
,
220 CRM_Core_Selector_Controller
::SESSION
,
223 $controller->setEmbedded(TRUE);
224 $query = &$selector->getQuery();
226 if ($this->_context
=== 'user') {
227 $query->setSkipPermission(TRUE);
233 * Probably more hackery than anything else.
235 * @throws \CRM_Core_Exception
237 public function fixFormValues() {
238 if (!$this->_force
) {
242 $status = CRM_Utils_Request
::retrieve('status', 'String', $this);
244 $this->_formValues
['activity_status_id'] = $status;
245 $this->_defaults
['activity_status_id'] = $status;
248 $survey = CRM_Utils_Request
::retrieve('survey', 'Positive');
251 $this->_formValues
['activity_survey_id'] = $this->_defaults
['activity_survey_id'] = $survey;
252 $sid = $this->_formValues
['activity_survey_id'] ??
NULL;
253 $activity_type_id = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id');
255 // since checkbox are replaced by multiple select option
256 $this->_formValues
['activity_type_id'] = $activity_type_id;
257 $this->_defaults
['activity_type_id'] = $activity_type_id;
259 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
262 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
264 $this->_formValues
['contact_id'] = $cid;
266 $activity_role = CRM_Utils_Request
::retrieve('activity_role', 'Positive', $this);
268 if ($activity_role) {
269 $this->_formValues
['activity_role'] = $activity_role;
272 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name');
274 // also assign individual mode to the template
275 $this->_single
= TRUE;
279 // Enable search activity by custom value
280 // @todo this is not good security practice. Instead define entity fields in metadata &
281 // use getEntity Defaults
282 $requestParams = CRM_Utils_Request
::exportValues();
283 foreach (array_keys($requestParams) as $key) {
284 if (substr($key, 0, 7) != 'custom_') {
287 elseif (empty($requestParams[$key])) {
290 $customValue = CRM_Utils_Request
::retrieve($key, 'String', $this);
292 $this->_formValues
[$key] = $customValue;
293 $this->_defaults
[$key] = $customValue;
297 if (!empty($this->_defaults
)) {
298 $this->setDefaults($this->_defaults
);
303 * Return a descriptive name for the page, used in wizard header
307 public function getTitle() {
308 return ts('Find Activities');
312 * Get metadata for the entity fields.
316 * @throws \CiviCRM_API3_Exception
318 protected function getEntityMetadata() {
319 return CRM_Activity_BAO_Query
::getSearchFieldMetadata();
323 * Set the metadata for the form.
325 protected function setSearchMetadata() {
326 $this->addSearchFieldMetadata(['Activity' => $this->getEntityMetadata()]);