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

问题描述 投票:0回答:2

在Suitecrm / SugarCRM 6.5中

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

在帐户模块中,name字段可能基于某些条件为空,我要求在发生这种情况时显示自定义字段的值。要做到这一点,首先我在Edit / Detail vies中像往常一样使用'customCode'+ smarty,但在几个帖子中提到这在ListView中是不可能的。让步是使用逻辑钩子+非db字段来存储计算的字段。在这个SO 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中验证了Account中的name字段是typename我不知道这对于获得它的价值意味着什么。

感谢您的评论

php sugarcrm suitecrm
2个回答
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。


0
投票

问题在于逻辑钩子是由于首先$bean无法访问自定义字段,所以我必须使用$bean->custom_fields->retrieve();调用它们。名称字段总是空的,我不得不使用DBManager to get only the name field

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

<?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字段,我理解其他字段仍然是空的。

我希望这很有用

© www.soinside.com 2019 - 2024. All rights reserved.