Bladeren bron

added WPS p5:selectAllFeaturesMatchingFilter

Piotr Labudda 8 jaren geleden
bovenliggende
commit
cc19b82616

+ 25 - 0
SE/se-lib/AclQueryFeatures.php

@@ -543,6 +543,31 @@ class AclQueryFeatures {
 		return $items;
 		return $items;
 	}
 	}
 
 
+	public function getRawQueryPrimaryKeys() {
+		if ($this->_legacyMode) throw new Exception("getRawQueryPrimaryKeys not supported in legacyMode");
+
+		$sortBy = ($this->hasParam('sortBy')) ? $this->getParam('sortBy') : null;
+		if (!$sortBy && array_key_exists('__backRef', $this->_params)) {
+			$sortBy = "refTable.REMOTE_PRIMARY_KEY DESC"; // TODO: order by refTable.SORT_PRIO
+		}
+		if (!$sortBy) {
+			$sortBy = $this->hasParam('order_by')
+				? ( $this->hasParam('order_dir')
+						? $this->getParam('order_by') . " " . $this->getParam('order_dir')
+						: $this->getParam('order_by')
+					)
+				: '';
+		}
+		$limit = V::get('limit', 10, $this->_params, 'int');
+		$offset = V::get('limitstart', 0, $this->_params, 'int');
+		return $this->getQuery()
+			->select(['@primaryKey'])
+			->limit($limit)
+			->offset($offset)
+			->orderBy($sortBy)
+			->generateSql();
+	}
+
 	function _beforeFetchData() {
 	function _beforeFetchData() {
 		if (method_exists($this->_acl, 'onBeforeFetchData')) $this->_acl->onBeforeFetchData();
 		if (method_exists($this->_acl, 'onBeforeFetchData')) $this->_acl->onBeforeFetchData();
 	}
 	}

+ 31 - 0
SE/se-lib/Api/Process/P5/SelectAllFeaturesMatchingFilter.php

@@ -0,0 +1,31 @@
+<?php
+
+Lib::loadClass('FeatureAttrSelected');
+
+class Api_Process_P5_SelectAllFeaturesMatchingFilter { // TODO: extends Api_ProcessBase
+
+	static function run($args, $responseForm) {
+		$typeName = (!empty($args['typeName'][0])) ? $args['typeName'][0] : '';
+		if (!$typeName) throw new Api_OwsException("Missing value for 'typeName'", 501, null, 'MissingParameterValue', 'request');
+		$listPrimaryKeys = (!empty($args['primaryKey'])) ? $args['primaryKey'] : [];
+		if (empty($listPrimaryKeys)) throw new Api_OwsException("Missing value for 'primaryKey'", 501, null, 'MissingParameterValue', 'request');
+		$filterQuery = (!empty($args['filterQuery'])) ? $args['filterQuery'] : [];
+
+		FeatureAttrSelected::selectAllByFilter($typeName, $filterQuery);
+		$listSelectedState = FeatureAttrSelected::getSelectState($typeName, $listPrimaryKeys);
+		$totalSelected = FeatureAttrSelected::getTotalSelected($typeName);
+
+		header('Content-Type: application/json');
+		echo json_encode([
+			'type' => "success",
+			'msg' => "done",
+			'body' => [
+				'TODO__$filterQuery' => $filterQuery,
+				'selected' => $listSelectedState,
+				'totalSelected' => $totalSelected,
+			]
+		]);
+		exit;
+	}
+
+}

+ 18 - 6
SE/se-lib/Api/WpsHelper.php

@@ -35,21 +35,26 @@ class Api_WpsHelper {
 
 
 		{
 		{
 			$selectFeatureArgTypeName = [
 			$selectFeatureArgTypeName = [
-				'maxOccurs' => "1",
-				'minOccurs' => "1",
+				'minOccurs' => "1", 'maxOccurs' => "1", // 1 arg is required
 				'identifier' => 'typeName',
 				'identifier' => 'typeName',
 				'title' => "Feature typeName",
 				'title' => "Feature typeName",
-				'description' => "Feature typeName eg. default_db/PROBLEMS",
+				'description' => "Feature typeName eg.: 'default_db/PROBLEMS'",
 				'type' => 'literal',
 				'type' => 'literal',
 			];
 			];
 			$selectFeatureArgPrimaryKey = [
 			$selectFeatureArgPrimaryKey = [
-				'maxOccurs' => "unbounded",
-				'minOccurs' => "1",
+				'minOccurs' => "1", 'maxOccurs' => "unbounded", // min 1 arg is required
 				'identifier' => 'primaryKey',
 				'identifier' => 'primaryKey',
 				'title' => "Feature primaryKey",
 				'title' => "Feature primaryKey",
 				'description' => "Feature primaryKey",
 				'description' => "Feature primaryKey",
 				'type' => 'literal',
 				'type' => 'literal',
 			];
 			];
+			$selectFeatureArgFilterQuery = [
+				'minOccurs' => "0", 'maxOccurs' => "1", // optional max 1 arg
+				'identifier' => 'filterQuery',
+				'title' => "Query string",
+				'description' => "Query string eg.: 'f_ID=123&sf_Msgs=NEW_MSGS'",
+				'type' => 'literal',
+			];
 			$selectFeatureBase = [
 			$selectFeatureBase = [
 				'dataInputs' => [
 				'dataInputs' => [
 					$selectFeatureArgTypeName,
 					$selectFeatureArgTypeName,
@@ -71,7 +76,14 @@ class Api_WpsHelper {
 			$defaultWpsProcessList['p5:getSelectedFeatures'] = array_merge([ 'title' => "Get selected features", 'descriptin' => "Get list of selected features (with attribute @selected = true)" ], $selectFeatureBase);
 			$defaultWpsProcessList['p5:getSelectedFeatures'] = array_merge([ 'title' => "Get selected features", 'descriptin' => "Get list of selected features (with attribute @selected = true)" ], $selectFeatureBase);
 			$defaultWpsProcessList['p5:unselectAllFeatures'] = array_merge([ 'title' => "Unselect all features", 'descriptin' => "Unselect all features (set attribute @selected to false)" ], $selectFeatureBase, [
 			$defaultWpsProcessList['p5:unselectAllFeatures'] = array_merge([ 'title' => "Unselect all features", 'descriptin' => "Unselect all features (set attribute @selected to false)" ], $selectFeatureBase, [
 				'dataInputs' => [
 				'dataInputs' => [
-					$selectFeatureArgTypeName
+					$selectFeatureArgTypeName,
+				]
+			]);
+			$defaultWpsProcessList['p5:selectAllFeaturesMatchingFilter'] = array_merge([ 'title' => "Select all features matching filter" ], $selectFeatureBase, [
+				'dataInputs' => [
+					$selectFeatureArgTypeName,
+					array_merge($selectFeatureArgPrimaryKey, [ 'minOccurs' => 0 ]), // optional arg
+					$selectFeatureArgFilterQuery,
 				]
 				]
 			]);
 			]);
 		}
 		}

+ 45 - 0
SE/se-lib/FeatureAttrSelected.php

@@ -31,6 +31,51 @@ class FeatureAttrSelected {
 		}, $listPrimaryKeys));
 		}, $listPrimaryKeys));
 	}
 	}
 
 
+	static function selectAllByFilter($typeName, array $filterQuery) {
+		$idUser = User::getID();
+		self::prepareUserTable($typeName, $idUser);
+		$tableName = self::getAttributeTableName($typeName, $idUser);
+		$acl = ACL::getAclByTypeName($typeName);
+
+		$args = [];
+		parse_str($filterQuery[0], $args);
+		DBG::log(['$filterQuery' => $filterQuery, '$args' => $args], 'array', "DBG: selectAllByFilter");
+
+		$params = [];
+		$params['limit'] = 0;
+		$params['cols'] = [ $acl->getPrimaryKeyField() ];
+		foreach ($args as $k => $v) {
+			if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
+				$params[$k] = $v;
+			}
+			else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
+				$params[$k] = $v;
+			}
+		}
+		$queryFeatures = $acl->buildQuery($params);
+		DBG::log($queryFeatures, 'array', '$queryFeatures');
+		try {
+			$sql = $queryFeatures->getRawQueryPrimaryKeys();
+			DBG::log($sql, 'string', 'sql getRawQueryPrimaryKeys');
+			Lib::loadClass('FeatureAttrSelected');
+			$userSelectedTableName = FeatureAttrSelected::getAttributeTableName($typeName = $acl->getNamespace(), $idUser = User::getID());
+			DB::getPDO()->execSql("
+				insert ignore into `{$userSelectedTableName}` (`primaryKey`)
+				select `selectedTable`.`@primaryKey`
+				from ( {$sql} ) as `selectedTable`
+			");
+		} catch (Exception $e) {
+			DBG::log($e);
+			DBG::log('legacyMode - execute query for all primary keys');
+			$listItems = $queryFeatures->getItems();
+			print_r($listItems);
+		}
+
+		// foreach ($listPrimaryKeys as $pk) {
+		// 	DB::getPDO()->execSql(" insert ignore `{$tableName}` (`primaryKey`) values ( :pk ) ", [ ':pk' => $pk ]);
+		// }
+	}
+
 	static function select($typeName, array $listPrimaryKeys) {
 	static function select($typeName, array $listPrimaryKeys) {
 		$idUser = User::getID();
 		$idUser = User::getID();
 		self::prepareUserTable($typeName, $idUser);
 		self::prepareUserTable($typeName, $idUser);