From 2f53e9bc9566c5ec86104dc931e90d9f4f1d0cd3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 5 Jan 2023 09:37:26 +1300 Subject: [PATCH] dev/core#4112 Separate code to handle exporting legacy custom searches into the extension --- CRM/Export/BAO/Export.php | 50 ------------ CRM/Export/Form/Select.php | 10 --- .../CRM/Contact/Form/Search/Action/Export.php | 77 +++++++++++++++++++ .../StateMachine/Search.php | 4 +- 4 files changed, 80 insertions(+), 61 deletions(-) create mode 100644 ext/legacycustomsearches/CRM/Contact/Form/Search/Action/Export.php diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 16637e75d8..3442efe22d 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -268,56 +268,6 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c CRM_Utils_System::civiExit(); } - /** - * @param $customSearchClass - * @param $formValues - * @param $order - */ - public static function exportCustom($customSearchClass, $formValues, $order) { - $ext = CRM_Extension_System::singleton()->getMapper(); - if (!$ext->isExtensionClass($customSearchClass)) { - require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php'; - } - else { - require_once $ext->classToPath($customSearchClass); - } - $search = new $customSearchClass($formValues); - - $includeContactIDs = FALSE; - if ($formValues['radio_ts'] == 'ts_sel') { - $includeContactIDs = TRUE; - } - - $sql = $search->all(0, 0, $order, $includeContactIDs); - - $columns = $search->columns(); - - $header = array_keys($columns); - $fields = array_values($columns); - - $rows = []; - $dao = CRM_Core_DAO::executeQuery($sql); - $alterRow = FALSE; - if (method_exists($search, 'alterRow')) { - $alterRow = TRUE; - } - while ($dao->fetch()) { - $row = []; - - foreach ($fields as $field) { - $unqualified_field = CRM_Utils_Array::First(array_slice(explode('.', $field), -1)); - $row[$field] = $dao->$unqualified_field; - } - if ($alterRow) { - $search->alterRow($row); - } - $rows[] = $row; - } - - CRM_Core_Report_Excel::writeCSVFile(ts('CiviCRM Contact Search'), $header, $rows); - CRM_Utils_System::civiExit(); - } - /** * @param \CRM_Export_BAO_ExportProcessor $processor * @param $details diff --git a/CRM/Export/Form/Select.php b/CRM/Export/Form/Select.php index 7136d5eb51..33b14a3bc3 100644 --- a/CRM/Export/Form/Select.php +++ b/CRM/Export/Form/Select.php @@ -68,16 +68,6 @@ class CRM_Export_Form_Select extends CRM_Core_Form_Task { */ public function preProcess() { $this->preventAjaxSubmit(); - - //special case for custom search, directly give option to download csv file - $customSearchID = $this->get('customSearchID'); - if ($customSearchID) { - CRM_Export_BAO_Export::exportCustom($this->get('customSearchClass'), - $this->get('formValues'), - $this->get(CRM_Utils_Sort::SORT_ORDER) - ); - } - $this->_selectAll = FALSE; $this->_exportMode = self::CONTACT_EXPORT; $this->_componentIds = []; diff --git a/ext/legacycustomsearches/CRM/Contact/Form/Search/Action/Export.php b/ext/legacycustomsearches/CRM/Contact/Form/Search/Action/Export.php new file mode 100644 index 0000000000..5d2b961b08 --- /dev/null +++ b/ext/legacycustomsearches/CRM/Contact/Form/Search/Action/Export.php @@ -0,0 +1,77 @@ +preventAjaxSubmit(); + self::exportCustom($this->get('customSearchClass'), + $this->get('formValues'), + $this->get(CRM_Utils_Sort::SORT_ORDER) + ); + } + + /** + * @param $customSearchClass + * @param $formValues + * @param $order + */ + public static function exportCustom($customSearchClass, $formValues, $order) { + $ext = CRM_Extension_System::singleton()->getMapper(); + if (!$ext->isExtensionClass($customSearchClass)) { + require_once str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php'; + } + else { + require_once $ext->classToPath($customSearchClass); + } + $search = new $customSearchClass($formValues); + + $includeContactIDs = FALSE; + if ($formValues['radio_ts'] == 'ts_sel') { + $includeContactIDs = TRUE; + } + + $sql = $search->all(0, 0, $order, $includeContactIDs); + + $columns = $search->columns(); + + $header = array_keys($columns); + $fields = array_values($columns); + + $rows = []; + $dao = CRM_Core_DAO::executeQuery($sql); + $alterRow = FALSE; + if (method_exists($search, 'alterRow')) { + $alterRow = TRUE; + } + while ($dao->fetch()) { + $row = []; + + foreach ($fields as $field) { + $unqualified_field = CRM_Utils_Array::First(array_slice(explode('.', $field), -1)); + $row[$field] = $dao->$unqualified_field; + } + if ($alterRow) { + $search->alterRow($row); + } + $rows[] = $row; + } + + CRM_Core_Report_Excel::writeCSVFile(ts('CiviCRM Contact Search'), $header, $rows); + CRM_Utils_System::civiExit(); + } + +} diff --git a/ext/legacycustomsearches/CRM/Legacycustomsearches/StateMachine/Search.php b/ext/legacycustomsearches/CRM/Legacycustomsearches/StateMachine/Search.php index 796e8d3db0..46a18ee942 100644 --- a/ext/legacycustomsearches/CRM/Legacycustomsearches/StateMachine/Search.php +++ b/ext/legacycustomsearches/CRM/Legacycustomsearches/StateMachine/Search.php @@ -73,7 +73,9 @@ class CRM_Legacycustomsearches_StateMachine_Search extends CRM_Core_StateMachine $value = $this->_controller->get('task'); } $this->_controller->set('task', $value); - + if ((int) $value === CRM_Core_Task::TASK_EXPORT) { + return ['CRM_Contact_Form_Search_Action_Export', FALSE]; + } $componentMode = $this->_controller->get('component_mode'); $modeValue = CRM_Contact_Form_Search::getModeValue($componentMode); $taskClassName = $modeValue['taskClassName']; -- 2.25.1