From: Tim Otten <totten@civicrm.org>
Date: Tue, 10 Dec 2019 06:05:10 +0000 (-0800)
Subject: Afform.get - Return computed fields, `has_local` and `has_packaged`
X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=10fd70a3b8004bb8d063d21319d6be967d352527;p=civicrm-core.git

Afform.get - Return computed fields, `has_local` and `has_packaged`
---

diff --git a/ext/afform/core/CRM/Afform/AfformScanner.php b/ext/afform/core/CRM/Afform/AfformScanner.php
index 6a6c797343..f0cc0f6f4d 100644
--- a/ext/afform/core/CRM/Afform/AfformScanner.php
+++ b/ext/afform/core/CRM/Afform/AfformScanner.php
@@ -152,6 +152,21 @@ class CRM_Afform_AfformScanner {
     }
   }
 
+  public function getComputedFields($name) {
+    // Ex: $allPaths['viewIndividual'][0] == '/var/www/foo/afform/view-individual'].
+    $allPaths = $this->findFilePaths()[$name];
+    // $activeLayoutPath = $this->findFilePath($name, self::LAYOUT_FILE);
+    // $activeMetaPath = $this->findFilePath($name, self::METADATA_FILE);
+    $localLayoutPath = $this->createSiteLocalPath($name, self::LAYOUT_FILE);
+    $localMetaPath = $this->createSiteLocalPath($name, self::METADATA_FILE);
+
+    $fields = [];
+    $fields['has_local'] = file_exists($localLayoutPath) || file_exists($localMetaPath);
+    $fields['has_packaged'] = ($fields['has_local'] && count($allPaths) > 1)
+      || (!$fields['has_local'] && count($allPaths) > 0);
+    return $fields;
+  }
+
   /**
    * @param string $formName
    *   Ex: 'view-individual'
diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Get.php b/ext/afform/core/Civi/Api4/Action/Afform/Get.php
index 92d5df4564..13288cc739 100644
--- a/ext/afform/core/Civi/Api4/Action/Afform/Get.php
+++ b/ext/afform/core/Civi/Api4/Action/Afform/Get.php
@@ -19,6 +19,7 @@ class Get extends \Civi\Api4\Generic\BasicGetAction {
     $values = [];
     foreach ($names as $name) {
       $record = $scanner->getMeta($name);
+      $record = array_merge($record, $scanner->getComputedFields($name));
       $layout = $this->_isFieldSelected('layout') ? $scanner->getLayout($name) : NULL;
       if ($layout !== NULL) {
         // FIXME check for validity?
diff --git a/ext/afform/core/Civi/Api4/Afform.php b/ext/afform/core/Civi/Api4/Afform.php
index 8bfecf5b1b..9dc720ddf8 100644
--- a/ext/afform/core/Civi/Api4/Afform.php
+++ b/ext/afform/core/Civi/Api4/Afform.php
@@ -86,7 +86,7 @@ class Afform extends AbstractEntity {
 
   public static function getFields() {
     return new BasicGetFieldsAction('Afform', __FUNCTION__, function($self) {
-      return [
+      $fields = [
         [
           'name' => 'name',
         ],
@@ -111,6 +111,17 @@ class Afform extends AbstractEntity {
           'name' => 'layout',
         ],
       ];
+
+      if ($self->getAction() === 'get') {
+        $fields[] = [
+          'name' => 'has_local',
+        ];
+        $fields[] = [
+          'name' => 'has_packaged',
+        ];
+      }
+
+      return $fields;
     });
   }
 
diff --git a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
index ed39450d07..700146ce95 100644
--- a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
+++ b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
@@ -35,6 +35,8 @@ trait AfformSaveTrait {
       $fields = \Civi\Api4\Afform::getfields()->setCheckPermissions(FALSE)->addSelect('name')->execute()->column('name');
       unset($fields[array_search('layout', $fields)]);
       unset($fields[array_search('name', $fields)]);
+      unset($fields[array_search('has_local', $fields)]);
+      unset($fields[array_search('has_packaged', $fields)]);
       $orig = \Civi\Api4\Afform::get()->setCheckPermissions(FALSE)->addWhere('name', '=', $item['name'])->setSelect($fields)->execute()->first();
     }
 
diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php
index 45bc30c2dc..c8645bf17b 100644
--- a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php
+++ b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php
@@ -59,6 +59,8 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
     $this->assertEquals($get($originalMetadata, 'description'), $get($result[0], 'description'), $message);
     $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
     $this->assertTrue(is_array($result[0]['layout']), $message);
+    $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message);
+    $this->assertEquals(FALSE, $get($result[0], 'has_local'), $message);
 
     $message = 'After updating with Afform.create, the revised data should be returned';
     $result = Civi\Api4\Afform::update()
@@ -75,6 +77,8 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
     $this->assertEquals('The temporary description', $get($result[0], 'description'), $message);
     $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
     $this->assertTrue(is_array($result[0]['layout']), $message);
+    $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message);
+    $this->assertEquals(TRUE, $get($result[0], 'has_local'), $message);
 
     Civi\Api4\Afform::revert()->addWhere('name', '=', $formName)->execute();
     $message = 'After reverting, the final Afform.get should return default data';
@@ -84,6 +88,8 @@ class api_v4_AfformTest extends api_v4_AfformTestCase {
     $this->assertEquals($get($originalMetadata, 'description'), $get($result[0], 'description'), $message);
     $this->assertEquals($get($originalMetadata, 'server_route'), $get($result[0], 'server_route'), $message);
     $this->assertTrue(is_array($result[0]['layout']), $message);
+    $this->assertEquals(TRUE, $get($result[0], 'has_packaged'), $message);
+    $this->assertEquals(FALSE, $get($result[0], 'has_local'), $message);
   }
 
   public function getFormatExamples() {