Skip to content
Snippets Groups Projects
Commit 704d3260 authored by colemanw's avatar colemanw
Browse files

CRM-13966 - entityRef - Render frozen text as link when appropriate

parent fd36866a
Branches
Tags
No related merge requests found
......@@ -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];
}
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment