Parcourir la source

+ _parseSqlWhere and cols filter in UserObject StorageAcl

Piotr Labudda il y a 6 ans
Parent
commit
cd839661a9
1 fichiers modifiés avec 140 ajouts et 28 suppressions
  1. 140 28
      SE/se-lib/Schema/UserObjectStorageAcl.php

+ 140 - 28
SE/se-lib/Schema/UserObjectStorageAcl.php

@@ -44,13 +44,13 @@ class Schema_UserObjectStorageAcl extends Core_AclSimpleSchemaBase {
 	public function setIdProcesFilter($idProcesFilter) { $this->idProcesFilter = intval($idProcesFilter); }
 	public function getIdProcesFilter() { return $this->idProcesFilter; }
 	public function updateCacheFeatures() {
-			$dbName = DB::getPDO()->getDatabaseName();
+		$dbName = DB::getPDO()->getDatabaseName();
 		$idDatabase = DB::getPDO()->getZasobId();
-			$sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
+		$sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
 		$sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
 		$sqlNoPrefixWhereAndIdProces = ($this->idProcesFilter > 0) ? "and ID_PROCES = {$this->idProcesFilter}" : "and ID_PROCES is NULL";
-			$sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
-			DB::getPDO()->execSql("
+		$sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
+		DB::getPDO()->execSql("
 			create table if not exists `{$this->_rootTableName}` (
 				`ID_USER` int(11) NOT NULL,
 				`ID_TABLE` int(11) NOT NULL,
@@ -114,26 +114,144 @@ class Schema_UserObjectStorageAcl extends Core_AclSimpleSchemaBase {
 	}
 
 	public function getTotal($params = []) {
-		// TODO: $sqlWhereAnd = $this->_parseSqlWhere($params);
-		$sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and t.ID_PROCES = {$this->idProcesFilter}" : "and t.ID_PROCES is NULL";
+		$sqlParsedWhere = $this->_parseSqlWhere($params);
+		$sqlWhere = " t.ID_USER = {$this->idUser} " .
+			(($sqlParsedWhere) ? " and " . $sqlParsedWhere : "") .
+			(($this->idProcesFilter > 0) ? " and t.ID_PROCES = {$this->idProcesFilter} " : " and t.ID_PROCES is NULL ")
+		;
 		return DB::getPDO()->fetchValue("
 			select count(1) as total
 			from `{$this->_rootTableName}` t
-			where t.ID_USER = {$this->idUser}
-				{$sqlWhereAndIdProces}
+			where {$sqlWhere}
 		");
 	}
 
-	public function _parseSqlWhere($params = []) {
-		$sqlWhereAnd = "";
-		// TODO: parse where/ogc, etc.
-		return $sqlWhereAnd;
+	function _parseSqlWhere($params = []) {
+		$sqlWhereAnd = array();
+		foreach ($params as $k => $v) {
+			if (strlen($k) > 3 && substr($k, 0, 2) == 'f_') {
+				DBG::log("TODO: Not implemented filter 'f_' for '{$k}' = '{$v}'");
+				// //$v = trim($v, '% ');
+				// //$sqlWhereAnd[] = "t.`" . substr($k, 2) . "` like '%" . DB::_($v) . "%'";
+				// $fldName = substr($k, 2);
+				// if ($this->isGeomField($fldName)) {
+				// 	$sqlFilter = $this->_sqlValueForGeomField($fldName, $v, 't');
+				// 	if ($sqlFilter) $sqlWhereAnd[] = $sqlFilter;
+				// 	continue;
+				// }
+				// if ($this->isCsvNumericField($fldName)) {
+				// 	$sqlFilter = $this->_sqlValueForCsvNumericField($fldName, $v, 't');
+				// 	if ($sqlFilter) $sqlWhereAnd[] = $sqlFilter;
+				// 	continue;
+				// }
+
+				// if (substr($v, 0, 1) == '=') {
+				// 	$v = $this->getDB()->_(substr($v, 1));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}`='{$v}'";
+				// }
+				// else if ($v == '!NULL' || $v == 'IS NOT NULL') {
+				// 	$sqlWhereAnd[] = "t.`{$fldName}` is not null";
+				// }
+				// else if (substr($v, 0, 1) == '!') {
+				// 	$v = $this->getDB()->_(substr($v, 1));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}` not like '{$v}'";
+				// }
+				// else if (substr($v, 0, 2) == '<=') {
+				// 	$v = $this->getDB()->_(substr($v, 2));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}`<='{$v}'";
+				// }
+				// else if (substr($v, 0, 2) == '>=') {
+				// 	$v = $this->getDB()->_(substr($v, 2));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}`>='{$v}'";
+				// }
+				// else if (substr($v, 0, 1) == '<') {
+				// 	$v = $this->getDB()->_(substr($v, 1));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}`<'{$v}'";
+				// }
+				// else if (substr($v, 0, 1) == '>') {
+				// 	$v = $this->getDB()->_(substr($v, 1));
+				// 	if (strlen($v)) $sqlWhereAnd[] = "t.`{$fldName}`>'{$v}'";
+				// }
+				// else if (false !== strpos($v, '%')) {
+				// 	$sqlWhereAnd[] = "t.`{$fldName}` like '{$v}'";
+				// }
+				// else if ($this->isColTypeNumber($fldName)) {
+				// 	$v = $this->getDB()->_($v);
+				// 	$sqlWhereAnd[] = "t.`{$fldName}`='{$v}'";
+				// }
+				// else {
+				// 	$queryWhereBuilder = new SqlQueryWhereBuilder();
+				// 	$searchWords = $queryWhereBuilder->splitQueryToWords($v);
+				// 	$sqlWhereWords = array();
+				// 	if (!empty($searchWords)) {
+				// 		foreach ($searchWords as $word) {
+				// 			$sqlWord = $this->getDB()->_($word);
+				// 			$sqlWhereWords[] = "t.`{$fldName}` like '%{$sqlWord}%'";
+				// 		}
+				// 	}
+				// 	if (!empty($searchWords)) {
+				// 		$sqlWhereAnd[] = "(" . implode(" and ", $sqlWhereWords) . ")";
+				// 	}
+				// }
+			}
+			else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_') {
+				DBG::log("TODO: Not implemented special filter 'sf_' for '{$k}' = '{$v}'");
+				// $sqlFltr = $this->_parseSpecialFilter(substr($k, 3), $v);
+				// if (!empty($sqlFltr)) {
+				// 	$sqlWhereAnd[] = $sqlFltr;
+				// }
+			}
+			else if ('ogc:Filter' == $k) {
+				DBG::log($v, 'string', "TODO: Not implemented ogc:Filter filter");
+				$sqlFltr = $this->_parseOgcFilter($v);
+				if (!empty($sqlFltr)) {
+					$sqlWhereAnd[] = $sqlFltr;
+				}
+			}
+			else if ('primaryKey' == $k) {
+				DBG::log("DBG: primaryKey filter '{$v}'");
+				if (!empty($v)) {
+					$primaryKeyField = $this->getPrimaryKeyField();
+					$pdo = DB::getPDO();
+					$sqlWhereAnd[] = "t." . $pdo->identifierQuote($primaryKeyField) . " = " . $pdo->quote($v);
+				}
+			}
+		}
+		return (!empty($sqlWhereAnd)) ? implode(" and ", $sqlWhereAnd) : "";
 	}
 
 	public function getItems($params = []) {
+		$sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
+		$cols = [];
+		$cols[] = [ 'ID_TABLE', "t.ID_TABLE" ];
+		$cols[] = [ 'ID_USER', "'{$this->idUser}'" ];
+		$cols[] = [ 'ID_PROCES', "'{$sqlIdProces}'" ];
+		$cols[] = [ 'db', "t.db" ];
+		$cols[] = [ 'name', "t.name" ];
+		$cols[] = [ 'label', "t.label" ];
+		$cols[] = [ 'opis', "t.opis" ];
+		$cols[] = [ '_rootTableName', "t._rootTableName" ];
+		$cols[] = [ 'namespace', "(select o.namespace from `CRM_#CACHE_ACL_OBJECT` o where o.idZasob = t.ID_TABLE)" ];
+		$colNameList = array_map(function ($x) { return $x[0]; }, $cols);
+		$filterCols = V::get('cols', [], $params);
+		$filterCols = array_filter($filterCols, function ($col) use ($colNameList) {
+			return in_array($col, $colNameList);
+		});
+		DBG::log($filterCols, 'array', "DBG:\$filterCols");
+		if (!empty($filterCols) && !in_array($this->getPrimaryKeyField(), $filterCols)) {
+			$filterCols[] = $this->getPrimaryKeyField();
+		}
+		$sqlListCols = array_reduce($cols, function ($ret, $col) use ($filterCols) {
+			if (!empty($filterCols) && !in_array($col[0], $filterCols)) return $ret;
+			$ret[] = "{$col[1]} as `{$col[0]}`";
+			return $ret;
+		}, []);
+		DBG::log($sqlListCols, 'array', "DBG:\$sqlListCols");
+		$sqlCols = implode("\n, ", $sqlListCols);
+		DBG::log($sqlCols, 'select', "DBG:\$sqlCols");
+
 		$sqlOrderBy = "";
 		$sqlLimitOffset = "";
-		$sqlWhereAnd = $this->_parseSqlWhere($params);
 
 		$currSortCol = V::get('order_by', 'ID', $params);
 		$currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
@@ -181,18 +299,14 @@ class Schema_UserObjectStorageAcl extends Core_AclSimpleSchemaBase {
 		// 		{$sqlWhereAndIdProces}
 		// ";
 		// $userAccessForTables = DB::getPDO()->fetchAllByKey($sql, 'ID_TABLE');
-		$sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
-		$sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
+		$sqlParsedWhere = $this->_parseSqlWhere($params);
+		$sqlWhere = ($sqlParsedWhere) ? $sqlParsedWhere : "1=1";
+		$subSqlWhere = "c.ID_USER = {$this->idUser}" .
+			(($this->idProcesFilter > 0) ? " and c.ID_PROCES = {$this->idProcesFilter} " : " and c.ID_PROCES is NULL ")
+		;
+
 		$items = DB::getPDO()->fetchAllByKey("
-			select t.ID_TABLE
-					, {$this->idUser} as ID_USER
-					, {$sqlIdProces} as ID_PROCES
-					, t.db
-					, t.name
-					, t.label
-					, t.opis
-					, t._rootTableName
-					, (select o.namespace from `CRM_#CACHE_ACL_OBJECT` o where o.idZasob = t.ID_TABLE) as namespace
+			select {$sqlCols}
 			from (
 				select c.ID_TABLE
 						, c.db
@@ -201,11 +315,9 @@ class Schema_UserObjectStorageAcl extends Core_AclSimpleSchemaBase {
 						, c.opis
 						, c.ROOT_TABLE_NAME as _rootTableName
 				from `{$this->_rootTableName}` as c
-				where c.ID_USER = {$this->idUser}
-					{$sqlWhereAndIdProces}
+				where {$subSqlWhere}
 			) as t
-			where 1=1
-				{$sqlWhereAnd}
+			where {$sqlWhere}
 			{$sqlOrderBy}
 			{$sqlLimitOffset}
 		", 'ID_TABLE');