Merge pull request #19223 from civicrm/5.33
[civicrm-core.git] / ext / afform / core / afform.php
index 910200a6208bf4887430a64c962b04d5f9e86855..49b3ba9f9977a3ed085f98e7e5e955a7fc2baff8 100644 (file)
@@ -127,6 +127,40 @@ function afform_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
  */
 function afform_civicrm_managed(&$entities) {
   _afform_civix_civicrm_managed($entities);
+
+  /** @var \CRM_Afform_AfformScanner $scanner */
+  if (\Civi::container()->has('afform_scanner')) {
+    $scanner = \Civi::service('afform_scanner');
+  }
+  else {
+    // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext.
+    // This AfformScanner instance only lives during this method call, and it feeds off the regular cache.
+    $scanner = new CRM_Afform_AfformScanner();
+  }
+
+  foreach ($scanner->getMetas() as $afform) {
+    if (empty($afform['is_dashlet']) || empty($afform['name'])) {
+      continue;
+    }
+    $entities[] = [
+      'module' => E::LONG_NAME,
+      'name' => 'afform_dashlet_' . $afform['name'],
+      'entity' => 'Dashboard',
+      'update' => 'always',
+      // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused
+      'cleanup' => 'always',
+      'params' => [
+        'version' => 3,
+        // Q: Should we loop through all domains?
+        'domain_id' => CRM_Core_BAO_Domain::getDomain()->id,
+        'is_active' => TRUE,
+        'name' => $afform['name'],
+        'label' => $afform['title'] ?? ts('(Untitled)'),
+        'directive' => _afform_angular_module_name($afform['name'], 'dash'),
+        'permission' => "@afform:" . $afform['name'],
+      ],
+    ];
+  }
 }
 
 /**
@@ -150,8 +184,7 @@ function afform_civicrm_caseTypes(&$caseTypes) {
 function afform_civicrm_angularModules(&$angularModules) {
   _afform_civix_civicrm_angularModules($angularModules);
 
-  $afforms = \Civi\Api4\Afform::get()
-    ->setCheckPermissions(FALSE)
+  $afforms = \Civi\Api4\Afform::get(FALSE)
     ->setSelect(['name', 'requires', 'module_name', 'directive_name'])
     ->execute();
 
@@ -352,15 +385,16 @@ function _af_fill_field_metadata($entityType, DOMElement $afField) {
   $params = [
     'action' => 'create',
     'where' => [['name', '=', $afField->getAttribute('name')]],
-    'select' => ['title', 'input_type', 'input_attrs', 'options'],
+    'select' => ['label', 'input_type', 'input_attrs', 'options'],
     'loadOptions' => TRUE,
   ];
   if (in_array($entityType, CRM_Contact_BAO_ContactType::basicTypes(TRUE))) {
     $params['values'] = ['contact_type' => $entityType];
     $entityType = 'Contact';
   }
-  $getFields = civicrm_api4($entityType, 'getFields', $params);
-  // Merge field definition data with whatever's already in the markup
+  // Merge field definition data with whatever's already in the markup.
+  // If the admin has chosen to include this field on the form, then it's OK for us to get metadata about the field - regardless of user's other permissions.
+  $getFields = civicrm_api4($entityType, 'getFields', $params + ['checkPermissions' => FALSE]);
   $deep = ['input_attrs'];
   foreach ($getFields as $fieldInfo) {
     $existingFieldDefn = trim(pq($afField)->attr('defn') ?: '');
@@ -488,7 +522,7 @@ function afform_civicrm_alterMenu(&$items) {
  * @see CRM_Utils_Hook::permission_check()
  */
 function afform_civicrm_permission_check($permission, &$granted, $contactId) {
-  if ($permission{0} !== '@') {
+  if ($permission[0] !== '@') {
     // Micro-optimization - this function may get hit a lot.
     return;
   }
@@ -508,6 +542,23 @@ function afform_civicrm_permission_check($permission, &$granted, $contactId) {
   }
 }
 
+/**
+ * Implements hook_civicrm_permissionList().
+ *
+ * @see CRM_Utils_Hook::permissionList()
+ */
+function afform_civicrm_permissionList(&$permissions) {
+  $scanner = Civi::service('afform_scanner');
+  foreach ($scanner->getMetas() as $name => $meta) {
+    $permissions['@afform:' . $name] = [
+      'group' => 'afform',
+      'title' => ts('Afform: Inherit permission of %1', [
+        1 => $name,
+      ]),
+    ];
+  }
+}
+
 /**
  * Clear any local/in-memory caches based on afform data.
  */
@@ -533,7 +584,7 @@ function _afform_angular_module_name($fileBaseName, $format = 'camel') {
       foreach (preg_split('/[-_ ]/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY) as $shortNamePart) {
         $camelCase .= ucfirst($shortNamePart);
       }
-      return strtolower($camelCase{0}) . substr($camelCase, 1);
+      return strtolower($camelCase[0]) . substr($camelCase, 1);
 
     case 'dash':
       return strtolower(implode('-', preg_split('/[-_ ]|(?=[A-Z])/', $fileBaseName, NULL, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));