| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This file is for activity search. |
| 36 | */ |
| 37 | class CRM_Activity_Form_Search extends CRM_Core_Form_Search { |
| 38 | |
| 39 | /** |
| 40 | * The params that are sent to the query. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $_queryParams; |
| 45 | |
| 46 | /** |
| 47 | * Are we restricting ourselves to a single contact. |
| 48 | * |
| 49 | * @var bool |
| 50 | */ |
| 51 | protected $_single = FALSE; |
| 52 | |
| 53 | /** |
| 54 | * Are we restricting ourselves to a single contact. |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
| 58 | protected $_limit = NULL; |
| 59 | |
| 60 | /** |
| 61 | * Prefix for the controller. |
| 62 | * @var string |
| 63 | */ |
| 64 | protected $_prefix = "activity_"; |
| 65 | |
| 66 | /** |
| 67 | * The saved search ID retrieved from the GET vars. |
| 68 | * |
| 69 | * @var int |
| 70 | */ |
| 71 | protected $_ssID; |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | public function getDefaultEntity() { |
| 77 | return 'Activity'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Processing needed for buildForm and later. |
| 82 | */ |
| 83 | public function preProcess() { |
| 84 | $this->set('searchFormName', 'Search'); |
| 85 | |
| 86 | // set the button names |
| 87 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 88 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 89 | |
| 90 | $this->_done = FALSE; |
| 91 | |
| 92 | $this->loadStandardSearchOptionsFromUrl(); |
| 93 | |
| 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); |
| 98 | } |
| 99 | else { |
| 100 | $this->_formValues = $this->get('formValues'); |
| 101 | |
| 102 | if ($this->_force) { |
| 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; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (empty($this->_formValues)) { |
| 113 | if (isset($this->_ssID)) { |
| 114 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ($this->_force) { |
| 119 | $this->postProcess(); |
| 120 | $this->set('force', 0); |
| 121 | } |
| 122 | |
| 123 | $sortID = NULL; |
| 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) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 131 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, |
| 132 | $this->_action, |
| 133 | NULL, |
| 134 | $this->_single, |
| 135 | $this->_limit, |
| 136 | $this->_context |
| 137 | ); |
| 138 | $prefix = NULL; |
| 139 | if ($this->_context == 'user') { |
| 140 | $prefix = $this->_prefix; |
| 141 | } |
| 142 | |
| 143 | $this->assign("{$prefix}limit", $this->_limit); |
| 144 | $this->assign("{$prefix}single", $this->_single); |
| 145 | |
| 146 | $controller = new CRM_Core_Selector_Controller($selector, |
| 147 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 148 | $sortID, |
| 149 | CRM_Core_Action::VIEW, |
| 150 | $this, |
| 151 | CRM_Core_Selector_Controller::TRANSFER, |
| 152 | $prefix |
| 153 | ); |
| 154 | $controller->setEmbedded(TRUE); |
| 155 | $controller->moveFromSessionToTemplate(); |
| 156 | |
| 157 | $this->assign('summary', $this->get('summary')); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Build the form object. |
| 162 | */ |
| 163 | public function buildQuickForm() { |
| 164 | parent::buildQuickForm(); |
| 165 | $this->addSortNameField(); |
| 166 | |
| 167 | CRM_Activity_BAO_Query::buildSearchForm($this); |
| 168 | |
| 169 | $rows = $this->get('rows'); |
| 170 | if (is_array($rows)) { |
| 171 | if (!$this->_single) { |
| 172 | $this->addRowSelectors($rows); |
| 173 | } |
| 174 | |
| 175 | $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * The post processing of the form gets done here. |
| 182 | * |
| 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 |
| 188 | * done. |
| 189 | * |
| 190 | * The processing consists of using a Selector / Controller framework for getting the |
| 191 | * search results. |
| 192 | */ |
| 193 | public function postProcess() { |
| 194 | if ($this->_done) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | $this->_done = TRUE; |
| 199 | $this->setFormValues(); |
| 200 | if (!empty($_POST)) { |
| 201 | $specialParams = [ |
| 202 | 'activity_type_id', |
| 203 | 'status_id', |
| 204 | 'priority_id', |
| 205 | ]; |
| 206 | $changeNames = [ |
| 207 | 'status_id' => 'activity_status_id', |
| 208 | 'priority_id' => 'activity_priority_id', |
| 209 | ]; |
| 210 | |
| 211 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams, $changeNames); |
| 212 | } |
| 213 | $this->fixFormValues(); |
| 214 | |
| 215 | if (isset($this->_ssID) && empty($_POST)) { |
| 216 | // if we are editing / running a saved search and the form has not been posted |
| 217 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 218 | } |
| 219 | |
| 220 | // We don't show test records in summaries or dashboards |
| 221 | if (empty($this->_formValues['activity_test']) && $this->_force) { |
| 222 | $this->_formValues["activity_test"] = 0; |
| 223 | } |
| 224 | |
| 225 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
| 226 | |
| 227 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 228 | |
| 229 | $this->set('formValues', $this->_formValues); |
| 230 | $this->set('queryParams', $this->_queryParams); |
| 231 | |
| 232 | $buttonName = $this->controller->getButtonName(); |
| 233 | if ($buttonName == $this->_actionButtonName) { |
| 234 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 235 | // hack, make sure we reset the task values |
| 236 | $stateMachine = $this->controller->getStateMachine(); |
| 237 | $formName = $stateMachine->getTaskFormName(); |
| 238 | $this->controller->resetPage($formName); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | $sortID = NULL; |
| 243 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 244 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 245 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 250 | |
| 251 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, |
| 252 | $this->_action, |
| 253 | NULL, |
| 254 | $this->_single, |
| 255 | $this->_limit, |
| 256 | $this->_context |
| 257 | ); |
| 258 | $selector->setKey($this->controller->_key); |
| 259 | |
| 260 | $prefix = NULL; |
| 261 | if ($this->_context == 'basic' || $this->_context == 'user') { |
| 262 | $prefix = $this->_prefix; |
| 263 | } |
| 264 | |
| 265 | $controller = new CRM_Core_Selector_Controller($selector, |
| 266 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 267 | $sortID, |
| 268 | CRM_Core_Action::VIEW, |
| 269 | $this, |
| 270 | CRM_Core_Selector_Controller::SESSION, |
| 271 | $prefix |
| 272 | ); |
| 273 | $controller->setEmbedded(TRUE); |
| 274 | $query = &$selector->getQuery(); |
| 275 | |
| 276 | if ($this->_context == 'user') { |
| 277 | $query->setSkipPermission(TRUE); |
| 278 | } |
| 279 | $controller->run(); |
| 280 | } |
| 281 | |
| 282 | public function fixFormValues() { |
| 283 | if (!$this->_force) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
| 288 | if ($status) { |
| 289 | $this->_formValues['activity_status_id'] = $status; |
| 290 | $this->_defaults['activity_status_id'] = $status; |
| 291 | } |
| 292 | |
| 293 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive'); |
| 294 | |
| 295 | if ($survey) { |
| 296 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
| 297 | $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues); |
| 298 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); |
| 299 | |
| 300 | // since checkbox are replaced by multiple select option |
| 301 | $this->_formValues['activity_type_id'] = $activity_type_id; |
| 302 | $this->_defaults['activity_type_id'] = $activity_type_id; |
| 303 | } |
| 304 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 305 | |
| 306 | if ($cid) { |
| 307 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 308 | if ($cid > 0) { |
| 309 | $this->_formValues['contact_id'] = $cid; |
| 310 | |
| 311 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); |
| 312 | |
| 313 | if ($activity_role) { |
| 314 | $this->_formValues['activity_role'] = $activity_role; |
| 315 | } |
| 316 | else { |
| 317 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
| 318 | } |
| 319 | // also assign individual mode to the template |
| 320 | $this->_single = TRUE; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Enable search activity by custom value |
| 325 | // @todo this is not good security practice. Instead define entity fields in metadata & |
| 326 | // use getEntity Defaults |
| 327 | $requestParams = CRM_Utils_Request::exportValues(); |
| 328 | foreach (array_keys($requestParams) as $key) { |
| 329 | if (substr($key, 0, 7) != 'custom_') { |
| 330 | continue; |
| 331 | } |
| 332 | elseif (empty($requestParams[$key])) { |
| 333 | continue; |
| 334 | } |
| 335 | $customValue = CRM_Utils_Request::retrieve($key, 'String', $this); |
| 336 | if ($customValue) { |
| 337 | $this->_formValues[$key] = $customValue; |
| 338 | $this->_defaults[$key] = $customValue; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (!empty($this->_defaults)) { |
| 343 | $this->setDefaults($this->_defaults); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Return a descriptive name for the page, used in wizard header |
| 349 | * |
| 350 | * @return string |
| 351 | */ |
| 352 | public function getTitle() { |
| 353 | return ts('Find Activities'); |
| 354 | } |
| 355 | |
| 356 | protected function getEntityMetadata() { |
| 357 | return CRM_Activity_BAO_Query::getSearchFieldMetadata(); |
| 358 | } |
| 359 | |
| 360 | } |