2013-03-06 74 views
2

我想要点击某个模块(示例联系人)的编辑视图中的保存时弹出一些消息(稍后我会在弹出的对话框中选择确定和取消)。SugarCRM-点击保存按钮时如何获取POPUP?

我的功能

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中什么也没有发生,但我想在那一刻得到弹出一个带有消息(稍后我会添加确定和取消选项)。

我在做什么错? 谢谢

与弹出只有一些条件码更新时间:

.... 
    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; 
     } 

回答

11

有许多方法可以做到的事情在SugarCRM的,这使得它都非常强大,有时很难定制 - 因为你有很多不同的选择。

为了使点击“保存”按钮时发生某种弹出或任何自定义日志,我建议下面的解决方案,而不是改变editviewdefs

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

您需要做的是创建一个自定义可安装包,并使用Module Loader将其安装到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数组元素。

'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' 
       ) 
     ) 

custom_popup_js_include.php文件帐户的JavaScript添加一个新方法到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实例的优点。

+0

男人你是天才!希望你知道:)不能够感谢你足够...正是我想要的。 - 在确定保存它取消不保存!还有一件小事 - 如果我希望只有在示例字段类型的联系人是“业务”时才弹出此警告。在哪里输入该条件?在一些PHP文件或JavaScript文件?如果你能告诉我那个额外的事情。预先感谢您 – Dejan 2013-03-07 01:11:11

+1

在'window.formToCheck = formname;'后面的'customPopUpContacts.js'中,您可以将这个'confirm'代码块封装在一个if语句中,以使其仅出现有条件。就个人而言,我使用SugarCRM的jQuery,因为我发现它更直接。但是你可以像'var contactTypeField = document.getElementById('contact_type');如果(contactTypeField.value =='Business'){//确认块将会在这里}' – 2013-03-07 01:15:43

+0

我试着用first_field进行测试。如果Tori出现,我进入了。如果我点击与托里联系作为名字,一切都很好。但是,如果我尝试点击带有其他名称的联系人(李)保存没有任何反应 - 它不会保存我的记录,但它应该保存记录,只是不显示弹出。在first_name!= Tori的情况下,我只能做取消,保存没有任何反应。请看看我的代码,我将它添加到原始文章。非常感谢 – Dejan 2013-03-07 01:30:45

2

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