From 4411002fc39cb27eb105d705ec055281abf4c513 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 4 Jan 2015 22:04:09 -0500 Subject: [PATCH] CRM-15759 - Support editable multi-value custom data --- .../Smarty/plugins/function.crmAttributes.php | 51 +++++++++++++++++++ .../Page/MultipleRecordFieldsListing.php | 43 +++++++++++++++- .../Page/MultipleRecordFieldsListing.tpl | 6 +-- 3 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 CRM/Core/Smarty/plugins/function.crmAttributes.php diff --git a/CRM/Core/Smarty/plugins/function.crmAttributes.php b/CRM/Core/Smarty/plugins/function.crmAttributes.php new file mode 100644 index 0000000000..f542f0b49e --- /dev/null +++ b/CRM/Core/Smarty/plugins/function.crmAttributes.php @@ -0,0 +1,51 @@ + to a specific part of the page + * + * @param array $params ['a'] array of attributes + * @param CRM_Core_Smarty $smarty + * + * @throws Exception + */ +function smarty_function_crmAttributes($params, &$smarty) { + $output = ''; + $attributes = isset($params['a']) ? $params['a'] : array(); + foreach ($attributes as $name => $vals) { + $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; + } + return $output; +} diff --git a/CRM/Profile/Page/MultipleRecordFieldsListing.php b/CRM/Profile/Page/MultipleRecordFieldsListing.php index 8da1a9a1c7..6c7b883337 100644 --- a/CRM/Profile/Page/MultipleRecordFieldsListing.php +++ b/CRM/Profile/Page/MultipleRecordFieldsListing.php @@ -220,7 +220,7 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { } if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { $options = array( ); - $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format'); + $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format', 'default_value', 'is_required'); foreach ($fieldIDs as $key => $fieldID) { $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID; $param = array('id' => $fieldIDs[$key]); @@ -239,6 +239,8 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type']; $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type']; + $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']); + $options[$fieldIDs[$key]]['attributes']['default_value'] = CRM_Utils_Array::value('default_value', $returnValues); $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues); @@ -296,10 +298,46 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { // assign to $result $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); } - if (!$customValue) { + // FIXME: getDisplayValue should always return a string so why is this necessary? + if (!$customValue && $customValue !== '0') { $customValue = ""; } + // Set field attributes to support crmEditable + // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type + // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable + $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_$recId"); + $editable = FALSE; + if ($linkAction & CRM_Core_Action::UPDATE) { + $spec = $options[$fieldId]['attributes']; + switch ($spec['html_type']) { + case 'Text': + // Other data types like money would require some extra validation + // FIXME: crmEditable currently does not support any validation rules :( + $supportedDataTypes = array('Float', 'String', 'Int'); + $editable = in_array($spec['data_type'], $supportedDataTypes); + break; + case 'TextArea': + $editable = TRUE; + $fieldAttributes['data-type'] = 'textarea'; + break; + case 'Radio': + case 'Select': + case 'Select Country': + case 'Select State/Province': + $editable = TRUE; + $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select'; + if (!$spec['is_required']) { + $fieldAttributes['data-empty-option'] = ts('- none -'); + } + break; + } + } + if ($editable) { + $fieldAttributes['class'] .= ' crm-editable'; + } + $attributes[$fieldId][$recId] = $fieldAttributes; + $op = NULL; if ($this->_pageViewType == 'profileDataView') { $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, @@ -359,6 +397,7 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { $this->assign('customGroupTitle', $this->_customGroupTitle); $this->assign('headers', $headers); $this->assign('records', $result); + $this->assign('attributes', $attributes); } /** diff --git a/templates/CRM/Profile/Page/MultipleRecordFieldsListing.tpl b/templates/CRM/Profile/Page/MultipleRecordFieldsListing.tpl index 8f4a4fd45b..a9ca4c6b92 100644 --- a/templates/CRM/Profile/Page/MultipleRecordFieldsListing.tpl +++ b/templates/CRM/Profile/Page/MultipleRecordFieldsListing.tpl @@ -32,7 +32,7 @@ {/if} {if $records and $headers} {include file="CRM/common/jsortable.tpl"} -
+
{strip} @@ -51,9 +51,9 @@ {foreach from=$headers key=hrecId item=head} {if $dateFieldsVals.$hrecId.$recId} - + {else} - + {/if} {/foreach} -- 2.25.1
{$rows.$hrecId|crmDate:"%b %d, %Y %l:%M %P"}{$rows.$hrecId|crmDate:"%b %d, %Y %l:%M %P"}{$rows.$hrecId}{$rows.$hrecId}{$rows.action}