SugarCRM 6.5 CE:如何根据条件删除详细视图中的按钮

SugarCRM 6.5 CE: how to remove button in detailview according to a condition

如果线索已经转换,我正在尝试删除线索详细视图中的按钮。

我看到了一个类似的 question,它使用 javascript 来隐藏按钮。我试图通过 php.

获得相同的结果

这是我在 custom\modules\Leads\views\ 文件夹中的 view.detail.php

class LeadsViewDetail extends ViewDetail {

    function __construct(){
        parent::__construct();
    }

    function preDisplay() {
        parent::preDisplay();

        if($this->bean->converted==1) {
            echo "hide";
            foreach ($this->dv->defs['templateMeta']['form']['buttons'] as $key => $value) {
                unset($this->dv->defs['templateMeta']['form']['buttons'][$key]);
            }
        } else {
            echo "show";
        }
    }
}

使用此代码,在快速修复和重建后,我根据潜在客户状态正确地看到 "hide" 或 "show",但按钮未正确更新。

如果我在 QR&R 之后打开转换后的潜在客户,我将永远不会看到按钮。

如果我在 QR&R 后打开未转换的潜在客户,我将始终看到按钮。

我被这种情况困住了。任何人都可以向我解释问题出在哪里吗?我该如何解决? 非常感谢您的帮助。

您可以在 custom/modules/Leads/metadata/detailviewdefs.php 中使用 Smarty 逻辑 ("customCode") 在不扩展 ViewDetail 的情况下处理这个问题。看起来 Convert 按钮已经仅在用户具有编辑权限时呈现,因此向其添加一个条件并不是什么大问题...

$viewdefs['Leads']['DetailView']['templateMeta']['form]['buttons'][] = array('customCode' => '
{if $bean->aclAccess("edit") && $bean->converted}
<input title="{$MOD.LBL_CONVERTLEAD_TITLE}"
  accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}"
  type="button"
  class="button" 
  name="convert"
  value="{$MOD.LBL_CONVERTLEAD}"
  onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" />
{/if}');

或者,如果您确实有多个条件,并且它们变得过于混乱或难以使 Smarty 逻辑变得合理,我们可以将少量 Smarty 逻辑与扩展的 ViewDetail 相结合。

custom/modules/Leads/metadata/detailviewdefs.php 外,这实际上是 SugarCRM CE 6.5.24 的开箱即用文件,看起来他们实际上已经尝试通过提供 Smarty var 来简化此自定义$DISABLE_CONVERT_ACTION。作为参考,它只需要设置和启用全局配置变量 disable_convert_lead,但我怀疑这是早期版本中未包含的相对较新的功能。尽管如此,它仍然是使用 View 设置一个简单的 Smarty 变量的好例子,我们可以以此为中心:

<?php
$viewdefs['Leads']['DetailView'] = array (
  'templateMeta' => array (
    'form' => array (
      'buttons' => array (
        'EDIT',
        'DUPLICATE',
        'DELETE',
        array (
          'customCode' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}<input title="{$MOD.LBL_CONVERTLEAD_TITLE}" accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}" type="button" class="button" onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" name="convert" value="{$MOD.LBL_CONVERTLEAD}">{/if}',
          //Bug#51778: The custom code will be replaced with sugar_html. customCode will be deplicated.
          'sugar_html' => array(
            'type' => 'button',
            'value' => '{$MOD.LBL_CONVERTLEAD}',
            'htmlOptions' => array(
              'title' => '{$MOD.LBL_CONVERTLEAD_TITLE}',
              'accessKey' => '{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}',
              'class' => 'button',
              'onClick' => 'document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'',
              'name' => 'convert',
              'id' => 'convert_lead_button',
            ),
            'template' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}[CONTENT]{/if}',
          ),
        ),

我们可以将此 $DISABLE_CONVERT_ACTION 引用与 custom/modules/Leads/views/view.detail.php 结合起来,如下所示,以根据我们想要的任何条件进行设置:

<?php
require_once('modules/Leads/views/view.detail.php');
class CustomLeadsViewDetail extends LeadsViewDetail {

  /*
   * while we might normally like to call parent::display() in this method to 
   * best emulate what the parnts will do, we instead here copy-and-paste the
   * parent methods' content because LeadsViewDetail::display() will set the
   * DISABLE_CONVERT_ACTION Smarty var differently than we want. 
   */
  public function display(){
    global $sugar_config;

    // Example One: Disable Conversion when status is Converted
    $disableConvert = ($this->bean->status == 'Converted');

    // Example Two: Disable Conversion when there is at lead one related Call
    // where the status is Held
    $disableConvert = FALSE;
    $this->bean->load_relationships('calls');
    foreach($this->bean->calls->getBeans() as $call){
      if($call->status == 'Held'){
        $disableConvert = TRUE;
        break; // exit foreach()
      }
    }

    // Example Three: Disable Conversion if the User is in a specific Role, e.g.
    // Interns who are great for data entry in Leads but shouldn't be making 
    // actual sales
    global $current_user;
    $disableConvert = $current_user->check_role_membership('No Lead Conversions');

    // In any of the above examples, once we have $disableConvert set up
    // as we want, let the Smarty template know.
    $this->ss->assign("DISABLE_CONVERT_ACTION", $disableConvert);

    // copied from ViewDetail::display();
    if(empty($this->bean->id)) {
      sugar_die($GLOBALS['app_strings']['ERROR_NO_RECORD']);
    }
    $this->dv->process();
    echo $this->dv->display();
  }
}