【问题标题】:SugarCRM- How to get POPUP when click on Save button?SugarCRM-点击保存按钮时如何获得弹出窗口?
【发布时间】:2013-03-06 18:02:35
【问题描述】:

我希望在某些模块(例如联系人)的编辑视图中单击“保存”时弹出一些消息(稍后我将在该弹出窗口中获得“确定”和“取消”选项。)。

我的功能

YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );

当我把它放在editviewdefs.php的顶部时工作(我还必须包括 cache/include/javascript/sugar_grp_yui_widgets.js) ) 文件,当打开该视图时,我会弹出该视图。但我希望它在保存时弹出,而不是在打开 EditView 时弹出(这只是向我展示YAHOO 函数正在工作的测试)。所以我尝试在custom/modules/Contacts 中创建单独的customJavascript.js 文件:

    //<script type="text/javascript"
 src="cache/include/javascript/sugar_grp_yui_widgets.js"></script>
    function check_custom_data()
    {
    YAHOO.SUGAR.MessageBox.show({msg: 'Foo'} );

    }

我修改了custom/modules/Contacts/metadata/editviewdefs.php

<?php
$module_name = 'Contacts';
$viewdefs ['Contacts'] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'form' => 
      array (
        'hidden' => 
        array (
          0 => '<input type="hidden" name="opportunity_id" value="{$smarty.request.opportunity_id}">',
          1 => '<input type="hidden" name="case_id" value="{$smarty.request.case_id}">',
          2 => '<input type="hidden" name="bug_id" value="{$smarty.request.bug_id}">',
          3 => '<input type="hidden" name="email_id" value="{$smarty.request.email_id}">',
          4 => '<input type="hidden" name="inbound_email_id" value="{$smarty.request.inbound_email_id}">',
        ),
      ),

      array(
         'buttons' =>
        array (
         0 =>
          array(
           'customCode' =>
            '<input title="Save [Alt+S]" accessKey="S" onclick="this.form.action.value=\'Save\'; return check_custom_data();" type="submit" name="button" value="'.$GLOBALS['app_strings']['LBL_SAVE_BUTTON_LABEL'].'">',
          ),
         1 =>'Cancel'
        )
      ),
        'includes'=> array(
   array('file'=>'custom/modules/Contacts/customJavascript.js'),
      ),
..........
.......

但是当我在 EditView 中单击 Save 时没有任何反应,但我希望在那一刻弹出消息(稍后我将添加 OK 和 Cancel 选项)。

我做错了什么? 谢谢

仅在某些条件下更新了弹出代码:

....
     window.formToCheck = formname;

        var contactTypeField = document.getElementById('first_name');
        if (contactTypeField.value == 'Tori')
        {
        if (confirm("This dialog will pop-up whenever the user click on the Save button. "
                + "If you click OK, then you can execute some custom code, and then "
                + "execute the old form check function, which will process and submit "
                + "the form, using SugarCRM's standard behavior.")) {

            var customCodeVariable = 5;
            customCodeVariable = 55 + (customCodeVariable * 5);

            return window.old_check_form(formname);
        }

        return false;
        }

【问题讨论】:

    标签: popup sugarcrm editview


    【解决方案1】:

    SugarCRM 中有多种执行方式,这使得它非常强大,有时也很难自定义 - 因为有很多不同的选项可供您使用。

    要在单击“保存”按钮时出现某种弹出窗口或任何自定义日志,我建议使用以下解决方案,而不是更改 editviewdefs

    以下解决方案不需要您修改任何核心 SugarCRM 文件,因此升级安全,可以轻松安装在另一个实例上。

    您需要做的是创建一个自定义的可安装包,并使用模块加载器将其安装到 SugarCRM 中。

    这是您最终需要的目录结构布局:

    SugarModuelPopUp
       ->custom
          ->include
             ->customPopUps
                ->custom_popup_js_include.php
                ->customPopUpContacts.js
       ->manifest.php
    

    创建SugarModuelPopUp 文件夹,该文件夹将作为此自定义包的根目录。

    SugarModuelPopUp 内部,创建一个名为manifest.php 的新PHP 文件。该文件告诉 SugarCRM 如何安装软件包。

    manifest.php中,粘贴以下代码:

    <?php
    $manifest = array(
            array(
                    'acceptable_sugar_versions' => array()
            ),
            array(
                    'acceptable_sugar_flavors' => array()
            ),
            'readme' => 'Please consult the operating manual for detailed installation instructions.',
            'key' => 'customSugarMod1',
            'author' => 'Kyle Lowry',
            'description' => 'Adds pop-up dialog on save on Contacts module.',
            'icon' => '',
            'is_uninstallable' => true,
            'name' => 'Pop-Up Dialog On Save',
            'published_date' => '2013-03-06 12:00:00',
            'type' => 'module',
            'version' => 'v1',
            'remove_tables' => 'prompt'
    );
    
    $installdefs = array(
            'id' => 'customSugarMod1',
            'copy' => array(
                    array(
                            'from' => '<basepath>/custom/',
                            'to' => 'custom/'
                    )
            ),
            'logic_hooks' => array(
                    array(
                            'module' => 'Contacts',
                            'hook' => 'after_ui_frame',
                            'order' => 1,
                            'description' => 'Creates pop-up dialog on save action.',
                            'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                            'class' => 'CustomPopJs',
                            'function' => 'getContactJs'
                    )
            )
    );
    

    接下来,您需要创建custom 文件夹。在其中,创建include 文件夹。在其中创建 customPopUps 文件夹。

    接下来,您将要创建custom_popup_js_include.php 文件。此文件控制您的自定义 JavaScript 何时何地被包含在页面中。粘贴以下代码:

    <?php
    // prevent people from accessing this file directly
    if (! defined('sugarEntry') || ! sugarEntry) {
        die('Not a valid entry point.');
    }
    class CustomPopJs {
        function getContactJs($event, $arguments) {
            // Prevent this script from being injected anywhere but the EditView.
            if ($_REQUEST['action'] != 'EditView') {
                // we are not in the EditView, so simply return without injecting
                // the Javascript
                return;
            }
            echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpContacts.js"></script>';
        }
    }
    

    接下来,您需要创建customPopUpContacts.js 文件,该文件将在单击联系人模块EditView 中的“保存”按钮时创建自定义弹出窗口。粘贴以下代码:

    function override_check_form() {
        // store a reference to the old form checking function
        window.old_check_form = window.check_form;
        // set the form checking function equal to something custom
        window.check_form = function(formname) {
            window.formToCheck = formname;
            // you can create the dialog however you wish, but for simplicity I am
            // just using standard javascript functions
            if (confirm("This dialog will pop-up whenever the user click on the Save button. "
                    + "If you click OK, then you can execute some custom code, and then "
                    + "execute the old form check function, which will process and submit "
                    + "the form, using SugarCRM's standard behavior.")) {
                // you have clicked OK, so do some custom code here,
                // replace this code with whatever you really want to do.
                var customCodeVariable = 5;
                customCodeVariable = 55 + (customCodeVariable * 5);
                // now that your custom code has executed, you can let
                // SugarCRM take control, process the form, and submit
                return window.old_check_form(formname);
            }
            // the user clicked on Cancel, so you can either just return false
            // and leave the person on the form, or you can execute some custom
            // code or do whatever else you want.
            return false;
        }
    }
    
    // call the override function, which will replace the old form checker
    // with something custom
    override_check_form();
    

    创建上述目录结构和正确文件夹中的文件后,您可以创建项目的 ZIP 文件。请务必注意,对于 SugarCRM 可安装包,您的 ZIP 文件必须包含项目目录中的所有内容。也就是说,您不会压缩 SugarModuelPopUp 文件夹,而是压缩其中的所有内容。

    接下来,您需要使用 SugarCRM 的模块加载器安装自定义包。你可以这样做:

    1. 转到 SugarCRM 管理页面。
    2. 点击“模块加载器”。
    3. 点击“浏览”并选择 ZIP 包。
    4. 点击“上传”按钮。
    5. 上传包后,在可安装包列表中找到它的条目,然后点击“安装”;继续执行标准 SugarCRM 安装过程。

    安装此自定义包后,无论何时单击联系人模块EditView 中的“保存”按钮,都会弹出一个对话框。你可以用你想要的任何东西替换对话框代码,这样你就不用修改框架它的代码了。

    此外,您应该能够将此项目用作将来向 SugarCRM EditViews 添加功能的基础。任何在单击“保存”按钮时使用check_form 方法的模块都可以执行这种自定义逻辑。

    例如,要对帐户执行此操作,您需要执行以下操作:

    在 manifest.php 中的 logic_hooks 数组元素中为 Accounts 添加一个条目。

    'logic_hooks' => array(
                    array(
                            'module' => 'Contacts',
                            'hook' => 'after_ui_frame',
                            'order' => 1,
                            'description' => 'Creates pop-up dialog on save action.',
                            'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                            'class' => 'CustomPopJs',
                            'function' => 'getContactJs'
                    ),
                    array(
                            'module' => 'Accounts',
                            'hook' => 'after_ui_frame',
                            'order' => 1,
                            'description' => 'Creates pop-up dialog on save action.',
                            'file' => 'custom/include/customPopUps/custom_popup_js_include.php',
                            'class' => 'CustomPopJs',
                            'function' => 'getAccountJs'
                    )
            )
    

    为帐户 JavaScript 的 custom_popup_js_include.php 文件中的 CustomPopJs 添加一个新方法。

    function getAccountJs($event, $arguments) {
            // Prevent this script from being injected anywhere but the EditView.
            if ($_REQUEST['action'] != 'EditView') {
                // we are not in the EditView, so simply return without injecting
                // the Javascript
                return;
            }
            echo '<script type="text/javascript" src="custom/include/customPopUps/customPopUpAccounts.js"></script>';
        }
    

    创建customPopUpAccounts.js 文件,并使用customPopUpContacts.js 代码作为所需功能的基础。

    还有其他方法可以在 SugarCRM 中实现您的目标,但这是我个人使用的一种,它的好处是可以安全升级并轻松迁移到其他 SugarCRM 实例。

    【讨论】:

    • 伙计,你是天才!希望你知道:) 非常感谢你......正是我想要的。-在确定保存它取消不保存它!!!还有一件小事-如果我希望仅在示例字段“联系人类型”为“业务”时才弹出此警告。在哪里输入那个条件?在一些 php 文件或 javascript 文件中?如果你能告诉我那额外的事情。提前谢谢你
    • customPopUpContacts.js 中,就在window.formToCheck = formname; 之后,您可以将confirm 代码块包装在if 语句中,使其仅在有条件的情况下出现。就个人而言,我将 jQuery 与 SugarCRM 一起使用,因为我发现它更易于使用。但你可以做类似var contactTypeField = document.getElementById('contact_type'); if (contactTypeField.value == 'Business') { // the confirm block will go here }
    • 我尝试使用 first_field 进行测试。如果 Tori 然后弹出,我输入了。如果我单击“与 Tori 联系”作为名字,一切都会很好。但是,如果我尝试在 Save 上单击具有其他名称(Lee)的联系人,则不会发生任何事情 - 它不会保存我的记录,但它应该保存记录,只是不显示弹出窗口。如果 first_name!=Tori 我只能取消,保存时不会发生任何事情。请看一下我将其添加到原始帖子中的代码。非常感谢
    • 对不起,在我告诉你之前就应该意识到这一点。在您添加的 if 块的末尾,您需要有一个 else 来执行默认的检查表单功能。因此,在该 if (contactTypeField.value == 'Tori') 块的末尾,添加以下内容:else { return window.old_check_form(formname); }
    • 抱歉打扰或耽误您的时间,但我想知道您是否可以帮助我和这个?我正在调用一些网络服务,我想在某个字段中设置响应(在同一个按钮上能够调用网络服务并在字段中设置响应)stackoverflow.com/questions/15272803/…提前谢谢你
    【解决方案2】:

    我有 SugarCRM CE 6.5,但这种方法对我不起作用(我的意思是创建新模块)。但是,我修改了 logic_hook 并将文件直接放在 custom/include 文件夹中,它可以工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多