This article will focus on the well-known open source CRM system - SuiteCRM. The openness of the system provides endless possibilities for customization, and, as an example, the process of creating dynamically loadable directories from the database using the select2 jQuery library will be considered.
Select2 can work with static sets of choices, as well as receive data from external sources, has widely customizable display formats using pictures, etc. The data format is in the form of JSON, so our task is to develop an EntryPoint so that the output will produce the necessary JSON structure.
The SuiteCRM is based on the MVC model, and all actions imply a call to the action controller. It happens that it is necessary to deviate from the standard MVC approach, since the use of the usual MVC approach can be difficult or simply not necessary.
Creating your own EntryPoints takes place in two stages. The first is to register such an EntryPoint through the embedded MVC framework. Listing 1 shows an example of registering an EntryPoint with the system.
')
Listing 1../custom/Extension/application/Ext/EntryPointRegistry/custom_entry_point_registry.php
<?php $entry_point_registry['CustomEntryPoint'] = array( 'file' => 'custom/modules/<Module>/customEntryPoint.php', 'auth' => true );
The second stage is the creation of a file that describes the logic of this EntryPoint itself (Listing 2).
Listing 2../custom//customEntryPoint.php
<?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); echo 'Hello World!';
After creating these files, you need to perform “Quick Repair and Rebuild”, then go to:
... index.php? EntryPoint = CustomEntryPoint.For the formation of JSON objects, you can use a different approach, for example, json_encode (), but in this example we will consider a simple string concatenation, since you only need to form a part of the JSON object.
Listing 3../custom//customEntryPoint.php
<?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); $response = '{"more": "false", "results":['; $sql = "SELECT stage, name FROM opportunity_stages order by name"; $res = $GLOBALS['db']->query($sql); if (!$res) { die('Bad query'); } while($row = $GLOBALS['db']->fetchByAssoc($res)){ $response .= '{"text":"' . $row["stage"] . '", ' . $row["name"] . '],'; } $response = rtrim($response, ","); $response .= ']}'; header('Content-Type: application/json'); echo $response;
The result of the work is shown in Listing 4.
Listing 4.index.php? entryPoint = CustomEntryPoint
{ "more": "false", "results": [ {"text":"", "id":"1"}, {"text":" ", "id":"2"}, {"text":" ", "id":"3"}, {"text":" ", "id":"4"}, {"text":" ", "id":"5"} ] }
Now you need to get this data in select2. An example of initialization is shown in Listing 5.
Listing 5. $('#select').select2({ placeholder: " ", minimumInputLength: 3, ajax: { url: "index.php?entryPoint=CustomEntryPoint", dataType: 'json', quietMillis: 250, data: function (term, page) { return { q: term, }; }, results: function (data, page) { return { results: data.results }; }, cache: true }, initSelection: function (element, callback){ var elementValue = $(element).val(); callback({"text":elementValue,"id":$('#select').val()}); }, id: function(object){ return object.text; }, formatSelection: customFormatSelection, }); function customFormatSelection(state){ return state.text; };
That's all. Now the page should have a convenient drop-down list. Despite the fact that in this example, implementation with 5 drop-down options for sales stages is considered, this approach can be very convenient when implementing directories with large amounts of data, for example, KLADR and others.
"A link to the select2 library:
https://select2.imtqy.com/examples.htmlThe article was prepared by Sergey Shirnin, consultant of Siebel, Jet Infosystems