Prevent duplication of value with ajax in crm sugar

I have a module using the constructor module, now I have a field called the name of the book now, if I give the same book name 2 times t takes.

I donโ€™t want to use and connect to check for a duplicate value because I want to learn the setup with code.

so I can call ajax and check the weather in the database, the same book name exists in db or not, but I donโ€™t know how the controller works in crm satra. and how to trigger ajax in sugar crm.

Can anyone visit me, your help is greatly appreciated.

+5
source share
2 answers

ajax, entryPoint. . javascript ajax. ajax , . EntryPoint . , entryPoint.

custom/include/MVC/Controller/entry_point_registry.php. , .

entry_point_registry.php:

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

:

  • . , "unique_book_value" . , .
  • , . . , .
  • 'auth' => true , , SugarCRM, . ( ) .

, custom/test.php ( 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() - , : http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of- sql-all-the-time/) , API , ajax . , 99% . , PDO , , , .

, https://crm.yourdomain.com/index.php?entryPoint=test , .

, , . URL-, entryPoint, , :

  1. - $ entry_point_registry ['test']. , URL read index.php? EntryPoint = what_you_put_as_the_array_key
  2. - , crm.yourdomain.com - , yourdomain.com/sugarcrm/, , , URL, . , ... https://yourdomain.com/sugarcrm/index.php?entryPoint=test
  3. , - , apache . , /etc/init.d/apache2 . , - ( vps, !!!, , !)

? "s" https? http $ 9, !

, . entryPoint. . " " (, " ", ).

URL : index.php? EntryPoint = test & book_name = Art %20of %20War

, URL ! .

, "". , , -. 2 URL , , "".

: Sugar, , , mysql, . , SO: SQL MySQL?

, entryPoint, , ajaxical. , , , , .

, , : custom/modules/CUSTOM_BOOK_MODULE/views/view.edit.php (, ...

, , , :

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();
    }
}

. : custom/modules/CUSTOM_BOOK_MODULE/tpls/unique_book_checker.tpl.js

:

  1. , Sugar 6. 5+ jquery . , jquery.

  2. . , , , javascript.

/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}

: , , Sugar . , , , float. , . , 3.14 - , , . , , .

! , , . : custom/modules/CUSTOM_BOOK_MODULE/metadata/editviewdefs.php. , , customCode, :

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

, , . Admin> > .

Boom! !

checking for unique book nameuh oh thats a duplicateokay safe to save

+17

NIT . , NIT, BD. , , , SuiteCRM.

0

Source: https://habr.com/ru/post/1538393/


All Articles