Merge pull request #21308 from mattwire/deprecatedjoi
[civicrm-core.git] / CRM / Logging / ReportDetail.php
index bd0676e0599dfc47129147eb0d1d1280d6d94c93..36404cf1d5450671ad898a8d09ed979b026ba2d0 100644 (file)
@@ -215,12 +215,25 @@ class CRM_Logging_ReportDetail extends CRM_Report_Form {
           $to = implode(', ', array_filter($tos));
         }
 
+        $tableDAOClass = CRM_Core_DAO_AllCoreTables::getClassForTable($table);
+        if (!empty($tableDAOClass)) {
+          $tableDAOFields = (new $tableDAOClass())->fields();
+          // If this field is a foreign key, then we can later use the foreign
+          // class to translate the id into something more useful for display.
+          $fkClassName = $tableDAOFields[$field]['FKClassName'] ?? NULL;
+        }
         if (isset($values[$field][$from])) {
           $from = $values[$field][$from];
         }
+        elseif (!empty($from) && !empty($fkClassName)) {
+          $from = $this->convertForeignKeyValuesToLabels($fkClassName, $field, $from);
+        }
         if (isset($values[$field][$to])) {
           $to = $values[$field][$to];
         }
+        elseif (!empty($to) && !empty($fkClassName)) {
+          $to = $this->convertForeignKeyValuesToLabels($fkClassName, $field, $to);
+        }
         if (isset($titles[$field])) {
           $field = $titles[$field];
         }
@@ -450,4 +463,26 @@ class CRM_Logging_ReportDetail extends CRM_Report_Form {
     }
   }
 
+  /**
+   * Given a key value that we know is a foreign key to another table, return
+   * what the DAO thinks is the "label" for the foreign entity. For example
+   * if it's referencing a contact then return the contact name, or if it's an
+   * activity then return the activity subject.
+   * If it's the type of DAO that doesn't have such a thing, just echo back
+   * what we were given.
+   *
+   * @param string $fkClassName
+   * @param string $field
+   * @param int $keyval
+   * @return string
+   */
+  private function convertForeignKeyValuesToLabels(string $fkClassName, string $field, int $keyval): string {
+    if (property_exists($fkClassName, '_labelField')) {
+      $labelValue = CRM_Core_DAO::getFieldValue($fkClassName, $keyval, $fkClassName::$_labelField);
+      // Not sure if this should use ts - there's not a lot of context (`%1 (id: %2)`) - and also the similar field labels above don't use ts.
+      return "{$labelValue} (id: {$keyval})";
+    }
+    return (string) $keyval;
+  }
+
 }