From 6af23f8f5d37fd7a2c1620be1e85fe74ff7e0d7e Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 19 Oct 2023 09:32:01 -0400 Subject: [PATCH] Afform - Add modified_date --- ext/afform/core/CRM/Afform/AfformScanner.php | 5 +++++ ext/afform/core/Civi/Api4/Afform.php | 6 ++++++ ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/ext/afform/core/CRM/Afform/AfformScanner.php b/ext/afform/core/CRM/Afform/AfformScanner.php index b93b54d21e..b965765c57 100644 --- a/ext/afform/core/CRM/Afform/AfformScanner.php +++ b/ext/afform/core/CRM/Afform/AfformScanner.php @@ -141,6 +141,7 @@ class CRM_Afform_AfformScanner { */ public function getMeta(string $name, bool $getLayout = FALSE): ?array { $defn = []; + $mtime = NULL; $jsonFile = $this->findFilePath($name, self::METADATA_JSON); $htmlFile = $this->findFilePath($name, self::LAYOUT_FILE); @@ -149,15 +150,18 @@ class CRM_Afform_AfformScanner { // Json takes priority because local overrides are always saved in that format. if ($jsonFile !== NULL) { $defn = json_decode(file_get_contents($jsonFile), 1); + $mtime = filemtime($jsonFile); } // Extensions may provide afform definitions in php files else { $phpFile = $this->findFilePath($name, self::METADATA_PHP); if ($phpFile !== NULL) { $defn = include $phpFile; + $mtime = filemtime($phpFile); } } if ($htmlFile !== NULL) { + $mtime = max($mtime, filemtime($htmlFile)); if ($getLayout) { // If the defn file included a layout, the html file overrides $defn['layout'] = file_get_contents($htmlFile); @@ -168,6 +172,7 @@ class CRM_Afform_AfformScanner { return NULL; } $defn['name'] = $name; + $defn['modified_date'] = date('Y-m-d H:i:s', $mtime); return $defn; } diff --git a/ext/afform/core/Civi/Api4/Afform.php b/ext/afform/core/Civi/Api4/Afform.php index 214ed88e5a..cac1f9f799 100644 --- a/ext/afform/core/Civi/Api4/Afform.php +++ b/ext/afform/core/Civi/Api4/Afform.php @@ -243,6 +243,12 @@ class Afform extends Generic\AbstractEntity { 'data_type' => 'Array', 'description' => 'HTML form layout; format is controlled by layoutFormat param', ], + [ + 'name' => 'modified_date', + 'title' => E::ts('Date Modified'), + 'data_type' => 'Timestamp', + 'readonly' => TRUE, + ], ]; // Calculated fields returned by get action if ($self->getAction() === 'get') { diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php index fa5766e857..2478ea0695 100644 --- a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php +++ b/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php @@ -34,6 +34,10 @@ class AfformGetTest extends \PHPUnit\Framework\TestCase implements HeadlessInter $this->assertEquals($this->formName, $result['name']); $this->assertArrayNotHasKey('directive_name', $result); $this->assertArrayNotHasKey('has_base', $result); + // Check modified date is reasonable + $this->assertGreaterThan('2023-01-01 12:00:00', $result['modified_date']); + // Hopefully this test won't need updating for the next 2000 years or so... + $this->assertLessThan('4000-01-01 12:00:00', $result['modified_date']); // Select * should also return regular fields only $result = Afform::get() -- 2.25.1