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

时间:2019-03-05 01:37:53

标签: php sugarcrm suitecrm

在Suitecrm / SugarCRM 6.5中

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

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

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类型,但我不知道这对于获取其值意味着什么。

感谢您的评论

2 个答案:

答案 0 :(得分:0)

问题出在逻辑钩子上是由于首先from operator import add [list(map(add, *p)) for p in zip(list1, list1[1:] + list1[:1])] 无法访问自定义字段,因此我必须使用[[10, 13, 16, 20, 24], [16, 18, 14, 32, 40], [10, 13, 10, 28, 34]] 来调用它们,而且名称字段始终为空,我不得不使用DBManager to get only the name field

最后一个逻辑钩子的逻辑如下:

$bean

我对方法$bean->custom_fields->retrieve();不熟悉,目前不知道<?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; } } 字段为何为空,而且我知道其他字段仍然为空。

我希望这是有用的

答案 1 :(得分:0)

我们实现了一个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。