From 704d3260f3b98a14a6c739a74b0a949a02fdc068 Mon Sep 17 00:00:00 2001
From: Coleman Watts <coleman@civicrm.org>
Date: Mon, 3 Feb 2014 08:32:25 -0800
Subject: [PATCH] CRM-13966 - entityRef - Render frozen text as link when
 appropriate

---
 CRM/Core/Form.php          | 30 +++++++++++++++++-------------
 CRM/Core/Form/Renderer.php | 38 +++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php
index 19123c69e9..9391cd6e6b 100644
--- a/CRM/Core/Form.php
+++ b/CRM/Core/Form.php
@@ -1220,7 +1220,20 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    * @return HTML_QuickForm_Element
    */
   function addEntityRef($name, $label, $props = array(), $required = FALSE) {
-    $props['class'] = isset($props['class']) ? $props['class'] . ' crm-select2' : 'crm-select2';
+    $props['api'] = CRM_Utils_Array::value('api', $props, array());
+    // Merge in defaults for api params
+    $props['api'] += array(
+      'entity' => 'contact',
+      'key' => 'id',
+    );
+    $props['api'] += array(
+      'action' => $props['api']['entity'] == 'contact' ? 'getquick' : 'get',
+      'search' => 'name',
+      'label' => 'data',
+    );
+
+    $props['class'] = isset($props['class']) ? $props['class'] . ' ' : '';
+    $props['class'] .= "crm-select2 crm-{$props['api']['entity']}-ref-field";
 
     $props['select'] = CRM_Utils_Array::value('select', $props, array()) + array(
       'minimumInputLength' => 1,
@@ -1232,14 +1245,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       //'formatNoMatches' => ts('No contacts found.'),
     );
 
-    $props['api'] = CRM_Utils_Array::value('api', $props, array()) + array(
-      'entity' => 'contact',
-      'action' => 'getquick',
-      'search' => 'name',
-      'label' => 'data',
-      'key' => 'id',
-    );
-
     $this->entityReferenceFields[] = $name;
     $this->formatReferenceFieldAttributes($props);
     return $this->add('text', $name, $label, $props, $required);
@@ -1267,7 +1272,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
         $field->setValue($val);
       }
       if ($val) {
-        $data = $labels = array();
+        $data = array();
         $api = json_decode($field->getAttribute('data-api-params'), TRUE);
         $select = json_decode($field->getAttribute('data-select-params'), TRUE);
         // Support serialized values
@@ -1279,14 +1284,13 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
           $result = civicrm_api3($api['entity'], $api['action'], array('sequential' => 1, $api['key'] => $v));
           if (!empty($result['values'])) {
             $data[] = array('id' => $v, 'text' => $result['values'][0][$api['label']]);
-            $labels[] = $result['values'][0][$api['label']];
           }
         }
         if ($field->isFrozen()) {
           $field->removeAttribute('class');
-          $field->setValue(implode(', ', $labels));
         }
-        elseif ($data) {
+        if ($data) {
+          // Simplify array for single selects - makes client-side code simpler (but feels somehow wrong)
           if (empty($select['multiple'])) {
             $data = $data[0];
           }
diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php
index f554149d89..17276b44c3 100644
--- a/CRM/Core/Form/Renderer.php
+++ b/CRM/Core/Form/Renderer.php
@@ -95,9 +95,9 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
    *
    * @access private
    *
-   * @param  object    An HTML_QuickForm_element object
-   * @param  bool      Whether an element is required
-   * @param  string    Error associated with the element
+   * @param  $element HTML_QuickForm_element
+   * @param  $required bool - Whether an element is required
+   * @param  $error string - Error associated with the element
    *
    * @return array
    */
@@ -117,6 +117,13 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
       }
     }
 
+    if (!empty($el['frozen'])) {
+      if ($element->getAttribute('data-api-params') && $element->getAttribute('data-entity-value')) {
+        $this->renderFrozenEntityRef($el, $element);
+      }
+      $el['html'] = '<div class="crm-frozen-field">' . $el['html'] . '</div>';
+    }
+
     return $el;
   }
 
@@ -173,6 +180,31 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
     $attributes['class'] = $class;
     $element->updateAttributes($attributes);
   }
+
+  /**
+   * Render entity references as text.
+   * If user has permission, format as link (or now limited to contacts).
+   * @param $el array
+   * @param $field HTML_QuickForm_element
+   */
+  function renderFrozenEntityRef(&$el, $field) {
+    $api = json_decode($field->getAttribute('data-api-params'), TRUE);
+    $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
+    if (isset($vals['id'])) {
+      $vals = array($vals);
+    }
+    $display = array();
+    foreach ($vals as $val) {
+      // Format contact as link
+      if ($api['entity'] == 'contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) {
+        $url = CRM_Utils_System::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id']));
+        $val['text'] = '<a href="' . $url . '" title="' . ts('View Contact') . '">' . $val['text'] . '</a>';
+      }
+      $display[] = $val['text'];
+    }
+
+    $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
+  }
 }
 // end CRM_Core_Form_Renderer
 
-- 
GitLab