我正在开发一个自定义Joomla模块,该模块返回LDAP目录,并能够使用AJAX在前端更改排序选项。
如果我在default.php模板文件中将getAjax函数作为字符串调用(绕过AJAX),那么getAjax函数会很好地返回目录:
echo $directoryList;
问题是,当我试图通过ajax返回变量"$content"时,在更改选择器时,目录不会显示。然而,在helper.php中,如果我将"return$content"更改为"return$sortOption",AJAX就会工作,并返回所选的排序选项。所以我知道AJAX正在工作。还要注意,如果我更改为"return$content.$sortOption",则会显示select选项变量,但不会显示目录。我认为这和LDAP没有通过AJAX正确加载有关。
Mod_nu_directory.php
// no direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once( dirname(__FILE__) . '/helper.php' );
// Instantiate global document object
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
$(document).on('change', '#sortDir select', function () {
var value = $('#sortDir option:selected').val(),
request = {
'option' : 'com_ajax',
'module' : 'nu_directory',
'data' : value,
'format' : 'raw'
};
$.ajax({
type : 'POST',
data : request,
success: function (response) {
$('.status').html(response);
}
});
return false;
});
})(jQuery)
JS;
$doc->addScriptDeclaration($js);
$dirDepts = $params->get('dirDepts', 'All');
$dirOptions = $params->get('dirOptions');
$directoryList = modNuDirectoryHelper::getAjax($dirDepts);
require( JModuleHelper::getLayoutPath('mod_nu_directory'));
helper.php
class modNuDirectoryHelper {
public static function getAjax($dirDepts) {
//get the sort variable from the select field using ajax:
$input = JFactory::getApplication()->input;
$sortOption = $input->get('data');
//Set our variables
$baseDN = 'CN=Users,DC=site,DC=local';
$adminDN = "admin";
$adminPswd = "P@55WorD";
$ldap_conn = ldap_connect('ldaps://ad.site.local');
$dirFilter = strtolower('(|(department=*' . implode('*)(department=*', $dirDepts) . '*))');
//if "All" categories are selected, dont add a filter, else add a directory filter
(strpos($dirFilter, 'all directory') !== false) ?
$filter = '(&(objectClass=user)(|(memberof=CN=Faculty,CN=Users,DC=site,DC=local)(memberof=CN=Staff,CN=Users,DC=site,DC=local)))' : $filter = '(&(objectClass=user)(|(memberof=CN=Faculty,CN=Users,DC=site,DC=local)(memberof=CN=Staff,CN=Users,DC=site,DC=local))' . $dirFilter . ')';
ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
$ldap_bind = ldap_bind($ldap_conn, $adminDN, $adminPswd);
if (!$ldap_bind) {
return 'Oh no! Unable to connect to the directory :(';
} else {
$attributes = array('displayname', 'mail', 'telephonenumber', 'title', 'department', 'physicalDelivery', 'OfficeName', 'samaccountname', 'wwwhomepage', 'sn', 'givenname');
$result = ldap_search($ldap_conn, $baseDN, $filter, $attributes);
//sort the entries by last name
ldap_sort($ldap_conn, $result, $sortOption);
$entries = ldap_get_entries($ldap_conn, $result);
// let's loop throught the directory
for ($i = 0; $i < $entries["count"]; $i++) {
// define the variables for each iteration within the loop
$userName = $entries[$i]['displayname'][0];
$userTitle = $entries[$i]['title'][0];
$userDept = $entries[$i]['department'][0];
$userPhone = '888-888-8888, ext. ' . $entries[$i]['telephonenumber'][0];
$userOffice = 'Office: ' . $entries[$i]['physicaldeliveryofficename'][0];
//person must have a name, title, and department
if ((!empty($userName)) || (!empty($userTitle)) || (!empty($userDept))) {
$content .= $userName . '<br />'
. $userTitle . '<br />'
. $userDept . '<br />'
. (!empty($userPhone) ? $userPhone : '') . '<br />'
. (!empty($userOffice) ? $userOffice : '') . '<br />'
. '<br />';
}
}
}
return $content;
}
}
默认.php
<?php
// No direct access
defined('_JEXEC') or die;
?>
<p>Displaying the following departments:<br />
<?php
foreach ($dirDepts as $dirDept) {
echo '[' . $dirDept . '] ';
}
?>
</p>
<p class="dirOptions">Displaying the following Options:<br />
<?php
foreach ($dirOptions as $dirOption) {
echo '[' . $dirOption . '] ';
}
?>
</p>
<?php
if (in_array('showSort', $dirOptions)) {
?>
<form method="post" id="sortDir">
<select name="sortDir" >
<option value="displayname" selected="selected">First name</option>
<option value="sn">Last name</option>
<option value="department">Department</option>
</select>
</form>
<?php } ?>
<div class="status"></div>
问题是$entries数组没有被视为实际数组。我已经通过用静态数组替换$entry数组进行了测试,AJAX回调行为正常。从那以后,我删除了ajax功能,只是呼应了该功能,并且运行良好。不过,这并不能解决AJAX不能拉取数组的原因。