Afform - Add submit_currently_open calc field
authorcolemanw <coleman@civicrm.org>
Thu, 31 Aug 2023 00:57:21 +0000 (20:57 -0400)
committercolemanw <coleman@civicrm.org>
Mon, 4 Sep 2023 15:18:30 +0000 (11:18 -0400)
ext/afform/admin/ang/afAdmin.js
ext/afform/admin/ang/afAdmin/afAdminList.html
ext/afform/core/Civi/Api4/Action/Afform/Get.php
ext/afform/core/Civi/Api4/Afform.php

index 758d85d7fa45c46f1a7fda51ccf672d15d5edf48..8d45473732edc232d2c3ca791a90eceea8bdf811 100644 (file)
@@ -11,7 +11,7 @@
           // Load data for lists
           afforms: function(crmApi4) {
             return crmApi4('Afform', 'get', {
-              select: ['name', 'title', 'type', 'server_route', 'is_public', 'submission_count', 'submission_date', 'has_local', 'has_base', 'base_module', 'base_module:label', 'is_dashlet', 'contact_summary:label']
+              select: ['name', 'title', 'type', 'server_route', 'is_public', 'submission_count', 'submission_date', 'submit_limit', 'submit_enabled', 'submit_currently_open', 'has_local', 'has_base', 'base_module', 'base_module:label', 'is_dashlet', 'contact_summary:label']
             });
           }
         }
index a4f20e2cf3fcbe3d2fad59db330dbdc11d355bb3..a15be805b4f47a1949352a5cdc0b115e8662199e 100644 (file)
         <i class="crm-i fa-sort-{{ $ctrl.sortDir ? 'asc' : 'desc' }}" ng-if="$ctrl.sortField === 'placement.length'"></i>
         {{:: ts('Placement') }}
       </th>
+      <th ng-if="$ctrl.tab === 'form'" title="{{:: ts('Click to sort') }}" ng-click="$ctrl.sortBy('submit_currently_open')">
+        <i class="crm-i fa-sort disabled" ng-if="$ctrl.sortField !== 'submit_currently_open'"></i>
+        <i class="crm-i fa-sort-{{ $ctrl.sortDir ? 'asc' : 'desc' }}" ng-if="$ctrl.sortField === 'submit_currently_open'"></i>
+        {{:: ts('Status') }}
+      </th>
       <th ng-if="$ctrl.tab === 'form'" title="{{:: ts('Click to sort') }}" ng-click="$ctrl.sortBy('submission_count')">
         <i class="crm-i fa-sort disabled" ng-if="$ctrl.sortField !== 'submission_count'"></i>
         <i class="crm-i fa-sort-{{ $ctrl.sortDir ? 'asc' : 'desc' }}" ng-if="$ctrl.sortField === 'submission_count'"></i>
         </a>
       </td>
       <td>{{:: afform.placement.join(', ') }}</td>
+      <td ng-if="$ctrl.tab === 'form'">
+        {{:: afform.submit_currently_open ? ts('Open') : ts('Closed') }}
+        <span ng-if="afform.submit_limit && afform.submit_enabled">
+          ({{:: ts('%1 remaining', {1: afform.submit_limit - afform.submission_count}) }})
+        </span>
+      </td>
       <td ng-if="$ctrl.tab === 'form'">
         <a ng-if="afform.submission_count" ng-href="{{:: crmUrl('civicrm/admin/afform/submissions#/?name=' + afform.name) }}">
           {{:: afform.submission_count === 1 ? ts('1 Submission') : ts('%1 Submissions', {1: afform.submission_count}) }}
index 9e0e5d448a80f98d42d0f50da1e1cf733a08f5c2..6ee84870734b4a534f1ebde417cca13907d0d304 100644 (file)
@@ -100,7 +100,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
     }
 
     // Fetch submission aggregates in bulk
-    if ($afforms && $this->_isFieldSelected('submission_count', 'submission_date')) {
+    if ($afforms && $this->_isFieldSelected('submission_count', 'submission_date', 'submit_currently_open')) {
       $afformSubmissions = \Civi\Api4\AfformSubmission::get(FALSE)
         ->addSelect('afform_name', 'COUNT(id) AS count', 'MAX(submission_date) AS date')
         ->addWhere('afform_name', 'IN', array_keys($afforms))
@@ -109,6 +109,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
       foreach ($afforms as $name => $record) {
         $afforms[$name]['submission_count'] = $afformSubmissions[$name]['count'] ?? 0;
         $afforms[$name]['submission_date'] = $afformSubmissions[$name]['date'] ?? NULL;
+        $afforms[$name]['submit_currently_open'] = ($record['submit_enabled'] ?? TRUE) && (empty($record['submit_limit']) || $record['submit_limit'] > $afforms[$name]['submission_count']);
       }
     }
 
index 9afadc5e1021efd92a574b604175c879622e34f4..f21cb4b5e69c5640aa29567479134150a15b6332 100644 (file)
@@ -277,6 +277,14 @@ class Afform extends Generic\AbstractEntity {
           'description' => 'Date & time of last form submission',
           'readonly' => TRUE,
         ];
+        $fields[] = [
+          'name' => 'submit_currently_open',
+          'type' => 'Extra',
+          'data_type' => 'Boolean',
+          'input_type' => 'Select',
+          'description' => 'Based on settings and current submission count, is the form open for submissions',
+          'readonly' => TRUE,
+        ];
         $fields[] = [
           'name' => 'has_local',
           'type' => 'Extra',