Skip to content

Commit

Permalink
dev/core#4112 Separate code to handle exporting legacy custom searche…
Browse files Browse the repository at this point in the history
…s into the extension
  • Loading branch information
eileenmcnaughton committed Feb 6, 2023
1 parent 4efa297 commit cafb33a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 61 deletions.
50 changes: 0 additions & 50 deletions CRM/Export/BAO/Export.php
Expand Up @@ -268,56 +268,6 @@ public static function invoke() {
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
Expand Down
10 changes: 0 additions & 10 deletions CRM/Export/Form/Select.php
Expand Up @@ -68,16 +68,6 @@ public function getTemplateFileName() {
*/
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 = [];
Expand Down
76 changes: 76 additions & 0 deletions ext/legacycustomsearches/CRM/Contact/Form/Search/Action/Export.php
@@ -0,0 +1,76 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Contact_Form_Search_Action_Export extends CRM_Core_Form_Task {
public function preProcess(): void {
$this->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();
}

}
Expand Up @@ -73,7 +73,9 @@ public function taskName($controller, $formName = 'Search') {
$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'];
Expand Down

0 comments on commit cafb33a

Please sign in to comment.