Commit 393f1232 authored by Carsten  Rose's avatar Carsten Rose
Browse files

TypeAhead.php: fixed typo

Ldap.php: fixed implementation errors of first run.
AbstractBuildForm.php: set defaults for limit and minlegth. Implement function checkSqlAppendLimit()
parent fddd5ab1
......@@ -9,9 +9,10 @@
namespace qfq;
use qfq;
use qfq\Store;
use qfq\OnArray;
use qfq\UserFormException;
//use qfq\Store;
//use qfq\OnArray;
//use qfq\UserFormException;
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
......@@ -787,23 +788,27 @@ abstract class AbstractBuildForm {
/**
* Check $formElement for FE_TYPE_AHEAD_SQL or FE_TYPE_AHEAD_LDAP_SERVER.
* If one of them is given: fill $urlParam.
* Set some parameter for later outside use, especially FE_TYPEAHEAD_LIMIT, FE_TYPEAHEAD_MINLENGTH
*
* @param array $formElement
* @return string
*/
private function typeAheadBuildParam(array $formElement) {
private function typeAheadBuildParam(array &$formElement) {
$urlParam = '';
$formElement[FE_TYPEAHEAD_LIMIT] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LIMIT, TYPEAHEAD_DEFAULT_LIMIT);
$formElement[FE_TYPEAHEAD_MINLENGTH] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_MINLENGTH, 2);
if (isset($formElement[FE_TYPEAHEAD_SQL])) {
$urlParam = FE_TYPEAHEAD_SQL . '=' . $formElement[FE_TYPEAHEAD_SQL];
$sql = $this->checkSqlAppendLimit($formElement[FE_TYPEAHEAD_SQL], $formElement[FE_TYPEAHEAD_LIMIT]);
$urlParam = FE_TYPEAHEAD_SQL . '=' . $sql;
} elseif (isset($formElement[FE_TYPEAHEAD_LDAP_SERVER])) {
$formElement[FE_TYPEAHEAD_LDAP_SERVER] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LDAP_SERVER);
$formElement[FE_TYPEAHEAD_LDAP_BASE_DN] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LDAP_BASE_DN);
$formElement[FE_TYPEAHEAD_LDAP_SEARCH] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LDAP_SEARCH);
$formElement[FE_TYPEAHEAD_LDAP_VALUE_PRINTF] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LDAP_VALUE_PRINTF);
$formElement[FE_TYPEAHEAD_LDAP_KEY_PRINTF] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LDAP_KEY_PRINTF);
$formElement[FE_TYPEAHEAD_LIMIT] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LIMIT, TYPEAHEAD_DEFAULT_LIMIT);
$arr = [
FE_TYPEAHEAD_LDAP_SERVER => $formElement[FE_TYPEAHEAD_LDAP_SERVER],
......@@ -817,12 +822,30 @@ abstract class AbstractBuildForm {
$urlParam = OnArray::toString($arr);
}
if ($urlParam != '') {
$formElement[FE_TYPEAHEAD_LIMIT] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_LIMIT, TYPEAHEAD_DEFAULT_LIMIT);
$formElement[FE_TYPEAHEAD_MINLENGTH] = Support::setIfNotSet($formElement, FE_TYPEAHEAD_MINLENGTH, 2);
return $urlParam;
}
/**
* Checks if $sql contains a SELECT statement.
* Check for existence of a LIMIT Parameter. If not found add one.
*
* @param $sql
* @param $limit
* @return string Checked and maybe extended $sql statement.
* @throws \qfq\UserFormException
*/
private function checkSqlAppendLimit($sql, $limit) {
$sql = trim($sql);
if (false === stristr(substr($sql, 0, 7), 'SELECT ')) {
throw new UserFormException("Expect a SELECT statement in " . FE_TYPEAHEAD_SQL . " - got: " . $sql, ERROR_BROKEN_PARAMETER);
}
return $urlParam;
if (false === stristr($sql, ' LIMIT ')) {
$sql .= " LIMIT $limit";
}
return $sql;
}
/**
......
......@@ -458,11 +458,13 @@ const DATA_HIDDEN = 'data-hidden';
const DATA_DISABLED = 'data-disabled';
const DATA_REQUIRED = 'data-required';
const CLASS_TYPEAHEAD = 'qfq-typeahead';
const DATA_TYPEAHEAD_SIP = 'data-typeahead-sip'; // Used for typeAhead
//const CLASS_TYPEAHEAD = 'qfq-type-ahead';
//const DATA_TYPEAHEAD_SIP = 'data-sip'; // Used for typeAhead
const DATA_TYPEAHEAD_LIMIT = 'data-typeahead-limit';
const DATA_TYPEAHEAD_MINLENGTH = 'data-typeahead-minlength';
const CLASS_TYPEAHEAD = 'qfq-typeahead';
// BuildForm
const SYMBOL_NEW = 'new';
......
......@@ -79,7 +79,7 @@ class TypeAhead {
$sipVars = $sipClass->getVarsFromSip($this->vars[TYPEAHEAD_API_SIP]);
if (isset($sipVars[FE_TYPEAHEAD_SQL])) {
$arr = typeAheadSql($sipVars, $this->vars[TYPEAHEAD_API_QUERY]);
$arr = $this->typeAheadSql($sipVars, $this->vars[TYPEAHEAD_API_QUERY]);
} elseif (isset($sipVars[FE_TYPEAHEAD_LDAP_SERVER])) {
$ldap = new Ldap();
$arr = $ldap->process($sipVars, $this->vars[TYPEAHEAD_API_QUERY]);
......
......@@ -33,7 +33,8 @@ class Ldap {
throw new UserFormException("Unable to connect to LDAP server: $ldapServer", ERROR_LDAP_CONNECT);
}
$sr = ldap_search($ds, $ldapBaseDn, $ldapSearch, null, null, $ldapLimit);
// $sr = ldap_search($ds, $ldapBaseDn, $ldapSearch, null, null, $ldapLimit);
$sr = ldap_search($ds, $ldapBaseDn, $ldapSearch);
$info = ldap_get_entries($ds, $sr);
for ($i = 0; $i < $info["count"]; $i++) {
......@@ -64,13 +65,13 @@ class Ldap {
// keyArr[0]: printf format string
// keyArr[1..x]: columnnames
$keyArr = KeyValueStringParser::explodeWrapped(',', $format);
if (count($keyArr) <= 2) {
if (count($keyArr) < 2) {
throw new UserFormException("Expect a sprintf compatible format string with a least one argument. Got: '" . $format . "'", ERROR_MISSING_PRINTF_ARGUMENTS);
}
$args = array();
$args[] = $keyArr[0];
$keyArr = array_shift($keyArr);
array_shift($keyArr);
foreach ($keyArr as $arg) {
$keyName = trim($arg);
$args[] = $infoElement[$keyName][0];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment