From acfb87f1fec6f94f2caecabc8679b42439f28e52 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 25 Sep 2020 12:16:58 +1200 Subject: [PATCH 1/3] Separate export form classes --- CRM/Activity/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Activity/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Activity/Task.php | 4 ++-- CRM/Contact/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Contact/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Contact/Task.php | 4 ++-- CRM/Contribute/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Contribute/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Contribute/Task.php | 4 ++-- CRM/Event/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Event/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Event/Task.php | 4 ++-- CRM/Grant/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Grant/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Grant/Task.php | 4 ++-- CRM/Pledge/Export/Form/Map.php | 23 ++++++++++++++++++++ CRM/Pledge/Export/Form/Select.php | 30 +++++++++++++++++++++++++++ CRM/Pledge/Task.php | 4 ++-- 18 files changed, 330 insertions(+), 12 deletions(-) create mode 100644 CRM/Activity/Export/Form/Map.php create mode 100644 CRM/Activity/Export/Form/Select.php create mode 100644 CRM/Contact/Export/Form/Map.php create mode 100644 CRM/Contact/Export/Form/Select.php create mode 100644 CRM/Contribute/Export/Form/Map.php create mode 100644 CRM/Contribute/Export/Form/Select.php create mode 100644 CRM/Event/Export/Form/Map.php create mode 100644 CRM/Event/Export/Form/Select.php create mode 100644 CRM/Grant/Export/Form/Map.php create mode 100644 CRM/Grant/Export/Form/Select.php create mode 100644 CRM/Pledge/Export/Form/Map.php create mode 100644 CRM/Pledge/Export/Form/Select.php diff --git a/CRM/Activity/Export/Form/Map.php b/CRM/Activity/Export/Form/Map.php new file mode 100644 index 000000000000..b977bef10de6 --- /dev/null +++ b/CRM/Activity/Export/Form/Map.php @@ -0,0 +1,23 @@ + [ 'title' => ts('Export activities'), 'class' => [ - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Activity_Export_Form_Select', + 'CRM_Activity_Export_Form_Map', ], 'result' => FALSE, ], diff --git a/CRM/Contact/Export/Form/Map.php b/CRM/Contact/Export/Form/Map.php new file mode 100644 index 000000000000..7e1451ebea00 --- /dev/null +++ b/CRM/Contact/Export/Form/Map.php @@ -0,0 +1,23 @@ + array( 'title' => ts('Export contacts'), 'class' => array( - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Contact_Export_Form_Select', + 'CRM_Contact_Export_Form_Map', ), 'result' => FALSE, ), diff --git a/CRM/Contribute/Export/Form/Map.php b/CRM/Contribute/Export/Form/Map.php new file mode 100644 index 000000000000..984a0d87187b --- /dev/null +++ b/CRM/Contribute/Export/Form/Map.php @@ -0,0 +1,23 @@ + [ 'title' => ts('Export contributions'), 'class' => [ - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Contribute_Export_Form_Select', + 'CRM_Contribute_Export_Form_Map', ], 'result' => FALSE, ], diff --git a/CRM/Event/Export/Form/Map.php b/CRM/Event/Export/Form/Map.php new file mode 100644 index 000000000000..1f580c90a50a --- /dev/null +++ b/CRM/Event/Export/Form/Map.php @@ -0,0 +1,23 @@ + [ 'title' => ts('Export participants'), 'class' => [ - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Event_Export_Form_Select', + 'CRM_Event_Export_Form_Map', ], 'result' => FALSE, ], diff --git a/CRM/Grant/Export/Form/Map.php b/CRM/Grant/Export/Form/Map.php new file mode 100644 index 000000000000..1dd59e910157 --- /dev/null +++ b/CRM/Grant/Export/Form/Map.php @@ -0,0 +1,23 @@ + [ 'title' => ts('Export grants'), 'class' => [ - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Grant_Export_Form_Select', + 'CRM_Grant_Export_Form_Map', ], 'result' => FALSE, ], diff --git a/CRM/Pledge/Export/Form/Map.php b/CRM/Pledge/Export/Form/Map.php new file mode 100644 index 000000000000..1dd59e910157 --- /dev/null +++ b/CRM/Pledge/Export/Form/Map.php @@ -0,0 +1,23 @@ + [ 'title' => ts('Export pledges'), 'class' => [ - 'CRM_Export_Form_Select', - 'CRM_Export_Form_Map', + 'CRM_Pledge_Export_Form_Select', + 'CRM_Pledge_Export_Form_Map', ], 'result' => FALSE, ], From 153a9bd74d63d7e26488abb6740dc8e0cb52e2f7 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 25 Sep 2020 13:15:08 +1200 Subject: [PATCH 2/3] Unravel all the handling of 'tasktiles' It turns out all this taskName assigning is just to compare it with 'Export Contacts' in the tpl This makes it more direct --- CRM/Activity/Export/Form/Select.php | 9 +++++++++ CRM/Contact/Export/Form/Select.php | 9 +++++++++ CRM/Contribute/Export/Form/Select.php | 9 +++++++++ CRM/Event/Export/Form/Select.php | 9 +++++++++ CRM/Export/Controller/Standalone.php | 2 +- CRM/Export/Form/Select.php | 24 +++++++++++++++++------- CRM/Export/Form/Select/Case.php | 16 ++++++++++++++++ CRM/Export/StateMachine/Standalone.php | 9 ++++++--- CRM/Grant/Export/Form/Select.php | 9 +++++++++ CRM/Member/Export/Form/Select.php | 16 ++++++++++++++++ CRM/Pledge/Export/Form/Select.php | 9 +++++++++ templates/CRM/Export/Form/Select.tpl | 2 +- 12 files changed, 111 insertions(+), 12 deletions(-) diff --git a/CRM/Activity/Export/Form/Select.php b/CRM/Activity/Export/Form/Select.php index 8bf02c3b3087..ef856053fc3f 100644 --- a/CRM/Activity/Export/Form/Select.php +++ b/CRM/Activity/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Activity_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Contact/Export/Form/Select.php b/CRM/Contact/Export/Form/Select.php index 2b5f4186398a..d15ff7afd0bb 100644 --- a/CRM/Contact/Export/Form/Select.php +++ b/CRM/Contact/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Contact_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return TRUE; + } + } diff --git a/CRM/Contribute/Export/Form/Select.php b/CRM/Contribute/Export/Form/Select.php index 44ff6a2045ef..921c0595644d 100644 --- a/CRM/Contribute/Export/Form/Select.php +++ b/CRM/Contribute/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Contribute_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Event/Export/Form/Select.php b/CRM/Event/Export/Form/Select.php index f908c28038a6..f281decc2312 100644 --- a/CRM/Event/Export/Form/Select.php +++ b/CRM/Event/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Event_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Export/Controller/Standalone.php b/CRM/Export/Controller/Standalone.php index 2d3074eebdca..aa954ee0e6db 100644 --- a/CRM/Export/Controller/Standalone.php +++ b/CRM/Export/Controller/Standalone.php @@ -42,7 +42,7 @@ public function __construct($title = NULL, $action = CRM_Core_Action::NONE, $mod $this->set('cids', implode(',', array_keys($perm['values']))); } - $this->_stateMachine = new CRM_Export_StateMachine_Standalone($this, $action); + $this->_stateMachine = new CRM_Export_StateMachine_Standalone($this, $action, $entity); // create and instantiate the pages $this->addPages($this->_stateMachine, $action); diff --git a/CRM/Export/Form/Select.php b/CRM/Export/Form/Select.php index 4772536ab763..95a9e3a83e3d 100644 --- a/CRM/Export/Form/Select.php +++ b/CRM/Export/Form/Select.php @@ -96,7 +96,7 @@ public function preProcess() { } $this->_exportMode = constant('CRM_Export_Form_Select::' . strtoupper($entityShortname) . '_EXPORT'); $formTaskClassName = "CRM_{$entityShortname}_Form_Task"; - $taskClassName = "CRM_{$entityShortname}_Task"; + if (isset($formTaskClassName::$entityShortname)) { $this::$entityShortname = $formTaskClassName::$entityShortname; if (isset($formTaskClassName::$tableName)) { @@ -124,17 +124,13 @@ public function preProcess() { } } - $formTaskClassName::preProcessCommon($this); + $this->callPreProcessing(); // $component is used on CRM/Export/Form/Select.tpl to display extra information for contact export ($this->_exportMode == self::CONTACT_EXPORT) ? $component = FALSE : $component = TRUE; $this->assign('component', $component); - // Set the task title - $componentTasks = $taskClassName::taskTitles(); - $this->_task = $values['task']; - $taskName = $componentTasks[$this->_task]; - $this->assign('taskName', $taskName); + $this->assign('isShowMergeOptions', $this->isShowContactMergeOptions()); if ($this->_componentTable) { $query = " @@ -441,6 +437,20 @@ public function getQueryMode() { return (int) ($this->queryMode ?: $this->controller->get('component_mode')); } + /** + * Call the pre-processing function. + */ + protected function callPreProcessing(): void { + throw new CRM_Core_Exception('This must be over-ridden'); + } + + /** + * Assign the title of the task to the tpl. + */ + protected function isShowContactMergeOptions() { + throw new CRM_Core_Exception('This must be over-ridden'); + } + /** * Get the name of the component. * diff --git a/CRM/Export/Form/Select/Case.php b/CRM/Export/Form/Select/Case.php index c310aa9a489e..cb7ac3e1c759 100644 --- a/CRM/Export/Form/Select/Case.php +++ b/CRM/Export/Form/Select/Case.php @@ -25,4 +25,20 @@ class CRM_Export_Form_Select_Case extends CRM_Export_Form_Select { */ protected $queryMode = CRM_Contact_BAO_Query::MODE_CASE; + /** + * Call the pre-processing function. + */ + protected function callPreProcessing(): void { + CRM_Case_Form_Task::preProcessCommon($this); + } + + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Export/StateMachine/Standalone.php b/CRM/Export/StateMachine/Standalone.php index 6d81238d0780..99d58647ad15 100644 --- a/CRM/Export/StateMachine/Standalone.php +++ b/CRM/Export/StateMachine/Standalone.php @@ -21,13 +21,16 @@ class CRM_Export_StateMachine_Standalone extends CRM_Core_StateMachine { * * @param object $controller * @param \const|int $action + * @param string $entity */ - public function __construct($controller, $action = CRM_Core_Action::NONE) { + public function __construct($controller, $action = CRM_Core_Action::NONE, $entity = 'Contact') { parent::__construct($controller, $action); + $entityMap = ['Contribution' => 'Contribute', 'Membership' => 'Member', 'Participant' => 'Event']; + $entity = $entityMap[$entity] ?? $entity; $this->_pages = [ - 'CRM_Export_Form_Select' => NULL, - 'CRM_Export_Form_Map' => NULL, + 'CRM_' . $entity . '_Export_Form_Select' => NULL, + 'CRM_' . $entity . '_Export_Form_Map' => NULL, ]; $this->addSequentialPages($this->_pages, $action); diff --git a/CRM/Grant/Export/Form/Select.php b/CRM/Grant/Export/Form/Select.php index 863787eb4191..a8c8d0df80e6 100644 --- a/CRM/Grant/Export/Form/Select.php +++ b/CRM/Grant/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Grant_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Member/Export/Form/Select.php b/CRM/Member/Export/Form/Select.php index f4cc4bacd015..61896fe7663e 100644 --- a/CRM/Member/Export/Form/Select.php +++ b/CRM/Member/Export/Form/Select.php @@ -20,4 +20,20 @@ */ class CRM_Member_Export_Form_Select extends CRM_Export_Form_Select { + /** + * Call the pre-processing function. + */ + protected function callPreProcessing(): void { + CRM_Member_Form_Task::preProcessCommon($this); + } + + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/CRM/Pledge/Export/Form/Select.php b/CRM/Pledge/Export/Form/Select.php index e6ee72dfec76..55e77b1ed435 100644 --- a/CRM/Pledge/Export/Form/Select.php +++ b/CRM/Pledge/Export/Form/Select.php @@ -27,4 +27,13 @@ protected function callPreProcessing(): void { CRM_Pledge_Form_Task::preProcessCommon($this); } + /** + * Does this export offer contact merging. + * + * @return bool + */ + protected function isShowContactMergeOptions() { + return FALSE; + } + } diff --git a/templates/CRM/Export/Form/Select.tpl b/templates/CRM/Export/Form/Select.tpl index 1a7e7ef44161..df984478d74b 100644 --- a/templates/CRM/Export/Form/Select.tpl +++ b/templates/CRM/Export/Form/Select.tpl @@ -40,7 +40,7 @@ {/if} - {if $taskName eq 'Export Contacts' OR $component eq false} + {if $isShowMergeOptions}
{ts}Merge Options{/ts} {help id="id-export_merge_options"}
From e5dac7b445881d4538cafe86d2df32cf815a09e1 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 25 Sep 2020 16:37:49 +1200 Subject: [PATCH 3/3] Add generic task controller --- CRM/Contribute/Controller/Task.php | 35 ++++++++ CRM/Contribute/Form/Task.php | 29 ++++--- CRM/Contribute/Form/Task/Delete.php | 2 +- CRM/Contribute/Form/Task/PDF.php | 12 +-- CRM/Contribute/Form/Task/PDFLetterCommon.php | 2 +- CRM/Contribute/Selector/Search.php | 1 + CRM/Contribute/Task.php | 71 +++++++++++++++++ CRM/Contribute/xml/Menu/Contribute.xml | 6 ++ CRM/Core/Controller/Task.php | 84 ++++++++++++++++++++ 9 files changed, 225 insertions(+), 17 deletions(-) create mode 100644 CRM/Contribute/Controller/Task.php create mode 100644 CRM/Core/Controller/Task.php diff --git a/CRM/Contribute/Controller/Task.php b/CRM/Contribute/Controller/Task.php new file mode 100644 index 000000000000..aad2f053fa46 --- /dev/null +++ b/CRM/Contribute/Controller/Task.php @@ -0,0 +1,35 @@ +controller->get('id')) { + return (array) $this->controller->get('id'); + } + return array_keys($this->_contributionIds); + } + + /** + * @param \CRM_Contribute_Form_Task $form * * @throws \CRM_Core_Exception */ @@ -63,15 +75,14 @@ public static function preProcessCommon(&$form) { $contributeTasks = CRM_Contribute_Task::tasks(); $form->assign('taskName', CRM_Utils_Array::value($form->_task, $contributeTasks)); - $ids = []; if (isset($values['radio_ts']) && $values['radio_ts'] == 'ts_sel') { 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); + $form->_contributionIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); } } } - else { + elseif (!$form->getIDs()) { $queryParams = $form->get('queryParams'); $isTest = FALSE; if (is_array($queryParams)) { @@ -117,7 +128,7 @@ public static function preProcessCommon(&$form) { } $result = $query->searchQuery(0, 0, $sortOrder); while ($result->fetch()) { - $ids[] = $result->contribution_id; + $form->_contributionIds[] = $result->contribution_id; if ($form->_includesSoftCredits) { $contactIds[$result->contact_id] = $result->contact_id; $contributionContactIds["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id; @@ -125,18 +136,18 @@ public static function preProcessCommon(&$form) { } $form->assign('totalSelectedContributions', $form->get('rowCount')); } + $form->_componentIds = $form->_contributionIds; - if (!empty($ids)) { - $form->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $ids) . ' ) '; + if (!empty($form->getIDs())) { + $form->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $form->getIDs()) . ' ) '; - $form->assign('totalSelectedContributions', count($ids)); + $form->assign('totalSelectedContributions', count($form->getIDs())); } if (!empty($form->_includesSoftCredits) && !empty($contactIds)) { $form->_contactIds = $contactIds; $form->_contributionContactIds = $contributionContactIds; } - $form->_contributionIds = $form->_componentIds = $ids; $form->set('contributionIds', $form->_contributionIds); //set the context for redirection for any task actions diff --git a/CRM/Contribute/Form/Task/Delete.php b/CRM/Contribute/Form/Task/Delete.php index 1b8aa33f988b..30b05bccb778 100644 --- a/CRM/Contribute/Form/Task/Delete.php +++ b/CRM/Contribute/Form/Task/Delete.php @@ -88,7 +88,7 @@ public function buildQuickForm() { */ public function postProcess() { $deleted = $failed = 0; - foreach ($this->_contributionIds as $contributionId) { + foreach ($this->getIDs() as $contributionId) { if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionId)) { $deleted++; } diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index 7b99516a88b9..33df631b205a 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -39,11 +39,11 @@ public function preProcess() { $this, FALSE ); - if ($id) { - $this->_contributionIds = [$id]; - $this->_componentClause = " civicrm_contribution.id IN ( $id ) "; - $this->_single = TRUE; - $this->assign('totalSelectedContributions', 1); + if ($this->getIDs()) { + $this->_contributionIds = $this->getIDs(); + $this->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $this->getIDs()) . ' ) '; + $this->_single = count($this->getIDs()) === 1; + $this->assign('totalSelectedContributions', count($this->getIDs())); } else { parent::preProcess(); @@ -139,7 +139,7 @@ public function postProcess() { $template = CRM_Core_Smarty::singleton(); $params = $this->controller->exportValues($this->_name); - $elements = self::getElements($this->_contributionIds, $params, $this->_contactIds); + $elements = self::getElements($this->getIDs(), $params, $this->_contactIds); foreach ($elements['details'] as $contribID => $detail) { $input = $ids = []; diff --git a/CRM/Contribute/Form/Task/PDFLetterCommon.php b/CRM/Contribute/Form/Task/PDFLetterCommon.php index a201830384ea..40e37d139478 100644 --- a/CRM/Contribute/Form/Task/PDFLetterCommon.php +++ b/CRM/Contribute/Form/Task/PDFLetterCommon.php @@ -77,7 +77,7 @@ public static function postProcess(&$form, $formValues = NULL) { // skip some contacts ? $skipOnHold = $form->skipOnHold ?? FALSE; $skipDeceased = $form->skipDeceased ?? TRUE; - $contributionIDs = $form->getVar('_contributionIds'); + $contributionIDs = $form->getIDs(); if ($form->_includesSoftCredits) { //@todo - comment on what is stored there $contributionIDs = $form->getVar('_contributionContactIds'); diff --git a/CRM/Contribute/Selector/Search.php b/CRM/Contribute/Selector/Search.php index 010d05116d85..56899a8e7335 100644 --- a/CRM/Contribute/Selector/Search.php +++ b/CRM/Contribute/Selector/Search.php @@ -445,6 +445,7 @@ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { 'title' => $buttonName, ]; } + $links += CRM_Contribute_Task::getContextualLinks($row); $row['action'] = CRM_Core_Action::formLink( $links, diff --git a/CRM/Contribute/Task.php b/CRM/Contribute/Task.php index eb9c057d7038..ff4f22e83a9d 100644 --- a/CRM/Contribute/Task.php +++ b/CRM/Contribute/Task.php @@ -50,11 +50,18 @@ public static function tasks() { 'title' => ts('Delete contributions'), 'class' => 'CRM_Contribute_Form_Task_Delete', 'result' => FALSE, + 'key' => 'delete', + 'is_support_standalone' => TRUE, + 'is_single_mode' => FALSE, ], self::TASK_PRINT => [ 'title' => ts('Print selected rows'), 'class' => 'CRM_Contribute_Form_Task_Print', 'result' => FALSE, + 'key' => 'print', + 'is_support_standalone' => TRUE, + // It works - just seems a bit odd. + 'is_single_mode' => FALSE, ], self::TASK_EXPORT => [ 'title' => ts('Export contributions'), @@ -62,7 +69,9 @@ public static function tasks() { 'CRM_Contribute_Export_Form_Select', 'CRM_Contribute_Export_Form_Map', ], + 'key' => 'export', 'result' => FALSE, + 'is_single_mode' => FALSE, ], self::BATCH_UPDATE => [ 'title' => ts('Update multiple contributions'), @@ -70,35 +79,68 @@ public static function tasks() { 'CRM_Contribute_Form_Task_PickProfile', 'CRM_Contribute_Form_Task_Batch', ], + 'key' => 'batch_update', 'result' => TRUE, + 'is_support_standalone' => FALSE, + 'is_single_mode' => FALSE, ], self::TASK_EMAIL => [ 'title' => ts('Email - send now (to %1 or less)', [ 1 => Civi::settings() ->get('simple_mail_limit'), ]), + 'title_single_mode' => ts('Email'), + 'key' => 'email', + 'name' => ts('Email'), 'class' => 'CRM_Contribute_Form_Task_Email', 'result' => TRUE, + // Not yet! + 'is_support_standalone' => FALSE, + 'is_single_mode' => FALSE, ], self::UPDATE_STATUS => [ 'title' => ts('Record payments for contributions'), 'class' => 'CRM_Contribute_Form_Task_Status', 'result' => TRUE, + 'key' => 'update_status', + 'is_support_standalone' => TRUE, + // We have a better link we can use for a single. + 'is_single_mode' => FALSE, ], self::PDF_RECEIPT => [ 'title' => ts('Receipts - print or email'), 'class' => 'CRM_Contribute_Form_Task_PDF', 'result' => FALSE, + 'name' => ts('Receipt'), + 'is_support_standalone' => TRUE, + 'key' => 'receipt', + 'filters' => ['contribution_status_id' => [CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')]], + 'is_single_mode' => TRUE, ], self::PDF_THANKYOU => [ 'title' => ts('Thank-you letters - print or email'), 'class' => 'CRM_Contribute_Form_Task_PDFLetter', 'result' => FALSE, + 'name' => ts('Thank'), + 'key' => 'thankyou', + 'is_support_standalone' => TRUE, + 'is_single_mode' => TRUE, + 'filters' => ['contribution_status_id' => [CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')]], ], self::PDF_INVOICE => [ 'title' => ts('Invoices - print or email'), 'class' => 'CRM_Contribute_Form_Task_Invoice', + 'name' => ts('Invoice'), 'result' => FALSE, + 'key' => 'invoice', + 'is_support_standalone' => TRUE, + 'is_single_mode' => TRUE, + 'filters' => [ + 'contribution_status_id' => [ + CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), + CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'), + ], + ], ], ]; @@ -123,6 +165,35 @@ public static function tasks() { return self::$_tasks; } + /** + * Get links appropriate to the context of the row. + * + * @param $row + * + * @return array + */ + public static function getContextualLinks($row) { + $tasks = self::tasks(); + foreach ($tasks as $key => $task) { + if (empty($task['is_single_mode'])) { + unset($tasks[$key]); + continue; + } + if (!empty($task['filters'])) { + foreach ($task['filters'] as $filter => $values) { + if (!in_array($row[$filter], $values, FALSE)) { + unset($tasks[$key]); + continue 2; + } + } + } + $tasks[$key]['url'] = 'civicrm/contribute/task'; + $tasks[$key]['qs'] = ['reset' => 1, 'id' => $row['contribution_id'], 'task' => $task['key']]; + $tasks[$key]['title'] = $task['title_single_mode'] ?? $task['title']; + } + return $tasks; + } + /** * Show tasks selectively based on the permission level * of the user diff --git a/CRM/Contribute/xml/Menu/Contribute.xml b/CRM/Contribute/xml/Menu/Contribute.xml index cfafe7f6c395..be445b9f4038 100644 --- a/CRM/Contribute/xml/Menu/Contribute.xml +++ b/CRM/Contribute/xml/Menu/Contribute.xml @@ -329,4 +329,10 @@ CRM_Member_Page_RecurringContributions access CiviContribute + + civicrm/contribute/task + Contribution Task + CRM_Contribute_Controller_Task + access CiviContribute + diff --git a/CRM/Core/Controller/Task.php b/CRM/Core/Controller/Task.php new file mode 100644 index 000000000000..4e68946790ca --- /dev/null +++ b/CRM/Core/Controller/Task.php @@ -0,0 +1,84 @@ +getEntity(), 'get', [ + 'return' => 'id', + 'options' => ['limit' => 0], + 'check_permissions' => 1, + 'id' => ['IN' => $id], + ])['values']; + if (empty($perm)) { + throw new CRM_Core_Exception(ts('No records available')); + } + $this->set('id', array_keys($perm)); + $pages = array_fill_keys($this->getTaskClass(), NULL); + + $this->_stateMachine = new CRM_Core_StateMachine($this); + $this->_stateMachine->addSequentialPages($pages); + // create and instantiate the pages + $this->addPages($this->_stateMachine, $action); + // add all the actions + $this->addActions(); + } + + /** + * Get the name used to construct the class. + * + * @return string + */ + abstract public function getEntity():string; + + /** + * Get the available tasks for the entity. + * + * @return array + */ + abstract public function getAvailableTasks():array; + + /** + * Get the class for the action. + * + * @return array Array of the classes for the form controlle. + * + * @throws \CRM_Core_Exception + */ + protected function getTaskClass(): array { + $task = CRM_Utils_Request::retrieve('task', 'Alphanumeric', $this, TRUE); + foreach ($this->getAvailableTasks() as $taskAction) { + if ($taskAction['key'] === $task) { + return (array) $taskAction['class']; + } + } + throw new CRM_Core_Exception(ts('Invalid task')); + } + +}