get('searchFormName')); if ($searchFormName === 'search') { $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $pathPart . '/search', $urlParams)); } else { $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName", $urlParams )); } } /** * Get the ids the user has selected or FALSE if selection has not been used. * * @param array $values * * @return array|bool */ public function getSelectedIDs(array $values) { if ($values['radio_ts'] === 'ts_sel') { $ids = []; foreach ($values as $name => $value) { if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); } } return $ids; } return FALSE; } /** * Build all the data structures needed to build the form. * * @throws \CRM_Core_Exception */ public function preProcess() { self::preProcessCommon($this); } /** * Common pre-processing function. * * @param CRM_Core_Form_Task $form * * @throws \CRM_Core_Exception */ public static function preProcessCommon(&$form) { $form->_entityIds = []; $searchFormValues = $form->getSearchFormValues(); $form->_task = $searchFormValues['task']; $entityIds = []; if ($searchFormValues['radio_ts'] == 'ts_sel') { foreach ($searchFormValues as $name => $value) { if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); } } } else { $queryParams = $form->get('queryParams'); $sortOrder = NULL; if ($form->get(CRM_Utils_Sort::SORT_ORDER)) { $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER); } $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode()); $query->_distinctComponentClause = $form->getDistinctComponentClause(); $query->_groupByComponentClause = $form->getGroupByComponentClause(); $result = $query->searchQuery(0, 0, $sortOrder); $selector = $form->getEntityAliasField(); while ($result->fetch()) { $entityIds[] = $result->$selector; } } if (!empty($entityIds)) { $form->_componentClause = ' ' . $form->getTableName() . '.id IN ( ' . implode(',', $entityIds) . ' ) '; $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds)); } $form->_entityIds = $form->_componentIds = $entityIds; // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds). // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids'; $form->$entitySpecificIdsName = $form->_entityIds; $form->setNextUrl($form::$entityShortname); } /** * Given the entity id, compute the contact id since its used for things like send email * For example, for cases we need to override this function as the table name is civicrm_case_contact */ public function setContactIDs() { $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds, $this->getTableName() ); } /** * Add buttons to the form. * * @param string $title * Title of the main button. * @param string $nextType * Button type for the form after processing. * @param string $backType * @param bool $submitOnce */ public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { $this->addButtons([ [ 'type' => $nextType, 'name' => $title, 'isDefault' => TRUE, ], [ 'type' => $backType, 'name' => ts('Cancel'), ], ]); } /** * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE) * Should be overridden by child classes in most cases * * @return int */ public function getQueryMode() { return $this->queryMode ?: CRM_Contact_BAO_Query::MODE_CONTACTS; } /** * Given the component id, compute the contact id * since it's used for things like send email. * * @todo At the moment this duplicates a similar function in CRM_Core_DAO * because right now only the case component is using this. Since the * default $orderBy is '' which is what the original does, others should be * easily convertable as NFC. * @todo The passed in variables should be class member variables. Shouldn't * need to have passed in vars. * * @param $componentIDs * @param string $tableName * @param string $idField * * @return array */ public function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') { $contactIDs = []; if (empty($componentIDs)) { return $contactIDs; } $orderBy = $this->orderBy(); $IDs = implode(',', $componentIDs); $query = " SELECT contact_id FROM $tableName WHERE $idField IN ( $IDs ) $orderBy "; $dao = CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { $contactIDs[] = $dao->contact_id; } return $contactIDs; } /** * Default ordering for getContactIDsFromComponent. Subclasses can override. * * @return string * SQL fragment. Either return '' or a valid order clause including the * words "ORDER BY", e.g. "ORDER BY `{$this->idField}`" */ public function orderBy() { return ''; } /** * Get the submitted values for the form. * * @return array */ public function getSearchFormValues() { if ($this->_action === CRM_Core_Action::ADVANCED) { return $this->controller->exportValues('Advanced'); } if ($this->_action === CRM_Core_Action::PROFILE) { return $this->controller->exportValues('Builder'); } if ($this->_action == CRM_Core_Action::COPY) { return $this->controller->exportValues('Custom'); } if ($this->get('entity') !== 'Contact') { return $this->controller->exportValues('Search'); } return $this->controller->exportValues('Basic'); } /** * Get the name of the table for the relevant entity. * * @return string */ public function getTableName() { CRM_Core_Error::deprecatedFunctionWarning('function should be overridden'); return $this::$tableName; } /** * Get the clause for grouping by the component. * * @return string */ public function getDistinctComponentClause() { return " ( " . $this->getTableName() . ".id )"; } /** * Get the group by clause for the component. * * @return string */ public function getGroupByComponentClause() { return " GROUP BY " . $this->getTableName() . ".id "; } /** * Get the group by clause for the component. * * @return string */ public function getEntityAliasField() { CRM_Core_Error::deprecatedFunctionWarning('function should be overridden'); return $this::$entityShortname . '_id'; } }