/** * Process the form after the input has been submitted and validated. */ public function postProcess() { // saved search form values // get form values of all the forms in this controller $formValues = $this->controller->exportValues(); $isAdvanced = $this->get('isAdvanced'); $isSearchBuilder = $this->get('isSearchBuilder'); // add mapping record only for search builder saved search $mappingId = NULL; if ($isAdvanced == '2' && $isSearchBuilder == '1') { //save the mapping for search builder if (!$this->_id) { //save record in mapping table $mappingParams = array('mapping_type' => 'Search Builder'); $temp = array(); $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); $mappingId = $mapping->id; } else { //get the mapping id from saved search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $this->_id; $savedSearch->find(TRUE); $mappingId = $savedSearch->mapping_id; } //save mapping fields CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId); } //save the search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $this->_id; $queryParams = $this->get('queryParams'); // CRM-18585 include selected operator in $savedSearch->form_values if (!empty($formValues['operator'])) { $queryParams[] = array('operator', '=', $formValues['operator'], 0, 0); } // Use the query parameters rather than the form values - these have already been assessed / converted // with the extra knowledge that the form has. // Note that we want to move towards a standardised way of saving the query that is not // an exact match for the form requirements & task the form layer with converting backwards and forwards. // Ideally per CRM-17075 we will use entity reference fields heavily in the form layer & convert to the // sql operator syntax at the query layer. if (!$isSearchBuilder) { CRM_Contact_BAO_SavedSearch::saveRelativeDates($queryParams, $formValues); $savedSearch->form_values = serialize($queryParams); } else { // We want search builder to be able to convert back & forth at the form layer // to a standardised style - but it can't yet! $savedSearch->form_values = serialize($formValues); } $savedSearch->mapping_id = $mappingId; $savedSearch->search_custom_id = $this->get('customSearchID'); $savedSearch->save(); $this->set('ssID', $savedSearch->id); CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success'); // also create a group that is associated with this saved search only if new saved search $params = array(); $params['title'] = $formValues['title']; $params['description'] = $formValues['description']; if (isset($formValues['group_type']) && is_array($formValues['group_type'])) { $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR; } else { $params['group_type'] = ''; } $params['visibility'] = 'User and User Admin Only'; $params['saved_search_id'] = $savedSearch->id; $params['is_active'] = 1; //CRM-14190 $params['parents'] = $formValues['parents']; if ($this->_id) { $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id'); } CRM_Contact_BAO_Group::create($params); // CRM-9464 $this->_id = $savedSearch->id; //CRM-14190 if (!empty($formValues['parents'])) { CRM_Contact_BAO_GroupNestingCache::update(); } }