From 239d4cf80e4b2e16d4a9a55c23fecea835310a4c Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 17 Jun 2020 21:08:14 +1200 Subject: [PATCH] WIP - fix for export --- CRM/Contact/Form/Task.php | 52 +++------------------------- CRM/Export/Controller/Standalone.php | 41 +++++++++++++++++++++- CRM/Export/Form/Select.php | 25 ++++++++----- 3 files changed, 60 insertions(+), 58 deletions(-) diff --git a/CRM/Contact/Form/Task.php b/CRM/Contact/Form/Task.php index b478fb4867..7a7d57093f 100644 --- a/CRM/Contact/Form/Task.php +++ b/CRM/Contact/Form/Task.php @@ -86,8 +86,6 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { $form->_contactIds = []; $form->_contactTypes = []; - $useTable = (CRM_Utils_System::getClassName($form->controller->getStateMachine()) == 'CRM_Export_StateMachine_Standalone'); - $isStandAlone = in_array('task', $form->urlPath) || in_array('standalone', $form->urlPath); if ($isStandAlone) { list($form->_task, $title) = CRM_Contact_Task::getTaskAndTitleByClass(get_class($form)); @@ -137,13 +135,6 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { $crmContactTaskTasks = CRM_Contact_Task::taskTitles(); $form->assign('taskName', CRM_Utils_Array::value($form->_task, $crmContactTaskTasks)); - if ($useTable) { - $tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('tskact')->setDurable()->setId($qfKey); - $form->_componentTable = $tempTable->getName(); - $tempTable->drop(); - $tempTable->createWithColumns('contact_id int primary key'); - } - // all contacts or action = save a search if ((CRM_Utils_Array::value('radio_ts', self::$_searchFormValues) == 'ts_all') || ($form->_task == CRM_Contact_Task::SAVE_SEARCH) @@ -151,34 +142,10 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { // since we don't store all contacts in prevnextcache, when user selects "all" use query to retrieve contacts // rather than prevnext cache table for most of the task actions except export where we rebuild query to fetch // final result set - if ($useTable) { - $allCids = Civi::service('prevnext')->getSelection($cacheKey, "getall"); - } - else { - $allCids[$cacheKey] = self::getContactIds($form); - } + $allCids[$cacheKey] = self::getContactIds($form); $form->_contactIds = []; - if ($useTable) { - $count = 0; - $insertString = []; - foreach ($allCids[$cacheKey] as $cid => $ignore) { - $count++; - $insertString[] = " ( {$cid} ) "; - if ($count % 200 == 0) { - $string = implode(',', $insertString); - $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; - CRM_Core_DAO::executeQuery($sql); - $insertString = []; - } - } - if (!empty($insertString)) { - $string = implode(',', $insertString); - $sql = "REPLACE INTO {$form->_componentTable} ( contact_id ) VALUES $string"; - CRM_Core_DAO::executeQuery($sql); - } - } - elseif (empty($form->_contactIds)) { + if (empty($form->_contactIds)) { // filter duplicates here // CRM-7058 // might be better to do this in the query, but that logic is a bit complex @@ -201,13 +168,7 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { // need to perform action on only selected contacts foreach (self::$_searchFormValues as $name => $value) { if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { - $contactID = substr($name, CRM_Core_Form::CB_PREFIX_LEN); - if ($useTable) { - $insertString[] = " ( {$contactID} ) "; - } - else { - $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); - } + $form->_contactIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); } } } @@ -215,12 +176,7 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { // fetching selected contact ids of passed cache key $selectedCids = Civi::service('prevnext')->getSelection($cacheKey); foreach ($selectedCids[$cacheKey] as $selectedCid => $ignore) { - if ($useTable) { - $insertString[] = " ( {$selectedCid} ) "; - } - else { - $form->_contactIds[] = $selectedCid; - } + $form->_contactIds[] = $selectedCid; } } diff --git a/CRM/Export/Controller/Standalone.php b/CRM/Export/Controller/Standalone.php index c2af8a5879..abf090cc1a 100644 --- a/CRM/Export/Controller/Standalone.php +++ b/CRM/Export/Controller/Standalone.php @@ -14,6 +14,25 @@ */ class CRM_Export_Controller_Standalone extends CRM_Core_Controller { + /** + * Yet another hardcoded list :( + * + * Very similar to the switch statement in CRM_Export_Form_Select::preProcess + * TODO: Make this extensible for extension export pages. + * + * @var string[] + */ + public $components = [ + 'Contact' => 'Contact', + 'Contribution' => 'Contribute', + 'Membership' => 'Member', + 'Participant' => 'Event', + 'Pledge' => 'Pledge', + 'Case' => 'Case', + 'Grant' => 'Grant', + 'Activity' => 'Activity', + ]; + /** * Class constructor. * @@ -49,6 +68,8 @@ class CRM_Export_Controller_Standalone extends CRM_Core_Controller { // add all the actions $this->addActions(); + $dao = CRM_Core_DAO_AllCoreTables::getFullName($entity); + CRM_Utils_System::setTitle(ts('Export %1', [1 => $dao::getEntityTitle()])); } /** @@ -67,7 +88,7 @@ class CRM_Export_Controller_Standalone extends CRM_Core_Controller { } } // Set the "task" selector value to Export - $className = 'CRM_' . $this->get('entity') . '_Task'; + $className = 'CRM_' . $this->getComponent($this->get('entity')) . '_Task'; foreach ($className::tasks() as $taskId => $task) { $taskForm = (array) $task['class']; if ($taskForm[0] == 'CRM_Export_Form_Select') { @@ -77,4 +98,22 @@ class CRM_Export_Controller_Standalone extends CRM_Core_Controller { return $values; } + /** + * Get the relevant entity name. + * + * @return string + */ + public function getComponent() { + return $this->components[$this->getEntity()]; + } + + /** + * Get the name used to construct the class. + * + * @return mixed + */ + public function getEntity() { + return $this->get('entity'); + } + } diff --git a/CRM/Export/Form/Select.php b/CRM/Export/Form/Select.php index af4b2bba7f..b151e766ac 100644 --- a/CRM/Export/Form/Select.php +++ b/CRM/Export/Form/Select.php @@ -120,15 +120,7 @@ class CRM_Export_Form_Select extends CRM_Core_Form_Task { break; default: - // FIXME: Code cleanup, we may not need to do this $componentName code here. - $formName = CRM_Utils_System::getClassName($this->controller->getStateMachine()); - $componentName = explode('_', $formName); - if ($formName == 'CRM_Export_StateMachine_Standalone') { - $componentName = ['CRM', $this->controller->get('entity')]; - } - // Contact - $entityShortname = $componentName[1]; - $entityDAOName = $entityShortname; + $entityShortname = $entityDAOName = $this->getComponentName(); break; } @@ -498,4 +490,19 @@ FROM {$this->_componentTable} return (int) ($this->queryMode ?: $this->controller->get('component_mode')); } + /** + * Get the name of the component. + * + * @return array + */ + protected function getComponentName(): string { + if (method_exists($this->controller, 'getComponent')) { + return $this->controller->getComponent(); + } + // This code is thought to be unreachable. + $formName = CRM_Utils_System::getClassName($this->controller->getStateMachine()); + $componentName = explode('_', $formName); + return $componentName[1]; + } + } -- 2.25.1