繁体   English   中英

如何在ListView中正确显示非db字段的计算值?

[英]How to properly display a calculated value of a non-db field in the ListView?

在Suitecrm / SugarCRM 6.5中

我需要修改ListView。 根据特定条件显示字段的值。

在“帐户”模块中,根据某些条件, name字段可能为空,我要求在发生这种情况时显示自定义字段的值。 为此,首先我像往常一样在“编辑/详细信息”视图中尝试使用“ customCode” + smarty,但是在几篇文章中提到在ListView中这是不可能的。 允许使用逻辑钩子+非数据库字段来存储计算出的字段。 按照这个答案,我写了以下内容:

自定义字段位于custom/Extension/modules/Accounts/Ext/Vardefs/custom_field_name_c.php

<?php

$dictionary['Account']['fields']['name_c']['name'] = 'account_name_c';
$dictionary['Account']['fields']['name_c']['vname'] = 'LBL_ACCOUNT_NAME_C';
$dictionary['Account']['fields']['name_c']['type'] = 'varchar';
$dictionary['Account']['fields']['name_c']['len'] = '255';
$dictionary['Account']['fields']['name_c']['source'] = 'non-db';
$dictionary['Account']['fields']['name_c']['dbType'] = 'non-db';
$dictionary['Account']['fields']['name_c']['studio'] = 'visible';

标签位于suitecrm/custom/Extension/modules/Accounts/Ext/Language/es_ES.account_name_c.php

<?php

$mod_strings['LBL_ACCOUNT_NAME_C'] = 'Nombre de cuenta';

逻辑钩子条目位于custom/modules/Accounts/logic_hooks.php

$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(
    2,
    'Get proper name',
    'custom/modules/Accounts/hooks/ListViewLogicHook.php',
    'ListViewLogicHook',
    'getProperName'
);

逻辑钩子位于custom/modules/Accounts/hooks/ListViewLogicHook.php

<?php

class ListViewLogicHook
{
    public function getProperName(&$bean, $event, $arguments)
    {
        if (empty($bean->fetched_row['name'])) {
            $bean->account_name_c = $bean->fetched_row['person_name_c'];
        } else {
            $bean->account_name_c = $bean->fetched_row['name'];
        }

        // Here I also tried, but same result
        // $bean->account_name_c = empty($bean->name) ? $bean->person_name_c : $bean->name;
    }
}

listviewdefs.php位于custom/modules/Accounts/metadata/listviewdefs.php

我添加了领域

'NAME_C' =>
  array (
      'width' => '20%',
      'label' => 'LBL_ACCOUNT_NAME_C',
      'default' => true,
  ),

经过这些修改和修复,我希望看到该字段具有正确的值。 但它似乎是空的。 我已经验证了逻辑钩子已正确调用,但是nameperson_name_c字段始终为空。 如果相关,我已经在Studio中确认“帐户”中的name字段是类型name我不知道这对于获取其值意味着什么。

感谢您的评论

问题在于逻辑钩子是由于首先$bean无法访问自定义字段,因此我必须使用$bean->custom_fields->retrieve();来调用它们$bean->custom_fields->retrieve(); 另外,名称字段始终为空,我不得不使用DBManager 仅获取名称字段

最终逻辑挂钩的逻辑如下:

<?php

class ListViewLogicHook
{
    public function getProperName($bean, $event, $arguments)
    {
        // Get access to custom fields from $bean
        $bean->custom_fields->retrieve();

        // Get access to name property using DBManager because $bean->name return null
        $sql = "SELECT name FROM accounts WHERE id = '{$bean->id}'";
        $name = $GLOBALS['db']->getOne($sql);

        // Assign a value to non-db field
        $bean->name_c = empty($name) ? $bean->nombre_persona_c : $name;
    }
}

我不熟悉$bean->custom_fields->retrieve() ,目前不知道name字段为何为空,并且我知道其他字段仍为空。

我希望这是有用的

我们实现了非常快速和简单的after_retrieve挂钩-这是从一个工作示例中提取的。

public function RemoveCost(&$bean, $event, $arguments)
{
 global $current_user;
 include_once(‘modules/ACLRoles/ACLRole.php’);
 $roles =  ACLRole::getUserRoleNames($current_user->id);
 if(!array_search("CanSeeCostRoleName",$roles)){
  $bean->cost = 0;
  $bean->cost_usdollar = 0;
 }
}

您所需要做的就是定义此函数并将其添加到模块logic_hooks.php中

您甚至可以针对特定的呼叫进行调整:

if (isset($_REQUEST['module'],$_REQUEST['action']) && $_REQUEST['module'] == 'Opportunities' && $_REQUEST['action'] == 'DetailView')

有些时候,您确实希望显示该字段,例如quicksearch popup。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM