糖CRM使用AJAX prevent重复值 [英] prevent duplicate value using ajax in sugar crm

查看:149
本文介绍了糖CRM使用AJAX prevent重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用模块生成器创建模块,现在我有一个叫做书名称字段 现在,如果我给同一本书的名字2时间t接受。

i have create module using module builder , now i am having a field called as book Name now if i give same book name 2 time t is accepting .

我不希望使用并插入检查重复的值,因为我想学习到code中的自定义。

i don't want to use and plug in for checking duplicate value because i want to learn the customization through code .

这样我就可以调用Ajax和检查数据库的天气同一本书的名字是存在于数据库或没有,但我不知道如何控制工作在糖CRM。以及如何调用AJAX糖CRM。

so i can call ajax and check in data base weather the same book name is exist in db or not but i don't know how controller works in sugar crm . and how to call ajax in sugar crm .

任何一个可以指导我,你的帮助是非常AP preciated。

can any one guide me , your help is much appreciated .

推荐答案

如果你真的想做到这一点用ajax那么我推荐一个为entryPoint的路要走。这种定制需要几个简单的事情。首先,你会写的JavaScript的一点点来执行实际的Ajax调用。这Ajax调用将张贴到你写的入口点。入口点会运行查询,为您和在编辑视图返回响应给你。因此我们要通过先写入口点开始。

If you really want to accomplish this using ajax then I'd recommend an entryPoint as the way to go. This customization will require a couple of simple things. First you'll write a little bit of javascript to perform the actual ajax call. That ajax call will post to the entryPoint you write. The entryPoint will run the query for you and return a response to you in the edit view. So lets get started by writing the entryPoint first.

首先,打开文件的定制/有/ MVC /控制器/ entry_point_registry.php。如果文件夹结构和文件还不存在,继续前进,创建它们。

First, open the file custom/include/MVC/Controller/entry_point_registry.php. If the folder structure and file do not exist yet, go ahead and create them.

添加以下code到entry_point_registry.php文件:

Add the following code to the entry_point_registry.php file:

$entry_point_registry['test'] = array('file' => 'custom/test.php', 'auth' => true);

有关该行的一些简单说明:

Some quick explanation about that line:

  • 在测试的指标值是可以改变的,以任何你喜欢的。也许unique_book_value更有意义,你的情况。您将看到如何使用该值在一分钟内。
  • 在阵列指向的文件的价值,你要去把你的实际code。你也应该给这个更有意义的名称。它并不需要上面提到的阵列密钥相匹配。
  • 的'权威性'=>真正的一部分决定了浏览器是否需要有一个活跃的登录会话的SugarCRM与否。在此情况下(几乎所有)我建议保持为true。

现在让我们看一下在code,将在自定义/ test.php的去(或你的情况unique_book_name.php):

Now lets look at the code that will go in custom/test.php (or in your case unique_book_name.php):

/* disclaimer: we are not gonna get all crazy with using PDO and parameterized queries at this point,
               but be aware that there is potential for sql injection here. The auth => true will help
               mitigate that somewhat, but you're never supposed to trust any input, blah blah blah. */

global $db; // load the global sugarcrm database object for your query

$book_name = urldecode($_REQUEST['book_name']); // we are gonna start with $_REQUEST to make this easier to test, but consider changing to $_POST when confirmed working as expected
$book_id   = urldecode($_REQUEST['book_id']);   // need to make sure this still works as expected when editing an existing record

// the $db->quote is an alias for mysql_real_escape_string() It still does not protect you completely from sql injection, but is better than not using it...
$sql = "SELECT id FROM book_module_table_name WHERE deleted = 0 AND name = '".$db->quote($book_name)."' AND id <> '".$db->quote($book_id)."'";

$res = $db->query($sql);

if ($db->getRowCount($res) > 0) {
    echo 'exists';
}
else {
    echo 'unique';
}

有关使用直接数据库查询的说明:有,你可以用它来完成此API方法。 (提示:$ bean-> retrieve_by_string_fields() - 看看这篇文章,如果你想要去的路线:<一href="http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/">http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/)不过,我觉得这个API是相当缓慢的,AJAX应该尽可能快。如果客户要求我提供此功能有一个99%的机会我会使用一个直接的数据库查询。可以使用PDO和参数化查询,如果我敢那天感觉花哨,但它是你的电话。

A note about using direct database queries: There are api methods you can use to accomplish this. (hint: $bean->retrieve_by_string_fields() - check out this article if you wanna go that route: http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/) However, I find the api to be rather slow and ajax should be as fast as possible. If a client asked me to provide this functionality there's a 99% chance I'd use a direct db query. Might use PDO and parameterized query if I'm feeling fancy that day, but it's your call.

使用上述code,你应该能够浏览到<一个href="https://crm.yourdomain.com/index.php?entryPoint=test">https://crm.yourdomain.com/index.php?entryPoint=test并运行code,我们只是写了。

Using the above code you should be able to navigate to https://crm.yourdomain.com/index.php?entryPoint=test and run the code we just wrote.

然而,在这一点上你会得到一个白色的屏幕。如果修改URL包括入口点的一部分,它加载您的主页或不走白屏有3个可能的原因:

However at this point all you're gonna get is a white screen. If you modify the url to include the entryPoint part and it loads your home page or does NOT go to a white screen there are 3 potential causes:

  1. 您把东西给$ entry_point_registry ['测试']不同。如果是更改URL阅读的index.php?为entryPoint = whatever_you_put_as_the_array_key
  2. 您有糖在域中的一个文件夹或东西在这样反而crm.yourdomain.com它位于某处又丑又笨的像yourdomain.com/sugarcrm/如果是这样的情况下,只要确保你正在修改的网址这样,实际的域部分是preserved。好吧,我会拼出来你... <一个href="https://yourdomain.com/sugarcrm/index.php?entryPoint=test">https://yourdomain.com/sugarcrm/index.php?entryPoint=test
  3. 这是较为少见,但由于某些原因,我想不通的apache有时需要增加一个新的入口点时重新加载。如果你有shell访问的快速/etc/init.d/apache2重装应该做的伎俩。如果你没有shell访问,你可能需要打开你的托管服务提供商的机票(或得到你有一定的控制!!!拜托男人fricking VPS!)

仍然没有工作?你是否注意到在HTTPS的S?尝试HTTP,而不是买了fricking $ 9 SSL证书,吉兹人!

Still not working? Did you notice the "s" in https? Try http instead and buy a fricking $9 ssl cert, geez man!

好前进。让我们测试出入口点位。添加一条记录到本模块。让我们添加的著作孙子兵法(不,不是孙子兵法,虽然你应该给一个书读得)。

Okay moving on. Let's test out the entryPoint a bit. Add a record to the book module. Let's add the book "War of Art" (no, not Art of War, although you should give that a read too).

现在的URL添加这样的:的index.php为entryPoint =测试及放大器; BOOK_NAME =艺术%20of%20War

Now in the url add this: index.php?entryPoint=test&book_name=Art%20of%20War

哦天啊那个URL编码是可怕的吧!不要担心。

Oh gawd that url encoding is hideous right! Don't worry about it.

您应该有希望得到一个丑陋的白色屏幕文本存在。如果我们要确保它也适用的其他方式。添加2在URL中的书的名字,希望它现​​在会说独一无二的。

You should hopefully get an ugly white screen with the text "exists". If you do let's make sure it also works the other way. Add a 2 to the book name in the url and hopefully it will now say "unique".

快速注:如果您使用的是糖,你可能也在使用MySQL在字符串搜索时它不区分大小写。如果你真的需要区分大小写看看这个SO文章: <一href="http://stackoverflow.com/questions/5629111/how-can-i-make-sql-case-sensitive-string-comparison-on-mysql">How我可以对MySQL的SQL字符串,区分大小写比较?

Quick note: if you're using Sugar you're probably also using mysql which is case insensitive when searching on strings. If you really need case sensitivity check out this SO article: How can I make SQL case sensitive string comparison on MySQL?

好了,现在我们有我们为entryPoint工作,我们可以继续让一切都ajaxical乐趣的一部分。有一对夫妇的方式来进行此事,而不是去最基本的途径,我要告诉你我所发现的最可靠途径。

Okay so now we have our entryPoint working and we can move on to the fun part of making everything all ajaxical. There are a couple ways to go about this, but rather than going the most basic route I'm gonna show you what I've found to be the most reliable route.

您可能需要创建以下文件:自定义/模块/ CUSTOM_BOOK_MODULE /视图/ view.edit.php(我希望现在我并不需要指出的改变这条道路使用您的模块名称...

You probably will need to create the following file: custom/modules/CUSTOM_BOOK_MODULE/views/view.edit.php (I hope by now I don't need to point out changing that path to use your module name...

假设该文件不存在,我们从头开始这里是它需要看起来像:

Assuming this file did not exist and we are starting from scratch here is what it will need to look like:

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class CUSTOM_BOOK_MODULEViewEdit extends ViewEdit
{
    public function display()
    {
        // make sure it works in the subpanel too
        $this->useForSubpanel = true;

        // make the name value available in the tpl file
        $this->ss->assign('name_value', $this->bean->name);

        // load the parsed contents of the tpl into this var
        $name_input_code = $this->ss->fetch('custom/modules/CUSTOM_BOOK_MODULE/tpls/unique_book_checker.tpl.js');

        // pass the parsed contents down into the editviewdefs
        $this->ss->assign('custom_name_code', $name_input_code);

        // definitely need to call the parent method
        parent::display();
    }
}

事情看起来不错。现在,我们得写code在该文件中:自定义/模块/ CUSTOM_BOOK_MODULE /第三方物流企业/ unique_book_checker.tpl.js

Things are looking good. Now we gotta write the code in this file: custom/modules/CUSTOM_BOOK_MODULE/tpls/unique_book_checker.tpl.js

第一几个假设:

  1. 我们要想到,这是糖6.5+和jQuery已经可用。如果您使用的是较早的版本,你需要手动包括jQuery的。

  1. We're going to expect that this is Sugar 6.5+ and jquery is already available. If you're on an earlier version you'll need to manually include jquery.

我们打算把事件侦听器的名称字段。如果您要查询的书名值实际上是一个不同的字段名称,然后简单地调整,在下面的JavaScript。

We're going to put the event listener on the name field. If the book name value that you want to check is actually a different field name then simply adjust that in the javascript below.

下面是code自定义/模块/ CUSTOM_BOOK_MODULE / unique_book_checker.tpl.js:

Here is the code for custom/modules/CUSTOM_BOOK_MODULE/unique_book_checker.tpl.js:

<input type="text" name="name" id="name" maxlength="255" value="{$name_value}" />
<span id="book_unique_result"></span>

{literal}
<script type="text/javascript">

$(document).ready(function() {

    $('#name').blur(function(){

        $('#book_unique_result').html('<strong> checking name...</strong>');

        $.post('index.php?entryPoint=test', {book_name: $('#name').val(), book_id: $('[name="record"]').val()}, function(data){

            if (data == 'exists') {
                removeFromValidate('EditView', 'name');
                addToValidate('EditView', 'name', 'float', true, 'Book Name Must be Unique.');

                $('#book_unique_result').html('<strong style="color:red;"> &#x2717;</strong>');
            }
            else if (data == 'unique') {
                removeFromValidate('EditView', 'name');
                addToValidate('EditView', 'name', '', true, 'Name Required');

                $('#book_unique_result').html('<strong style="color:green;"> &#x2713;</strong>');
            }
            else {
                // uh oh! maybe you have php display errors on?
            }

        });
    });
});
</script>
{/literal}

另注:当code检测到该名称已经存在,我们得到一点点哈克,用糖内置的验证的东西prevent记录从节约。基本上,我们要说的是,如果这个名字已经存在,那么这个名字值必须是一个浮动。我想这是pretty的可能性不大,并会做的伎俩。但是,如果你有一本书名叫3.14或类似的东西,并试图创建一个重复的这个code将不会prevent保存。它会告诉你,一式两份被发现,但它不会prevent的保存。

Another Note: When the code detects that the name already exists we get a little hacky and use Sugar's built in validation stuff to prevent the record from saving. Basically, we are saying that if the name already exists then the name value MUST be a float. I figured this is pretty unlikely and will do the trick. However if you have a book named 3.14 or something like that and you try to create a duplicate this code will NOT prevent the save. It will tell you that a duplicate was found, but it will not prevent the save.

唷!好了最后两个步骤,他们很容易。 首先,打开文件:自定义/模块/ CUSTOM_BOOK_MODULE /元/ editviewdefs.php。 其次,发现提供的元数据名称字段中的部分并添加这个定制code属性,以便它看起来是这样的:

Phew! Okay last two steps and they are easy. First, open the file: custom/modules/CUSTOM_BOOK_MODULE/metadata/editviewdefs.php. Next, find the section that provides the metadata for the name field and add this customCode attribute so that it looks like this:

array (
'name' => 'name',
'customCode' => '{$custom_name_code}',
),

最后,你需要做一个快速的修复和重建的元数据更改生效。进入管理>修复>快速修复和放大器;重建。

Finally, you'll need to do a quick repair and rebuild for the metadata changes to take effect. Go to Admin > Repair > Quick Repair & Rebuild.

轰!你应该是好去!

这篇关于糖CRM使用AJAX prevent重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆