Explorar o código

fixed sql and dbg in Typespecial

Piotr Labudda %!s(int64=9) %!d(string=hai) anos
pai
achega
fb518af7e8
Modificáronse 2 ficheiros con 25 adicións e 31 borrados
  1. 3 2
      SE/se-lib/TableAjax.php
  2. 22 29
      SE/se-lib/Typespecial.php

+ 3 - 2
SE/se-lib/TableAjax.php

@@ -5954,7 +5954,7 @@ jQuery(document).ready(function(){
 
 		$fldName = '';
 		$fld = $this->_acl->getField($fldID);
-		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fld('.$fldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fld);echo'</pre>';}
+		DBG::log($fld, 'array', "\$fld({$fldID})");
 		if (!$fld) {
 			// 404
 		} else {
@@ -5965,9 +5965,10 @@ jQuery(document).ready(function(){
 		$typeSpecial = Typespecial::getInstance($fldID, $fldName);
 		if ($typeSpecial) {
 			$query = V::get('q', '', $_REQUEST);
+			DBG::log("\$query({$query})");
 			$rawRows = null;
 			$rows = $typeSpecial->getValuesWithExports($query);
-			if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'</pre>';}
+			DBG::log($rows, 'array', "\$rows({$query})");
 			foreach ($rows as $kID => $vItem) {
 				$itemJson = new stdClass();
 				$itemJson->id = $vItem->id;

+ 22 - 29
SE/se-lib/Typespecial.php

@@ -827,31 +827,23 @@ jQuery('#typeahead-{$fName}').typeahead({
 		$query = trim($query);
 		$sqlWhereAdd = "";
 		if (!empty($query)) {
-			$qArr = array();
-			$queryArr = explode(' ', $query);
-			foreach ($queryArr as $q) {
-				$q = trim($q);
-				if (!empty($q)) {
-					$qArr[] = $q;
-				}
-			}
+			Lib::loadClass('SqlQueryWhereBuilder');
+			$queryWhereBuilder = new SqlQueryWhereBuilder();
+			$qArr = $queryWhereBuilder->splitQueryToWords($query);
 			DBG::log(['msg'=>"qArr", $qArr]);
-
 			if (!empty($qArr)) {
 				$sqlWhereAnd = array();
 				foreach ($qArr as $q) {
-					$dbQ = $db->_($q);
-					if (is_numeric($dbQ)) {
-						$dbQ = "{$dbQ}%";
-					} else {
-						$dbQ = "%{$dbQ}%";
-					}
+					$sqlSearchWord = DB::getPDO()->quote(
+						(is_numeric($q)) ? "{$q}%" : "%{$q}%",
+						PDO::PARAM_STR
+					);
 					$sqlWhereOr = array();
 					foreach ($this->param_out->alias as $fldID => $fldName) {
 						if (false === strpos($this->param_out->format, "{{$fldName}}")) continue;
 						$tblInfo = V::get($fldID, null, $_SESSION['Typespecial_Cache']['sqlTablesInfo']);
 						if ($tblInfo) {
-							$sqlWhereOr[] = "`t_{$tblInfo->tbl_id}`.`{$fldName}` like '{$dbQ}'";
+							$sqlWhereOr[] = "`t_{$tblInfo->tbl_id}`.`{$fldName}` like {$sqlSearchWord}";
 						}
 					}
 					if (!empty($sqlWhereOr)) {
@@ -863,10 +855,13 @@ jQuery('#typeahead-{$fName}').typeahead({
 				}
 			}
 		}
+		DBG::log(['msg'=>"this", $this]);
+		// TODO: for all tables filter rows by acl groups
+		// 'tblAliasList' => [
+		//       4095 => 'ADMIN_USERS',
+		//     ),
 		$sqlLimit = V::get('limit', 0, $this->sqlQuery, 'int');
-		if ($sqlLimit <= 0) {
-			$sqlLimit = 20;
-		}
+		$sqlLimit = ($sqlLimit > 0) ? $sqlLimit : 20;
 
 		$histRows = array();
 		{
@@ -912,17 +907,16 @@ jQuery('#typeahead-{$fName}').typeahead({
 				}
 			}
 		}
-		$sql = "select " . implode("\n\t\t,", $this->sqlQuery->select) . "
+		$rawRows = DB::getPDO()->fetchAll("
+			select " . implode("\n\t\t,", $this->sqlQuery->select) . "
 			from " . implode(", ", $this->sqlQuery->from) . "
 			where {$this->sqlQuery->where}
 				{$sqlWhereAdd}
 			limit {$sqlLimit}
-		";
-		DBG::log($sql, 'sql');
-		$res = $db->query($sql);
-		while ($r = $db->fetch($res)) {
+		");
+		foreach ($rawRows as $r) {
 			$retID = V::get("f_{$this->param_out->retID}", '', $r);
-			$rows[$retID] = $r;
+			$rows[$retID] = (object)$r;
 		}
 		if (!empty($histRows)) {
 			$sordedRows = array();
@@ -1215,13 +1209,13 @@ if(V::get('DBG_TS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 	}
 
 	private function saveCache() {
-if(V::get('DBG_TS', 0, $_GET) > 3){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial_Cache (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SESSION['Typespecial_Cache']);echo'</pre>';}
+		if(V::get('DBG_TS', 0, $_GET) > 3){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial_Cache (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SESSION['Typespecial_Cache']);echo'</pre>';}
 		$typeSpecialID = $_SESSION['Typespecial_Cache']['map'][$this->fldID];
 		$_SESSION['Typespecial_Cache'][$typeSpecialID] = array('param_out'=>$this->param_out, 'sqlQuery'=>$this->sqlQuery, 'tblAliasList'=>$this->tblAliasList);
 	}
 
 	public static function getInstance($fieldID, $colName) {
-		if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($colName);echo'</pre>';}
+		DBG::log("Typespecial::getInstance({$fieldID}, {$colName})");
 		if (!empty($colName)) {
 			Lib::loadClass('Config');
 			$LIB_CLASS_NAME_PREFIX = Config::get('LIB_CLASS_NAME_PREFIX');
@@ -1237,9 +1231,8 @@ if(V::get('DBG_TS', 0, $_GET) > 3){echo'<pre style="max-height:200px;overflow:au
 			}
 
 			Lib::loadClass('TypespecialVariable');
-		if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial2('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($colName);echo'</pre>';}
 			$typeSpecial = TypespecialVariable::getInstance($fieldID, $colName);
-			if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
+			DBG::log($typeSpecial, 'array', "TypespecialVariable::getInstance({$fieldID}, {$colName})");
 			if ($typeSpecial) {
 				return $typeSpecial;
 			}