| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 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 boolean |
| 50 | */ |
| 51 | protected $_single = FALSE; |
| 52 | |
| 53 | /** |
| 54 | * Are we restricting ourselves to a single contact. |
| 55 | * |
| 56 | * @var boolean |
| 57 | */ |
| 58 | protected $_limit = NULL; |
| 59 | |
| 60 | /** |
| 61 | * Prefix for the controller. |
| 62 | */ |
| 63 | protected $_prefix = "activity_"; |
| 64 | |
| 65 | /** |
| 66 | * The saved search ID retrieved from the GET vars. |
| 67 | * |
| 68 | * @var int |
| 69 | */ |
| 70 | protected $_ssID; |
| 71 | |
| 72 | /** |
| 73 | * Processing needed for buildForm and later. |
| 74 | */ |
| 75 | public function preProcess() { |
| 76 | $this->set('searchFormName', 'Search'); |
| 77 | |
| 78 | // set the button names |
| 79 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 80 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 81 | |
| 82 | $this->_done = FALSE; |
| 83 | $this->defaults = array(); |
| 84 | |
| 85 | // we allow the controller to set force/reset externally, useful when we are being |
| 86 | // driven by the wizard framework |
| 87 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 88 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
| 89 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
| 90 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
| 91 | |
| 92 | $this->assign("context", $this->_context); |
| 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 | |
| 103 | if (empty($this->_formValues)) { |
| 104 | if (isset($this->_ssID)) { |
| 105 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if ($this->_force) { |
| 110 | $this->postProcess(); |
| 111 | $this->set('force', 0); |
| 112 | } |
| 113 | |
| 114 | $sortID = NULL; |
| 115 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 116 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 117 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 122 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, |
| 123 | $this->_action, |
| 124 | NULL, |
| 125 | $this->_single, |
| 126 | $this->_limit, |
| 127 | $this->_context |
| 128 | ); |
| 129 | $prefix = NULL; |
| 130 | if ($this->_context == 'user') { |
| 131 | $prefix = $this->_prefix; |
| 132 | } |
| 133 | |
| 134 | $this->assign("{$prefix}limit", $this->_limit); |
| 135 | $this->assign("{$prefix}single", $this->_single); |
| 136 | |
| 137 | $controller = new CRM_Core_Selector_Controller($selector, |
| 138 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 139 | $sortID, |
| 140 | CRM_Core_Action::VIEW, |
| 141 | $this, |
| 142 | CRM_Core_Selector_Controller::TRANSFER, |
| 143 | $prefix |
| 144 | ); |
| 145 | $controller->setEmbedded(TRUE); |
| 146 | $controller->moveFromSessionToTemplate(); |
| 147 | |
| 148 | $this->assign('summary', $this->get('summary')); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Build the form object. |
| 153 | */ |
| 154 | public function buildQuickForm() { |
| 155 | parent::buildQuickForm(); |
| 156 | $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
| 157 | |
| 158 | CRM_Activity_BAO_Query::buildSearchForm($this); |
| 159 | |
| 160 | $rows = $this->get('rows'); |
| 161 | if (is_array($rows)) { |
| 162 | if (!$this->_single) { |
| 163 | $this->addRowSelectors($rows); |
| 164 | } |
| 165 | |
| 166 | $permission = CRM_Core_Permission::getPermission(); |
| 167 | |
| 168 | $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles($permission)); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * The post processing of the form gets done here. |
| 175 | * |
| 176 | * Key things done during post processing are |
| 177 | * - check for reset or next request. if present, skip post procesing. |
| 178 | * - now check if user requested running a saved search, if so, then |
| 179 | * the form values associated with the saved search are used for searching. |
| 180 | * - if user has done a submit with new values the regular post submissing is |
| 181 | * done. |
| 182 | * |
| 183 | * The processing consists of using a Selector / Controller framework for getting the |
| 184 | * search results. |
| 185 | */ |
| 186 | public function postProcess() { |
| 187 | if ($this->_done) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | $this->_done = TRUE; |
| 192 | |
| 193 | if (!empty($_POST)) { |
| 194 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 195 | foreach (array('activity_type_id', 'status_id', 'activity_subject') as $element) { |
| 196 | $value = CRM_Utils_Array::value($element, $this->_formValues); |
| 197 | if ($value) { |
| 198 | if (is_array($value)) { |
| 199 | if ($element == 'status_id') { |
| 200 | unset($this->_formValues[$element]); |
| 201 | $this->_formValues['activity_' . $element] = $value; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | $this->_formValues[$element] = array('LIKE' => "%$value%"); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $this->fixFormValues(); |
| 212 | |
| 213 | if (isset($this->_ssID) && empty($_POST)) { |
| 214 | // if we are editing / running a saved search and the form has not been posted |
| 215 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 216 | } |
| 217 | |
| 218 | // We don't show test records in summaries or dashboards |
| 219 | if (empty($this->_formValues['activity_test']) && $this->_force) { |
| 220 | $this->_formValues["activity_test"] = 0; |
| 221 | } |
| 222 | |
| 223 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
| 224 | |
| 225 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 226 | |
| 227 | $this->set('formValues', $this->_formValues); |
| 228 | $this->set('queryParams', $this->_queryParams); |
| 229 | |
| 230 | $buttonName = $this->controller->getButtonName(); |
| 231 | if ($buttonName == $this->_actionButtonName) { |
| 232 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 233 | // hack, make sure we reset the task values |
| 234 | $stateMachine = $this->controller->getStateMachine(); |
| 235 | $formName = $stateMachine->getTaskFormName(); |
| 236 | $this->controller->resetPage($formName); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | $sortID = NULL; |
| 241 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 242 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 243 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 248 | |
| 249 | $selector = new CRM_Activity_Selector_Search($this->_queryParams, |
| 250 | $this->_action, |
| 251 | NULL, |
| 252 | $this->_single, |
| 253 | $this->_limit, |
| 254 | $this->_context |
| 255 | ); |
| 256 | $selector->setKey($this->controller->_key); |
| 257 | |
| 258 | $prefix = NULL; |
| 259 | if ($this->_context == 'basic' || $this->_context == 'user') { |
| 260 | $prefix = $this->_prefix; |
| 261 | } |
| 262 | |
| 263 | $controller = new CRM_Core_Selector_Controller($selector, |
| 264 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 265 | $sortID, |
| 266 | CRM_Core_Action::VIEW, |
| 267 | $this, |
| 268 | CRM_Core_Selector_Controller::SESSION, |
| 269 | $prefix |
| 270 | ); |
| 271 | $controller->setEmbedded(TRUE); |
| 272 | $query = &$selector->getQuery(); |
| 273 | |
| 274 | if ($this->_context == 'user') { |
| 275 | $query->setSkipPermission(TRUE); |
| 276 | } |
| 277 | $controller->run(); |
| 278 | } |
| 279 | |
| 280 | public function fixFormValues() { |
| 281 | if (!$this->_force) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | $status = CRM_Utils_Request::retrieve('status', 'String', $this); |
| 286 | if ($status) { |
| 287 | $this->_formValues['activity_status_id'] = $status; |
| 288 | $this->_defaults['activity_status_id'] = $status; |
| 289 | } |
| 290 | |
| 291 | $survey = CRM_Utils_Request::retrieve('survey', 'Positive', CRM_Core_DAO::$_nullObject); |
| 292 | |
| 293 | if ($survey) { |
| 294 | $this->_formValues['activity_survey_id'] = $this->_defaults['activity_survey_id'] = $survey; |
| 295 | $sid = CRM_Utils_Array::value('activity_survey_id', $this->_formValues); |
| 296 | $activity_type_id = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $sid, 'activity_type_id'); |
| 297 | |
| 298 | // since checkbox are replaced by multiple select option |
| 299 | $this->_formValues['activity_type_id'] = $activity_type_id; |
| 300 | $this->_defaults['activity_type_id'] = $activity_type_id; |
| 301 | } |
| 302 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 303 | |
| 304 | if ($cid) { |
| 305 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 306 | if ($cid > 0) { |
| 307 | $this->_formValues['contact_id'] = $cid; |
| 308 | |
| 309 | $activity_role = CRM_Utils_Request::retrieve('activity_role', 'Positive', $this); |
| 310 | |
| 311 | if ($activity_role) { |
| 312 | $this->_formValues['activity_role'] = $activity_role; |
| 313 | } |
| 314 | else { |
| 315 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'sort_name'); |
| 316 | } |
| 317 | // also assign individual mode to the template |
| 318 | $this->_single = TRUE; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Added for membership search |
| 323 | |
| 324 | $signupType = CRM_Utils_Request::retrieve('signupType', 'Positive', |
| 325 | CRM_Core_DAO::$_nullObject |
| 326 | ); |
| 327 | |
| 328 | if ($signupType) { |
| 329 | //$this->_formValues['activity_type_id'] = array(); |
| 330 | $this->_formValues['activity_role'] = 1; |
| 331 | $this->_defaults['activity_role'] = 1; |
| 332 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
| 333 | |
| 334 | $renew = CRM_Utils_Array::key('Membership Renewal', $activityTypes); |
| 335 | $signup = CRM_Utils_Array::key('Membership Signup', $activityTypes); |
| 336 | |
| 337 | switch ($signupType) { |
| 338 | case 3: // signups and renewals |
| 339 | $this->_formValues['activity_type_id'][$renew] = 1; |
| 340 | $this->_defaults['activity_type_id'][$renew] = 1; |
| 341 | case 1: // signups only |
| 342 | $this->_formValues['activity_type_id'][$signup] = 1; |
| 343 | $this->_defaults['activity_type_id'][$signup] = 1; |
| 344 | break; |
| 345 | |
| 346 | case 2: // renewals only |
| 347 | $this->_formValues['activity_type_id'][$renew] = 1; |
| 348 | $this->_defaults['activity_type_id'][$renew] = 1; |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | $dateLow = CRM_Utils_Request::retrieve('dateLow', 'String', |
| 354 | CRM_Core_DAO::$_nullObject |
| 355 | ); |
| 356 | |
| 357 | if ($dateLow) { |
| 358 | $dateLow = date('m/d/Y', strtotime($dateLow)); |
| 359 | $this->_formValues['activity_date_relative'] = 0; |
| 360 | $this->_defaults['activity_date_relative'] = 0; |
| 361 | $this->_formValues['activity_date_low'] = $dateLow; |
| 362 | $this->_defaults['activity_date_low'] = $dateLow; |
| 363 | } |
| 364 | |
| 365 | $dateHigh = CRM_Utils_Request::retrieve('dateHigh', 'String', |
| 366 | CRM_Core_DAO::$_nullObject |
| 367 | ); |
| 368 | |
| 369 | if ($dateHigh) { |
| 370 | // Activity date time assumes midnight at the beginning of the date |
| 371 | // This sets it to almost midnight at the end of the date |
| 372 | /* if ($dateHigh <= 99999999) { |
| 373 | $dateHigh = 1000000 * $dateHigh + 235959; |
| 374 | } */ |
| 375 | $dateHigh = date('m/d/Y', strtotime($dateHigh)); |
| 376 | $this->_formValues['activity_date_relative'] = 0; |
| 377 | $this->_defaults['activity_date_relative'] = 0; |
| 378 | $this->_formValues['activity_date_high'] = $dateHigh; |
| 379 | $this->_defaults['activity_date_high'] = $dateHigh; |
| 380 | } |
| 381 | |
| 382 | if (!empty($this->_defaults)) { |
| 383 | $this->setDefaults($this->_defaults); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @return null |
| 389 | */ |
| 390 | public function getFormValues() { |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Return a descriptive name for the page, used in wizard header |
| 396 | * |
| 397 | * @return string |
| 398 | */ |
| 399 | public function getTitle() { |
| 400 | return ts('Find Activities'); |
| 401 | } |
| 402 | |
| 403 | } |