Skip to content

Commit 6057c3f

Browse files
authored
Merge pull request #175 from ltb-project/169-configure-the-dn_list-editor
New configuration settings for dn_link component
2 parents cef1d55 + c959335 commit 6057c3f

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

conf/config.inc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484

8585
$attributes_list = array();
8686
$dn_link_label_attributes = array("cn");
87+
#$dn_link_search_display_macro = "%fullname% (%mail%)";
88+
$dn_link_search_min_chars = 3;
89+
$dn_link_search_size_limit = 10;
8790
$group_dn_link_label_attributes = array("description","cn");
8891
$usergroup_dn_link_label_attributes = array("description","cn");
8992

docs/attributes.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ It is also possible to configure which attribute is displayed as value on ``dn_l
7979
$group_dn_link_label_attributes = array("description","cn");
8080
$usergroup_dn_link_label_attributes = array("description","cn");
8181
82+
The component ``dn_link`` can be used when updating an entry. In this case it is an autocomplete field that will search for entries in the directory.
83+
Some configuration parameters can be used:
84+
85+
* What to display as search result label: it can be useful to use more thanone attribute to display the entry found by the search. This is possible by configuring a macro. For example to display the full name with the email in parenthesis:
86+
87+
.. code-block:: php
88+
89+
$dn_link_search_display_macro = "%fullname% (%mail%)";
90+
91+
* Minimal characters needed to launch the search (default is 3):
92+
93+
.. code-block:: php
94+
95+
$dn_link_search_min_chars = 2;
96+
97+
* Maximal number of entries to return (default is 10):
98+
99+
.. code-block:: php
100+
101+
$dn_link_search_size_limit = 5;
102+
82103
.. tip::
83104

84105
You can translate attribute label by defining the ``label_item`` messages in custom lang file, for example :

htdocs/api/search_dn.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,32 @@
1616
# Search attributes
1717
$attributes = array('cn');
1818

19+
if (isset($_POST["search_type"]) and $_POST["search_type"] == "dn_link") {
20+
$attributes = $dn_link_label_attributes;
21+
if ($dn_link_search_display_macro) {
22+
preg_match_all('/%(\w+)%/', $dn_link_search_display_macro, $matches);
23+
foreach($matches[1] as $item) {
24+
$attributes[] = $attributes_map[$item]['attribute'];
25+
}
26+
}
27+
if (isset($dn_link_search_size_limit)) {
28+
$ldapInstance->ldap_size_limit = $dn_link_search_size_limit;
29+
}
30+
}
31+
1932
[$ldap,$result,$nb_entries,$entries,$size_limit_reached] = $ldapInstance->search($ldap_filter, $attributes, $attributes_map, $search_result_title, $search_result_sortby, $search_result_items, $ldap_scope);
2033

2134
if ($nb_entries) {
2235
foreach($entries as $entry) {
23-
$data["entries"][] = array( "dn" => $entry["dn"], "display" => $entry["cn"][0]);
36+
$display = $entry["cn"][0];
37+
if (isset($_POST["search_type"]) and $_POST["search_type"] == "dn_link" and $dn_link_search_display_macro) {
38+
$display = preg_replace_callback('/%(\w+)%/',
39+
function ($matches) use ($entry, $attributes_map) {
40+
return $entry[ $attributes_map[$matches[1]]['attribute'] ][0];
41+
},
42+
$dn_link_search_display_macro);
43+
}
44+
$data["entries"][] = array( "dn" => $entry["dn"], "display" => $display);
2445
}
2546
}
2647

htdocs/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
$smarty->assign('logout_link',isset($logout_link) ? $logout_link : false);
107107
$smarty->assign('attributes_list',$attributes_list);
108108
$smarty->assign('dn_link_label_attributes',implode(",",$dn_link_label_attributes));
109+
$smarty->assign('dn_link_search_min_chars',$dn_link_search_min_chars);
109110
$smarty->assign('group_dn_link_label_attributes',implode(",",$group_dn_link_label_attributes));
110111
$smarty->assign('usergroup_dn_link_label_attributes',implode(",",$usergroup_dn_link_label_attributes));
111112
$smarty->assign('require_auth',$require_auth);

htdocs/js/update.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ $(document).ready(function(){
22
let timer;
33

44
$(".dn_link_container input[type=text]").on("keyup", function (event) {
5+
var conf_search_min_chars = $(this).data("conf-searchminchars");
6+
var search_min_chars = conf_search_min_chars ? conf_search_min_chars : 3;
57
// Remove value if field is emptied or less than minimal characters
6-
if ($(this).val().length <= 2) {
8+
if ($(this).val().length < search_min_chars) {
79
$(this).siblings('input[type=hidden]').val('') ;
810
$(this).siblings('div.dn_link_suggestions').empty();
911
}
1012
// Minimal search characters
11-
if ($(this).val().length > 2) {
13+
if ($(this).val().length >= search_min_chars) {
1214
if (timer) {
1315
clearTimeout(timer);
1416
$(this).siblings('div.dn_link_suggestions').empty();
1517
}
1618

1719
timer = setTimeout(() => {
18-
$.post("index.php", { 'apiendpoint': 'search_dn', 'search': $(this).val() }, (data) => {
20+
$.post("index.php", { 'apiendpoint': 'search_dn', 'search': $(this).val(), 'search_type': 'dn_link' }, (data) => {
1921
// clear existing list
2022
$(this).siblings('div.dn_link_suggestions').empty();
2123
if (data.entries) {

templates/value_editor.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<input type="number" name="{$item}{$itemindex}" class="form-control" value="{$value}" data-role="value" />
2626
{else if $type eq 'dn_link'}
2727
<div class="dn_link_container">
28-
<input type="text" class="form-control" value="{get_attribute dn="{$value}" attribute="cn" ldap_url="{$ldap_params.ldap_url}" ldap_starttls="{$ldap_params.ldap_starttls}" ldap_binddn="{$ldap_params.ldap_binddn}" ldap_bindpw="{$ldap_params.ldap_bindpw}" ldap_filter="{$ldap_params.ldap_user_filter}" ldap_network_timeout="{$ldap_params.ldap_network_timeout}"}" data-role="display"/>
28+
<input type="text" class="form-control" value="{get_attribute dn="{$value}" attribute="cn" ldap_url="{$ldap_params.ldap_url}" ldap_starttls="{$ldap_params.ldap_starttls}" ldap_binddn="{$ldap_params.ldap_binddn}" ldap_bindpw="{$ldap_params.ldap_bindpw}" ldap_filter="{$ldap_params.ldap_user_filter}" ldap_network_timeout="{$ldap_params.ldap_network_timeout}"}" data-role="display" data-conf-searchminchars="{$dn_link_search_min_chars}"/>
2929
<input type="hidden" name="{$item}{$itemindex}" value="{$value}" data-role="value" />
3030
<div class="z-3 position-absolute list-group dn_link_suggestions"></div>
3131
</div>

0 commit comments

Comments
 (0)