2014-10-28 83 views
-1

我正在處理一個自定義Joomla模塊,該模塊返回一個LDAP目錄,並且能夠使用AJAX更改前端的排序選項。如果我把它作爲如default.php模板文件(繞過AJAX)的字符串AJAX不返回LDAP響應,但在AJAX之外調用函數時工作

的getAjax函數返回目錄就好了:

echo $directoryList; 

問題是,當我嘗試返回變量「$內容「通過ajax,目錄不會顯示更改選擇器時。然而,在helper.php中,如果將「return $ content」更改爲「return $ sortOption」,AJAX將工作並返回所選的排序選項。所以我知道AJAX正在工作。另請注意,如果我更改爲「return $ content。$ sortOption」,則會顯示選擇選項變量,但不顯示目錄。我認爲這與通過AJAX無法正確加載LDAP有關。

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 = "[email protected]"; 
     $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; 
    } 
} 

如default.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> 

回答

-1

的問題是$條目陣列沒有被視爲實際的數組。我已經通過用靜態數組替換$ entry數組並且AJAX回調正常運行來測試這一點。我自去除了ajax功能,只是迴應功能,並正常工作。這並不能解決爲什麼AJAX不能拉動數組。

+0

不知道這些倒票來自哪裏。突然間,我的每一個職位都被拒絕投票。展示你自己!!! – 2015-10-31 15:28:09