From cb80705c0df2c1e106774ebc74c0452fd1e01f92 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 8 Nov 2021 10:00:31 -0500 Subject: [PATCH] Afform - Add table of submission results Uses a searchDisplay in an Afform to display form submissions. Takes advantage of the new 'unmodified' update mode. --- .../Civi/AfformAdmin/AfformAdminMeta.php | 2 +- .../ang/afAdmin/afAdminList.controller.js | 12 ++ ext/afform/admin/ang/afAdmin/afAdminList.html | 17 ++- .../ang/afAdminFormSubmissionList.aff.html | 10 ++ .../ang/afAdminFormSubmissionList.aff.json | 6 + ext/afform/admin/info.xml | 1 + .../managed/FormSubmissionSavedSearch.mgd.php | 104 ++++++++++++++++++ ext/afform/core/managed/AfformType.mgd.php | 37 +++---- 8 files changed, 162 insertions(+), 27 deletions(-) create mode 100644 ext/afform/admin/ang/afAdminFormSubmissionList.aff.html create mode 100644 ext/afform/admin/ang/afAdminFormSubmissionList.aff.json create mode 100644 ext/afform/admin/managed/FormSubmissionSavedSearch.mgd.php diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index bd72efb865..6ac1d03503 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -20,7 +20,7 @@ class AfformAdminMeta { ->execute(); // Pluralize tabs (too bad option groups only store a single label) $plurals = [ - 'form' => E::ts('Custom Forms'), + 'form' => E::ts('Submission Forms'), 'search' => E::ts('Search Forms'), 'block' => E::ts('Field Blocks'), 'system' => E::ts('System Forms'), diff --git a/ext/afform/admin/ang/afAdmin/afAdminList.controller.js b/ext/afform/admin/ang/afAdmin/afAdminList.controller.js index f7a0070102..2b1ee33b94 100644 --- a/ext/afform/admin/ang/afAdmin/afAdminList.controller.js +++ b/ext/afform/admin/ang/afAdmin/afAdminList.controller.js @@ -36,6 +36,18 @@ afforms[afform.type].push(afform); }, {}); + // Load aggregated submission stats for each form + crmApi4('AfformSubmission', 'get', { + select: ['afform_name', 'MAX(submission_date) AS last_submission', 'COUNT(id) AS submission_count'], + groupBy: ['afform_name'] + }).then(function(submissions) { + _.each(submissions, function(submission) { + var afform = _.findWhere(afforms, {name: submission.afform_name}) || {}; + afform.last_submission = CRM.utils.formatDate(submission.last_submission); + afform.submission_count = submission.submission_count; + }); + }); + // Change sort field/direction when clicking a column header this.sortBy = function(col) { ctrl.sortDir = ctrl.sortField === col ? !ctrl.sortDir : false; diff --git a/ext/afform/admin/ang/afAdmin/afAdminList.html b/ext/afform/admin/ang/afAdmin/afAdminList.html index bfa5c19eb7..0f119d02d5 100644 --- a/ext/afform/admin/ang/afAdmin/afAdminList.html +++ b/ext/afform/admin/ang/afAdmin/afAdminList.html @@ -50,7 +50,7 @@ {{:: ts('Name') }} - + {{:: ts('Page') }} @@ -60,6 +60,11 @@ {{:: ts('Placement') }} + + + + {{:: ts('Submissions') }} + @@ -74,13 +79,21 @@ {{:: afform.name }} - + {{:: afform.server_route }} {{:: afform.placement.join(', ') }} + + + {{:: afform.submission_count === 1 ? ts('1 Submission') : ts('%1 Submissions', {1: afform.submission_count}) }} + +
+ {{:: ts('Last submitted: %1', {1: afform.last_submission}) }} +
+ {{:: afform['base_module:label'] }} diff --git a/ext/afform/admin/ang/afAdminFormSubmissionList.aff.html b/ext/afform/admin/ang/afAdminFormSubmissionList.aff.html new file mode 100644 index 0000000000..f26eb38f85 --- /dev/null +++ b/ext/afform/admin/ang/afAdminFormSubmissionList.aff.html @@ -0,0 +1,10 @@ +
+

{{ ts('%1 Submissions', {1: api4.result.title || ts('Loading')}) }}

+
+
+ + +
diff --git a/ext/afform/admin/ang/afAdminFormSubmissionList.aff.json b/ext/afform/admin/ang/afAdminFormSubmissionList.aff.json new file mode 100644 index 0000000000..3e4fa2160d --- /dev/null +++ b/ext/afform/admin/ang/afAdminFormSubmissionList.aff.json @@ -0,0 +1,6 @@ +{ + "type": "search", + "title": "Form Submissions", + "server_route": "civicrm/admin/afform/submissions", + "permission": "administer CiviCRM" +} diff --git a/ext/afform/admin/info.xml b/ext/afform/admin/info.xml index 41ab945bee..4c6ca916c7 100644 --- a/ext/afform/admin/info.xml +++ b/ext/afform/admin/info.xml @@ -23,6 +23,7 @@ Administer, edit and compose CiviCRM Afforms. org.civicrm.afform + org.civicrm.search_kit CRM/AfformAdmin diff --git a/ext/afform/admin/managed/FormSubmissionSavedSearch.mgd.php b/ext/afform/admin/managed/FormSubmissionSavedSearch.mgd.php new file mode 100644 index 0000000000..bd6634a37e --- /dev/null +++ b/ext/afform/admin/managed/FormSubmissionSavedSearch.mgd.php @@ -0,0 +1,104 @@ + 'AfAdmin_Submission_List', + 'entity' => 'SavedSearch', + 'update' => 'unmodified', + 'cleanup' => 'unused', + 'params' => [ + 'version' => 4, + 'values' => [ + 'name' => 'AfAdmin_Submission_List', + 'label' => E::ts('Form Submissions'), + 'form_values' => NULL, + 'mapping_id' => NULL, + 'search_custom_id' => NULL, + 'api_entity' => 'AfformSubmission', + 'api_params' => [ + 'version' => 4, + 'select' => [ + 'id', + 'contact_id.display_name', + 'submission_date', + ], + ], + ], + ], + ], + [ + 'name' => 'AfAdmin_Submission_List_Display', + 'entity' => 'SearchDisplay', + 'update' => 'unmodified', + 'cleanup' => 'unused', + 'params' => [ + 'version' => 4, + 'values' => [ + 'name' => 'AfAdmin_Submission_List_Display', + 'label' => E::ts('Form Submissions Table'), + 'saved_search_id.name' => 'AfAdmin_Submission_List', + 'type' => 'table', + 'actions' => TRUE, + 'acl_bypass' => FALSE, + 'settings' => [ + 'actions' => TRUE, + 'limit' => 50, + 'classes' => [ + 'table', + 'table-striped', + ], + 'pager' => [ + 'show_count' => TRUE, + 'expose_limit' => TRUE, + ], + 'columns' => [ + [ + 'type' => 'field', + 'key' => 'id', + 'dataType' => 'Integer', + 'label' => E::ts('Id'), + 'sortable' => TRUE, + ], + [ + 'type' => 'field', + 'key' => 'contact_id.display_name', + 'dataType' => 'String', + 'label' => E::ts('Submitted by'), + 'sortable' => TRUE, + 'link' => [ + 'entity' => 'Contact', + 'action' => 'view', + 'join' => 'contact_id', + 'target' => '_blank', + ], + 'empty_value' => E::ts('Anonymous'), + 'cssRules' => [ + [ + 'disabled', + 'contact_id.display_name', + '=', + ], + ], + ], + [ + 'type' => 'field', + 'key' => 'submission_date', + 'dataType' => 'Timestamp', + 'label' => E::ts('Submission Date/Time'), + 'sortable' => TRUE, + ], + ], + 'sort' => [ + [ + 'submission_date', + 'ASC', + ], + ], + ], + ], + ], + ], +]; diff --git a/ext/afform/core/managed/AfformType.mgd.php b/ext/afform/core/managed/AfformType.mgd.php index baeaccb8c6..25da5682b7 100644 --- a/ext/afform/core/managed/AfformType.mgd.php +++ b/ext/afform/core/managed/AfformType.mgd.php @@ -17,11 +17,23 @@ $mgd = [ 'option_group_id' => 'afform_type', 'name' => 'form', 'value' => 'form', - 'label' => 'Custom Form', + 'label' => 'Submission Form', 'weight' => 0, 'icon' => 'fa-list-alt', ], ], + [ + 'name' => 'AfformType:search', + 'entity' => 'OptionValue', + 'params' => [ + 'option_group_id' => 'afform_type', + 'name' => 'search', + 'value' => 'search', + 'label' => 'Search Form', + 'weight' => 10, + 'icon' => 'fa-search', + ], + ], [ 'name' => 'AfformType:block', 'entity' => 'OptionValue', @@ -48,27 +60,4 @@ $mgd = [ ], ]; -try { - $search = civicrm_api3('Extension', 'getsingle', [ - 'full_name' => 'org.civicrm.search_kit', - ]); - if ($search['status'] === 'installed') { - $mgd[] = [ - 'name' => 'AfformType:search', - 'entity' => 'OptionValue', - 'params' => [ - 'option_group_id' => 'afform_type', - 'name' => 'search', - 'value' => 'search', - 'label' => 'Search Form', - 'weight' => 10, - 'icon' => 'fa-search', - ], - ]; - } -} -catch (Exception $e) { - // ¯\_(ツ)_/¯ -} - return $mgd; -- 2.25.1